From 66094e129bbed47e27facb41cd6bc3bd78558358 Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Mon, 17 Jan 2011 18:24:26 +0100 Subject: [PATCH] fix resolve for enums --- .../src/cs2j/CLR/TranslationTemplate.cs | 167 +++++++++++------- .../antlr3/src/cs2j/CSharp/NetMaker.g | 22 +-- 2 files changed, 105 insertions(+), 84 deletions(-) diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs index f61cfb3..916016f 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs @@ -838,7 +838,7 @@ namespace RusticiSoftware.Translator.CLR } public override string mkJava() { - return Name; + return "${this}." + Name; } @@ -908,6 +908,29 @@ namespace RusticiSoftware.Translator.CLR } + private string[] _inherits; + [XmlArrayItem("Type")] + public string[] Inherits { + get { + if (_inherits == null) + { + _inherits = new string[] { "System.Object" }; + } + return _inherits; + } + set { + if (value != null) { + _inherits= new string[value.Length]; + for (int i = 0; i < value.Length; i++) { + _inherits[i] = value[i].Replace('<','[').Replace('>',']'); + } + } + else { + _inherits = null; + } + } + } + protected TypeRepTemplate (string typeName) : this() { TypeName = typeName; @@ -937,8 +960,59 @@ namespace RusticiSoftware.Translator.CLR } } + public virtual ResolveResult Resolve(String name, List args, DirectoryHT AppEnv) + { + if (Inherits != null) + { + foreach (String b in Inherits) + { + TypeRepTemplate baseType = AppEnv.Search(Uses, b); + if (baseType != null) + { + ResolveResult ret = baseType.Resolve(name,args,AppEnv); + if (ret != null) + return ret; + } + } + } + return null; + } + + public virtual ResolveResult Resolve(String name, DirectoryHT AppEnv) + { + if (Inherits != null) + { + foreach (String b in Inherits) + { + TypeRepTemplate baseType = AppEnv.Search(Uses, b); + if (baseType != null) + { + ResolveResult ret = baseType.Resolve(name,AppEnv); + if (ret != null) + return ret; + } + } + } + return null; + } + // Returns true if other is a subclass, or implements our interface public virtual bool IsA (TypeRepTemplate other, DirectoryHT AppEnv) { + if (other.TypeName == this.TypeName) + { + return true; + } + if (Inherits != null) + { + foreach (String ibase in Inherits) + { + TypeRepTemplate tbase = AppEnv.Search(ibase, new UnknownRepTemplate(ibase)); + if (tbase.IsA(other,AppEnv)) + { + return true; + } + } + } return false; } @@ -1123,6 +1197,24 @@ namespace RusticiSoftware.Translator.CLR return new EnumRep (); } + public override ResolveResult Resolve(String name, DirectoryHT AppEnv) + { + if (Members != null) + { + foreach (EnumMemberRepTemplate m in Members) + { + if (m.Name == name) + { + ResolveResult res = new ResolveResult(); + res.Result = m; + res.ResultType = this; + return res; + } + } + } + return base.Resolve(name, AppEnv); + } + #region Equality public bool Equals (EnumRepTemplate other) { @@ -1273,30 +1365,6 @@ namespace RusticiSoftware.Translator.CLR [XmlType("Interface")] public class InterfaceRepTemplate : TypeRepTemplate, IEquatable { - - private string[] _inherits; - [XmlArrayItem("Type")] - public string[] Inherits { - get { - if (_inherits == null) - { - _inherits = new string[] { "System.Object" }; - } - return _inherits; - } - set { - if (value != null) { - _inherits= new string[value.Length]; - for (int i = 0; i < value.Length; i++) { - _inherits[i] = value[i].Replace('<','[').Replace('>',']'); - } - } - else { - _inherits = null; - } - } - } - private List _methods = null; [XmlArrayItem("Method")] public List Methods { @@ -1368,18 +1436,7 @@ namespace RusticiSoftware.Translator.CLR { return true; } - if (Inherits != null) - { - foreach (String ibase in Inherits) - { - TypeRepTemplate tbase = AppEnv.Search(ibase, new UnknownRepTemplate(ibase)); - if (tbase.IsA(other,AppEnv)) - { - return true; - } - } - } - return false; + return base.IsA(other,AppEnv); } public override TypeRep mkEmptyRep () @@ -1387,7 +1444,7 @@ namespace RusticiSoftware.Translator.CLR return new InterfaceRep (); } - public virtual ResolveResult Resolve(String name, DirectoryHT AppEnv) + public override ResolveResult Resolve(String name, DirectoryHT AppEnv) { if (Properties != null) @@ -1403,23 +1460,10 @@ namespace RusticiSoftware.Translator.CLR } } } - if (Inherits != null) - { - foreach (String b in Inherits) - { - InterfaceRepTemplate baseType = AppEnv.Search(Uses, b) as InterfaceRepTemplate; - if (baseType != null) - { - ResolveResult ret = baseType.Resolve(name,AppEnv); - if (ret != null) - return ret; - } - } - } - return null; + return base.Resolve(name,AppEnv); } - public virtual ResolveResult Resolve(String name, List args, DirectoryHT AppEnv) + public override ResolveResult Resolve(String name, List args, DirectoryHT AppEnv) { if (Methods != null) @@ -1464,20 +1508,7 @@ namespace RusticiSoftware.Translator.CLR } } } - if (Inherits != null) - { - foreach (String b in Inherits) - { - InterfaceRepTemplate baseType = AppEnv.Search(Uses, b) as InterfaceRepTemplate; - if (baseType != null) - { - ResolveResult ret = baseType.Resolve(name,args,AppEnv); - if (ret != null) - return ret; - } - } - } - return null; + return base.Resolve(name, args, AppEnv); } @@ -1553,7 +1584,7 @@ namespace RusticiSoftware.Translator.CLR public static bool operator != (InterfaceRepTemplate a1, InterfaceRepTemplate a2) { return !(a1 == a2); - } + } public override int GetHashCode () { diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g index 4ced39d..81af229 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g @@ -147,16 +147,6 @@ scope SymTab { return (CommonTree)adaptor.RulePostProcessing(root); } - // Resolve Routines -// protected ResolveResult ResolveGetter(InterfaceRepTemplate thisType, String name) { -// ResolveResult ret = null; -// PropRepTemplate entity = thisType.Resolve(name, AppEnv) as PropRepTemplate; -// if (entity != null) { -// ret = entity.JavaGet; -// } -// return ret; -// } - protected CommonTree dupTree(CommonTree t) { return (CommonTree)adaptor.DupTree(t); } @@ -219,7 +209,7 @@ scope { @init { $primary_expression::parentIsApply = false; CommonTree ret = null; - InterfaceRepTemplate expType = SymTabLookup("this") as InterfaceRepTemplate; + TypeRepTemplate expType = SymTabLookup("this"); bool implicitThis = true; } @after { @@ -228,7 +218,7 @@ scope { }: ^(INDEX expression expression_list?) | (^(APPLY (^('.' expression identifier)|identifier) argument_list?)) => - ^(APPLY (^('.' e2=expression {expType = $e2.dotNetType as InterfaceRepTemplate; implicitThis = false;} i2=identifier)|i2=identifier) argument_list?) + ^(APPLY (^('.' e2=expression {expType = $e2.dotNetType; implicitThis = false;} i2=identifier)|i2=identifier) argument_list?) { if (expType != null) { ResolveResult methodResult = expType.Resolve($i2.thetext, $argument_list.argTypes ?? new List(), AppEnv); @@ -257,7 +247,7 @@ scope { // - accessing a property/field of some object // - a qualified type name // - part of a qualified type name - expType = $e1.dotNetType as InterfaceRepTemplate; + expType = $e1.dotNetType; // Is it a property read? Ensure we are not being applied to arguments or about to be assigned if (expType != null && @@ -307,7 +297,7 @@ scope { } if (!found) { // Not a variable, is it a property? - InterfaceRepTemplate thisType = SymTabLookup("this") as InterfaceRepTemplate; + TypeRepTemplate thisType = SymTabLookup("this"); // Is it a property read? Ensure we are not being applied to arguments or about to be assigned if (thisType != null && @@ -650,7 +640,7 @@ assignment ((^('.' expression identifier generic_argument_list?) | identifier) '=') => (^('.' se=expression i=identifier generic_argument_list?) | i=identifier { isThis = true;}) a='=' rhs=expression { - InterfaceRepTemplate seType = (isThis ? SymTabLookup("this") : $se.dotNetType) as InterfaceRepTemplate; + TypeRepTemplate seType = (isThis ? SymTabLookup("this") : $se.dotNetType); if (seType != null) { ResolveResult fieldResult = seType.Resolve($i.thetext, AppEnv); if (fieldResult != null && fieldResult.Result is PropRepTemplate) { @@ -890,7 +880,7 @@ scope NSContext,SymTab; $SymTab::symtab["this"] = classTypeRep; ClassRepTemplate baseType = ObjectType; if (classTypeRep.Inherits != null && classTypeRep.Inherits.Length > 0) { - // if Inherits[0] is a class tyhen it is parent, else system.object + // if Inherits[0] is a class then it is parent, else system.object ClassRepTemplate parent = AppEnv.Search(classTypeRep.Uses, classTypeRep.Inherits[0], ObjectType) as ClassRepTemplate; if (parent != null) baseType = parent;