Bug 753159: Allow empty structs. r=bent

This commit is contained in:
Chris Jones 2012-05-30 15:37:26 -07:00
parent 045aefbfce
commit e52fe0f199
2 changed files with 14 additions and 6 deletions

View File

@ -1816,10 +1816,14 @@ def _generateCxxStruct(sd):
return ExprCall(assignvar, return ExprCall(assignvar,
args=[ f.initExpr(oexpr) for f in sd.fields ]) args=[ f.initExpr(oexpr) for f in sd.fields ])
# Struct() # If this is an empty struct (no fields), then the default ctor
defctor = ConstructorDefn(ConstructorDecl(sd.name)) # and "create-with-fields" ctors are equivalent. So don't bother
defctor.addstmt(StmtExpr(callinit)) # with the default ctor.
struct.addstmts([ defctor, Whitespace.NL ]) if len(sd.fields):
# Struct()
defctor = ConstructorDefn(ConstructorDecl(sd.name))
defctor.addstmt(StmtExpr(callinit))
struct.addstmts([ defctor, Whitespace.NL ])
# Struct(const field1& _f1, ...) # Struct(const field1& _f1, ...)
valctor = ConstructorDefn(ConstructorDecl(sd.name, valctor = ConstructorDefn(ConstructorDecl(sd.name,

View File

@ -274,8 +274,12 @@ def p_NamespaceThing(p):
p[0] = p[4] p[0] = p[4]
def p_StructDecl(p): def p_StructDecl(p):
"""StructDecl : STRUCT ID '{' StructFields '}' ';'""" """StructDecl : STRUCT ID '{' StructFields '}' ';'
p[0] = StructDecl(locFromTok(p, 1), p[2], p[4]) | STRUCT ID '{' '}' ';'"""
if 7 == len(p):
p[0] = StructDecl(locFromTok(p, 1), p[2], p[4])
else:
p[0] = StructDecl(locFromTok(p, 1), p[2], [ ])
def p_StructFields(p): def p_StructFields(p):
"""StructFields : StructFields StructField ';' """StructFields : StructFields StructField ';'