3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
14 // (1) Figure out why error productions dont work. `type-declaration' is a
15 // great spot to put an `error' because you can reproduce it with this input:
18 // Possible optimization:
19 // Run memory profiler with parsing only, and consider dropping
20 // arraylists where not needed. Some pieces can use linked lists.
28 using System.Collections;
33 public class CSharpParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
36 TypeContainer current_class;
38 IIteratorContainer iterator_container;
41 /// Current block is used to add statements as we find
44 Block current_block, top_current_block;
46 Delegate current_delegate;
49 /// This is used by the unary_expression code to resolve
50 /// a name against a parameter.
52 Parameters current_local_parameters;
55 /// Using during property parsing to describe the implicit
56 /// value parameter that is passed to the "set" and "get"accesor
57 /// methods (properties and indexers).
59 Expression implicit_value_parameter_type;
60 Parameters indexer_parameters;
63 /// Used to determine if we are parsing the get/set pair
64 /// of an indexer or a property
69 /// An out-of-band stack.
78 static public int yacc_verbose_flag;
80 // Name of the file we are parsing
89 /// Temporary Xml documentation cache.
90 /// For enum types, we need one more temporary store.
93 string enumTypeComment;
95 /// Current attribute target
96 string current_attr_target;
98 /// assembly and module attribute definition is enabled
99 bool global_attrs_enabled = true;
103 %token NONE /* This token is never returned by our lexer */
104 %token ERROR // This is used not by the parser, but by the tokenizer.
108 *These are the C# keywords
195 /* C# keywords which are not really keywords */
201 /* C# single character operators/punctuation. */
202 %token OPEN_BRACE "{"
203 %token CLOSE_BRACE "}"
204 %token OPEN_BRACKET "["
205 %token CLOSE_BRACKET "]"
206 %token OPEN_PARENS "("
207 %token CLOSE_PARENS ")"
219 %token OP_GENERICS_LT "<"
221 %token OP_GENERICS_GT ">"
222 %token BITWISE_AND "&"
223 %token BITWISE_OR "|"
230 /* C# multi-character operators. */
233 %token OP_SHIFT_LEFT "<<"
234 %token OP_SHIFT_RIGHT ">>"
241 %token OP_MULT_ASSIGN "*="
242 %token OP_DIV_ASSIGN "/="
243 %token OP_MOD_ASSIGN "%="
244 %token OP_ADD_ASSIGN "+="
245 %token OP_SUB_ASSIGN "-="
246 %token OP_SHIFT_LEFT_ASSIGN "<<="
247 %token OP_SHIFT_RIGHT_ASSIGN ">>="
248 %token OP_AND_ASSIGN "&="
249 %token OP_XOR_ASSIGN "^="
250 %token OP_OR_ASSIGN "|="
254 %token LITERAL_INTEGER "int literal"
255 %token LITERAL_FLOAT "float literal"
256 %token LITERAL_DOUBLE "double literal"
257 %token LITERAL_DECIMAL "decimal literal"
258 %token LITERAL_CHARACTER "character literal"
259 %token LITERAL_STRING "string literal"
262 %token CLOSE_PARENS_CAST
263 %token CLOSE_PARENS_NO_CAST
264 %token CLOSE_PARENS_OPEN_PARENS
265 %token CLOSE_PARENS_MINUS
266 %token DEFAULT_OPEN_PARENS
267 %token GENERIC_DIMENSION
269 /* Add precedence rules to solve dangling else s/r conflict */
278 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
280 %left STAR DIV PERCENT
281 %right BANG CARRET UMINUS
282 %nonassoc OP_INC OP_DEC
284 %left OPEN_BRACKET OPEN_BRACE
288 %start compilation_unit
292 : outer_declarations opt_EOF
293 | outer_declarations global_attributes opt_EOF
294 | global_attributes opt_EOF
295 | opt_EOF /* allow empty files */
301 Lexer.check_incorrect_doc_comment ();
305 Lexer.check_incorrect_doc_comment ();
311 | outer_declarations outer_declaration
316 | namespace_member_declaration
321 | using_directives using_directive
325 : using_alias_directive
327 if (RootContext.Documentation != null)
328 Lexer.doc_state = XmlCommentState.Allowed;
330 | using_namespace_directive
332 if (RootContext.Documentation != null)
333 Lexer.doc_state = XmlCommentState.Allowed;
337 using_alias_directive
338 : USING IDENTIFIER ASSIGN
339 namespace_or_type_name SEMICOLON
341 current_namespace.UsingAlias ((string) $2, (MemberName) $4, lexer.Location);
344 CheckIdentifierToken (yyToken);
348 using_namespace_directive
349 : USING namespace_name SEMICOLON
351 current_namespace.Using ((string) $2, lexer.Location);
356 // Strictly speaking, namespaces don't have attributes but
357 // we parse global attributes along with namespace declarations and then
360 namespace_declaration
361 : opt_attributes NAMESPACE namespace_or_type_name
364 Report.Error(1518, Lexer.Location, "Attributes cannot be applied to namespaces."
365 + " Expected class, delegate, enum, interface, or struct");
368 MemberName name = (MemberName) $3;
370 if (name.TypeArguments != null)
371 syntax_error (lexer.Location, "namespace name expected");
372 else if ((current_namespace.Parent != null) && (name.Left != null)) {
373 Report.Error (134, lexer.Location,
374 "Cannot use qualified namespace names in nested " +
375 "namespace declarations");
378 current_namespace = new NamespaceEntry (
379 current_namespace, file, name.GetName (), lexer.Location);
381 namespace_body opt_semicolon
383 current_namespace = current_namespace.Parent;
398 : namespace_or_type_name {
399 MemberName name = (MemberName) $1;
401 if (name.TypeArguments != null)
402 syntax_error (lexer.Location, "namespace name expected");
404 $$ = name.GetName ();
411 if (RootContext.Documentation != null)
412 Lexer.doc_state = XmlCommentState.Allowed;
415 opt_namespace_member_declarations
426 opt_namespace_member_declarations
428 | namespace_member_declarations
431 namespace_member_declarations
432 : namespace_member_declaration
433 | namespace_member_declarations namespace_member_declaration
436 namespace_member_declaration
443 Class c = (Class) $1;
444 mod_flags = c.ModFlags;
446 } else if ($1 is Struct){
447 Struct s = (Struct) $1;
448 mod_flags = s.ModFlags;
453 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
455 1527, lexer.Location,
456 "Namespace elements cant be explicitly " +
457 "declared private or protected in `" + name + "'");
459 current_namespace.DeclarationFound = true;
461 | namespace_declaration {
462 current_namespace.DeclarationFound = true;
465 | field_declaration {
466 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
468 | method_declaration {
469 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
476 | interface_declaration
478 | delegate_declaration
480 // Enable this when we have handled all errors, because this acts as a generic fallback
483 // Console.WriteLine ("Token=" + yyToken);
484 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
496 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
504 global_attrs_enabled = false;
509 global_attrs_enabled = false;
518 ArrayList sect = (ArrayList) $1;
520 if (global_attrs_enabled) {
521 if (current_attr_target == "module") {
522 CodeGen.Module.AddAttributes (sect);
524 } else if (current_attr_target == "assembly") {
525 CodeGen.Assembly.AddAttributes (sect);
528 $$ = new Attributes (sect);
531 if (RootContext.Documentation != null) {
532 Lexer.check_incorrect_doc_comment ();
534 XmlCommentState.Allowed;
538 $$ = new Attributes (sect);
540 current_attr_target = null;
542 | attribute_sections attribute_section
544 Attributes attrs = $1 as Attributes;
545 ArrayList sect = (ArrayList) $2;
547 if (global_attrs_enabled) {
548 if (current_attr_target == "module") {
549 CodeGen.Module.AddAttributes (sect);
551 } else if (current_attr_target == "assembly") {
552 CodeGen.Assembly.AddAttributes (sect);
556 attrs = new Attributes (sect);
558 attrs.AddAttributes (sect);
562 attrs = new Attributes (sect);
564 attrs.AddAttributes (sect);
567 current_attr_target = null;
572 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
576 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
582 attribute_target_specifier
583 : attribute_target COLON
585 current_attr_target = (string)$1;
593 CheckAttributeTarget ((string) $1);
596 | EVENT { $$ = "event"; }
597 | RETURN { $$ = "return"; }
603 ArrayList attrs = new ArrayList (4);
609 | attribute_list COMMA attribute
611 ArrayList attrs = (ArrayList) $1;
623 opt_attribute_arguments
625 MemberName mname = (MemberName) $1;
626 if (mname.IsGeneric) {
627 Report.Error (404, lexer.Location,
628 "'<' unexpected: attributes cannot be generic");
631 string name = mname.GetName ();
632 if (current_attr_target == "assembly" || current_attr_target == "module")
633 $$ = new GlobalAttribute (current_container, current_attr_target,
634 name, (ArrayList) $3, (Location) $2);
636 $$ = new Attribute (current_attr_target, name, (ArrayList) $3,
642 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
645 opt_attribute_arguments
646 : /* empty */ { $$ = null; }
647 | OPEN_PARENS attribute_arguments CLOSE_PARENS
655 : opt_positional_argument_list
660 ArrayList args = new ArrayList (4);
666 | positional_argument_list COMMA named_argument_list
668 ArrayList args = new ArrayList (4);
674 | named_argument_list
676 ArrayList args = new ArrayList (4);
685 opt_positional_argument_list
686 : /* empty */ { $$ = null; }
687 | positional_argument_list
690 positional_argument_list
693 ArrayList args = new ArrayList (4);
694 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
698 | positional_argument_list COMMA expression
700 ArrayList args = (ArrayList) $1;
701 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
710 ArrayList args = new ArrayList (4);
715 | named_argument_list COMMA named_argument
717 ArrayList args = (ArrayList) $1;
722 | named_argument_list COMMA expression
724 Report.Error (1016, lexer.Location, "Named attribute argument expected");
730 : IDENTIFIER ASSIGN expression
732 $$ = new DictionaryEntry (
734 new Argument ((Expression) $3, Argument.AType.Expression));
740 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
743 opt_class_member_declarations
745 | class_member_declarations
748 class_member_declarations
749 : class_member_declaration
750 | class_member_declarations
751 class_member_declaration
754 class_member_declaration
755 : constant_declaration // done
756 | field_declaration // done
757 | method_declaration // done
758 | property_declaration // done
759 | event_declaration // done
760 | indexer_declaration // done
761 | operator_declaration // done
762 | constructor_declaration // done
763 | destructor_declaration // done
773 MemberName name = MakeName ((MemberName) $5);
774 bool partial = (bool) $3;
777 ClassPart part = PartialContainer.CreatePart (
778 current_namespace, current_container, name, (int) $2,
779 (Attributes) $1, Kind.Struct, lexer.Location);
781 current_container = part.PartialContainer;
782 current_class = part;
784 current_class = new Struct (
785 current_namespace, current_container, name, (int) $2,
786 (Attributes) $1, lexer.Location);
788 current_container = current_class;
789 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
792 lexer.ConstraintsParsing = true;
795 opt_type_parameter_constraints_clauses
797 lexer.ConstraintsParsing = false;
800 current_class.Bases = (ArrayList) $7;
802 current_class.SetParameterInfo ((ArrayList) $8);
804 if (RootContext.Documentation != null)
805 current_class.DocComment = Lexer.consume_doc_comment ();
807 current_class.Register ();
811 if (RootContext.Documentation != null)
812 Lexer.doc_state = XmlCommentState.Allowed;
818 current_container = current_container.Parent;
819 current_class = current_container;
821 | opt_attributes opt_modifiers opt_partial STRUCT error {
822 CheckIdentifierToken (yyToken);
829 if (RootContext.Documentation != null)
830 Lexer.doc_state = XmlCommentState.Allowed;
832 opt_struct_member_declarations CLOSE_BRACE
835 opt_struct_member_declarations
837 | struct_member_declarations
840 struct_member_declarations
841 : struct_member_declaration
842 | struct_member_declarations struct_member_declaration
845 struct_member_declaration
846 : constant_declaration
849 | property_declaration
851 | indexer_declaration
852 | operator_declaration
853 | constructor_declaration
857 * This is only included so we can flag error 575:
858 * destructors only allowed on class types
860 | destructor_declaration
871 int modflags = (int) $2;
872 foreach (VariableDeclaration constant in (ArrayList) $5){
873 Location l = constant.Location;
874 if ((modflags & Modifiers.STATIC) != 0) {
875 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
879 Const c = new Const (
880 current_class, (Expression) $4, (string) constant.identifier,
881 (Expression) constant.expression_or_array_initializer, modflags,
884 if (RootContext.Documentation != null) {
885 c.DocComment = Lexer.consume_doc_comment ();
886 Lexer.doc_state = XmlCommentState.Allowed;
888 current_container.AddConstant (c);
894 : constant_declarator
896 ArrayList constants = new ArrayList (4);
901 | constant_declarators COMMA constant_declarator
904 ArrayList constants = (ArrayList) $1;
911 : IDENTIFIER ASSIGN constant_expression
913 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
917 // A const field requires a value to be provided
918 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
930 Expression type = (Expression) $3;
933 foreach (VariableDeclaration var in (ArrayList) $4){
934 Location l = var.Location;
936 Field field = new Field (current_class, type, mod, var.identifier,
937 var.expression_or_array_initializer,
940 if (RootContext.Documentation != null) {
941 field.DocComment = Lexer.consume_doc_comment ();
942 Lexer.doc_state = XmlCommentState.Allowed;
944 current_container.AddField (field);
952 Report.Error (670, lexer.Location, "void type is not allowed for fields");
957 : variable_declarator
959 ArrayList decl = new ArrayList (4);
963 | variable_declarators COMMA variable_declarator
965 ArrayList decls = (ArrayList) $1;
972 : IDENTIFIER ASSIGN variable_initializer
974 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
978 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
991 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
993 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
997 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1004 iterator_container = (IIteratorContainer) $1;
1005 if (RootContext.Documentation != null)
1006 Lexer.doc_state = XmlCommentState.NotAllowed;
1010 Method method = (Method) $1;
1011 Block b = (Block) $3;
1012 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1015 if ((method.ModFlags & extern_abstract) == 0){
1017 501, lexer.Location, current_container.MakeName (method.Name) +
1018 "must declare a body because it is not marked abstract or extern");
1021 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1023 179, lexer.Location, current_container.MakeName (method.Name) +
1024 " is declared extern, but has a body");
1028 method.Block = (ToplevelBlock) $3;
1029 current_container.AddMethod (method);
1031 current_local_parameters = null;
1032 iterator_container = null;
1034 if (RootContext.Documentation != null)
1035 Lexer.doc_state = XmlCommentState.Allowed;
1049 1585, lexer.Location, "Member modifier `" +
1050 Modifiers.Name (i) + "' must precede member type and name");
1061 type namespace_or_type_name
1062 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1064 lexer.ConstraintsParsing = true;
1066 opt_type_parameter_constraints_clauses
1068 lexer.ConstraintsParsing = false;
1070 MemberName name = (MemberName) $4;
1072 if ($9 != null && name.TypeArguments == null)
1073 Report.Error (80, lexer.Location,
1074 "Contraints are not allowed on non-generic declarations");
1078 GenericMethod generic = null;
1079 if (name.TypeArguments != null) {
1080 generic = new GenericMethod (current_namespace, current_class,
1081 name, lexer.Location);
1083 generic.SetParameterInfo ((ArrayList) $9);
1086 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1087 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1089 current_local_parameters = (Parameters) $6;
1091 if (RootContext.Documentation != null)
1092 method.DocComment = Lexer.consume_doc_comment ();
1098 VOID namespace_or_type_name
1099 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1101 lexer.ConstraintsParsing = true;
1103 opt_type_parameter_constraints_clauses
1105 lexer.ConstraintsParsing = false;
1107 MemberName name = (MemberName) $4;
1109 if ($9 != null && name.TypeArguments == null)
1110 Report.Error (80, lexer.Location,
1111 "Contraints are not allowed on non-generic declarations");
1114 GenericMethod generic = null;
1115 if (name.TypeArguments != null) {
1116 generic = new GenericMethod (current_namespace, current_class,
1117 name, lexer.Location);
1119 generic.SetParameterInfo ((ArrayList) $9);
1122 method = new Method (current_class, generic, TypeManager.system_void_expr,
1123 (int) $2, false, name, (Parameters) $6, (Attributes) $1,
1126 current_local_parameters = (Parameters) $6;
1128 if (RootContext.Documentation != null)
1129 method.DocComment = Lexer.consume_doc_comment ();
1136 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1138 Report.Error (1585, lexer.Location,
1139 String.Format ("Modifier {0} should appear before type",
1140 Modifiers.Name ((int) $4)));
1141 MemberName name = (MemberName) $4;
1143 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1144 0, false, name, (Parameters) $6, (Attributes) $1,
1147 current_local_parameters = (Parameters) $6;
1149 if (RootContext.Documentation != null)
1150 method.DocComment = Lexer.consume_doc_comment ();
1158 | SEMICOLON { $$ = null; }
1161 opt_formal_parameter_list
1162 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1163 | formal_parameter_list
1166 formal_parameter_list
1169 ArrayList pars_list = (ArrayList) $1;
1171 Parameter [] pars = new Parameter [pars_list.Count];
1172 pars_list.CopyTo (pars);
1174 $$ = new Parameters (pars, null, lexer.Location);
1176 | fixed_parameters COMMA parameter_array
1178 ArrayList pars_list = (ArrayList) $1;
1180 Parameter [] pars = new Parameter [pars_list.Count];
1181 pars_list.CopyTo (pars);
1183 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1185 | fixed_parameters COMMA ARGLIST
1187 ArrayList pars_list = (ArrayList) $1;
1189 Parameter [] pars = new Parameter [pars_list.Count];
1190 pars_list.CopyTo (pars);
1192 $$ = new Parameters (pars, true, lexer.Location);
1196 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1200 $$ = new Parameters (null, true, lexer.Location);
1207 ArrayList pars = new ArrayList (4);
1212 | fixed_parameters COMMA fixed_parameter
1214 ArrayList pars = (ArrayList) $1;
1223 opt_parameter_modifier
1227 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1230 opt_parameter_modifier
1233 Report.Error (1001, lexer.Location, "Identifier expected");
1237 opt_parameter_modifier
1240 CheckIdentifierToken (yyToken);
1244 opt_parameter_modifier
1250 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1255 opt_parameter_modifier
1256 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1257 | parameter_modifier
1261 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1262 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1266 : opt_attributes PARAMS type IDENTIFIER
1268 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1269 note ("type must be a single-dimension array type");
1271 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1273 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1276 | opt_attributes PARAMS type error {
1277 CheckIdentifierToken (yyToken);
1282 property_declaration
1286 namespace_or_type_name
1288 if (RootContext.Documentation != null)
1289 tmpComment = Lexer.consume_doc_comment ();
1293 implicit_value_parameter_type = (Expression) $3;
1295 lexer.PropertyParsing = true;
1297 $$ = lexer.Location;
1299 iterator_container = SimpleIteratorContainer.GetSimple ();
1301 accessor_declarations
1303 lexer.PropertyParsing = false;
1308 Pair pair = (Pair) $8;
1309 Accessor get_block = (Accessor) pair.First;
1310 Accessor set_block = (Accessor) pair.Second;
1312 Location loc = (Location) $7;
1313 MemberName name = (MemberName) $4;
1315 if (name.TypeArguments != null)
1316 syntax_error (lexer.Location, "a property can't have type arguments");
1318 prop = new Property (current_container, (Expression) $3, (int) $2, false,
1319 name, (Attributes) $1, get_block, set_block, loc);
1320 if (SimpleIteratorContainer.Simple.Yields)
1323 current_container.AddProperty (prop);
1324 implicit_value_parameter_type = null;
1325 iterator_container = null;
1327 if (RootContext.Documentation != null)
1328 prop.DocComment = ConsumeStoredComment ();
1333 accessor_declarations
1334 : get_accessor_declaration opt_set_accessor_declaration
1336 $$ = new Pair ($1, $2);
1338 | set_accessor_declaration opt_get_accessor_declaration
1340 $$ = new Pair ($2, $1);
1344 opt_get_accessor_declaration
1345 : /* empty */ { $$ = null; }
1346 | get_accessor_declaration
1349 opt_set_accessor_declaration
1350 : /* empty */ { $$ = null; }
1351 | set_accessor_declaration
1354 get_accessor_declaration
1355 : opt_attributes opt_modifiers GET
1357 // If this is not the case, then current_local_parameters has already
1358 // been set in indexer_declaration
1359 if (parsing_indexer == false)
1360 current_local_parameters = null;
1362 current_local_parameters = indexer_parameters;
1363 lexer.PropertyParsing = false;
1367 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1368 current_local_parameters = null;
1369 lexer.PropertyParsing = true;
1371 if (RootContext.Documentation != null)
1372 if (Lexer.doc_state == XmlCommentState.Error)
1373 Lexer.doc_state = XmlCommentState.NotAllowed;
1377 set_accessor_declaration
1378 : opt_attributes opt_modifiers SET
1381 Parameter implicit_value_parameter = new Parameter (
1382 implicit_value_parameter_type, "value",
1383 Parameter.Modifier.NONE, null);
1385 if (parsing_indexer == false) {
1386 args = new Parameter [1];
1387 args [0] = implicit_value_parameter;
1388 current_local_parameters = new Parameters (args, null, lexer.Location);
1390 Parameter [] fpars = indexer_parameters.FixedParameters;
1393 int count = fpars.Length;
1395 args = new Parameter [count + 1];
1396 fpars.CopyTo (args, 0);
1397 args [count] = implicit_value_parameter;
1400 current_local_parameters = new Parameters (
1401 args, indexer_parameters.ArrayParameter, lexer.Location);
1404 lexer.PropertyParsing = false;
1408 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1409 current_local_parameters = null;
1410 lexer.PropertyParsing = true;
1412 if (RootContext.Documentation != null
1413 && Lexer.doc_state == XmlCommentState.Error)
1414 Lexer.doc_state = XmlCommentState.NotAllowed;
1420 | SEMICOLON { $$ = null; }
1423 interface_declaration
1427 INTERFACE member_name
1429 MemberName name = MakeName ((MemberName) $5);
1430 bool partial = (bool) $3;
1433 ClassPart part = PartialContainer.CreatePart (
1434 current_namespace, current_container, name, (int) $2,
1435 (Attributes) $1, Kind.Interface, lexer.Location);
1437 current_container = part.PartialContainer;
1438 current_class = part;
1440 current_class = new Interface (
1441 current_namespace, current_container, name, (int) $2,
1442 (Attributes) $1, lexer.Location);
1444 current_container = current_class;
1445 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1448 lexer.ConstraintsParsing = true;
1451 opt_type_parameter_constraints_clauses
1453 lexer.ConstraintsParsing = false;
1456 current_class.Bases = (ArrayList) $7;
1458 current_class.SetParameterInfo ((ArrayList) $8);
1460 if (RootContext.Documentation != null) {
1461 current_class.DocComment = Lexer.consume_doc_comment ();
1462 Lexer.doc_state = XmlCommentState.Allowed;
1465 current_class.Register ();
1469 if (RootContext.Documentation != null)
1470 Lexer.doc_state = XmlCommentState.Allowed;
1476 current_container = current_container.Parent;
1477 current_class = current_container;
1479 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1480 CheckIdentifierToken (yyToken);
1486 opt_interface_member_declarations
1490 opt_interface_member_declarations
1492 | interface_member_declarations
1495 interface_member_declarations
1496 : interface_member_declaration
1497 | interface_member_declarations interface_member_declaration
1500 interface_member_declaration
1501 : interface_method_declaration
1503 Method m = (Method) $1;
1505 if (m.IsExplicitImpl)
1506 Report.Error (541, lexer.Location,
1507 "Explicit interface declaration can only be declared in a class or struct");
1509 current_container.AddMethod (m);
1511 if (RootContext.Documentation != null)
1512 Lexer.doc_state = XmlCommentState.Allowed;
1514 | interface_property_declaration
1516 Property p = (Property) $1;
1518 if (p.IsExplicitImpl)
1519 Report.Error (541, lexer.Location,
1520 "Explicit interface declaration can only be declared in a class or struct");
1522 current_container.AddProperty (p);
1524 if (RootContext.Documentation != null)
1525 Lexer.doc_state = XmlCommentState.Allowed;
1527 | interface_event_declaration
1530 Event e = (Event) $1;
1532 if (e.IsExplicitImpl)
1533 Report.Error (541, lexer.Location,
1534 "Explicit interface declaration can only be declared in a class or struct");
1536 current_container.AddEvent (e);
1539 if (RootContext.Documentation != null)
1540 Lexer.doc_state = XmlCommentState.Allowed;
1542 | interface_indexer_declaration
1544 Indexer i = (Indexer) $1;
1546 if (i.IsExplicitImpl)
1547 Report.Error (541, lexer.Location,
1548 "Explicit interface declaration can only be declared in a class or struct");
1550 current_container.AddIndexer (i);
1552 if (RootContext.Documentation != null)
1553 Lexer.doc_state = XmlCommentState.Allowed;
1555 | delegate_declaration
1557 Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
1561 Report.Error (524, lexer.Location, "Interfaces can not declare classes");
1563 | struct_declaration
1565 Report.Error (524, lexer.Location, "Interfaces can not declare structures");
1569 Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
1571 | interface_declaration
1573 Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
1581 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1586 interface_method_declaration
1587 : opt_attributes opt_new type namespace_or_type_name
1588 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1590 lexer.ConstraintsParsing = true;
1592 opt_type_parameter_constraints_clauses
1595 lexer.ConstraintsParsing = false;
1597 MemberName name = (MemberName) $4;
1599 if ($9 != null && name.TypeArguments == null)
1600 Report.Error (80, lexer.Location,
1601 "Contraints are not allowed on non-generic declarations");
1603 GenericMethod generic = null;
1604 if (name.TypeArguments != null) {
1605 generic = new GenericMethod (current_namespace, current_class,
1606 name, lexer.Location);
1608 generic.SetParameterInfo ((ArrayList) $9);
1611 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1612 (Parameters) $6, (Attributes) $1, lexer.Location);
1613 if (RootContext.Documentation != null)
1614 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1616 | opt_attributes opt_new VOID namespace_or_type_name
1617 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1619 lexer.ConstraintsParsing = true;
1621 opt_type_parameter_constraints_clauses
1624 lexer.ConstraintsParsing = false;
1626 MemberName name = (MemberName) $4;
1628 if ($9 != null && name.TypeArguments == null)
1629 Report.Error (80, lexer.Location,
1630 "Contraints are not allowed on non-generic declarations");
1632 GenericMethod generic = null;
1633 if (name.TypeArguments != null) {
1634 generic = new GenericMethod (current_namespace, current_class,
1635 name, lexer.Location);
1637 generic.SetParameterInfo ((ArrayList) $9);
1640 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1641 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1642 if (RootContext.Documentation != null)
1643 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1647 interface_property_declaration
1652 { lexer.PropertyParsing = true; }
1654 { lexer.PropertyParsing = false; }
1657 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1659 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1660 new MemberName ((string) $4), (Attributes) $1,
1661 pinfo.Get, pinfo.Set, lexer.Location);
1662 if (RootContext.Documentation != null)
1663 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1668 CheckIdentifierToken (yyToken);
1674 : opt_attributes opt_modifiers GET SEMICOLON
1675 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1676 | opt_attributes opt_modifiers SET SEMICOLON
1677 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1678 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1679 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1680 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1681 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1684 interface_event_declaration
1685 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1687 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1688 new MemberName ((string) $5), null,
1689 (Attributes) $1, lexer.Location);
1690 if (RootContext.Documentation != null)
1691 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1693 | opt_attributes opt_new EVENT type error {
1694 CheckIdentifierToken (yyToken);
1697 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1698 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1701 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1702 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1707 interface_indexer_declaration
1708 : opt_attributes opt_new type THIS
1709 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1711 { lexer.PropertyParsing = true; }
1713 { lexer.PropertyParsing = false; }
1716 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1718 $$ = new Indexer (current_class, (Expression) $3,
1719 new MemberName (TypeContainer.DefaultIndexerName),
1720 (int) $2, true, (Parameters) $6, (Attributes) $1,
1721 info.Get, info.Set, lexer.Location);
1722 if (RootContext.Documentation != null)
1723 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1727 operator_declaration
1728 : opt_attributes opt_modifiers operator_declarator
1730 iterator_container = SimpleIteratorContainer.GetSimple ();
1734 OperatorDeclaration decl = (OperatorDeclaration) $3;
1736 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1738 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1739 if (decl.arg2type != null)
1740 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1742 Operator op = new Operator (
1743 current_class, decl.optype, decl.ret_type, (int) $2,
1744 new Parameters (param_list, null, decl.location),
1745 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1747 if (RootContext.Documentation != null) {
1748 op.DocComment = tmpComment;
1749 Lexer.doc_state = XmlCommentState.Allowed;
1752 if (SimpleIteratorContainer.Simple.Yields)
1755 // Note again, checking is done in semantic analysis
1756 current_container.AddOperator (op);
1758 current_local_parameters = null;
1759 iterator_container = null;
1765 | SEMICOLON { $$ = null; }
1768 : type OPERATOR overloadable_operator
1769 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1771 Operator.OpType op = (Operator.OpType) $3;
1772 CheckUnaryOperator (op);
1774 if (op == Operator.OpType.Addition)
1775 op = Operator.OpType.UnaryPlus;
1777 if (op == Operator.OpType.Subtraction)
1778 op = Operator.OpType.UnaryNegation;
1780 Parameter [] pars = new Parameter [1];
1781 Expression type = (Expression) $5;
1783 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1785 current_local_parameters = new Parameters (pars, null, lexer.Location);
1787 if (RootContext.Documentation != null) {
1788 tmpComment = Lexer.consume_doc_comment ();
1789 Lexer.doc_state = XmlCommentState.NotAllowed;
1792 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1793 null, null, lexer.Location);
1795 | type OPERATOR overloadable_operator
1797 type IDENTIFIER COMMA
1801 CheckBinaryOperator ((Operator.OpType) $3);
1803 Parameter [] pars = new Parameter [2];
1805 Expression typeL = (Expression) $5;
1806 Expression typeR = (Expression) $8;
1808 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1809 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1811 current_local_parameters = new Parameters (pars, null, lexer.Location);
1813 if (RootContext.Documentation != null) {
1814 tmpComment = Lexer.consume_doc_comment ();
1815 Lexer.doc_state = XmlCommentState.NotAllowed;
1818 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1820 typeR, (string) $9, lexer.Location);
1822 | conversion_operator_declarator
1825 overloadable_operator
1827 : BANG { $$ = Operator.OpType.LogicalNot; }
1828 | TILDE { $$ = Operator.OpType.OnesComplement; }
1829 | OP_INC { $$ = Operator.OpType.Increment; }
1830 | OP_DEC { $$ = Operator.OpType.Decrement; }
1831 | TRUE { $$ = Operator.OpType.True; }
1832 | FALSE { $$ = Operator.OpType.False; }
1833 // Unary and binary:
1834 | PLUS { $$ = Operator.OpType.Addition; }
1835 | MINUS { $$ = Operator.OpType.Subtraction; }
1837 | STAR { $$ = Operator.OpType.Multiply; }
1838 | DIV { $$ = Operator.OpType.Division; }
1839 | PERCENT { $$ = Operator.OpType.Modulus; }
1840 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1841 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1842 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1843 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1844 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1845 | OP_EQ { $$ = Operator.OpType.Equality; }
1846 | OP_NE { $$ = Operator.OpType.Inequality; }
1847 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1848 | OP_LT { $$ = Operator.OpType.LessThan; }
1849 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1850 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1853 conversion_operator_declarator
1854 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1856 Parameter [] pars = new Parameter [1];
1858 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1860 current_local_parameters = new Parameters (pars, null, lexer.Location);
1862 if (RootContext.Documentation != null) {
1863 tmpComment = Lexer.consume_doc_comment ();
1864 Lexer.doc_state = XmlCommentState.NotAllowed;
1867 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1868 null, null, lexer.Location);
1870 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1872 Parameter [] pars = new Parameter [1];
1874 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1876 current_local_parameters = new Parameters (pars, null, lexer.Location);
1878 if (RootContext.Documentation != null) {
1879 tmpComment = Lexer.consume_doc_comment ();
1880 Lexer.doc_state = XmlCommentState.NotAllowed;
1883 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1884 null, null, lexer.Location);
1888 syntax_error (lexer.Location, "'operator' expected");
1892 syntax_error (lexer.Location, "'operator' expected");
1896 constructor_declaration
1899 constructor_declarator
1902 Constructor c = (Constructor) $3;
1903 c.Block = (ToplevelBlock) $4;
1904 c.OptAttributes = (Attributes) $1;
1905 c.ModFlags = (int) $2;
1907 if (RootContext.Documentation != null)
1908 c.DocComment = ConsumeStoredComment ();
1910 if (c.Name == current_container.Basename){
1911 if ((c.ModFlags & Modifiers.STATIC) != 0){
1912 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1914 515, c.Location, String.Format (
1915 "`{0}.{1}': static constructor can not have access modifiers",
1916 c.Name, current_container.Name));
1919 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1921 if (c.Initializer != null){
1924 "Static constructors can not have an explicit this or base " +
1925 "constructor invocations");
1928 if (!c.Parameters.Empty){
1930 132, c.Location, "Static constructors should not have parameters");
1933 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1936 // We let another layer check the validity of the constructor.
1937 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1940 current_container.AddConstructor (c);
1942 current_local_parameters = null;
1943 if (RootContext.Documentation != null)
1944 Lexer.doc_state = XmlCommentState.Allowed;
1948 constructor_declarator
1951 if (RootContext.Documentation != null) {
1952 tmpComment = Lexer.consume_doc_comment ();
1953 Lexer.doc_state = XmlCommentState.Allowed;
1956 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
1958 current_local_parameters = (Parameters) $4;
1960 opt_constructor_initializer
1962 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1963 (ConstructorInitializer) $8, (Location) $6);
1969 | SEMICOLON { $$ = null; }
1972 opt_constructor_initializer
1973 : /* empty */ { $$ = null; }
1974 | constructor_initializer
1977 constructor_initializer
1978 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1980 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1982 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1984 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1987 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1993 : /* EMPTY */ { $$ = 0; }
1994 | UNSAFE { $$ = Modifiers.UNSAFE; }
1995 | EXTERN { $$ = Modifiers.EXTERN; }
1998 destructor_declaration
1999 : opt_attributes opt_finalizer TILDE
2001 if (RootContext.Documentation != null) {
2002 tmpComment = Lexer.consume_doc_comment ();
2003 Lexer.doc_state = XmlCommentState.NotAllowed;
2006 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2008 if ((string) $5 != current_container.Basename){
2009 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
2010 } else if (current_container.Kind != Kind.Class){
2011 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
2013 Location l = lexer.Location;
2016 if (!RootContext.StdLib && current_container.Name == "System.Object")
2017 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2019 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2021 if ((m & Modifiers.UNSAFE) != 0){
2022 if (!RootContext.Unsafe){
2023 Report.Error (227, l,
2024 "Unsafe code requires the -unsafe command " +
2025 "line option to be specified");
2029 Method d = new Destructor (
2030 current_class, TypeManager.system_void_expr, m, "Finalize",
2031 new Parameters (null, null, l), (Attributes) $1, l);
2032 if (RootContext.Documentation != null)
2033 d.DocComment = ConsumeStoredComment ();
2035 d.Block = (ToplevelBlock) $8;
2036 current_container.AddMethod (d);
2044 EVENT type variable_declarators SEMICOLON
2046 foreach (VariableDeclaration var in (ArrayList) $5) {
2048 MemberName name = new MemberName (var.identifier);
2050 Event e = new EventField (
2051 current_class, (Expression) $4, (int) $2, false, name,
2052 var.expression_or_array_initializer, (Attributes) $1,
2055 current_container.AddEvent (e);
2057 if (RootContext.Documentation != null) {
2058 e.DocComment = Lexer.consume_doc_comment ();
2059 Lexer.doc_state = XmlCommentState.Allowed;
2065 EVENT type namespace_or_type_name
2068 implicit_value_parameter_type = (Expression) $4;
2069 lexer.EventParsing = true;
2071 event_accessor_declarations
2073 lexer.EventParsing = false;
2077 Location loc = (Location) $7;
2080 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2083 Pair pair = (Pair) $9;
2085 MemberName name = (MemberName) $5;
2087 if (name.TypeArguments != null)
2088 syntax_error (lexer.Location, "an event can't have type arguments");
2090 Event e = new EventProperty (
2091 current_class, (Expression) $4, (int) $2, false, name, null,
2092 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2094 if (RootContext.Documentation != null) {
2095 e.DocComment = Lexer.consume_doc_comment ();
2096 Lexer.doc_state = XmlCommentState.Allowed;
2099 current_container.AddEvent (e);
2100 implicit_value_parameter_type = null;
2103 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2104 MemberName mn = (MemberName) $5;
2106 if (mn.Left != null)
2107 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2109 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2111 if (RootContext.Documentation != null)
2112 Lexer.doc_state = XmlCommentState.Allowed;
2116 event_accessor_declarations
2117 : add_accessor_declaration remove_accessor_declaration
2119 $$ = new Pair ($1, $2);
2121 | remove_accessor_declaration add_accessor_declaration
2123 $$ = new Pair ($2, $1);
2125 | add_accessor_declaration { $$ = null; }
2126 | remove_accessor_declaration { $$ = null; }
2129 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2135 add_accessor_declaration
2136 : opt_attributes ADD
2138 Parameter [] args = new Parameter [1];
2139 Parameter implicit_value_parameter = new Parameter (
2140 implicit_value_parameter_type, "value",
2141 Parameter.Modifier.NONE, null);
2143 args [0] = implicit_value_parameter;
2145 current_local_parameters = new Parameters (args, null, lexer.Location);
2146 lexer.EventParsing = false;
2150 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2151 lexer.EventParsing = true;
2153 | opt_attributes ADD error {
2154 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2159 remove_accessor_declaration
2160 : opt_attributes REMOVE
2162 Parameter [] args = new Parameter [1];
2163 Parameter implicit_value_parameter = new Parameter (
2164 implicit_value_parameter_type, "value",
2165 Parameter.Modifier.NONE, null);
2167 args [0] = implicit_value_parameter;
2169 current_local_parameters = new Parameters (args, null, lexer.Location);
2170 lexer.EventParsing = false;
2174 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2175 lexer.EventParsing = true;
2177 | opt_attributes REMOVE error {
2178 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2184 : opt_attributes opt_modifiers indexer_declarator
2187 IndexerDeclaration decl = (IndexerDeclaration) $3;
2189 implicit_value_parameter_type = decl.type;
2191 lexer.PropertyParsing = true;
2192 parsing_indexer = true;
2194 indexer_parameters = decl.param_list;
2196 accessor_declarations
2198 lexer.PropertyParsing = false;
2199 parsing_indexer = false;
2203 // The signature is computed from the signature of the indexer. Look
2204 // at section 3.6 on the spec
2205 Location loc = (Location) $5;
2207 IndexerDeclaration decl = (IndexerDeclaration) $3;
2208 Pair pair = (Pair) $7;
2209 Accessor get_block = (Accessor) pair.First;
2210 Accessor set_block = (Accessor) pair.Second;
2213 if (decl.interface_type != null)
2214 name = new MemberName (decl.interface_type,
2215 TypeContainer.DefaultIndexerName, null);
2217 name = new MemberName (TypeContainer.DefaultIndexerName);
2219 indexer = new Indexer (current_class, decl.type, name,
2220 (int) $2, false, decl.param_list, (Attributes) $1,
2221 get_block, set_block, loc);
2222 if (RootContext.Documentation != null)
2223 indexer.DocComment = ConsumeStoredComment ();
2225 current_container.AddIndexer (indexer);
2227 current_local_parameters = null;
2228 implicit_value_parameter_type = null;
2229 indexer_parameters = null;
2234 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2236 Parameters pars = (Parameters) $4;
2237 if (pars.HasArglist) {
2238 // "__arglist is not valid in this context"
2239 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2240 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2241 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2243 if (RootContext.Documentation != null) {
2244 tmpComment = Lexer.consume_doc_comment ();
2245 Lexer.doc_state = XmlCommentState.Allowed;
2248 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2250 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2252 Parameters pars = (Parameters) $6;
2254 if (pars.HasArglist) {
2255 // "__arglist is not valid in this context"
2256 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2257 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2258 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2261 MemberName name = (MemberName) $2;
2263 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2265 if (RootContext.Documentation != null) {
2266 tmpComment = Lexer.consume_doc_comment ();
2267 Lexer.doc_state = XmlCommentState.Allowed;
2277 if (RootContext.Documentation != null)
2278 enumTypeComment = Lexer.consume_doc_comment ();
2283 Location enum_location = lexer.Location;
2285 MemberName full_name = MakeName (new MemberName ((string) $4));
2286 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
2287 full_name, (Attributes) $1, enum_location);
2289 if (RootContext.Documentation != null)
2290 e.DocComment = enumTypeComment;
2292 foreach (VariableDeclaration ev in (ArrayList) $7) {
2293 e.AddEnumMember (ev.identifier,
2294 (Expression) ev.expression_or_array_initializer,
2295 ev.Location, ev.OptAttributes,
2299 string name = full_name.GetName ();
2300 current_container.AddEnum (e);
2301 RootContext.Tree.RecordDecl (name, e);
2307 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2308 | COLON type { $$ = $2; }
2314 if (RootContext.Documentation != null)
2315 Lexer.doc_state = XmlCommentState.Allowed;
2317 opt_enum_member_declarations
2319 // here will be evaluated after CLOSE_BLACE is consumed.
2320 if (RootContext.Documentation != null)
2321 Lexer.doc_state = XmlCommentState.Allowed;
2329 opt_enum_member_declarations
2330 : /* empty */ { $$ = new ArrayList (4); }
2331 | enum_member_declarations opt_comma { $$ = $1; }
2334 enum_member_declarations
2335 : enum_member_declaration
2337 ArrayList l = new ArrayList (4);
2342 | enum_member_declarations COMMA enum_member_declaration
2344 ArrayList l = (ArrayList) $1;
2352 enum_member_declaration
2353 : opt_attributes IDENTIFIER
2355 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2357 if (RootContext.Documentation != null) {
2358 vd.DocComment = Lexer.consume_doc_comment ();
2359 Lexer.doc_state = XmlCommentState.Allowed;
2364 | opt_attributes IDENTIFIER
2366 $$ = lexer.Location;
2367 if (RootContext.Documentation != null) {
2368 tmpComment = Lexer.consume_doc_comment ();
2369 Lexer.doc_state = XmlCommentState.NotAllowed;
2374 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2376 if (RootContext.Documentation != null)
2377 vd.DocComment = ConsumeStoredComment ();
2383 delegate_declaration
2386 DELEGATE type member_name
2387 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2389 Location l = lexer.Location;
2390 MemberName name = MakeName ((MemberName) $5);
2391 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2392 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2394 if (RootContext.Documentation != null) {
2395 del.DocComment = Lexer.consume_doc_comment ();
2396 Lexer.doc_state = XmlCommentState.Allowed;
2399 current_container.AddDelegate (del);
2400 RootContext.Tree.RecordDecl (name.GetName (true), del);
2402 current_delegate = del;
2404 lexer.ConstraintsParsing = true;
2406 opt_type_parameter_constraints_clauses
2408 lexer.ConstraintsParsing = false;
2412 current_delegate.SetParameterInfo ((ArrayList) $9);
2414 current_delegate = null;
2418 DELEGATE VOID member_name
2419 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2421 Location l = lexer.Location;
2422 MemberName name = MakeName ((MemberName) $5);
2423 Delegate del = new Delegate (
2424 current_namespace, current_container,
2425 TypeManager.system_void_expr, (int) $2, name,
2426 (Parameters) $7, (Attributes) $1, l);
2428 if (RootContext.Documentation != null) {
2429 del.DocComment = Lexer.consume_doc_comment ();
2430 Lexer.doc_state = XmlCommentState.Allowed;
2433 current_container.AddDelegate (del);
2434 RootContext.Tree.RecordDecl (name.GetName (true), del);
2436 current_delegate = del;
2438 lexer.ConstraintsParsing = true;
2440 opt_type_parameter_constraints_clauses
2442 lexer.ConstraintsParsing = false;
2446 current_delegate.SetParameterInfo ((ArrayList) $9);
2448 current_delegate = null;
2452 namespace_or_type_name
2454 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2455 $$ = new MemberName ((MemberName) $1, (string) $3, (TypeArguments) $4);
2460 : IDENTIFIER opt_type_argument_list {
2461 $$ = new MemberName ((string) $1, (TypeArguments) $2);
2465 opt_type_argument_list
2466 : /* empty */ { $$ = null; }
2467 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2473 $$ = new TypeArguments ((int) $1, lexer.Location);
2479 TypeArguments type_args = new TypeArguments (lexer.Location);
2480 type_args.Add ((Expression) $1);
2483 | type_arguments COMMA type {
2484 TypeArguments type_args = (TypeArguments) $1;
2485 type_args.Add ((Expression) $3);
2491 * Before you think of adding a return_type, notice that we have been
2492 * using two rules in the places where it matters (one rule using type
2493 * and another identical one that uses VOID as the return type). This
2494 * gets rid of a shift/reduce couple
2497 : namespace_or_type_name
2499 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2511 // Note that here only unmanaged types are allowed but we
2512 // can't perform checks during this phase - we do it during
2513 // semantic analysis.
2515 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2519 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2525 | non_expression_type rank_specifier
2527 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2529 | non_expression_type STAR
2531 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2533 | expression rank_specifiers
2535 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2539 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2543 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2544 // through this different path
2546 | multiplicative_expression STAR
2548 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2555 ArrayList types = new ArrayList (4);
2560 | type_list COMMA type
2562 ArrayList types = (ArrayList) $1;
2570 * replaces all the productions for isolating the various
2571 * simple types, but we need this to reuse it easily in local_variable_type
2574 : OBJECT { $$ = TypeManager.system_object_expr; }
2575 | STRING { $$ = TypeManager.system_string_expr; }
2576 | BOOL { $$ = TypeManager.system_boolean_expr; }
2577 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2578 | FLOAT { $$ = TypeManager.system_single_expr; }
2579 | DOUBLE { $$ = TypeManager.system_double_expr; }
2584 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2585 | BYTE { $$ = TypeManager.system_byte_expr; }
2586 | SHORT { $$ = TypeManager.system_int16_expr; }
2587 | USHORT { $$ = TypeManager.system_uint16_expr; }
2588 | INT { $$ = TypeManager.system_int32_expr; }
2589 | UINT { $$ = TypeManager.system_uint32_expr; }
2590 | LONG { $$ = TypeManager.system_int64_expr; }
2591 | ULONG { $$ = TypeManager.system_uint64_expr; }
2592 | CHAR { $$ = TypeManager.system_char_expr; }
2593 | VOID { $$ = TypeManager.system_void_expr; }
2597 : type rank_specifiers
2599 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2604 // Expressions, section 7.5
2614 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2616 | parenthesized_expression
2617 | default_value_expression
2619 | invocation_expression
2623 | post_increment_expression
2624 | post_decrement_expression
2628 | checked_expression
2629 | unchecked_expression
2630 | pointer_member_access
2631 | anonymous_method_expression
2638 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2639 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2640 | NULL { $$ = NullLiteral.Null; }
2644 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2645 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2646 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2651 object v = lexer.Value;
2657 $$ = IntLiteral.Zero;
2659 $$ = IntLiteral.One;
2661 $$ = new IntLiteral (i);
2662 } else if (v is uint)
2663 $$ = new UIntLiteral ((UInt32) v);
2665 $$ = new LongLiteral ((Int64) v);
2666 else if (v is ulong)
2667 $$ = new ULongLiteral ((UInt64) v);
2669 Console.WriteLine ("OOPS. Unexpected result from scanner");
2674 : TRUE { $$ = new BoolLiteral (true); }
2675 | FALSE { $$ = new BoolLiteral (false); }
2678 parenthesized_expression_0
2679 : OPEN_PARENS expression CLOSE_PARENS
2682 lexer.Deambiguate_CloseParens ();
2683 // After this, the next token returned is one of
2684 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2685 // or CLOSE_PARENS_MINUS.
2689 parenthesized_expression
2690 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2694 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2696 // If a parenthesized expression is followed by a minus, we need to wrap
2697 // the expression inside a ParenthesizedExpression for the CS0075 check
2698 // in Binary.DoResolve().
2699 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2704 : primary_expression DOT IDENTIFIER opt_type_argument_list
2706 $$ = new MemberAccess ((Expression) $1, (string) $3,
2707 (TypeArguments) $4, lexer.Location);
2709 | predefined_type DOT IDENTIFIER opt_type_argument_list
2711 $$ = new MemberAccess ((Expression) $1, (string) $3,
2712 (TypeArguments) $4, lexer.Location);
2720 invocation_expression
2721 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2724 Location l = lexer.Location;
2725 Report.Error (1, l, "Parse error");
2727 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2729 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2731 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2733 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2735 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2737 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2739 ArrayList args = new ArrayList (1);
2741 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2743 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2745 ArrayList args = ((ArrayList) $4);
2747 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2752 : /* empty */ { $$ = null; }
2759 ArrayList list = new ArrayList (4);
2763 | argument_list COMMA argument
2765 ArrayList list = (ArrayList) $1;
2769 | argument_list error {
2770 CheckToken (1026, yyToken, ", or ) expected");
2777 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2779 | non_simple_argument
2786 : REF variable_reference
2788 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2790 | OUT variable_reference
2792 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2794 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2796 ArrayList list = (ArrayList) $3;
2797 Argument[] args = new Argument [list.Count];
2798 list.CopyTo (args, 0);
2800 Expression expr = new Arglist (args, lexer.Location);
2801 $$ = new Argument (expr, Argument.AType.Expression);
2805 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2810 : expression { note ("section 5.4"); $$ = $1; }
2814 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2816 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2818 | primary_expression rank_specifiers
2820 // So the super-trick is that primary_expression
2821 // can only be either a SimpleName or a MemberAccess.
2822 // The MemberAccess case arises when you have a fully qualified type-name like :
2824 // SimpleName is when you have
2827 Expression expr = (Expression) $1;
2828 if (expr is ComposedCast){
2829 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2830 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2831 Error_ExpectingTypeName (lexer.Location, expr);
2832 $$ = TypeManager.system_object_expr;
2835 // So we extract the string corresponding to the SimpleName
2838 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2846 ArrayList list = new ArrayList (4);
2850 | expression_list COMMA expression
2852 ArrayList list = (ArrayList) $1;
2861 $$ = new This (current_block, lexer.Location);
2866 : BASE DOT IDENTIFIER
2868 $$ = new BaseAccess ((string) $3, lexer.Location);
2870 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2872 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2875 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2880 post_increment_expression
2881 : primary_expression OP_INC
2883 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2884 (Expression) $1, lexer.Location);
2888 post_decrement_expression
2889 : primary_expression OP_DEC
2891 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2892 (Expression) $1, lexer.Location);
2897 : object_or_delegate_creation_expression
2898 | array_creation_expression
2901 object_or_delegate_creation_expression
2902 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2904 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2908 array_creation_expression
2909 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2911 opt_array_initializer
2913 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2915 | NEW type rank_specifiers array_initializer
2917 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2921 Report.Error (1031, lexer.Location, "Type expected");
2926 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2942 : rank_specifier opt_rank_specifier
2944 $$ = (string) $2 + (string) $1;
2949 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2951 $$ = "[" + (string) $2 + "]";
2971 | dim_separators COMMA
2973 $$ = (string) $1 + ",";
2977 opt_array_initializer
2989 : OPEN_BRACE CLOSE_BRACE
2991 ArrayList list = new ArrayList (4);
2994 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2996 $$ = (ArrayList) $2;
3000 variable_initializer_list
3001 : variable_initializer
3003 ArrayList list = new ArrayList (4);
3007 | variable_initializer_list COMMA variable_initializer
3009 ArrayList list = (ArrayList) $1;
3015 void_pointer_expression
3016 : void_pointer_expression STAR
3018 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3022 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3027 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3029 $$ = new TypeOfVoid (lexer.Location);
3031 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3033 $$ = new TypeOf ((Expression) $3, lexer.Location);
3035 | TYPEOF OPEN_PARENS
3037 lexer.TypeOfParsing = true;
3041 lexer.TypeOfParsing = false;
3042 $$ = new TypeOf ((Expression) $4, lexer.Location);
3047 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3048 $$ = new SizeOf ((Expression) $3, lexer.Location);
3053 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3055 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
3059 unchecked_expression
3060 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3062 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
3066 pointer_member_access
3067 : primary_expression OP_PTR IDENTIFIER
3071 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
3072 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
3076 anonymous_method_expression
3077 : DELEGATE opt_anonymous_method_signature _mark_
3079 oob_stack.Push (current_local_parameters);
3080 current_local_parameters = (Parameters)$2;
3082 // Force the next block to be created as a ToplevelBlock
3083 oob_stack.Push (current_block);
3084 oob_stack.Push (top_current_block);
3085 current_block = null;
3089 Location loc = (Location) $3;
3090 top_current_block = (Block) oob_stack.Pop ();
3091 current_block = (Block) oob_stack.Pop ();
3092 if (RootContext.Version == LanguageVersion.ISO_1){
3093 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3096 ToplevelBlock anon_block = (ToplevelBlock) $5;
3098 anon_block.Parent = current_block;
3099 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3102 current_local_parameters = (Parameters) oob_stack.Pop ();
3106 opt_anonymous_method_signature
3107 : /* empty */ { $$ = null; }
3108 | anonymous_method_signature
3111 anonymous_method_signature
3112 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3115 $$ = Parameters.EmptyReadOnlyParameters;
3117 ArrayList par_list = (ArrayList) $2;
3118 Parameter [] pars = new Parameter [par_list.Count];
3119 par_list.CopyTo (pars);
3120 $$ = new Parameters (pars, null, lexer.Location);
3125 opt_anonymous_method_parameter_list
3126 : /* empty */ { $$ = null; }
3127 | anonymous_method_parameter_list { $$ = $1; }
3130 anonymous_method_parameter_list
3131 : anonymous_method_parameter
3133 ArrayList a = new ArrayList (4);
3137 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3139 ArrayList a = (ArrayList) $1;
3145 anonymous_method_parameter
3146 : opt_parameter_modifier type IDENTIFIER {
3147 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3149 | PARAMS type IDENTIFIER {
3150 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
3155 default_value_expression
3156 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3158 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3163 : primary_expression
3164 | BANG prefixed_unary_expression
3166 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3168 | TILDE prefixed_unary_expression
3170 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3176 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3178 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3180 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3182 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3188 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3190 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3195 // The idea to split this out is from Rhys' grammar
3196 // to solve the problem with casts.
3198 prefixed_unary_expression
3200 | PLUS prefixed_unary_expression
3202 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3204 | MINUS prefixed_unary_expression
3206 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3208 | OP_INC prefixed_unary_expression
3210 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3211 (Expression) $2, lexer.Location);
3213 | OP_DEC prefixed_unary_expression
3215 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3216 (Expression) $2, lexer.Location);
3218 | STAR prefixed_unary_expression
3220 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3222 | BITWISE_AND prefixed_unary_expression
3224 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3228 pre_increment_expression
3229 : OP_INC prefixed_unary_expression
3231 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3232 (Expression) $2, lexer.Location);
3236 pre_decrement_expression
3237 : OP_DEC prefixed_unary_expression
3239 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3240 (Expression) $2, lexer.Location);
3244 multiplicative_expression
3245 : prefixed_unary_expression
3246 | multiplicative_expression STAR prefixed_unary_expression
3248 $$ = new Binary (Binary.Operator.Multiply,
3249 (Expression) $1, (Expression) $3, lexer.Location);
3251 | multiplicative_expression DIV prefixed_unary_expression
3253 $$ = new Binary (Binary.Operator.Division,
3254 (Expression) $1, (Expression) $3, lexer.Location);
3256 | multiplicative_expression PERCENT prefixed_unary_expression
3258 $$ = new Binary (Binary.Operator.Modulus,
3259 (Expression) $1, (Expression) $3, lexer.Location);
3264 : multiplicative_expression
3265 | additive_expression PLUS multiplicative_expression
3267 $$ = new Binary (Binary.Operator.Addition,
3268 (Expression) $1, (Expression) $3, lexer.Location);
3270 | additive_expression MINUS multiplicative_expression
3272 $$ = new Binary (Binary.Operator.Subtraction,
3273 (Expression) $1, (Expression) $3, lexer.Location);
3278 : additive_expression
3279 | shift_expression OP_SHIFT_LEFT additive_expression
3281 $$ = new Binary (Binary.Operator.LeftShift,
3282 (Expression) $1, (Expression) $3, lexer.Location);
3284 | shift_expression OP_SHIFT_RIGHT additive_expression
3286 $$ = new Binary (Binary.Operator.RightShift,
3287 (Expression) $1, (Expression) $3, lexer.Location);
3291 relational_expression
3293 | relational_expression OP_LT shift_expression
3295 $$ = new Binary (Binary.Operator.LessThan,
3296 (Expression) $1, (Expression) $3, lexer.Location);
3298 | relational_expression OP_GT shift_expression
3300 $$ = new Binary (Binary.Operator.GreaterThan,
3301 (Expression) $1, (Expression) $3, lexer.Location);
3303 | relational_expression OP_LE shift_expression
3305 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3306 (Expression) $1, (Expression) $3, lexer.Location);
3308 | relational_expression OP_GE shift_expression
3310 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3311 (Expression) $1, (Expression) $3, lexer.Location);
3313 | relational_expression IS type
3315 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3317 | relational_expression AS type
3319 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3324 : relational_expression
3325 | equality_expression OP_EQ relational_expression
3327 $$ = new Binary (Binary.Operator.Equality,
3328 (Expression) $1, (Expression) $3, lexer.Location);
3330 | equality_expression OP_NE relational_expression
3332 $$ = new Binary (Binary.Operator.Inequality,
3333 (Expression) $1, (Expression) $3, lexer.Location);
3338 : equality_expression
3339 | and_expression BITWISE_AND equality_expression
3341 $$ = new Binary (Binary.Operator.BitwiseAnd,
3342 (Expression) $1, (Expression) $3, lexer.Location);
3346 exclusive_or_expression
3348 | exclusive_or_expression CARRET and_expression
3350 $$ = new Binary (Binary.Operator.ExclusiveOr,
3351 (Expression) $1, (Expression) $3, lexer.Location);
3355 inclusive_or_expression
3356 : exclusive_or_expression
3357 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3359 $$ = new Binary (Binary.Operator.BitwiseOr,
3360 (Expression) $1, (Expression) $3, lexer.Location);
3364 conditional_and_expression
3365 : inclusive_or_expression
3366 | conditional_and_expression OP_AND inclusive_or_expression
3368 $$ = new Binary (Binary.Operator.LogicalAnd,
3369 (Expression) $1, (Expression) $3, lexer.Location);
3373 conditional_or_expression
3374 : conditional_and_expression
3375 | conditional_or_expression OP_OR conditional_and_expression
3377 $$ = new Binary (Binary.Operator.LogicalOr,
3378 (Expression) $1, (Expression) $3, lexer.Location);
3382 conditional_expression
3383 : conditional_or_expression
3384 | conditional_or_expression INTERR expression COLON expression
3386 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3390 assignment_expression
3391 : prefixed_unary_expression ASSIGN expression
3393 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3395 | prefixed_unary_expression OP_MULT_ASSIGN expression
3397 Location l = lexer.Location;
3399 $$ = new CompoundAssign (
3400 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3402 | prefixed_unary_expression OP_DIV_ASSIGN expression
3404 Location l = lexer.Location;
3406 $$ = new CompoundAssign (
3407 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3409 | prefixed_unary_expression OP_MOD_ASSIGN expression
3411 Location l = lexer.Location;
3413 $$ = new CompoundAssign (
3414 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3416 | prefixed_unary_expression OP_ADD_ASSIGN expression
3418 Location l = lexer.Location;
3420 $$ = new CompoundAssign (
3421 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3423 | prefixed_unary_expression OP_SUB_ASSIGN expression
3425 Location l = lexer.Location;
3427 $$ = new CompoundAssign (
3428 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3430 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3432 Location l = lexer.Location;
3434 $$ = new CompoundAssign (
3435 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3437 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3439 Location l = lexer.Location;
3441 $$ = new CompoundAssign (
3442 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3444 | prefixed_unary_expression OP_AND_ASSIGN expression
3446 Location l = lexer.Location;
3448 $$ = new CompoundAssign (
3449 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3451 | prefixed_unary_expression OP_OR_ASSIGN expression
3453 Location l = lexer.Location;
3455 $$ = new CompoundAssign (
3456 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3458 | prefixed_unary_expression OP_XOR_ASSIGN expression
3460 Location l = lexer.Location;
3462 $$ = new CompoundAssign (
3463 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3468 : conditional_expression
3469 | assignment_expression
3489 MemberName name = MakeName ((MemberName) $5);
3490 bool partial = (bool) $3;
3491 int mod_flags = (int) $2;
3494 ClassPart part = PartialContainer.CreatePart (
3495 current_namespace, current_container, name, mod_flags,
3496 (Attributes) $1, Kind.Class, lexer.Location);
3498 current_container = part.PartialContainer;
3499 current_class = part;
3501 if ((mod_flags & Modifiers.STATIC) != 0) {
3502 current_class = new StaticClass (
3503 current_namespace, current_container, name,
3504 mod_flags, (Attributes) $1, lexer.Location);
3506 current_class = new Class (
3507 current_namespace, current_container, name,
3508 mod_flags, (Attributes) $1, lexer.Location);
3511 current_container = current_class;
3512 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3515 lexer.ConstraintsParsing = true;
3518 opt_type_parameter_constraints_clauses
3520 lexer.ConstraintsParsing = false;
3523 if (current_class.Name == "System.Object") {
3524 Report.Error (537, current_class.Location,
3525 "The class System.Object cannot have a base " +
3526 "class or implement an interface.");
3528 current_class.Bases = (ArrayList) $7;
3531 current_class.SetParameterInfo ((ArrayList) $8);
3533 if (RootContext.Documentation != null) {
3534 current_class.DocComment = Lexer.consume_doc_comment ();
3535 Lexer.doc_state = XmlCommentState.Allowed;
3538 current_class.Register ();
3542 if (RootContext.Documentation != null)
3543 Lexer.doc_state = XmlCommentState.Allowed;
3549 current_container = current_container.Parent;
3550 current_class = current_container;
3556 { $$ = (bool) false; }
3558 { $$ = (bool) true; }
3562 : /* empty */ { $$ = (int) 0; }
3568 | modifiers modifier
3573 if ((m1 & m2) != 0) {
3574 Location l = lexer.Location;
3575 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3577 $$ = (int) (m1 | m2);
3582 : NEW { $$ = Modifiers.NEW; }
3583 | PUBLIC { $$ = Modifiers.PUBLIC; }
3584 | PROTECTED { $$ = Modifiers.PROTECTED; }
3585 | INTERNAL { $$ = Modifiers.INTERNAL; }
3586 | PRIVATE { $$ = Modifiers.PRIVATE; }
3587 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3588 | SEALED { $$ = Modifiers.SEALED; }
3589 | STATIC { $$ = Modifiers.STATIC; }
3590 | READONLY { $$ = Modifiers.READONLY; }
3591 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3592 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3593 | EXTERN { $$ = Modifiers.EXTERN; }
3594 | VOLATILE { $$ = Modifiers.VOLATILE; }
3595 | UNSAFE { $$ = Modifiers.UNSAFE; }
3599 : /* empty */ { $$ = null; }
3600 | class_base { $$ = $1; }
3604 : COLON type_list { $$ = $2; }
3607 opt_type_parameter_constraints_clauses
3608 : /* empty */ { $$ = null; }
3609 | type_parameter_constraints_clauses
3613 type_parameter_constraints_clauses
3614 : type_parameter_constraints_clause {
3615 ArrayList constraints = new ArrayList (1);
3616 constraints.Add ($1);
3619 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3620 ArrayList constraints = (ArrayList) $1;
3622 constraints.Add ($2);
3627 type_parameter_constraints_clause
3628 : WHERE IDENTIFIER COLON type_parameter_constraints {
3629 $$ = new Constraints ((string) $2, (ArrayList) $4, lexer.Location);
3633 type_parameter_constraints
3634 : type_parameter_constraint {
3635 ArrayList constraints = new ArrayList (1);
3636 constraints.Add ($1);
3639 | type_parameter_constraints COMMA type_parameter_constraint {
3640 ArrayList constraints = (ArrayList) $1;
3642 constraints.Add ($3);
3647 type_parameter_constraint
3649 | NEW OPEN_PARENS CLOSE_PARENS {
3650 $$ = SpecialConstraint.Constructor;
3653 $$ = SpecialConstraint.ReferenceType;
3656 $$ = SpecialConstraint.ValueType;
3665 // A block is "contained" on the following places:
3667 // property_declaration as part of the accessor body (get/set)
3668 // operator_declaration
3669 // constructor_declaration
3670 // destructor_declaration
3671 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3676 if (current_block == null){
3677 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3678 top_current_block = current_block;
3680 current_block = new Block (current_block, current_local_parameters,
3681 lexer.Location, Location.Null);
3684 opt_statement_list CLOSE_BRACE
3686 while (current_block.Implicit)
3687 current_block = current_block.Parent;
3689 current_block.SetEndLocation (lexer.Location);
3690 current_block = current_block.Parent;
3691 if (current_block == null)
3692 top_current_block = null;
3703 | statement_list statement
3707 : declaration_statement
3709 if ($1 != null && (Block) $1 != current_block){
3710 current_block.AddStatement ((Statement) $1);
3711 current_block = (Block) $1;
3714 | valid_declaration_statement
3716 current_block.AddStatement ((Statement) $1);
3721 valid_declaration_statement
3724 | expression_statement
3725 | selection_statement
3726 | iteration_statement
3730 | unchecked_statement
3738 : valid_declaration_statement
3739 | declaration_statement
3741 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3746 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3754 $$ = EmptyStatement.Value;
3761 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3763 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3764 current_block.AddStatement (labeled);
3769 declaration_statement
3770 : local_variable_declaration SEMICOLON
3773 DictionaryEntry de = (DictionaryEntry) $1;
3775 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3779 | local_constant_declaration SEMICOLON
3782 DictionaryEntry de = (DictionaryEntry) $1;
3784 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3790 * The following is from Rhys' grammar:
3791 * > Types in local variable declarations must be recognized as
3792 * > expressions to prevent reduce/reduce errors in the grammar.
3793 * > The expressions are converted into types during semantic analysis.
3796 : primary_expression opt_rank_specifier
3798 // FIXME: Do something smart here regarding the composition of the type.
3800 // Ok, the above "primary_expression" is there to get rid of
3801 // both reduce/reduce and shift/reduces in the grammar, it should
3802 // really just be "type_name". If you use type_name, a reduce/reduce
3803 // creeps up. If you use namespace_or_type_name (which is all we need
3804 // really) two shift/reduces appear.
3807 // So the super-trick is that primary_expression
3808 // can only be either a SimpleName or a MemberAccess.
3809 // The MemberAccess case arises when you have a fully qualified type-name like :
3811 // SimpleName is when you have
3814 Expression expr = (Expression) $1;
3815 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3816 Error_ExpectingTypeName (lexer.Location, expr);
3820 // So we extract the string corresponding to the SimpleName
3824 if ((string) $2 == "")
3827 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3830 | builtin_types opt_rank_specifier
3832 if ((string) $2 == "")
3835 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3839 local_variable_pointer_type
3840 : primary_expression STAR
3842 Expression expr = (Expression) $1;
3843 Location l = lexer.Location;
3845 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3846 Error_ExpectingTypeName (l, expr);
3850 $$ = new ComposedCast ((Expression) $1, "*", l);
3852 | builtin_types STAR
3854 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3858 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3860 | local_variable_pointer_type STAR
3862 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3866 local_variable_declaration
3867 : local_variable_type variable_declarators
3870 $$ = new DictionaryEntry ($1, $2);
3874 | local_variable_pointer_type opt_rank_specifier variable_declarators
3879 if ((string) $2 == "")
3880 t = (Expression) $1;
3882 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3883 $$ = new DictionaryEntry (t, $3);
3889 local_constant_declaration
3890 : CONST local_variable_type constant_declarators
3893 $$ = new DictionaryEntry ($2, $3);
3899 expression_statement
3900 : statement_expression SEMICOLON
3907 // We have to do the wrapping here and not in the case above,
3908 // because statement_expression is used for example in for_statement
3910 statement_expression
3911 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3912 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3913 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3914 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3915 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3916 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3917 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3919 Report.Error (1002, lexer.Location, "Expecting `;'");
3924 object_creation_expression
3925 : object_or_delegate_creation_expression
3926 { note ("complain if this is a delegate maybe?"); }
3935 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3938 Location l = (Location) $3;
3940 $$ = new If ((Expression) $4, (Statement) $6, l);
3942 if (RootContext.WarningLevel >= 4){
3943 if ($6 == EmptyStatement.Value)
3944 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3948 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3949 embedded_statement ELSE embedded_statement
3951 Location l = (Location) $3;
3953 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
3958 : SWITCH OPEN_PARENS _mark_
3960 switch_stack.Push (current_block);
3962 expression CLOSE_PARENS
3965 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
3966 current_block = (Block) switch_stack.Pop ();
3982 Report.Error (1522, lexer.Location, "Empty switch block");
3990 ArrayList sections = new ArrayList (4);
3995 | switch_sections switch_section
3997 ArrayList sections = (ArrayList) $1;
4007 current_block = current_block.CreateSwitchBlock (lexer.Location);
4011 Block topmost = current_block;
4013 while (topmost.Implicit)
4014 topmost = topmost.Parent;
4015 $$ = new SwitchSection ((ArrayList) $1, topmost);
4022 ArrayList labels = new ArrayList (4);
4027 | switch_labels switch_label
4029 ArrayList labels = (ArrayList) ($1);
4037 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
4038 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
4041 1523, lexer.Location,
4042 "The keyword case or default must precede code in switch block");
4054 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
4056 Location l = (Location) $3;
4057 $$ = new While ((Expression) $4, (Statement) $6, l);
4059 if (RootContext.WarningLevel >= 4){
4060 if ($6 == EmptyStatement.Value)
4061 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4067 : DO embedded_statement
4068 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
4070 Location l = (Location) $5;
4072 $$ = new Do ((Statement) $2, (Expression) $6, l);
4078 opt_for_initializer SEMICOLON _mark_
4080 Block assign_block = new Block (current_block);
4081 current_block = assign_block;
4083 if ($3 is DictionaryEntry){
4084 DictionaryEntry de = (DictionaryEntry) $3;
4086 Expression type = (Expression) de.Key;
4087 ArrayList var_declarators = (ArrayList) de.Value;
4089 foreach (VariableDeclaration decl in var_declarators){
4093 vi = current_block.AddVariable (
4094 type, decl.identifier, current_local_parameters, decl.Location);
4098 Location l = lexer.Location;
4100 if (decl.expression_or_array_initializer is Expression){
4101 expr = (Expression) decl.expression_or_array_initializer;
4102 } else if (decl.expression_or_array_initializer == null) {
4105 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4106 expr = new ArrayCreation (type, "", init, decl.Location);
4109 LocalVariableReference var;
4110 var = new LocalVariableReference (assign_block, decl.identifier, l);
4113 Assign a = new Assign (var, expr, decl.Location);
4115 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4119 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4120 // This can be referred to as $6 below.
4126 opt_for_condition SEMICOLON
4127 opt_for_iterator CLOSE_PARENS
4130 Location l = (Location) $5;
4132 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
4134 if (RootContext.WarningLevel >= 4){
4135 if ($11 == EmptyStatement.Value)
4136 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4139 current_block.AddStatement (f);
4140 while (current_block.Implicit)
4141 current_block = current_block.Parent;
4143 current_block = current_block.Parent;
4148 : /* empty */ { $$ = EmptyStatement.Value; }
4153 : local_variable_declaration
4154 | statement_expression_list
4158 : /* empty */ { $$ = null; }
4159 | boolean_expression
4163 : /* empty */ { $$ = EmptyStatement.Value; }
4168 : statement_expression_list
4171 statement_expression_list
4172 : statement_expression
4174 // CHANGE: was `null'
4175 Block b = new Block (current_block, Block.Flags.Implicit);
4177 b.AddStatement ((Statement) $1);
4180 | statement_expression_list COMMA statement_expression
4182 Block b = (Block) $1;
4184 b.AddStatement ((Statement) $3);
4190 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4192 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4195 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4196 expression CLOSE_PARENS
4198 Block foreach_block = new Block (current_block);
4199 current_block = foreach_block;
4201 Location l = lexer.Location;
4204 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
4208 // Get a writable reference to this read-only variable.
4210 // Note that the $$ here refers to the value of _this_ code block,
4211 // not the value of the LHS non-terminal. This can be referred to as $9 below.
4212 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4219 LocalVariableReference v = (LocalVariableReference) $9;
4220 Location l = (Location) $6;
4223 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4224 current_block.AddStatement (f);
4227 while (current_block.Implicit)
4228 current_block = current_block.Parent;
4230 current_block = current_block.Parent;
4236 | continue_statement
4246 $$ = new Break (lexer.Location);
4251 : CONTINUE SEMICOLON
4253 $$ = new Continue (lexer.Location);
4258 : GOTO IDENTIFIER SEMICOLON
4260 $$ = new Goto (current_block, (string) $2, lexer.Location);
4262 | GOTO CASE constant_expression SEMICOLON
4264 $$ = new GotoCase ((Expression) $3, lexer.Location);
4266 | GOTO DEFAULT SEMICOLON
4268 $$ = new GotoDefault (lexer.Location);
4273 : RETURN opt_expression SEMICOLON
4275 $$ = new Return ((Expression) $2, lexer.Location);
4280 : THROW opt_expression SEMICOLON
4282 $$ = new Throw ((Expression) $2, lexer.Location);
4287 : IDENTIFIER RETURN expression SEMICOLON
4289 string s = (string) $1;
4291 Report.Error (1003, lexer.Location, "; expected");
4294 if (RootContext.Version == LanguageVersion.ISO_1){
4295 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4298 if (iterator_container == null){
4299 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4302 iterator_container.SetYields ();
4303 $$ = new Yield ((Expression) $3, lexer.Location);
4306 | IDENTIFIER BREAK SEMICOLON
4308 string s = (string) $1;
4310 Report.Error (1003, lexer.Location, "; expected");
4313 if (RootContext.Version == LanguageVersion.ISO_1){
4314 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4317 if (iterator_container == null){
4318 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4321 iterator_container.SetYields ();
4322 $$ = new YieldBreak (lexer.Location);
4333 : TRY block catch_clauses
4337 ArrayList c = (ArrayList)$3;
4338 for (int i = 0; i < c.Count; ++i) {
4339 Catch cc = (Catch) c [i];
4341 if (i != c.Count - 1)
4342 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4349 // Now s contains the list of specific catch clauses
4350 // and g contains the general one.
4352 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4354 | TRY block opt_catch_clauses FINALLY block
4357 ArrayList s = new ArrayList (4);
4358 ArrayList catch_list = (ArrayList) $3;
4360 if (catch_list != null){
4361 foreach (Catch cc in catch_list) {
4369 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4373 Report.Error (1524, lexer.Location, "Expected catch or finally");
4378 : /* empty */ { $$ = null; }
4385 ArrayList l = new ArrayList (4);
4390 | catch_clauses catch_clause
4392 ArrayList l = (ArrayList) $1;
4400 : /* empty */ { $$ = null; }
4405 : CATCH opt_catch_args
4407 Expression type = null;
4411 DictionaryEntry cc = (DictionaryEntry) $2;
4412 type = (Expression) cc.Key;
4413 id = (string) cc.Value;
4416 ArrayList one = new ArrayList (4);
4417 Location loc = lexer.Location;
4419 one.Add (new VariableDeclaration (id, null, loc));
4421 current_block = new Block (current_block);
4422 Block b = declare_local_variables (type, one, loc);
4427 Expression type = null;
4431 DictionaryEntry cc = (DictionaryEntry) $2;
4432 type = (Expression) cc.Key;
4433 id = (string) cc.Value;
4436 while (current_block.Implicit)
4437 current_block = current_block.Parent;
4438 current_block = current_block.Parent;
4442 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4447 : /* empty */ { $$ = null; }
4452 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4454 $$ = new DictionaryEntry ($2, $3);
4461 $$ = new Checked ((Block) $2);
4468 $$ = new Unchecked ((Block) $2);
4475 if (!RootContext.Unsafe){
4476 Report.Error (227, lexer.Location,
4477 "Unsafe code can only be used if --unsafe is used");
4480 $$ = new Unsafe ((Block) $3);
4486 type fixed_pointer_declarators
4489 ArrayList list = (ArrayList) $4;
4490 Expression type = (Expression) $3;
4491 Location l = lexer.Location;
4492 int top = list.Count;
4494 Block assign_block = new Block (current_block);
4495 current_block = assign_block;
4497 for (int i = 0; i < top; i++){
4498 Pair p = (Pair) list [i];
4501 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4513 Location l = (Location) $6;
4515 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4517 if (RootContext.WarningLevel >= 4){
4518 if ($8 == EmptyStatement.Value)
4519 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4522 current_block.AddStatement (f);
4523 while (current_block.Implicit)
4524 current_block = current_block.Parent;
4526 current_block = current_block.Parent;
4530 fixed_pointer_declarators
4531 : fixed_pointer_declarator {
4532 ArrayList declarators = new ArrayList (4);
4534 declarators.Add ($1);
4537 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4539 ArrayList declarators = (ArrayList) $1;
4541 declarators.Add ($3);
4546 fixed_pointer_declarator
4547 : IDENTIFIER ASSIGN expression
4549 $$ = new Pair ($1, $3);
4553 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4559 : LOCK OPEN_PARENS expression CLOSE_PARENS
4565 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4570 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4572 Block assign_block = new Block (current_block);
4573 current_block = assign_block;
4575 if ($3 is DictionaryEntry){
4576 DictionaryEntry de = (DictionaryEntry) $3;
4577 Location l = lexer.Location;
4579 Expression type = (Expression) de.Key;
4580 ArrayList var_declarators = (ArrayList) de.Value;
4582 ArrayList vars = new ArrayList (4);
4584 foreach (VariableDeclaration decl in var_declarators){
4586 LocalInfo vi = current_block.AddVariable (
4587 type, decl.identifier,
4588 current_local_parameters, decl.Location);
4594 if (decl.expression_or_array_initializer is Expression){
4595 expr = (Expression) decl.expression_or_array_initializer;
4597 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4599 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4602 expr = new ArrayCreation (type, "", init, decl.Location);
4605 LocalVariableReference var;
4607 // Get a writable reference to this read-only variable.
4608 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4610 // This is so that it is not a warning on using variables
4613 vars.Add (new DictionaryEntry (var, expr));
4615 // Assign a = new Assign (var, expr, decl.Location);
4616 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4619 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4620 // It can be referred to as $6 below.
4621 $$ = new DictionaryEntry (type, vars);
4628 Using u = new Using ($6, (Statement) $7, (Location) $5);
4629 current_block.AddStatement (u);
4630 while (current_block.Implicit)
4631 current_block = current_block.Parent;
4633 current_block = current_block.Parent;
4637 resource_acquisition
4638 : local_variable_declaration
4642 // Utility rule to save location information
4645 { $$ = lexer.Location; }
4650 // A class used to pass around variable declarations and constants
4652 public class VariableDeclaration {
4653 public string identifier;
4654 public object expression_or_array_initializer;
4655 public Location Location;
4656 public Attributes OptAttributes;
4657 public string DocComment;
4659 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4661 this.identifier = id;
4662 this.expression_or_array_initializer = eoai;
4664 this.OptAttributes = opt_attrs;
4667 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4673 /// Used to pass around interface property information
4675 public class InterfaceAccessorInfo {
4677 public readonly Accessor Get, Set;
4679 public InterfaceAccessorInfo (bool has_get, bool has_set,
4680 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4683 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4685 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4688 Get = new Accessor (null, 0, get_attrs, get_loc);
4690 Set = new Accessor (null, 0, set_attrs, set_loc);
4697 // A class used to hold info about an indexer declarator
4699 public class IndexerDeclaration {
4700 public Expression type;
4701 public MemberName interface_type;
4702 public Parameters param_list;
4704 public IndexerDeclaration (Expression type, MemberName interface_type,
4705 Parameters param_list)
4708 this.interface_type = interface_type;
4709 this.param_list = param_list;
4714 // We use this when we do not have an object in advance that is an IIteratorContainer
4716 public class SimpleIteratorContainer : IIteratorContainer {
4719 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4724 public static SimpleIteratorContainer GetSimple () {
4725 Simple.Yields = false;
4729 public void SetYields () { Yields = true; }
4733 // A class used to hold info about an operator declarator
4735 public class OperatorDeclaration {
4736 public Operator.OpType optype;
4737 public Expression ret_type, arg1type, arg2type;
4738 public string arg1name, arg2name;
4739 public Location location;
4741 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4742 Expression arg1type, string arg1name,
4743 Expression arg2type, string arg2name, Location location)
4746 this.ret_type = ret_type;
4747 this.arg1type = arg1type;
4748 this.arg1name = arg1name;
4749 this.arg2type = arg2type;
4750 this.arg2name = arg2name;
4751 this.location = location;
4756 void Error_ExpectingTypeName (Location l, Expression expr)
4758 if (expr is Invocation){
4759 Report.Error (1002, l, "; expected");
4761 Report.Error (-1, l, "Invalid Type definition");
4766 // Given the @class_name name, it creates a fully qualified name
4767 // based on the containing declaration space
4770 MakeName (MemberName class_name)
4772 string ns = current_namespace.FullName;
4774 if (current_container.Name == ""){
4776 return new MemberName (new MemberName (ns), class_name);
4780 return new MemberName (current_container.MemberName, class_name);
4784 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4786 Block implicit_block;
4787 ArrayList inits = null;
4790 // We use the `Used' property to check whether statements
4791 // have been added to the current block. If so, we need
4792 // to create another block to contain the new declaration
4793 // otherwise, as an optimization, we use the same block to
4794 // add the declaration.
4796 // FIXME: A further optimization is to check if the statements
4797 // that were added were added as part of the initialization
4798 // below. In which case, no other statements have been executed
4799 // and we might be able to reduce the number of blocks for
4800 // situations like this:
4802 // int j = 1; int k = j + 1;
4804 if (current_block.Used)
4805 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4807 implicit_block = current_block;
4809 foreach (VariableDeclaration decl in variable_declarators){
4811 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4812 if (decl.expression_or_array_initializer != null){
4814 inits = new ArrayList (4);
4821 return implicit_block;
4823 foreach (VariableDeclaration decl in inits){
4827 if (decl.expression_or_array_initializer is Expression){
4828 expr = (Expression) decl.expression_or_array_initializer;
4831 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4833 expr = new ArrayCreation (type, "", init, decl.Location);
4836 LocalVariableReference var;
4837 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4839 assign = new Assign (var, expr, decl.Location);
4841 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4844 return implicit_block;
4847 Block declare_local_constants (Expression type, ArrayList declarators)
4849 Block implicit_block;
4851 if (current_block.Used)
4852 implicit_block = new Block (current_block, Block.Flags.Implicit);
4854 implicit_block = current_block;
4856 foreach (VariableDeclaration decl in declarators){
4857 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4858 current_local_parameters, decl.Location);
4861 return implicit_block;
4864 void CheckAttributeTarget (string a)
4868 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4872 Location l = lexer.Location;
4873 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4879 void CheckUnaryOperator (Operator.OpType op)
4883 case Operator.OpType.LogicalNot:
4884 case Operator.OpType.OnesComplement:
4885 case Operator.OpType.Increment:
4886 case Operator.OpType.Decrement:
4887 case Operator.OpType.True:
4888 case Operator.OpType.False:
4889 case Operator.OpType.Addition:
4890 case Operator.OpType.Subtraction:
4895 Location l = lexer.Location;
4896 Report.Error (1019, l, "Overloadable unary operator expected");
4902 void CheckBinaryOperator (Operator.OpType op)
4906 case Operator.OpType.Addition:
4907 case Operator.OpType.Subtraction:
4908 case Operator.OpType.Multiply:
4909 case Operator.OpType.Division:
4910 case Operator.OpType.Modulus:
4911 case Operator.OpType.BitwiseAnd:
4912 case Operator.OpType.BitwiseOr:
4913 case Operator.OpType.ExclusiveOr:
4914 case Operator.OpType.LeftShift:
4915 case Operator.OpType.RightShift:
4916 case Operator.OpType.Equality:
4917 case Operator.OpType.Inequality:
4918 case Operator.OpType.GreaterThan:
4919 case Operator.OpType.LessThan:
4920 case Operator.OpType.GreaterThanOrEqual:
4921 case Operator.OpType.LessThanOrEqual:
4925 Location l = lexer.Location;
4926 Report.Error (1020, l, "Overloadable binary operator expected");
4932 void syntax_error (Location l, string msg)
4934 Report.Error (1003, l, "Syntax error, " + msg);
4937 void output (string s)
4939 Console.WriteLine (s);
4942 void note (string s)
4944 // Used to put annotations
4949 public Tokenizer Lexer {
4955 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4957 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4958 this.name = file.Name;
4960 current_container = RootContext.Tree.Types;
4961 current_container.NamespaceEntry = current_namespace;
4962 oob_stack = new Stack ();
4963 switch_stack = new Stack ();
4965 lexer = new Tokenizer (reader, file, defines);
4968 public void parse ()
4971 if (yacc_verbose_flag > 1)
4972 yyparse (lexer, new yydebug.yyDebugSimple ());
4975 Tokenizer tokenizer = lexer as Tokenizer;
4976 tokenizer.cleanup ();
4977 } catch (Exception e){
4979 // Removed for production use, use parser verbose to get the output.
4981 // Console.WriteLine (e);
4982 Report.Error (-25, lexer.Location, "Parsing error");
4983 if (yacc_verbose_flag > 0)
4984 Console.WriteLine (e);
4988 void CheckToken (int error, int yyToken, string msg)
4990 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4991 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4994 Report.Error (error, lexer.Location, msg);
4997 void CheckIdentifierToken (int yyToken)
4999 CheckToken (1041, yyToken, "Identifier expected");
5002 string ConsumeStoredComment ()
5004 string s = tmpComment;
5006 Lexer.doc_state = XmlCommentState.Allowed;