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

pass dotnettype through expressions. build symtab for fields, vars

This commit is contained in:
Kevin Glynn 2011-01-12 09:38:52 +01:00
parent 1fc6442396
commit c24abeeae7

View File

@ -78,6 +78,28 @@ scope SymTab {
} }
} }
private ClassRepTemplate boolType = null;
protected ClassRepTemplate BoolType {
get {
if (boolType == null) {
boolType = (ClassRepTemplate)AppEnv.Search("System.Boolean", new UnknownRepTemplate("System.Boolean"));
}
return boolType;
}
}
private ClassRepTemplate voidType = null;
protected ClassRepTemplate VoidType {
get {
if (voidType == null) {
voidType = (ClassRepTemplate)AppEnv.Search("System.Void", new UnknownRepTemplate("System.Void"));
}
return voidType;
}
}
protected TypeRepTemplate SymTabLookup(string name) { protected TypeRepTemplate SymTabLookup(string name) {
return SymTabLookup(name, null); return SymTabLookup(name, null);
} }
@ -124,7 +146,7 @@ modifier:
| 'readonly' | 'volatile' | 'extern' | 'virtual' | 'override' | FINAL ; | 'readonly' | 'volatile' | 'extern' | 'virtual' | 'override' | FINAL ;
class_member_declaration: class_member_declaration:
^(CONST attributes? modifiers? type constant_declarators) ^(CONST attributes? modifiers? type constant_declarators[$type.dotNetType])
| ^(EVENT attributes? modifiers? event_declaration) | ^(EVENT attributes? modifiers? event_declaration)
| ^(METHOD attributes? modifiers? type member_name type_parameter_constraints_clauses? type_parameter_list? formal_parameter_list? method_body exception*) | ^(METHOD attributes? modifiers? type member_name type_parameter_constraints_clauses? type_parameter_list? formal_parameter_list? method_body exception*)
| ^(INTERFACE attributes? modifiers? interface_declaration) | ^(INTERFACE attributes? modifiers? interface_declaration)
@ -144,12 +166,12 @@ exception:
primary_expression returns [TypeRepTemplate dotNetType]: primary_expression returns [TypeRepTemplate dotNetType]:
^(INDEX expression expression_list?) ^(INDEX expression expression_list?)
| ^(APPLY identifier argument_list?) // | ^(APPLY identifier argument_list?)
| ^(APPLY ^('.' expression identifier) argument_list?) // | ^(APPLY ^('.' expression identifier) argument_list?)
// | ^(APPLY expression argument_list?) | ^(APPLY expression argument_list?)
| ^(POSTINC expression) | ^(POSTINC expression)
| ^(POSTDEC expression) | ^(POSTDEC expression)
| ^(access_operator expression type_or_generic) | ^(access_operator expression identifier generic_argument_list?)
| predefined_type { $dotNetType = $predefined_type.dotNetType; } | predefined_type { $dotNetType = $predefined_type.dotNetType; }
| 'this' { $dotNetType = SymTabLookup("this"); } | 'this' { $dotNetType = SymTabLookup("this"); }
| SUPER { $dotNetType = SymTabLookup("super"); } | SUPER { $dotNetType = SymTabLookup("super"); }
@ -236,9 +258,9 @@ member_declarator_list:
member_declarator (',' member_declarator)* ; member_declarator (',' member_declarator)* ;
member_declarator: member_declarator:
qid ('=' expression)? ; qid ('=' expression)? ;
primary_or_array_creation_expression: primary_or_array_creation_expression returns [TypeRepTemplate dotNetType]:
(array_creation_expression) => array_creation_expression (array_creation_expression) => array_creation_expression
| primary_expression | primary_expression { $dotNetType = $primary_expression.dotNetType; }
; ;
// new Type[2] { } // new Type[2] { }
array_creation_expression: array_creation_expression:
@ -343,8 +365,8 @@ commas:
// Type Section // Type Section
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
type_name returns [TypeRepTemplate dotNetType]: type_name returns [string name, TypeRepTemplate dotNetType]:
namespace_or_type_name { $dotNetType = findType($namespace_or_type_name.name); } ; namespace_or_type_name { $name = $namespace_or_type_name.name; $dotNetType = findType($namespace_or_type_name.name); } ;
namespace_or_type_name returns [String name, List<string> tyargs]: namespace_or_type_name returns [String name, List<string> tyargs]:
type_or_generic { $name = $type_or_generic.name; $tyargs = $type_or_generic.tyargs; } type_or_generic { $name = $type_or_generic.name; $tyargs = $type_or_generic.tyargs; }
| ^('::' namespace_or_type_name type_or_generic) { $name = "System.Object"; } // give up, we don't support these | ^('::' namespace_or_type_name type_or_generic) { $name = "System.Object"; } // give up, we don't support these
@ -430,8 +452,8 @@ statement_list:
// Expression Section // Expression Section
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
expression returns [TypeRepTemplate dotNetType]: expression returns [TypeRepTemplate dotNetType]:
(unary_expression assignment_operator) => assignment (unary_expression assignment_operator) => assignment { $dotNetType = VoidType; }
| non_assignment_expression | non_assignment_expression { $dotNetType = $non_assignment_expression.dotNetType; }
; ;
expression_list: expression_list:
expression (',' expression)* ; expression (',' expression)* ;
@ -442,7 +464,7 @@ unary_expression returns [TypeRepTemplate dotNetType]:
//(cast_expression) => cast_expression //(cast_expression) => cast_expression
^(CAST_EXPR type unary_expression) { $dotNetType = $type.dotNetType; } ^(CAST_EXPR type unary_expression) { $dotNetType = $type.dotNetType; }
| primary_or_array_creation_expression | primary_or_array_creation_expression { $dotNetType = $primary_or_array_creation_expression.dotNetType; }
| ^(MONOPLUS u1=unary_expression) { $dotNetType = $u1.dotNetType; } | ^(MONOPLUS u1=unary_expression) { $dotNetType = $u1.dotNetType; }
| ^(MONOMINUS u2=unary_expression) { $dotNetType = $u2.dotNetType; } | ^(MONOMINUS u2=unary_expression) { $dotNetType = $u2.dotNetType; }
| ^(MONONOT u3=unary_expression) { $dotNetType = $u3.dotNetType; } | ^(MONONOT u3=unary_expression) { $dotNetType = $u3.dotNetType; }
@ -466,33 +488,34 @@ assignment_operator:
//addressof_expression: //addressof_expression:
// '&' unary_expression ; // '&' unary_expression ;
non_assignment_expression: non_assignment_expression returns [TypeRepTemplate dotNetType]:
//'non ASSIGNment' //'non ASSIGNment'
(anonymous_function_signature '=>') => lambda_expression (anonymous_function_signature '=>') => lambda_expression
| (query_expression) => query_expression | (query_expression) => query_expression
| ^(COND_EXPR non_assignment_expression expression expression) | ^(COND_EXPR non_assignment_expression e1=expression e2=expression) {$dotNetType = $e1.dotNetType; }
| ^('??' non_assignment_expression non_assignment_expression) | ^('??' n1=non_assignment_expression non_assignment_expression) {$dotNetType = $n1.dotNetType; }
| ^('||' non_assignment_expression non_assignment_expression) | ^('||' n2=non_assignment_expression non_assignment_expression) {$dotNetType = $n2.dotNetType; }
| ^('&&' non_assignment_expression non_assignment_expression) | ^('&&' n3=non_assignment_expression non_assignment_expression) {$dotNetType = $n3.dotNetType; }
| ^('|' non_assignment_expression non_assignment_expression) | ^('|' n4=non_assignment_expression non_assignment_expression) {$dotNetType = $n4.dotNetType; }
| ^('^' non_assignment_expression non_assignment_expression) | ^('^' n5=non_assignment_expression non_assignment_expression) {$dotNetType = $n5.dotNetType; }
| ^('&' non_assignment_expression non_assignment_expression) | ^('&' n6=non_assignment_expression non_assignment_expression) {$dotNetType = $n6.dotNetType; }
| ^('==' non_assignment_expression non_assignment_expression) | ^('==' non_assignment_expression non_assignment_expression) {$dotNetType = BoolType; }
| ^('!=' non_assignment_expression non_assignment_expression) | ^('!=' non_assignment_expression non_assignment_expression) {$dotNetType = BoolType; }
| ^('>' non_assignment_expression non_assignment_expression) | ^('>' non_assignment_expression non_assignment_expression) {$dotNetType = BoolType; }
| ^('<' non_assignment_expression non_assignment_expression) | ^('<' non_assignment_expression non_assignment_expression) {$dotNetType = BoolType; }
| ^('>=' non_assignment_expression non_assignment_expression) | ^('>=' non_assignment_expression non_assignment_expression) {$dotNetType = BoolType; }
| ^('<=' non_assignment_expression non_assignment_expression) | ^('<=' non_assignment_expression non_assignment_expression) {$dotNetType = BoolType; }
| ^(INSTANCEOF non_assignment_expression non_nullable_type) | ^(INSTANCEOF non_assignment_expression non_nullable_type) {$dotNetType = BoolType; }
| ^('<<' non_assignment_expression non_assignment_expression) | ^('<<' n7=non_assignment_expression non_assignment_expression) {$dotNetType = $n7.dotNetType; }
| ^('>>' non_assignment_expression non_assignment_expression) | ^('>>' n8=non_assignment_expression non_assignment_expression) {$dotNetType = $n8.dotNetType; }
| ^('+' non_assignment_expression non_assignment_expression) // TODO: need to munge these numeric types
| ^('-' non_assignment_expression non_assignment_expression) | ^('+' n9=non_assignment_expression non_assignment_expression) {$dotNetType = $n9.dotNetType; }
| ^('*' non_assignment_expression non_assignment_expression) | ^('-' n10=non_assignment_expression non_assignment_expression) {$dotNetType = $n10.dotNetType; }
| ^('/' non_assignment_expression non_assignment_expression) | ^('*' n11=non_assignment_expression non_assignment_expression) {$dotNetType = $n11.dotNetType; }
| ^('%' non_assignment_expression non_assignment_expression) | ^('/' n12=non_assignment_expression non_assignment_expression) {$dotNetType = $n12.dotNetType; }
| ^('%' n13=non_assignment_expression non_assignment_expression) {$dotNetType = $n13.dotNetType; }
// | ^(UNARY_EXPRESSION unary_expression) // | ^(UNARY_EXPRESSION unary_expression)
| unary_expression | unary_expression {$dotNetType = $unary_expression.dotNetType; }
; ;
// /////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////
@ -660,7 +683,7 @@ scope NSContext,SymTab;
ClassRepTemplate baseType = ObjectType; ClassRepTemplate baseType = ObjectType;
if (classTypeRep.Inherits != null && classTypeRep.Inherits.Length > 0) { 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 tyhen it is parent, else system.object
ClassRepTemplate parent = (ClassRepTemplate)AppEnv.Search(classTypeRep.Uses, classTypeRep.Inherits[0], ObjectType) as ClassRepTemplate; ClassRepTemplate parent = AppEnv.Search(classTypeRep.Uses, classTypeRep.Inherits[0], ObjectType) as ClassRepTemplate;
if (parent != null) if (parent != null)
baseType = parent; baseType = parent;
} }
@ -700,21 +723,21 @@ class_member_declarations:
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
constant_declaration: constant_declaration:
'const' type constant_declarators ';' ; 'const' type constant_declarators[$type.dotNetType] ';' ;
constant_declarators: constant_declarators[TypeRepTemplate ty]:
constant_declarator (',' constant_declarator)* ; constant_declarator[$ty] (',' constant_declarator[$ty])* ;
constant_declarator: constant_declarator[TypeRepTemplate ty]:
identifier ('=' constant_expression)? ; identifier { $SymTab::symtab[$identifier.thetext] = $ty; } ('=' constant_expression)? ;
constant_expression: constant_expression:
expression; expression;
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
field_declaration[TypeRepTemplate fieldType]: field_declaration[TypeRepTemplate ty]:
variable_declarators[$fieldType] ; variable_declarators[$ty] ;
variable_declarators[TypeRepTemplate varType]: variable_declarators[TypeRepTemplate ty]:
variable_declarator[varType] (',' variable_declarator[varType])* ; variable_declarator[ty] (',' variable_declarator[ty])* ;
variable_declarator[TypeRepTemplate varType]: variable_declarator[TypeRepTemplate ty]:
type_name ('=' variable_initializer)? ; // eg. event EventHandler IInterface.VariableName = Foo; type_name { $SymTab::symtab[$type_name.name] = $ty; } ('=' variable_initializer)? ; // eg. event EventHandler IInterface.VariableName = Foo;
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
method_declaration method_declaration
@ -820,7 +843,7 @@ fixed_parameters:
fixed_parameter (',' fixed_parameter)* ; fixed_parameter (',' fixed_parameter)* ;
// 4.0 // 4.0
fixed_parameter: fixed_parameter:
parameter_modifier? type identifier default_argument? ; parameter_modifier? type identifier { $SymTab::symtab[$identifier.thetext] = $type.dotNetType; } default_argument? ;
// 4.0 // 4.0
default_argument: default_argument:
'=' expression; '=' expression;
@ -841,7 +864,11 @@ interface_body:
'{' interface_member_declarations? '}' ; '{' interface_member_declarations? '}' ;
interface_member_declarations: interface_member_declarations:
interface_member_declaration+ ; interface_member_declaration+ ;
interface_member_declaration: interface_member_declaration
scope SymTab;
@init {
$SymTab::symtab = new Dictionary<string,TypeRepTemplate>();
}:
^(EVENT attributes? modifiers? event_declaration) ^(EVENT attributes? modifiers? event_declaration)
| ^(METHOD attributes? modifiers? type identifier type_parameter_constraints_clauses? type_parameter_list? formal_parameter_list? exception*) | ^(METHOD attributes? modifiers? type identifier type_parameter_constraints_clauses? type_parameter_list? formal_parameter_list? exception*)
| ^(INDEXER attributes? modifiers? type type_name? indexer_declaration) | ^(INDEXER attributes? modifiers? type type_name? indexer_declaration)
@ -936,15 +963,15 @@ 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 local_variable_declarators[$local_variable_type.dotNetType] ;
local_variable_type: local_variable_type returns [TypeRepTemplate dotNetType]:
('var') => 'var' ('var') => 'var' { $dotNetType = new UnknownRepTemplate("System.Object"); }
| ('dynamic') => 'dynamic' | ('dynamic') => 'dynamic' { $dotNetType = new UnknownRepTemplate("System.Object"); }
| type ; | type { $dotNetType = $type.dotNetType; };
local_variable_declarators: local_variable_declarators[TypeRepTemplate ty]:
local_variable_declarator (',' local_variable_declarator)* ; local_variable_declarator[$ty] (',' local_variable_declarator[$ty])* ;
local_variable_declarator: local_variable_declarator[TypeRepTemplate ty]:
identifier ('=' local_variable_initializer)? ; identifier { $SymTab::symtab[$identifier.thetext] = $ty; } ('=' local_variable_initializer)? ;
local_variable_initializer: local_variable_initializer:
expression expression
| array_initializer | array_initializer
@ -952,7 +979,7 @@ local_variable_initializer:
stackalloc_initializer: stackalloc_initializer:
'stackalloc' unmanaged_type '[' expression ']' ; 'stackalloc' unmanaged_type '[' expression ']' ;
local_constant_declaration: local_constant_declaration:
'const' type constant_declarators ; 'const' type constant_declarators[$type.dotNetType] ;
expression_statement: expression_statement:
expression ';' ; expression ';' ;
@ -1000,8 +1027,12 @@ goto_statement:
| 'default') ';' ; | 'default') ';' ;
catch_clauses: catch_clauses:
catch_clause+ ; catch_clause+ ;
catch_clause: catch_clause
^('catch' class_type identifier block) ; scope SymTab;
@init {
$SymTab::symtab = new Dictionary<string,TypeRepTemplate>();
}:
^('catch' class_type identifier { $SymTab::symtab[$identifier.thetext] = $class_type.dotNetType; } block) ;
finally_clause: finally_clause:
^('finally' block) ; ^('finally' block) ;
checked_statement: checked_statement: