* ILParser.jay: Add module refs, allow them to be referenced.
[mono.git] / mcs / ilasm / parser / ILParser.jay
index 0ce9c42ef1af58206f7237f2c55f28f9d5cd1e8c..886f47990a93a1012a8fbb9d239ca671e07883d8 100644 (file)
@@ -24,11 +24,12 @@ namespace Mono.ILASM {
                 private string pinvoke_mod;\r
                 private string pinvoke_meth;\r
                 private PEAPI.PInvokeAttr pinvoke_attr;\r
-                private StreamReader stream;\r
+                private ILTokenizer tokenizer;\r
                                 \r
-               public ILParser (CodeGen codegen, StreamReader stream) {\r
+               public ILParser (CodeGen codegen, ILTokenizer tokenizer)\r
+                {\r
                        this.codegen = codegen;\r
-                        this.stream = stream;\r
+                        this.tokenizer = tokenizer;\r
                }\r
 \r
                public CodeGen CodeGen {\r
@@ -609,6 +610,13 @@ class_ref          : OPEN_BRACKET slashed_name CLOSE_BRACKET slashed_name
                                 }\r
                           }\r
                        | OPEN_BRACKET D_MODULE slashed_name CLOSE_BRACKET slashed_name\r
+                          {\r
+                                if (codegen.IsThisModule ((string) $3)) {\r
+                                        $$ = new TypeRef ((string) $5, false, null);\r
+                                } else {\r
+                                        $$ = codegen.ExternTable.GetModuleTypeRef ((string) $3, (string) $5, false);\r
+                                }\r
+                          }\r
                        | slashed_name\r
                           {\r
                                 PrimitiveTypeRef prim = PrimitiveTypeRef.GetPrimitiveType ((string) $1);\r
@@ -775,6 +783,10 @@ type                       : K_CLASS class_ref
                           {\r
                                 $$ = new PrimitiveTypeRef (PrimitiveType.Char, "System.Char");\r
                           }\r
+                       | K_WCHAR\r
+                         {\r
+                               $$ = new PrimitiveTypeRef (PrimitiveType.Char, "System.Char");\r
+                         }\r
                        | K_VOID\r
                           {\r
                                 $$ = new PrimitiveTypeRef (PrimitiveType.Void, "System.Void");\r
@@ -870,6 +882,10 @@ bounds                     : bound
                        ;\r
 \r
 bound                  : /* EMPTY */\r
+                          {\r
+                                // This is shortref for no lowerbound or size\r
+                                $$ = new DictionaryEntry (TypeRef.Ellipsis, TypeRef.Ellipsis);\r
+                          }\r
                        | ELLIPSIS\r
                           {\r
                                 // No lower bound or size\r
@@ -1127,11 +1143,11 @@ field_decl              : D_FIELD repeat_opt field_attr type id at_opt init_opt
                                 if ($2 != null) {\r
                                         field_def.SetOffset ((uint) $2);\r
                                 }\r
-                                \r
+\r
                                 if ($6 != null) {\r
                                         field_def.AddDataValue ((string) $6);\r
                                 }\r
-                                \r
+\r
                                 if ($7 != null) {\r
                                         field_def.SetValue ((Constant) $7);\r
                                 }\r
@@ -1235,7 +1251,7 @@ field_init                : K_FLOAT32 OPEN_PARENS float64 CLOSE_PARENS
                           }\r
                        | K_FLOAT64 OPEN_PARENS int64 CLOSE_PARENS\r
                           {\r
-                                $$ = new DoubleConst (Convert.ToDouble (3));\r
+                                $$ = new DoubleConst (Convert.ToDouble ($3));\r
                           }\r
                        | K_INT64 OPEN_PARENS int64 CLOSE_PARENS\r
                           {\r
@@ -1253,6 +1269,10 @@ field_init               : K_FLOAT32 OPEN_PARENS float64 CLOSE_PARENS
                           {\r
                                 $$ = new CharConst (Convert.ToChar ($3));\r
                           }\r
+                       | K_WCHAR OPEN_PARENS int64 CLOSE_PARENS\r
+                         {\r
+                               $$ = new CharConst (Convert.ToChar ($3));\r
+                         }\r
                        | K_INT8 OPEN_PARENS int64 CLOSE_PARENS\r
                           {\r
                                 $$ = new IntConst (Convert.ToByte ($3));\r
@@ -1261,9 +1281,9 @@ field_init                : K_FLOAT32 OPEN_PARENS float64 CLOSE_PARENS
                           {\r
                                 $$ = new BoolConst ((bool) $3);\r
                           }\r
-                       | K_BYTEARRAY OPEN_PARENS bytes CLOSE_PARENS\r
+                       | K_BYTEARRAY bytes_list\r
                           {\r
-                                $$ = new ByteArrConst ((byte[]) $3);\r
+                                $$ = new ByteArrConst ((byte[]) $2);\r
                           }\r
                        | comp_qstring\r
                           {\r
@@ -1330,23 +1350,28 @@ dataitem                : K_CHAR STAR OPEN_PARENS comp_qstring CLOSE_PARENS
                           {\r
                                 $$ = new StringConst ((string) $4);\r
                           }\r
+                       | K_WCHAR STAR OPEN_PARENS comp_qstring CLOSE_PARENS\r
+                         {\r
+                               $$ = new StringConst ((string) $4);\r
+                         }\r
                        | AMPERSAND OPEN_PARENS id CLOSE_PARENS\r
                           {\r
                            //     DataDef def = codegen.CurrentTypeDef.GetDataDef ((string) $3);\r
                            //     $$ = new AddressConstant ((DataConstant) def.PeapiConstant);\r
                           }\r
-                       | K_BYTEARRAY ASSIGN OPEN_PARENS bytes CLOSE_PARENS\r
+                       | K_BYTEARRAY ASSIGN bytes_list\r
                           {\r
-                                $$ = new ByteArrConst ((byte[]) $4);\r
+                                $$ = new ByteArrConst ((byte[]) $3);\r
                           }\r
-                       | K_BYTEARRAY OPEN_PARENS bytes CLOSE_PARENS\r
+                       | K_BYTEARRAY bytes_list\r
                           {\r
                                 // ******** THIS IS NOT IN THE SPECIFICATION ******** //\r
-                                $$ = new ByteArrConst ((byte[]) $3);\r
+                                $$ = new ByteArrConst ((byte[]) $2);\r
                           }\r
                        | K_FLOAT32 OPEN_PARENS float64 CLOSE_PARENS repeat_opt\r
                           {\r
-                                FloatConst float_const = new FloatConst ((float) $3);\r
+                                double d = (double) $3;\r
+                                FloatConst float_const = new FloatConst ((float) d);\r
 \r
                                 if ($5 != null)\r
                                         $$ = new RepeatedConstant (float_const, (int) $5);\r
@@ -1382,7 +1407,8 @@ dataitem          : K_CHAR STAR OPEN_PARENS comp_qstring CLOSE_PARENS
                           }\r
                        | K_INT16 OPEN_PARENS int32 CLOSE_PARENS repeat_opt\r
                           {\r
-                                IntConst int_const = new IntConst ((short) $3);\r
+                                int i = (int) $3;\r
+                                IntConst int_const = new IntConst ((short) i);\r
 \r
                                 if ($5 != null)\r
                                         $$ = new RepeatedConstant (int_const, (int) $5);\r
@@ -1391,7 +1417,8 @@ dataitem          : K_CHAR STAR OPEN_PARENS comp_qstring CLOSE_PARENS
                           }\r
                        | K_INT8 OPEN_PARENS int32 CLOSE_PARENS repeat_opt\r
                           {\r
-                                IntConst int_const = new IntConst ((sbyte) $3);\r
+                                int i = (int) $3;\r
+                                IntConst int_const = new IntConst ((sbyte) i);\r
 \r
                                 if ($5 != null)\r
                                         $$ = new RepeatedConstant (int_const, (int) $5);\r
@@ -1740,6 +1767,9 @@ type_spec         : class_ref
                                 // assembly. This is not supported in the MS version of ilasm\r
                           }\r
                        | OPEN_BRACKET D_MODULE slashed_name CLOSE_BRACKET\r
+                          {\r
+\r
+                          }\r
                        | type\r
                        ;\r
 \r
@@ -1937,7 +1967,7 @@ instr                     : INSTR_NONE
                                         break;\r
                                 }\r
                           }\r
-                       | INSTR_R OPEN_PARENS bytes CLOSE_PARENS\r
+                       | INSTR_R bytes_list\r
                        | INSTR_BRTARGET int32\r
                           {\r
                                LabelInfo target = codegen.CurrentMethodDef.AddLabel ((int) $2);\r
@@ -1980,17 +2010,15 @@ instr                   : INSTR_NONE
                                 if ((MiscInstr) $1 == MiscInstr.ldstr)\r
                                         codegen.CurrentMethodDef.AddInstr (new LdstrInstr ((string) $2));\r
                           }\r
-                       | INSTR_STRING K_BYTEARRAY ASSIGN OPEN_PARENS\r
-                          bytes CLOSE_PARENS\r
+                       | INSTR_STRING K_BYTEARRAY ASSIGN bytes_list\r
                           {\r
-                                byte[] bs = (byte[]) $5;\r
+                                byte[] bs = (byte[]) $4;\r
                                 if ((MiscInstr) $1 == MiscInstr.ldstr)\r
                                         codegen.CurrentMethodDef.AddInstr (new LdstrInstr (bs));\r
                           }\r
-                       | INSTR_STRING K_BYTEARRAY OPEN_PARENS bytes\r
-                          CLOSE_PARENS\r
+                       | INSTR_STRING K_BYTEARRAY bytes_list\r
                           {\r
-                                byte[] bs = (byte[]) $4;\r
+                                byte[] bs = (byte[]) $3;\r
                                 if ((MiscInstr) $1 == MiscInstr.ldstr)\r
                                         codegen.CurrentMethodDef.AddInstr (new LdstrInstr (bs));\r
                           }\r
@@ -2226,6 +2254,9 @@ prop_attr                 : /* EMPTY */
                           {\r
                                 $$ = (FeatureAttr) $1 | FeatureAttr.Specialname;\r
                           }\r
+                        | prop_attr K_INSTANCE\r
+                          {\r
+                          }\r
                        ;\r
 \r
 prop_decls             : /* EMPTY */\r
@@ -2254,15 +2285,15 @@ customattr_decl         : D_CUSTOM custom_type
                                 $$ = new CustomAttr ((IMethodRef) $2, null);\r
                           }\r
                        | D_CUSTOM custom_type ASSIGN comp_qstring\r
-                       | D_CUSTOM custom_type ASSIGN OPEN_PARENS bytes CLOSE_PARENS\r
+                       | D_CUSTOM custom_type ASSIGN bytes_list\r
                           {\r
                                 $$ = new CustomAttr ((IMethodRef) $2,\r
-                                        (byte[]) $5);\r
+                                        (byte[]) $4);\r
                           }\r
                        | D_CUSTOM OPEN_PARENS owner_type CLOSE_PARENS custom_type\r
                        | D_CUSTOM OPEN_PARENS owner_type CLOSE_PARENS custom_type ASSIGN comp_qstring\r
                        | D_CUSTOM OPEN_PARENS owner_type CLOSE_PARENS custom_type ASSIGN\r
-                         OPEN_PARENS bytes CLOSE_PARENS\r
+                         bytes_list\r
                        ;\r
                        \r
 custom_type            : call_conv type type_spec DOUBLE_COLON D_CTOR OPEN_PARENS type_list CLOSE_PARENS\r
@@ -2296,7 +2327,7 @@ custom_type               : call_conv type type_spec DOUBLE_COLON D_CTOR OPEN_PARENS type_lis
 \r
 sec_decl               : D_PERMISSION sec_action type_spec OPEN_PARENS nameval_pairs CLOSE_PARENS\r
                        | D_PERMISSION sec_action type_spec\r
-                       | D_PERMISSIONSET sec_action ASSIGN OPEN_PARENS bytes CLOSE_PARENS\r
+                       | D_PERMISSIONSET sec_action ASSIGN bytes_list\r
                        ;\r
 \r
 nameval_pairs          : nameval_pair \r
@@ -2334,12 +2365,20 @@ sec_action              : K_REQUEST
                        ;\r
 \r
 module_head            : D_MODULE\r
+                          {\r
+                          }\r
                        | D_MODULE comp_name\r
+                          {\r
+                                codegen.SetModuleName ((string) $2);\r
+                          }\r
                        | D_MODULE K_EXTERN comp_name\r
+                          {\r
+                                codegen.ExternTable.AddModule ((string) $3);                         \r
+                          }\r
                        ;\r
 \r
 file_decl              : D_FILE file_attr comp_name file_entry D_HASH ASSIGN\r
-                         OPEN_PARENS bytes CLOSE_PARENS file_entry\r
+                         bytes_list file_entry\r
                        | D_FILE file_attr comp_name file_entry\r
                        ;\r
 \r
@@ -2370,15 +2409,34 @@ assembly_decls          : /* EMPTY */
                        | assembly_decls assembly_decl\r
                        ;\r
 \r
-assembly_decl          : D_HASH K_ALGORITHM int32\r
+assembly_decl          : D_PUBLICKEY ASSIGN bytes_list\r
+                         {\r
+                               codegen.SetAssemblyPublicKey ((byte []) $3);\r
+                         }\r
+                       | D_VER int32 COLON int32 COLON int32 COLON int32\r
+                         {\r
+                               codegen.SetAssemblyVersion ((int) $2, (int) $4, (int) $6, (int) $8);\r
+                         }\r
+                       | D_LOCALE comp_qstring\r
+                         {\r
+                               codegen.SetAssemblyLocale ((string) $2);\r
+                         }\r
+                       | D_LOCALE ASSIGN bytes_list\r
+                       | D_HASH K_ALGORITHM int32\r
+                         {\r
+                               codegen.SetAssemblyHashAlgorithm ((int) $3);\r
+                         }\r
+                       | customattr_decl\r
+                         {\r
+                               codegen.AddAssemblyCustomAttribute ((CustomAttr) $1);\r
+                         }\r
                        | sec_decl\r
-                       | asm_or_ref_decl\r
                        ;\r
 \r
-asm_or_ref_decl                : D_PUBLICKEY ASSIGN OPEN_PARENS bytes CLOSE_PARENS\r
+asm_or_ref_decl                : D_PUBLICKEY ASSIGN bytes_list\r
                        | D_VER int32 COLON int32 COLON int32 COLON int32 \r
                        | D_LOCALE comp_qstring\r
-                       | D_LOCALE ASSIGN OPEN_PARENS bytes CLOSE_PARENS\r
+                       | D_LOCALE ASSIGN bytes_list\r
                        | customattr_decl\r
                        ;\r
 \r
@@ -2390,14 +2448,14 @@ assemblyref_head        : D_ASSEMBLY K_EXTERN slashed_name
                                 System.Reflection.AssemblyName asmb_name = \r
                                        new System.Reflection.AssemblyName ();\r
                                asmb_name.Name = (string) $3;\r
-                               codegen.ExternTable.AddAssembly ((string) $3, asmb_name);\r
+                               codegen.BeginAssemblyRef ((string) $3, asmb_name);\r
                           }\r
                        | D_ASSEMBLY K_EXTERN slashed_name K_AS slashed_name\r
                           {\r
                                 System.Reflection.AssemblyName asmb_name = \r
                                        new System.Reflection.AssemblyName ();\r
                                asmb_name.Name = (string) $3;\r
-                               codegen.ExternTable.AddAssembly ((string) $5, asmb_name);\r
+                               codegen.BeginAssemblyRef ((string) $5, asmb_name);\r
                           }\r
                        ;\r
 \r
@@ -2405,9 +2463,29 @@ assemblyref_decls        : /* EMPTY */
                        | assemblyref_decls assemblyref_decl\r
                        ;\r
 \r
-assemblyref_decl       : D_HASH ASSIGN OPEN_PARENS bytes CLOSE_PARENS\r
-                       | asm_or_ref_decl\r
-                       | D_PUBLICKEYTOKEN ASSIGN OPEN_PARENS bytes CLOSE_PARENS\r
+assemblyref_decl       : D_VER int32 COLON int32 COLON int32 COLON int32\r
+                          {\r
+                                codegen.CurrentAssemblyRef.SetVersion ((int) $2, (int) $4, (int) $6, (int) $8);\r
+                          }\r
+                        | D_PUBLICKEY ASSIGN bytes_list\r
+                          {\r
+                                codegen.CurrentAssemblyRef.SetPublicKey ((byte []) $3);\r
+                          }\r
+                        | D_PUBLICKEYTOKEN ASSIGN bytes_list\r
+                          {\r
+                                codegen.CurrentAssemblyRef.SetPublicKeyToken ((byte []) $3);\r
+                          }\r
+                       | D_LOCALE comp_qstring\r
+                          {\r
+                                codegen.CurrentAssemblyRef.SetLocale ((string) $2);\r
+                          }\r
+                       | D_LOCALE ASSIGN bytes_list\r
+                       \r
+                        | D_HASH ASSIGN bytes_list\r
+                          {\r
+                                codegen.CurrentAssemblyRef.SetHash ((byte []) $3);\r
+                          }\r
+                        | customattr_decl\r
                        ;\r
 \r
 exptype_all            : exptype_head OPEN_BRACE exptype_decls CLOSE_BRACE\r
@@ -2465,7 +2543,7 @@ int32                     : INT64
                           {\r
                                 long l = (long) $1;\r
                                 byte[] intb = BitConverter.GetBytes (l);\r
-                                $$ = BitConverter.ToInt32 (intb, 0);\r
+                                $$ = BitConverter.ToInt32 (intb, BitConverter.IsLittleEndian ? 0 : 4);\r
                           }\r
                        ;\r
 \r
@@ -2477,38 +2555,39 @@ float64                 : FLOAT64
                           {\r
                                 int i = (int) $3;\r
                                 byte[] intb = BitConverter.GetBytes (i);\r
-                                $$ = (double) BitConverter.ToSingle (intb, 0);\r
+                                $$ = (double) BitConverter.ToSingle (intb, BitConverter.IsLittleEndian ? 0 : 4);\r
                           }\r
                         | K_FLOAT32 OPEN_PARENS INT64 CLOSE_PARENS\r
                           {\r
                                 long l = (long) $3;\r
                                 byte[] intb = BitConverter.GetBytes (l);\r
-                                $$ = (double) BitConverter.ToSingle (intb, 0);\r
+                                $$ = (double) BitConverter.ToSingle (intb, BitConverter.IsLittleEndian ? 0 : 4);\r
                           }\r
                        | K_FLOAT64 OPEN_PARENS INT64 CLOSE_PARENS\r
                           {\r
                                 byte[] intb = BitConverter.GetBytes ((long) $3);\r
-                               $$ = BitConverter.ToDouble (intb, 0);\r
+                               $$ = BitConverter.ToDouble (intb, BitConverter.IsLittleEndian ? 0 : 4);\r
                           }\r
                         | K_FLOAT64 OPEN_PARENS INT32 CLOSE_PARENS\r
                           {\r
                                 byte[] intb = BitConverter.GetBytes ((int) $3);\r
-                                $$ = (double) BitConverter.ToSingle (intb, 0);\r
+                                $$ = (double) BitConverter.ToSingle (intb, BitConverter.IsLittleEndian ? 0 : 4);\r
                           }\r
                        ;\r
 \r
 hexbyte                        : HEXBYTE       { }\r
-                        | INT64\r
+                       ;\r
+\r
+bytes_list              : OPEN_PARENS\r
                           {\r
-                                long l = (long) $1;\r
-                                byte b = Byte.Parse (l.ToString (), NumberStyles.HexNumber);\r
-                                $$ = b;\r
+                                tokenizer.InByteArray = true;\r
                           }\r
-                        | ID\r
+                          bytes CLOSE_PARENS\r
                           {\r
-                                $$ = Byte.Parse ((string) $1, NumberStyles.HexNumber);\r
+                                $$ = $3;\r
+                                tokenizer.InByteArray = false;\r
                           }\r
-                       ;\r
+                        ;\r
 \r
 bytes                  : /* EMPTY */   { $$ = new byte[0]; }\r
                        | hexbytes\r
@@ -2532,7 +2611,13 @@ hexbytes         : hexbyte
                        ;\r
 \r
 truefalse              : K_TRUE\r
+                          {\r
+                                $$ = true;\r
+                          }\r
                        | K_FALSE\r
+                          {\r
+                                $$ = false;\r
+                          }\r
                        ;\r
 \r
 id                     : ID\r