From 922f535dcc5b6fc1f77edc4cefa085a9856afebd Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Mon, 28 Feb 2011 20:29:00 +0100 Subject: [PATCH] generate static constructors in the NetMaker phase, mainly to stop a failed resolve error from the introduced exception constructors which are Java --- .../CS2JTranslator/CS2JTransform/JavaMaker.g | 3 +- .../CS2JTranslator/CS2JTransform/NetMaker.g | 43 ++++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/JavaMaker.g b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/JavaMaker.g index 5aab3c3..97021a6 100644 --- a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/JavaMaker.g +++ b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/JavaMaker.g @@ -1285,8 +1285,7 @@ operator_body: /////////////////////////////////////////////////////// constructor_declaration[CommonTree atts, CommonTree mods, List modList]: - i=identifier '(' p=formal_parameter_list? s=')' init=constructor_initializer? b=constructor_body[$init.tree] sb=magicSmotherExceptionsThrow[$b.tree, "ExceptionInInitializerError"] magicThrowsException[true,$s.token] - -> {modList.Contains("static")}? ^(STATIC_CONSTRUCTOR[$i.tree.Token, "CONSTRUCTOR"] { dupTree($atts) } { dupTree($mods) } $sb) + i=identifier '(' p=formal_parameter_list? s=')' init=constructor_initializer? b=constructor_body[$init.tree] magicThrowsException[true,$s.token] -> ^(CONSTRUCTOR[$i.tree.Token, "CONSTRUCTOR"] { dupTree($atts) } { dupTree($mods) } $i $p? $b magicThrowsException); constructor_initializer: ':' tok='this' '(' argument_list? ')' diff --git a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetMaker.g b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetMaker.g index 4eac289..c97f972 100644 --- a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetMaker.g +++ b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetMaker.g @@ -281,6 +281,7 @@ scope SymTab { // counter to ensure that the vars we introduce are unique protected int dummyScrutVarCtr = 0; protected int dummyForeachVarCtr = 0; + protected int dummyStaticConstructorCatchVarCtr = 0; protected CommonTree convertSectionsToITE(List sections) { CommonTree ret = null; @@ -467,8 +468,11 @@ type_declaration: qualified_identifier: identifier ('.' identifier)*; -modifiers: - modifier+ ; +modifiers returns [List modList] +@init { + $modList = new List(); +}: + (modifier { $modList.Add($modifier.tree.Text); } )+ ; modifier: 'new' | 'public' | 'protected' | 'private' | 'abstract' | 'sealed' | 'static' | 'readonly' | 'volatile' | 'extern' | 'virtual' | 'override' | FINAL ; @@ -484,16 +488,27 @@ class_member_declaration: | enum_declaration | delegate_declaration | ^(CONVERSION_OPERATOR attributes? modifiers? conversion_operator_declaration[$attributes.tree, $modifiers.tree]) -> conversion_operator_declaration - | ^(CONSTRUCTOR attributes? modifiers? identifier formal_parameter_list? block exception*) - | ^(STATIC_CONSTRUCTOR attributes? modifiers? block) + | constructor_declaration ; exception: EXCEPTION; +constructor_declaration +@init { + bool isStatic = false; +}: + ^(c=CONSTRUCTOR attributes? (modifiers { isStatic = $modifiers.modList.Contains("static"); })? identifier formal_parameter_list? block exception* sb=magicSmotherExceptionsThrow[$block.tree, "ExceptionInInitializerError"]) + -> { isStatic }? ^(STATIC_CONSTRUCTOR[$c.token, "CONSTRUCTOR"] attributes? modifiers? $sb) + -> ^($c attributes? modifiers? identifier formal_parameter_list? block exception*) + ; + + + // rmId is the rightmost ID in an expression like fdfd.dfdsf.returnme, otherwise it is null // used in switch labels to strip down qualified types, which Java doesn't grok -primary_expression returns [TypeRepTemplate dotNetType, string rmId, TypeRepTemplate typeofType] +// thedottedtext is the text read so far that *might* be part of a qualified type +primary_expression returns [TypeRepTemplate dotNetType, string rmId, TypeRepTemplate typeofType, string thedottedtext] scope { bool parentIsApply; } @@ -2199,3 +2214,21 @@ magicConstructDefaultEnum[bool isOn, TypeRepTemplate ty, string zero, IToken tok -> { isOn }? ^(DOT[tok, "."] IDENTIFIER[tok, ty.Java] IDENTIFIER[tok, zero]) -> ; + +magicSmotherExceptionsThrow[CommonTree body, string exception]: + v=magicCatchVar magicThrowableType[true, body.Token] + -> OPEN_BRACE["{"] + ^(TRY["try"] + { dupTree(body) } + ^(CATCH["catch"] magicThrowableType { dupTree($v.tree) } + OPEN_BRACE["{"] ^(THROW["throw"] ^(NEW["new"] ^(TYPE["TYPE"] IDENTIFIER[exception]) ^(ARGS["ARGS"] { dupTree($v.tree) }))) CLOSE_BRACE["}"])) + CLOSE_BRACE["}"] +; + +magicCatchVar: + -> IDENTIFIER["__dummyStaticConstructorCatchVar" + dummyStaticConstructorCatchVarCtr++]; + +magicThrowableType[bool isOn, IToken tok]: + -> {isOn}? ^(TYPE[tok, "TYPE"] IDENTIFIER[tok, Cfg.TranslatorExceptionIsThrowable ? "Throwable" : "Exception"]) + -> +;