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

in var x = exp; If we know the type of exp then use it for x

This commit is contained in:
Kevin Glynn 2011-05-23 12:23:59 +02:00
parent e99f0edee7
commit 10261f6406

View File

@ -2676,17 +2676,31 @@ declaration_statement:
(local_variable_declaration (local_variable_declaration
| local_constant_declaration) ';' ; | local_constant_declaration) ';' ;
local_variable_declaration: local_variable_declaration:
local_variable_type local_variable_declarators[$local_variable_type.tree, $local_variable_type.dotNetType] ; local_variable_type local_variable_declarators[$local_variable_type.tree, $local_variable_type.dotNetType, $local_variable_type.isVar]
local_variable_type returns [bool isTypeNode, TypeRepTemplate dotNetType] -> {$local_variable_type.isVar && $local_variable_declarators.bestTy != null && !$local_variable_declarators.bestTy.IsUnknownType}?
^(TYPE[$local_variable_type.tree.Token, "TYPE"] IDENTIFIER[$local_variable_type.tree.Token, $local_variable_declarators.bestTy.mkFormattedTypeName(false, "<",">")]) local_variable_declarators
-> local_variable_type local_variable_declarators
;
local_variable_type returns [bool isTypeNode, bool isVar, TypeRepTemplate dotNetType]
@init { @init {
$isTypeNode = false; $isTypeNode = false;
$isVar = false;
}: }:
TYPE_VAR { $dotNetType = new UnknownRepTemplate("System.Object"); } TYPE_VAR { $dotNetType = new UnknownRepTemplate("System.Object"); $isVar = true;}
| TYPE_DYNAMIC { $dotNetType = new UnknownRepTemplate("System.Object"); } | TYPE_DYNAMIC { $dotNetType = new UnknownRepTemplate("System.Object"); }
| type { $dotNetType = $type.dotNetType; $isTypeNode = true; }; | type { $dotNetType = $type.dotNetType; $isTypeNode = true; };
local_variable_declarators[CommonTree tyTree, TypeRepTemplate ty]: local_variable_declarators[CommonTree tyTree, TypeRepTemplate ty, bool isVar] returns [TypeRepTemplate bestTy]:
local_variable_declarator[$tyTree, $ty] (',' local_variable_declarator[$tyTree, $ty])* ; d1=local_variable_declarator[$tyTree, $ty] { if ($isVar) $bestTy = $d1.dotNetType; }
local_variable_declarator[CommonTree tyTree, TypeRepTemplate ty] (',' dn=local_variable_declarator[$tyTree, $ty]
{
if ($isVar) {
if (!$dn.dotNetType.IsUnknownType && $bestTy.IsA($dn.dotNetType, AppEnv)) {
$bestTy = $dn.dotNetType;
}
}
}
)* ;
local_variable_declarator[CommonTree tyTree, TypeRepTemplate ty] returns [TypeRepTemplate dotNetType]
@init { @init {
bool hasInit = false; bool hasInit = false;
bool constructStruct = $ty != null && $ty is StructRepTemplate ; bool constructStruct = $ty != null && $ty is StructRepTemplate ;
@ -2699,7 +2713,7 @@ local_variable_declarator[CommonTree tyTree, TypeRepTemplate ty]
} }
}: }:
i=identifier { $SymTab::symtab[$i.thetext] = $ty; } i=identifier { $SymTab::symtab[$i.thetext] = $ty; }
(e='=' local_variable_initializer[$ty ?? ObjectType] { hasInit = true; constructStruct = false; constructEnum = false; } )? (e='=' local_variable_initializer[$ty ?? ObjectType] { hasInit = true; constructStruct = false; constructEnum = false; $dotNetType = $local_variable_initializer.dotNetType; } )?
magicConstructStruct[constructStruct, $tyTree, ($i.tree != null ? $i.tree.Token : null)] magicConstructStruct[constructStruct, $tyTree, ($i.tree != null ? $i.tree.Token : null)]
magicConstructDefaultEnum[constructEnum, $ty, zeroEnum, $identifier.tree != null ? $identifier.tree.Token : null] magicConstructDefaultEnum[constructEnum, $ty, zeroEnum, $identifier.tree != null ? $identifier.tree.Token : null]
// eg. event EventHandler IInterface.VariableName = Foo; // eg. event EventHandler IInterface.VariableName = Foo;
@ -2708,8 +2722,11 @@ local_variable_declarator[CommonTree tyTree, TypeRepTemplate ty]
-> {constructEnum}? $i ASSIGN[$i.tree.Token, "="] magicConstructDefaultEnum -> {constructEnum}? $i ASSIGN[$i.tree.Token, "="] magicConstructDefaultEnum
-> $i -> $i
; ;
local_variable_initializer[TypeRepTemplate typeCtxt]: local_variable_initializer[TypeRepTemplate typeCtxt] returns [TypeRepTemplate dotNetType]
expression[$typeCtxt] @init {
$dotNetType = ObjectType;
}:
expression[$typeCtxt] { $dotNetType = $expression.dotNetType; }
| array_initializer | array_initializer
| stackalloc_initializer; | stackalloc_initializer;
stackalloc_initializer: stackalloc_initializer: