From 9c303dedcecedffd3d0cc1fbd102591d1ddc0565 Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Tue, 21 Dec 2010 17:11:10 +0100 Subject: [PATCH] structs need default constructors --- .../antlr3/src/cs2j/CSharp/JavaMaker.g | 27 ++++++++++++++----- .../antlr3/src/cs2j/CSharp/JavaPrettyPrint.g | 11 ++------ .../antlr3/src/cs2j/CSharp/NetMaker.g | 9 +------ .../src/cs2j/bin/Debug/templates/java.stg | 4 +-- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g index 5572a08..d8ac4d9 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g @@ -282,12 +282,12 @@ class_member_declaration: m=modifiers? ( c='const' ct=type constant_declarators ';' -> ^(FIELD[$c.token, "FIELD"] $a? $m? { addConstModifiers($c.token, $m.modList) } $ct constant_declarators) | ed=event_declaration -> ^(EVENT[$ed.start.Token, "EVENT"] $a? $m? $ed) - | p='partial' { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); } (v1=void_type m3=method_declaration[$a.tree, $m.tree, $m.modList, $v1.tree, $v1.text] -> $m3 //-> ^(METHOD[$v1.token, "METHOD"] $a? $m? ^(TYPE $v1) $m3) + | p='partial' { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); } (v1=void_type m3=method_declaration[$a.tree, $m.tree, $m.modList, $v1.tree, $v1.text] -> $m3 | i1=interface_declaration -> ^(INTERFACE[$i1.start.Token, "INTERFACE"] $a? $m? $i1) | c1=class_declaration -> ^(CLASS[$c1.start.Token, "CLASS"] $a? $m? $c1) | s1=struct_declaration) -> ^(CLASS[$s1.start.Token, "CLASS"] $a? $m? $s1) | i2=interface_declaration -> ^(INTERFACE[$i2.start.Token, "INTERFACE"] $a? $m? $i2) // 'interface' - | v2=void_type m1=method_declaration[$a.tree, $m.tree, $m.modList, $v2.tree, $v2.text] -> $m1 //-> ^(METHOD[$v.token, "METHOD"] $a? $m? ^(TYPE[$v.token, "TYPE"] $v) $m1) + | v2=void_type m1=method_declaration[$a.tree, $m.tree, $m.modList, $v2.tree, $v2.text] -> $m1 | t=type ( (member_name type_parameter_list? '(') => m2=method_declaration[$a.tree, $m.tree, $m.modList, $t.tree, $t.text] -> $m2 | (member_name '{') => pd=property_declaration[$a.tree, $m.tree, $t.tree] -> $pd | (member_name '.' 'this') => type_name '.' ix1=indexer_declaration[$a.tree, $m.tree, $t.tree] -> $ix1 @@ -1165,8 +1165,11 @@ interface_accessor_declaration [CommonTree atts, CommonTree mods, CommonTree typ struct_declaration returns [string name] scope TypeContext; : - c='struct' identifier { $TypeContext::typeName = $identifier.text; } type_parameter_list? { $name = mkTypeName($identifier.text, $type_parameter_list.names); } class_base? type_parameter_constraints_clauses? class_body ';'? - -> ^(CLASS[$c.Token, "class"] identifier type_parameter_constraints_clauses? type_parameter_list? class_base? class_body ); + c='struct' identifier { $TypeContext::typeName = $identifier.text; } type_parameter_list? { $name = mkTypeName($identifier.text, $type_parameter_list.names); } class_base? type_parameter_constraints_clauses? struct_body[$identifier.text] ';'? + -> ^(CLASS[$c.Token, "class"] identifier type_parameter_constraints_clauses? type_parameter_list? class_base? struct_body ); + +struct_body [String structName]: + o='{' magicDefaultConstructor[$o.token, structName] class_member_declarations? '}' ; // UNUSED, HOPEFULLY // struct_modifiers: @@ -1258,9 +1261,9 @@ operator_body: /////////////////////////////////////////////////////// constructor_declaration[CommonTree atts, CommonTree mods, List modList]: - i=identifier '(' p=formal_parameter_list? ')' init=constructor_initializer? b=constructor_body[$init.tree] sb=magicSmotherExceptionsThrow[$b.tree, "ExceptionInInitializerError"] + i=identifier '(' p=formal_parameter_list? ')' init=constructor_initializer? b=constructor_body[$init.tree] sb=magicSmotherExceptionsThrow[$b.tree, "ExceptionInInitializerError"] magicThrowable -> {modList.Contains("static")}? ^(STATIC_CONSTRUCTOR[$i.tree.Token, "CONSTRUCTOR"] { dupTree($atts) } { dupTree($mods) } $sb) - -> ^(CONSTRUCTOR[$i.tree.Token, "CONSTRUCTOR"] { dupTree($atts) } { dupTree($mods) } $i $p? $b); + -> ^(CONSTRUCTOR[$i.tree.Token, "CONSTRUCTOR"] { dupTree($atts) } { dupTree($mods) } $i $p? $b magicThrowable); constructor_initializer: ':' tok='this' '(' argument_list? ')' -> ^(APPLY[$tok.token, "APPLY"] $tok argument_list?) SEMI[$tok.token, ";"] @@ -1675,7 +1678,7 @@ magicFinally[IToken tok, CommonTree statement_list]: magicFinalize[IToken tok, CommonTree body]: -> ^(METHOD[tok, "METHOD"] - PUBLIC[tok, "protected"] + PROTECTED[tok, "protected"] ^(TYPE[tok, "TYPE"] IDENTIFIER[tok, "void"]) IDENTIFIER[tok, "finalize"] OPEN_BRACE[tok, "{"] ^(TRY[tok, "try"] { dupTree(body) } @@ -1684,3 +1687,13 @@ magicFinalize[IToken tok, CommonTree body]: EXCEPTION[tok, "Throwable"]) ; +magicDefaultConstructor[IToken tok, String name]: +-> + ^(CONSTRUCTOR[tok, "CONSTRUCTOR"] + PUBLIC[tok, "public"] + IDENTIFIER[tok, name] + OPEN_BRACE[tok, "{"] + CLOSE_BRACE[tok, "}"] + ) +; + diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g index d05b575..ca3fb8d 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g @@ -267,11 +267,10 @@ class_member_declaration returns [List preComments]: | ^(ENUM attributes? modifiers? { $preComments = CollectedComments; } enum_declaration[$modifiers.st]) | ^(DELEGATE attributes? modifiers? { $preComments = CollectedComments; } delegate_declaration) | ^(CONVERSION_OPERATOR attributes? modifiers? conversion_operator_declaration) - | ^(CONSTRUCTOR attributes? modifiers? identifier formal_parameter_list? { $preComments = CollectedComments; } block) - -> constructor(modifiers={$modifiers.st}, name={ $identifier.st }, params={ $formal_parameter_list.st }, bodyIsSemi = { $block.isSemi }, body={ $block.st }) + | ^(CONSTRUCTOR attributes? modifiers? identifier formal_parameter_list? { $preComments = CollectedComments; } block exception*) + -> constructor(modifiers={$modifiers.st}, name={ $identifier.st }, params={ $formal_parameter_list.st }, exceptions = { $exception.st}, bodyIsSemi = { $block.isSemi }, body={ $block.st }) | ^(STATIC_CONSTRUCTOR attributes? modifiers? block) -> static_constructor(modifiers={$modifiers.st}, bodyIsSemi = { $block.isSemi }, body={ $block.st }) - | ^(DESTRUCTOR attributes? modifiers? destructor_declaration) ; // class_member_declaration: @@ -1146,12 +1145,6 @@ operator_body: //static_constructor_body: // block ; -/////////////////////////////////////////////////////// -destructor_declaration: - '~' identifier '(' ')' destructor_body ; -destructor_body: - block ; - /////////////////////////////////////////////////////// invocation_expression: invocation_start (((arguments ('['|'.'|'->')) => arguments invocation_part) diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g index 8853365..324986d 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g @@ -46,9 +46,8 @@ class_member_declaration: | ^(ENUM attributes? modifiers? enum_declaration) | ^(DELEGATE attributes? modifiers? delegate_declaration) | ^(CONVERSION_OPERATOR attributes? modifiers? conversion_operator_declaration) - | ^(CONSTRUCTOR attributes? modifiers? identifier formal_parameter_list? block) + | ^(CONSTRUCTOR attributes? modifiers? identifier formal_parameter_list? block exception*) | ^(STATIC_CONSTRUCTOR attributes? modifiers? block) - | ^(DESTRUCTOR attributes? modifiers? destructor_declaration) ; // class_member_declaration: // attributes? @@ -805,12 +804,6 @@ operator_body: //static_constructor_body: // block ; -/////////////////////////////////////////////////////// -destructor_declaration: - '~' identifier '(' ')' destructor_body ; -destructor_body: - block ; - /////////////////////////////////////////////////////// invocation_expression: invocation_start (((arguments ('['|'.'|'->')) => arguments invocation_part) diff --git a/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg b/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg index 5d7aa9b..bd9fecb 100644 --- a/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg +++ b/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg @@ -53,8 +53,8 @@ class_member(comments, member) ::= << >> -constructor(modifiers, name, params, exception="Throwable", body, bodyIsSemi) ::= << -() throws ; +constructor(modifiers, name, params, exceptions, body, bodyIsSemi) ::= << +() throws ;