1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

resolve for object creation

This commit is contained in:
Kevin Glynn 2011-01-17 15:32:26 +01:00
parent 7b8e66413f
commit 3655ee59b9
3 changed files with 66 additions and 3 deletions

View File

@ -1692,6 +1692,52 @@ namespace RusticiSoftware.Translator.CLR
return base.Resolve(name, AppEnv);
}
public virtual ResolveResult Resolve(List<TypeRepTemplate> args, DirectoryHT<TypeRepTemplate> 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)

View File

@ -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<string,CommonTree> myMap = new Dictionary<string,CommonTree>();
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

View File

@ -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: