diff --git a/CSharpTranslator/antlr3/cs2jTest/TestDLLs/Various.cs b/CSharpTranslator/antlr3/cs2jTest/TestDLLs/Various.cs index de05828..7a09141 100644 --- a/CSharpTranslator/antlr3/cs2jTest/TestDLLs/Various.cs +++ b/CSharpTranslator/antlr3/cs2jTest/TestDLLs/Various.cs @@ -13,7 +13,7 @@ namespace cs2jTest.Various.Features.join.yield public const int MY_IMPORTANT_INT = 23; public const String MY_IMPORTANT_STRING = "Kevin"; - + private int myInt = 0; private System.Collections.Generic.IDictionary myDict; public string TestRWProperty {get; set;} @@ -55,7 +55,11 @@ namespace cs2jTest.Various.Features.join.yield return "Various"; } - + public static Various operator ++(Various v) { + Various temp = new Various(); + temp.myInt = v.myInt+1; + return temp; + } } } diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs index 0d6d711..d334fef 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs @@ -1452,6 +1452,26 @@ namespace RusticiSoftware.Translator.CLR } } + private List _unaryOps = null; + [XmlArrayItem("UnaryOp")] + public List UnaryOps { + get { + if (_unaryOps == null) + _unaryOps = new List (); + return _unaryOps; + } + } + + private List _binaryOps = null; + [XmlArrayItem("BinaryOp")] + public List BinaryOps { + get { + if (_binaryOps == null) + _binaryOps = new List (); + return _binaryOps; + } + } + public ClassRepTemplate () { } @@ -1515,6 +1535,25 @@ namespace RusticiSoftware.Translator.CLR } } + if (UnaryOps != other.UnaryOps) { + if (UnaryOps == null || other.UnaryOps == null || UnaryOps.Count != other.UnaryOps.Count) + return false; + for (int i = 0; i < UnaryOps.Count; i++) { + if (UnaryOps[i] != other.UnaryOps[i]) + return false; + } + } + + if (BinaryOps != other.BinaryOps) { + if (BinaryOps == null || other.BinaryOps == null || BinaryOps.Count != other.BinaryOps.Count) + return false; + for (int i = 0; i < BinaryOps.Count; i++) { + if (BinaryOps[i] != other.BinaryOps[i]) + return false; + } + } + + return base.Equals(other); } diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g index 7c27a5c..2bf8b28 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g @@ -187,7 +187,7 @@ class_member_declaration: | (member_name '.' 'this') => type_name '.' indexer_declaration[$rt.thetext, $type_name.thetext+"."] | indexer_declaration[$rt.thetext, ""] //this | field_declaration[$rt.thetext] // qid - | operator_declaration + | operator_declaration[$rt.thetext] ) // common_modifiers// (method_modifiers | field_modifiers) @@ -948,7 +948,7 @@ struct_member_declaration: | (member_name '.' 'this') => type_name '.' indexer_declaration[$rt.thetext, $type_name.thetext+"."] | indexer_declaration[$rt.thetext, ""] //this | field_declaration[$rt.thetext] // qid - | operator_declaration + | operator_declaration[$rt.thetext] ) // common_modifiers// (method_modifiers | field_modifiers) @@ -971,19 +971,33 @@ indexer_declarator [string returnType, string prefix]: ; /////////////////////////////////////////////////////// -operator_declaration: - operator_declarator operator_body ; -operator_declarator: +operator_declaration [string returnType]: + operator_declarator[$returnType] operator_body ; +operator_declarator [string returnType] +@init { + string opText = ""; + List paramList = new List(); + bool unaryOp = false; +} +@after { + MethodRepTemplate meth = new MethodRepTemplate($returnType, opText, null, paramList); + if (unaryOp) { + ((ClassRepTemplate)$NSContext::currentTypeRep).UnaryOps.Add(meth); + } + else { + ((ClassRepTemplate)$NSContext::currentTypeRep).BinaryOps.Add(meth); + } +}: 'operator' - (('+' | '-') '(' type identifier (binary_operator_declarator | unary_operator_declarator) - | overloadable_unary_operator unary_operator_declarator - | overloadable_binary_operator binary_operator_declarator) ; + (('+' { opText = "+"; } | '-' { opText = "-"; }) '(' t0=type i0=identifier { paramList.Add(new ParamRepTemplate($t0.thetext, $i0.text)); } (binary_operator_declarator[paramList] | unary_operator_declarator { unaryOp = true; }) + | overloadable_unary_operator { opText = $overloadable_unary_operator.text; } '(' t1=type i1=identifier { paramList.Add(new ParamRepTemplate($t1.thetext, $i1.text)); } unary_operator_declarator { unaryOp = true; } + | overloadable_binary_operator { opText = $overloadable_binary_operator.text; } '(' t2=type i2=identifier { paramList.Add(new ParamRepTemplate($t2.thetext, $i2.text)); } binary_operator_declarator[paramList] ) ; unary_operator_declarator: ')' ; overloadable_unary_operator: /*'+' | '-' | */ '!' | '~' | '++' | '--' | 'true' | 'false' ; -binary_operator_declarator: - ',' type identifier ')' ; +binary_operator_declarator [List paramList]: + ',' type identifier ')' { $paramList.Add(new ParamRepTemplate($type.thetext, $identifier.text)); } ; // >> check needed overloadable_binary_operator: /*'+' | '-' | */ '*' | '/' | '%' | '&' | '|' | '^' | '<<' | '>' '>' | '==' | '!=' | '>' | '<' | '>=' | '<=' ; @@ -991,7 +1005,9 @@ overloadable_binary_operator: conversion_operator_declaration: conversion_operator_declarator operator_body ; conversion_operator_declarator: - ('implicit' | 'explicit') 'operator' type '(' type identifier ')' ; + (i='implicit' { Warning($i.line, "[UNSUPPORTED] implicit user defined casts, an explicit cast is always required."); } | 'explicit') 'operator' tt=type '(' tf=type identifier ')' + { ((ClassRepTemplate)$NSContext::currentTypeRep).Casts.Add(new CastRepTemplate($tf.thetext, $tt.thetext)); } + ; operator_body: block ; diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g index f43f428..54f421e 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g @@ -812,8 +812,8 @@ operator_declaration: operator_declarator: 'operator' (('+' | '-') '(' type identifier (binary_operator_declarator | unary_operator_declarator) - | overloadable_unary_operator unary_operator_declarator - | overloadable_binary_operator binary_operator_declarator) ; + | overloadable_unary_operator '(' type identifier unary_operator_declarator + | overloadable_binary_operator '(' type identifier binary_operator_declarator) ; unary_operator_declarator: ')' ; overloadable_unary_operator: