From 3655ee59b950cfd645fc3a0364a4f031ad439102 Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Mon, 17 Jan 2011 15:32:26 +0100 Subject: [PATCH] resolve for object creation --- .../src/cs2j/CLR/TranslationTemplate.cs | 46 +++++++++++++++++++ .../antlr3/src/cs2j/CSharp/NetMaker.g | 16 ++++++- .../src/cs2j/CSharp/TemplateExtracter.g | 7 ++- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs index 27684a3..ba98017 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs @@ -1692,6 +1692,52 @@ namespace RusticiSoftware.Translator.CLR return base.Resolve(name, AppEnv); } + public virtual ResolveResult Resolve(List args, DirectoryHT AppEnv) + { + + if (Constructors != null) + { + foreach (ConstructorRepTemplate c in Constructors) + { + bool matchingArgs = true; + // If either params are null then make sure both represent zero length args + if (c.Params == null || args == null) + { + // Are they both zero length? + matchingArgs = (c.Params == null || c.Params.Count == 0) && (args == null || args.Count == 0); + } + else + { + // Are num args the same? + if (c.Params.Count != args.Count) + { + matchingArgs = false; + } + else + { + // check that for each argument in the caller its type 'IsA' the type of the formal argument + for (int idx = 0; idx < c.Params.Count; idx++) { + if (args[idx] == null || !args[idx].IsA(AppEnv.Search(Uses, c.Params[idx].Type, new UnknownRepTemplate(c.Params[idx].Type)),AppEnv)) + { + matchingArgs = false; + break; + } + } + } + if (matchingArgs) + { + ResolveResult res = new ResolveResult(); + res.Result = c; + res.ResultType = AppEnv.Search(Uses, TypeName); + return res; + } + } + } + } + // We don't search base, constructors aren't inherited + return null; + } + #region Equality public bool Equals (ClassRepTemplate other) diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g index 8d3908f..4ced39d 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g @@ -343,7 +343,21 @@ scope { // ('this' brackets) => 'this' brackets primary_expression_part* // | ('base' brackets) => 'this' brackets primary_expression_part* // | primary_expression_start primary_expression_part* - | ^(NEW type argument_list? object_or_collection_initializer?) + | ^(n=NEW type argument_list? object_or_collection_initializer?) + { + ClassRepTemplate conType = $type.dotNetType as ClassRepTemplate; + ResolveResult conResult = conType.Resolve($argument_list.argTypes, AppEnv); + if (conResult != null) { + ConstructorRepTemplate conRep = conResult.Result as ConstructorRepTemplate; + Dictionary myMap = new Dictionary(); + for (int idx = 0; idx < conRep.Params.Count; idx++) { + myMap[conRep.Params[idx].Name] = wrapArgument($argument_list.argTrees[idx], $n.token); + } + ret = mkJavaWrapper(conResult.Result.Java, myMap, $n.token); + Imports.Add(conResult.Result.Imports); + $dotNetType = conResult.ResultType; + } + } | 'new' ( // try the simple one first, this has no argS and no expressions // symantically could be object creation diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g index 314ab6c..1b73169 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g @@ -1148,8 +1148,11 @@ constructor_declaration: constructor_declarator constructor_body ; constructor_declarator: identifier '(' fpl=formal_parameter_list? ')' constructor_initializer? - { ((ClassRepTemplate)$NSContext::currentTypeRep).Constructors.Add(new ConstructorRepTemplate($fpl.paramlist)); - Debug("Processing constructor declaration"); + { + ConstructorRepTemplate cRep = new ConstructorRepTemplate($fpl.paramlist); + cRep.SurroundingTypeName = $NSContext::currentTypeRep.TypeName; + ((ClassRepTemplate)$NSContext::currentTypeRep).Constructors.Add(cRep); + Debug("Processing constructor declaration"); } ; constructor_initializer: