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;
47 /// This is used by the unary_expression code to resolve
48 /// a name against a parameter.
50 Parameters current_local_parameters;
53 /// Using during property parsing to describe the implicit
54 /// value parameter that is passed to the "set" and "get"accesor
55 /// methods (properties and indexers).
57 Expression implicit_value_parameter_type;
58 Parameters indexer_parameters;
61 /// Used to determine if we are parsing the get/set pair
62 /// of an indexer or a property
67 /// An out-of-band stack.
76 static public int yacc_verbose_flag;
78 // Name of the file we are parsing
87 /// Temporary Xml documentation cache.
88 /// For enum types, we need one more temporary store.
91 string enumTypeComment;
93 /// Current attribute target
94 string current_attr_target;
96 /// assembly and module attribute definition is enabled
97 bool global_attrs_enabled = true;
99 bool has_get, has_set;
104 %token NONE /* This token is never returned by our lexer */
105 %token ERROR // This is used not by the parser, but by the tokenizer.
109 *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 ")"
220 %token BITWISE_AND "&"
221 %token BITWISE_OR "|"
228 /* C# multi-character operators. */
231 %token OP_SHIFT_LEFT "<<"
232 %token OP_SHIFT_RIGHT ">>"
239 %token OP_MULT_ASSIGN "*="
240 %token OP_DIV_ASSIGN "/="
241 %token OP_MOD_ASSIGN "%="
242 %token OP_ADD_ASSIGN "+="
243 %token OP_SUB_ASSIGN "-="
244 %token OP_SHIFT_LEFT_ASSIGN "<<="
245 %token OP_SHIFT_RIGHT_ASSIGN ">>="
246 %token OP_AND_ASSIGN "&="
247 %token OP_XOR_ASSIGN "^="
248 %token OP_OR_ASSIGN "|="
252 %token LITERAL_INTEGER "int literal"
253 %token LITERAL_FLOAT "float literal"
254 %token LITERAL_DOUBLE "double literal"
255 %token LITERAL_DECIMAL "decimal literal"
256 %token LITERAL_CHARACTER "character literal"
257 %token LITERAL_STRING "string literal"
260 %token CLOSE_PARENS_CAST
261 %token CLOSE_PARENS_NO_CAST
262 %token CLOSE_PARENS_OPEN_PARENS
263 %token CLOSE_PARENS_MINUS
265 /* Add precedence rules to solve dangling else s/r conflict */
274 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
276 %left STAR DIV PERCENT
277 %right BANG CARRET UMINUS
278 %nonassoc OP_INC OP_DEC
280 %left OPEN_BRACKET OPEN_BRACE
284 %start compilation_unit
288 : outer_declarations opt_EOF
289 | outer_declarations global_attributes opt_EOF
290 | global_attributes opt_EOF
291 | opt_EOF /* allow empty files */
297 Lexer.check_incorrect_doc_comment ();
301 Lexer.check_incorrect_doc_comment ();
307 | outer_declarations outer_declaration
312 | namespace_member_declaration
317 | using_directives using_directive
321 : using_alias_directive
323 if (RootContext.Documentation != null)
324 Lexer.doc_state = XmlCommentState.Allowed;
326 | using_namespace_directive
328 if (RootContext.Documentation != null)
329 Lexer.doc_state = XmlCommentState.Allowed;
333 using_alias_directive
334 : USING IDENTIFIER ASSIGN
335 namespace_or_type_name SEMICOLON
337 current_namespace.UsingAlias ((string) $2, (MemberName) $4, lexer.Location);
340 CheckIdentifierToken (yyToken);
344 using_namespace_directive
345 : USING namespace_name SEMICOLON
347 current_namespace.Using ((MemberName) $2, lexer.Location);
352 // Strictly speaking, namespaces don't have attributes but
353 // we parse global attributes along with namespace declarations and then
356 namespace_declaration
357 : opt_attributes NAMESPACE namespace_or_type_name
360 Report.Error(1671, Lexer.Location, "A namespace declaration cannot have modifiers or attributes");
363 MemberName name = (MemberName) $3;
365 current_namespace = new NamespaceEntry (
366 current_namespace, file, name.GetName (), lexer.Location);
368 namespace_body opt_semicolon
370 current_namespace = current_namespace.Parent;
385 : namespace_or_type_name
391 if (RootContext.Documentation != null)
392 Lexer.doc_state = XmlCommentState.Allowed;
395 opt_namespace_member_declarations
406 opt_namespace_member_declarations
408 | namespace_member_declarations
411 namespace_member_declarations
412 : namespace_member_declaration
413 | namespace_member_declarations namespace_member_declaration
416 namespace_member_declaration
420 DeclSpace ds = (DeclSpace)$1;
422 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
423 Report.Error (1527, lexer.Location,
424 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
427 current_namespace.DeclarationFound = true;
429 | namespace_declaration {
430 current_namespace.DeclarationFound = true;
433 | field_declaration {
434 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
436 | method_declaration {
437 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
444 | interface_declaration
446 | delegate_declaration
448 // Enable this when we have handled all errors, because this acts as a generic fallback
451 // Console.WriteLine ("Token=" + yyToken);
452 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
464 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
472 global_attrs_enabled = false;
477 global_attrs_enabled = false;
486 ArrayList sect = (ArrayList) $1;
488 if (global_attrs_enabled) {
489 if (current_attr_target == "module") {
490 CodeGen.Module.AddAttributes (sect);
492 } else if (current_attr_target == "assembly") {
493 CodeGen.Assembly.AddAttributes (sect);
496 $$ = new Attributes (sect);
499 if (RootContext.Documentation != null) {
500 Lexer.check_incorrect_doc_comment ();
502 XmlCommentState.Allowed;
506 $$ = new Attributes (sect);
508 current_attr_target = null;
510 | attribute_sections attribute_section
512 Attributes attrs = $1 as Attributes;
513 ArrayList sect = (ArrayList) $2;
515 if (global_attrs_enabled) {
516 if (current_attr_target == "module") {
517 CodeGen.Module.AddAttributes (sect);
519 } else if (current_attr_target == "assembly") {
520 CodeGen.Assembly.AddAttributes (sect);
524 attrs = new Attributes (sect);
526 attrs.AddAttributes (sect);
530 attrs = new Attributes (sect);
532 attrs.AddAttributes (sect);
535 current_attr_target = null;
540 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
544 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
550 attribute_target_specifier
551 : attribute_target COLON
553 current_attr_target = (string)$1;
561 CheckAttributeTarget ((string) $1);
564 | EVENT { $$ = "event"; }
565 | RETURN { $$ = "return"; }
571 ArrayList attrs = new ArrayList (4);
577 | attribute_list COMMA attribute
579 ArrayList attrs = (ArrayList) $1;
591 opt_attribute_arguments
593 Location loc = (Location) $2;
594 MemberName mname = (MemberName) $1;
595 MemberName left = mname.Left;
596 string identifier = mname.Name;
598 Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
600 if (current_attr_target == "assembly" || current_attr_target == "module")
601 $$ = new GlobalAttribute (current_class, current_attr_target,
602 left_expr, identifier, (ArrayList) $3, loc);
604 $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
609 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
612 opt_attribute_arguments
613 : /* empty */ { $$ = null; }
614 | OPEN_PARENS attribute_arguments CLOSE_PARENS
622 : opt_positional_argument_list
627 ArrayList args = new ArrayList (4);
633 | positional_argument_list COMMA named_argument_list
635 ArrayList args = new ArrayList (4);
641 | named_argument_list
643 ArrayList args = new ArrayList (4);
652 opt_positional_argument_list
653 : /* empty */ { $$ = null; }
654 | positional_argument_list
657 positional_argument_list
660 ArrayList args = new ArrayList (4);
661 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
665 | positional_argument_list COMMA expression
667 ArrayList args = (ArrayList) $1;
668 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
677 ArrayList args = new ArrayList (4);
682 | named_argument_list COMMA named_argument
684 ArrayList args = (ArrayList) $1;
689 | named_argument_list COMMA expression
691 Report.Error (1016, lexer.Location, "Named attribute argument expected");
697 : IDENTIFIER ASSIGN expression
699 $$ = new DictionaryEntry (
701 new Argument ((Expression) $3, Argument.AType.Expression));
707 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
710 opt_class_member_declarations
712 | class_member_declarations
715 class_member_declarations
716 : class_member_declaration
717 | class_member_declarations
718 class_member_declaration
721 class_member_declaration
722 : constant_declaration // done
723 | field_declaration // done
724 | method_declaration // done
725 | property_declaration // done
726 | event_declaration // done
727 | indexer_declaration // done
728 | operator_declaration // done
729 | constructor_declaration // done
730 | destructor_declaration // done
740 MemberName name = MakeName ((MemberName) $5);
741 bool partial = (bool) $3;
744 ClassPart part = PartialContainer.CreatePart (
745 current_namespace, current_class, name, (int) $2,
746 (Attributes) $1, Kind.Struct, lexer.Location);
748 current_container = part.PartialContainer;
749 current_class = part;
751 current_class = new Struct (
752 current_namespace, current_class, name, (int) $2,
753 (Attributes) $1, lexer.Location);
755 current_container.AddClassOrStruct (current_class);
756 current_container = current_class;
757 RootContext.Tree.RecordDecl (name, current_class);
763 current_class.Bases = (ArrayList) $7;
765 if (RootContext.Documentation != null)
766 current_class.DocComment = Lexer.consume_doc_comment ();
770 if (RootContext.Documentation != null)
771 Lexer.doc_state = XmlCommentState.Allowed;
775 $$ = pop_current_class ();
777 | opt_attributes opt_modifiers opt_partial STRUCT error {
778 CheckIdentifierToken (yyToken);
785 if (RootContext.Documentation != null)
786 Lexer.doc_state = XmlCommentState.Allowed;
788 opt_struct_member_declarations CLOSE_BRACE
791 opt_struct_member_declarations
793 | struct_member_declarations
796 struct_member_declarations
797 : struct_member_declaration
798 | struct_member_declarations struct_member_declaration
801 struct_member_declaration
802 : constant_declaration
805 | property_declaration
807 | indexer_declaration
808 | operator_declaration
809 | constructor_declaration
813 * This is only included so we can flag error 575:
814 * destructors only allowed on class types
816 | destructor_declaration
827 int modflags = (int) $2;
828 foreach (VariableDeclaration constant in (ArrayList) $5){
829 Location l = constant.Location;
830 if ((modflags & Modifiers.STATIC) != 0) {
831 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
835 Const c = new Const (
836 current_class, (Expression) $4, (string) constant.identifier,
837 (Expression) constant.expression_or_array_initializer, modflags,
840 if (RootContext.Documentation != null) {
841 c.DocComment = Lexer.consume_doc_comment ();
842 Lexer.doc_state = XmlCommentState.Allowed;
844 current_container.AddConstant (c);
850 : constant_declarator
852 ArrayList constants = new ArrayList (4);
857 | constant_declarators COMMA constant_declarator
860 ArrayList constants = (ArrayList) $1;
867 : IDENTIFIER ASSIGN constant_expression
869 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
873 // A const field requires a value to be provided
874 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
886 Expression type = (Expression) $3;
889 foreach (VariableDeclaration var in (ArrayList) $4){
890 Field field = new Field (current_class, type, mod, var.identifier,
891 var.expression_or_array_initializer,
892 (Attributes) $1, var.Location);
894 if (RootContext.Documentation != null) {
895 field.DocComment = Lexer.consume_doc_comment ();
896 Lexer.doc_state = XmlCommentState.Allowed;
898 current_container.AddField (field);
905 fixed_variable_declarators
908 Expression type = (Expression) $4;
911 foreach (VariableDeclaration var in (ArrayList) $5) {
912 FixedField field = new FixedField (current_class, type, mod, var.identifier,
913 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
915 if (RootContext.Documentation != null) {
916 field.DocComment = Lexer.consume_doc_comment ();
917 Lexer.doc_state = XmlCommentState.Allowed;
919 current_container.AddField (field);
927 Report.Error (670, lexer.Location, "void type is not allowed for fields");
931 fixed_variable_declarators
932 : fixed_variable_declarator
934 ArrayList decl = new ArrayList (2);
938 | fixed_variable_declarators COMMA fixed_variable_declarator
940 ArrayList decls = (ArrayList) $1;
946 fixed_variable_declarator
947 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
949 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
954 : variable_declarator
956 ArrayList decl = new ArrayList (4);
960 | variable_declarators COMMA variable_declarator
962 ArrayList decls = (ArrayList) $1;
969 : IDENTIFIER ASSIGN variable_initializer
971 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
975 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
977 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
979 Report.Error (650, lexer.Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
980 "To declare a fixed buffer field, use the fixed keyword before the field type");
993 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
995 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
999 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1006 iterator_container = (IIteratorContainer) $1;
1007 if (RootContext.Documentation != null)
1008 Lexer.doc_state = XmlCommentState.NotAllowed;
1012 Method method = (Method) $1;
1013 Block b = (Block) $3;
1014 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1017 if ((method.ModFlags & extern_abstract) == 0){
1019 501, lexer.Location, current_container.MakeName (method.Name) +
1020 "must declare a body because it is not marked abstract or extern");
1023 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1025 179, lexer.Location, current_container.MakeName (method.Name) +
1026 " is declared extern, but has a body");
1030 method.Block = (ToplevelBlock) $3;
1031 current_container.AddMethod (method);
1033 current_local_parameters = null;
1034 iterator_container = null;
1036 if (RootContext.Documentation != null)
1037 Lexer.doc_state = XmlCommentState.Allowed;
1051 1585, lexer.Location, "Member modifier `" +
1052 Modifiers.Name (i) + "' must precede member type and name");
1063 type namespace_or_type_name
1064 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1066 MemberName name = (MemberName) $4;
1068 Method method = new Method (current_class, (Expression) $3, (int) $2,
1069 false, name, (Parameters) $6, (Attributes) $1,
1072 current_local_parameters = (Parameters) $6;
1074 if (RootContext.Documentation != null)
1075 method.DocComment = Lexer.consume_doc_comment ();
1081 VOID namespace_or_type_name
1082 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1084 MemberName name = (MemberName) $4;
1086 Method method = new Method (current_class, TypeManager.system_void_expr,
1087 (int) $2, false, name, (Parameters) $6,
1088 (Attributes) $1, lexer.Location);
1090 current_local_parameters = (Parameters) $6;
1092 if (RootContext.Documentation != null)
1093 method.DocComment = Lexer.consume_doc_comment ();
1100 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1102 Report.Error (1585, lexer.Location,
1103 String.Format ("Modifier {0} should appear before type",
1104 Modifiers.Name ((int) $4)));
1105 MemberName name = (MemberName) $4;
1107 Method method = new Method (current_class, TypeManager.system_void_expr,
1108 0, false, name, (Parameters) $7, (Attributes) $1,
1111 current_local_parameters = (Parameters) $7;
1113 if (RootContext.Documentation != null)
1114 method.DocComment = Lexer.consume_doc_comment ();
1122 | SEMICOLON { $$ = null; }
1125 opt_formal_parameter_list
1126 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1127 | formal_parameter_list
1130 formal_parameter_list
1133 ArrayList pars_list = (ArrayList) $1;
1135 Parameter [] pars = new Parameter [pars_list.Count];
1136 pars_list.CopyTo (pars);
1138 $$ = new Parameters (pars, null, lexer.Location);
1140 | fixed_parameters COMMA parameter_array
1142 ArrayList pars_list = (ArrayList) $1;
1144 Parameter [] pars = new Parameter [pars_list.Count];
1145 pars_list.CopyTo (pars);
1147 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1149 | fixed_parameters COMMA ARGLIST
1151 ArrayList pars_list = (ArrayList) $1;
1153 Parameter [] pars = new Parameter [pars_list.Count];
1154 pars_list.CopyTo (pars);
1156 $$ = new Parameters (pars, true, lexer.Location);
1158 | parameter_array COMMA fixed_parameters
1160 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1163 | ARGLIST COMMA fixed_parameters
1165 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1170 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1174 $$ = new Parameters (null, true, lexer.Location);
1181 ArrayList pars = new ArrayList (4);
1186 | fixed_parameters COMMA fixed_parameter
1188 ArrayList pars = (ArrayList) $1;
1197 opt_parameter_modifier
1201 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1204 opt_parameter_modifier
1207 Report.Error (1001, lexer.Location, "Identifier expected");
1211 opt_parameter_modifier
1214 CheckIdentifierToken (yyToken);
1218 opt_parameter_modifier
1224 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1229 opt_parameter_modifier
1230 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1231 | parameter_modifier
1235 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1236 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1240 : opt_attributes PARAMS type IDENTIFIER
1242 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1243 note ("type must be a single-dimension array type");
1245 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1247 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1250 | opt_attributes PARAMS type error {
1251 CheckIdentifierToken (yyToken);
1256 property_declaration
1260 namespace_or_type_name
1262 if (RootContext.Documentation != null)
1263 tmpComment = Lexer.consume_doc_comment ();
1267 implicit_value_parameter_type = (Expression) $3;
1269 lexer.PropertyParsing = true;
1271 $$ = lexer.Location;
1273 iterator_container = SimpleIteratorContainer.GetSimple ();
1275 accessor_declarations
1277 lexer.PropertyParsing = false;
1278 has_get = has_set = false;
1286 Pair pair = (Pair) $8;
1287 Accessor get_block = (Accessor) pair.First;
1288 Accessor set_block = (Accessor) pair.Second;
1290 Location loc = (Location) $7;
1291 MemberName name = (MemberName) $4;
1293 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1294 name, (Attributes) $1, get_block, set_block, loc);
1295 if (SimpleIteratorContainer.Simple.Yields)
1298 current_container.AddProperty (prop);
1299 implicit_value_parameter_type = null;
1300 iterator_container = null;
1302 if (RootContext.Documentation != null)
1303 prop.DocComment = ConsumeStoredComment ();
1308 accessor_declarations
1309 : get_accessor_declaration
1311 $$ = new Pair ($1, null);
1313 | get_accessor_declaration accessor_declarations
1315 Pair pair = (Pair) $2;
1319 | set_accessor_declaration
1321 $$ = new Pair (null, $1);
1323 | set_accessor_declaration accessor_declarations
1325 Pair pair = (Pair) $2;
1331 Report.Error (1014, lexer.Location, "A get or set accessor expected");
1336 get_accessor_declaration
1337 : opt_attributes opt_modifiers GET
1339 // If this is not the case, then current_local_parameters has already
1340 // been set in indexer_declaration
1341 if (parsing_indexer == false)
1342 current_local_parameters = null;
1344 current_local_parameters = indexer_parameters;
1345 lexer.PropertyParsing = false;
1350 Report.Error (1007, lexer.Location, "Property accessor already defined");
1353 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1355 current_local_parameters = null;
1356 lexer.PropertyParsing = true;
1358 if (RootContext.Documentation != null)
1359 if (Lexer.doc_state == XmlCommentState.Error)
1360 Lexer.doc_state = XmlCommentState.NotAllowed;
1364 set_accessor_declaration
1365 : opt_attributes opt_modifiers SET
1368 Parameter implicit_value_parameter = new Parameter (
1369 implicit_value_parameter_type, "value",
1370 Parameter.Modifier.NONE, null);
1372 if (parsing_indexer == false) {
1373 args = new Parameter [1];
1374 args [0] = implicit_value_parameter;
1375 current_local_parameters = new Parameters (args, null, lexer.Location);
1377 Parameter [] fpars = indexer_parameters.FixedParameters;
1380 int count = fpars.Length;
1382 args = new Parameter [count + 1];
1383 fpars.CopyTo (args, 0);
1384 args [count] = implicit_value_parameter;
1387 current_local_parameters = new Parameters (
1388 args, indexer_parameters.ArrayParameter, lexer.Location);
1391 lexer.PropertyParsing = false;
1396 Report.Error (1007, lexer.Location, "Property accessor already defined");
1399 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1401 current_local_parameters = null;
1402 lexer.PropertyParsing = true;
1404 if (RootContext.Documentation != null
1405 && Lexer.doc_state == XmlCommentState.Error)
1406 Lexer.doc_state = XmlCommentState.NotAllowed;
1412 | SEMICOLON { $$ = null; }
1415 interface_declaration
1419 INTERFACE member_name
1421 MemberName name = MakeName ((MemberName) $5);
1422 bool partial = (bool) $3;
1425 ClassPart part = PartialContainer.CreatePart (
1426 current_namespace, current_class, name, (int) $2,
1427 (Attributes) $1, Kind.Interface, lexer.Location);
1429 current_container = part.PartialContainer;
1430 current_class = part;
1432 current_class = new Interface (
1433 current_namespace, current_class, name, (int) $2,
1434 (Attributes) $1, lexer.Location);
1436 current_container.AddInterface (current_class);
1437 current_container = current_class;
1438 RootContext.Tree.RecordDecl (name, current_class);
1443 current_class.Bases = (ArrayList) $7;
1445 if (RootContext.Documentation != null) {
1446 current_class.DocComment = Lexer.consume_doc_comment ();
1447 Lexer.doc_state = XmlCommentState.Allowed;
1452 if (RootContext.Documentation != null)
1453 Lexer.doc_state = XmlCommentState.Allowed;
1457 $$ = pop_current_class ();
1459 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1460 CheckIdentifierToken (yyToken);
1466 opt_interface_member_declarations
1470 opt_interface_member_declarations
1472 | interface_member_declarations
1475 interface_member_declarations
1476 : interface_member_declaration
1477 | interface_member_declarations interface_member_declaration
1480 interface_member_declaration
1481 : interface_method_declaration
1486 Method m = (Method) $1;
1488 if (m.IsExplicitImpl)
1489 Report.Error (541, lexer.Location,
1490 "Explicit interface declaration can only be declared in a class or struct");
1492 current_container.AddMethod (m);
1494 if (RootContext.Documentation != null)
1495 Lexer.doc_state = XmlCommentState.Allowed;
1497 | interface_property_declaration
1502 Property p = (Property) $1;
1504 if (p.IsExplicitImpl)
1505 Report.Error (541, lexer.Location,
1506 "Explicit interface declaration can only be declared in a class or struct");
1508 current_container.AddProperty (p);
1510 if (RootContext.Documentation != null)
1511 Lexer.doc_state = XmlCommentState.Allowed;
1513 | interface_event_declaration
1516 Event e = (Event) $1;
1518 if (e.IsExplicitImpl)
1519 Report.Error (541, lexer.Location,
1520 "Explicit interface declaration can only be declared in a class or struct");
1522 current_container.AddEvent (e);
1525 if (RootContext.Documentation != null)
1526 Lexer.doc_state = XmlCommentState.Allowed;
1528 | interface_indexer_declaration
1533 Indexer i = (Indexer) $1;
1535 if (i.IsExplicitImpl)
1536 Report.Error (541, lexer.Location,
1537 "Explicit interface declaration can only be declared in a class or struct");
1539 current_container.AddIndexer (i);
1541 if (RootContext.Documentation != null)
1542 Lexer.doc_state = XmlCommentState.Allowed;
1544 | delegate_declaration
1546 Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
1550 Report.Error (524, lexer.Location, "Interfaces can not declare classes");
1552 | struct_declaration
1554 Report.Error (524, lexer.Location, "Interfaces can not declare structures");
1558 Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
1560 | interface_declaration
1562 Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
1564 | constant_declaration
1566 Report.Error (525, lexer.Location, "Interfaces cannot contain constants");
1574 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1579 interface_method_declaration
1580 : opt_attributes opt_new type namespace_or_type_name
1581 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1584 MemberName name = (MemberName) $4;
1586 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1587 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1588 if (RootContext.Documentation != null)
1589 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1591 | opt_attributes opt_new type namespace_or_type_name
1592 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1593 OPEN_BRACE opt_statement_list CLOSE_BRACE
1595 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", $4);
1600 interface_property_declaration
1605 { lexer.PropertyParsing = true; }
1607 { lexer.PropertyParsing = false; }
1610 if ($3 == TypeManager.system_void_expr) {
1611 Report.Error (547, lexer.Location, "'{0}': property cannot have void type", $4);
1618 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1620 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1621 new MemberName ((string) $4), (Attributes) $1,
1622 pinfo.Get, pinfo.Set, lexer.Location);
1623 if (RootContext.Documentation != null)
1624 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1629 CheckIdentifierToken (yyToken);
1635 : opt_attributes opt_modifiers GET SEMICOLON
1636 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1637 | opt_attributes opt_modifiers GET OPEN_BRACE
1639 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", ".get");
1642 | opt_attributes opt_modifiers SET SEMICOLON
1643 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1644 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1645 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1646 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1647 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1650 Report.Error (548, lexer.Location, "'{0}' : property or indexer must have at least one accessor", "");
1654 interface_event_declaration
1655 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1657 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1658 new MemberName ((string) $5), null,
1659 (Attributes) $1, lexer.Location);
1660 if (RootContext.Documentation != null)
1661 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1663 | opt_attributes opt_new EVENT type error {
1664 CheckIdentifierToken (yyToken);
1667 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1668 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1671 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1673 lexer.EventParsing = true;
1675 event_accessor_declarations
1677 lexer.EventParsing = false;
1680 Report.Error (69, lexer.Location, "Event in interface cannot have add or remove accessors");
1685 interface_indexer_declaration
1686 : opt_attributes opt_new type THIS
1687 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1689 { lexer.PropertyParsing = true; }
1691 { lexer.PropertyParsing = false; }
1697 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1699 $$ = new Indexer (current_class, (Expression) $3,
1700 new MemberName (TypeContainer.DefaultIndexerName),
1701 (int) $2, true, (Parameters) $6, (Attributes) $1,
1702 info.Get, info.Set, lexer.Location);
1703 if (RootContext.Documentation != null)
1704 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1708 operator_declaration
1709 : opt_attributes opt_modifiers operator_declarator
1711 iterator_container = SimpleIteratorContainer.GetSimple ();
1718 OperatorDeclaration decl = (OperatorDeclaration) $3;
1720 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1722 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1723 if (decl.arg2type != null)
1724 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1726 Operator op = new Operator (
1727 current_class, decl.optype, decl.ret_type, (int) $2,
1728 new Parameters (param_list, null, decl.location),
1729 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1731 if (RootContext.Documentation != null) {
1732 op.DocComment = tmpComment;
1733 Lexer.doc_state = XmlCommentState.Allowed;
1736 if (SimpleIteratorContainer.Simple.Yields)
1739 // Note again, checking is done in semantic analysis
1740 current_container.AddOperator (op);
1742 current_local_parameters = null;
1743 iterator_container = null;
1749 | SEMICOLON { $$ = null; }
1752 : type OPERATOR overloadable_operator
1753 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1755 Operator.OpType op = (Operator.OpType) $3;
1756 CheckUnaryOperator (op);
1758 if (op == Operator.OpType.Addition)
1759 op = Operator.OpType.UnaryPlus;
1761 if (op == Operator.OpType.Subtraction)
1762 op = Operator.OpType.UnaryNegation;
1764 Parameter [] pars = new Parameter [1];
1765 Expression type = (Expression) $5;
1767 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1769 current_local_parameters = new Parameters (pars, null, lexer.Location);
1771 if (RootContext.Documentation != null) {
1772 tmpComment = Lexer.consume_doc_comment ();
1773 Lexer.doc_state = XmlCommentState.NotAllowed;
1776 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1777 null, null, lexer.Location);
1779 | type OPERATOR overloadable_operator
1781 type IDENTIFIER COMMA
1785 CheckBinaryOperator ((Operator.OpType) $3);
1787 Parameter [] pars = new Parameter [2];
1789 Expression typeL = (Expression) $5;
1790 Expression typeR = (Expression) $8;
1792 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1793 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1795 current_local_parameters = new Parameters (pars, null, lexer.Location);
1797 if (RootContext.Documentation != null) {
1798 tmpComment = Lexer.consume_doc_comment ();
1799 Lexer.doc_state = XmlCommentState.NotAllowed;
1802 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1804 typeR, (string) $9, lexer.Location);
1806 | conversion_operator_declarator
1807 | type OPERATOR overloadable_operator
1809 type IDENTIFIER COMMA
1810 type IDENTIFIER COMMA
1814 Report.Error (1534, lexer.Location, "Overloaded binary operator '{0}' takes two parameters", $3);
1820 overloadable_operator
1822 : BANG { $$ = Operator.OpType.LogicalNot; }
1823 | TILDE { $$ = Operator.OpType.OnesComplement; }
1824 | OP_INC { $$ = Operator.OpType.Increment; }
1825 | OP_DEC { $$ = Operator.OpType.Decrement; }
1826 | TRUE { $$ = Operator.OpType.True; }
1827 | FALSE { $$ = Operator.OpType.False; }
1828 // Unary and binary:
1829 | PLUS { $$ = Operator.OpType.Addition; }
1830 | MINUS { $$ = Operator.OpType.Subtraction; }
1832 | STAR { $$ = Operator.OpType.Multiply; }
1833 | DIV { $$ = Operator.OpType.Division; }
1834 | PERCENT { $$ = Operator.OpType.Modulus; }
1835 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1836 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1837 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1838 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1839 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1840 | OP_EQ { $$ = Operator.OpType.Equality; }
1841 | OP_NE { $$ = Operator.OpType.Inequality; }
1842 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1843 | OP_LT { $$ = Operator.OpType.LessThan; }
1844 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1845 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1848 conversion_operator_declarator
1849 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1851 Parameter [] pars = new Parameter [1];
1853 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1855 current_local_parameters = new Parameters (pars, null, lexer.Location);
1857 if (RootContext.Documentation != null) {
1858 tmpComment = Lexer.consume_doc_comment ();
1859 Lexer.doc_state = XmlCommentState.NotAllowed;
1862 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1863 null, null, lexer.Location);
1865 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1867 Parameter [] pars = new Parameter [1];
1869 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1871 current_local_parameters = new Parameters (pars, null, lexer.Location);
1873 if (RootContext.Documentation != null) {
1874 tmpComment = Lexer.consume_doc_comment ();
1875 Lexer.doc_state = XmlCommentState.NotAllowed;
1878 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1879 null, null, lexer.Location);
1883 syntax_error (lexer.Location, "'operator' expected");
1887 syntax_error (lexer.Location, "'operator' expected");
1891 constructor_declaration
1894 constructor_declarator
1897 Constructor c = (Constructor) $3;
1898 c.Block = (ToplevelBlock) $4;
1899 c.OptAttributes = (Attributes) $1;
1900 c.ModFlags = (int) $2;
1902 if (RootContext.Documentation != null)
1903 c.DocComment = ConsumeStoredComment ();
1905 if (c.Name == current_container.Basename){
1906 if ((c.ModFlags & Modifiers.STATIC) != 0){
1907 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1909 515, c.Location, String.Format (
1910 "`{0}.{1}': static constructor can not have access modifiers",
1911 c.Name, current_container.Name));
1914 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1916 if (c.Initializer != null){
1919 "Static constructors can not have an explicit this or base " +
1920 "constructor invocations");
1923 if (!c.Parameters.Empty){
1925 132, c.Location, "Static constructors should not have parameters");
1928 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1931 // We let another layer check the validity of the constructor.
1932 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1935 current_container.AddConstructor (c);
1937 current_local_parameters = null;
1938 if (RootContext.Documentation != null)
1939 Lexer.doc_state = XmlCommentState.Allowed;
1943 constructor_declarator
1946 if (RootContext.Documentation != null) {
1947 tmpComment = Lexer.consume_doc_comment ();
1948 Lexer.doc_state = XmlCommentState.Allowed;
1951 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
1953 current_local_parameters = (Parameters) $4;
1955 opt_constructor_initializer
1957 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1958 (ConstructorInitializer) $8, (Location) $6);
1964 | SEMICOLON { $$ = null; }
1967 opt_constructor_initializer
1968 : /* empty */ { $$ = null; }
1969 | constructor_initializer
1972 constructor_initializer
1973 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1975 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1977 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1979 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1982 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1988 : /* EMPTY */ { $$ = 0; }
1989 | UNSAFE { $$ = Modifiers.UNSAFE; }
1990 | EXTERN { $$ = Modifiers.EXTERN; }
1993 destructor_declaration
1994 : opt_attributes opt_finalizer TILDE
1996 if (RootContext.Documentation != null) {
1997 tmpComment = Lexer.consume_doc_comment ();
1998 Lexer.doc_state = XmlCommentState.NotAllowed;
2001 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2003 if ((string) $5 != current_container.Basename){
2004 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
2005 } else if (current_container.Kind != Kind.Class){
2006 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
2008 Location l = lexer.Location;
2011 if (!RootContext.StdLib && current_container.Name == "System.Object")
2012 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2014 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2016 if ((m & Modifiers.UNSAFE) != 0){
2017 if (!RootContext.Unsafe){
2018 Report.Error (227, l,
2019 "Unsafe code requires the -unsafe command " +
2020 "line option to be specified");
2024 Method d = new Destructor (
2025 current_class, TypeManager.system_void_expr, m, "Finalize",
2026 new Parameters (null, null, l), (Attributes) $1, l);
2027 if (RootContext.Documentation != null)
2028 d.DocComment = ConsumeStoredComment ();
2030 d.Block = (ToplevelBlock) $8;
2031 current_container.AddMethod (d);
2039 EVENT type variable_declarators SEMICOLON
2041 foreach (VariableDeclaration var in (ArrayList) $5) {
2043 MemberName name = new MemberName (var.identifier);
2045 Event e = new EventField (
2046 current_class, (Expression) $4, (int) $2, false, name,
2047 var.expression_or_array_initializer, (Attributes) $1,
2050 current_container.AddEvent (e);
2052 if (RootContext.Documentation != null) {
2053 e.DocComment = Lexer.consume_doc_comment ();
2054 Lexer.doc_state = XmlCommentState.Allowed;
2060 EVENT type namespace_or_type_name
2063 implicit_value_parameter_type = (Expression) $4;
2064 lexer.EventParsing = true;
2066 event_accessor_declarations
2068 lexer.EventParsing = false;
2072 Location loc = (Location) $7;
2075 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2078 Pair pair = (Pair) $9;
2080 MemberName name = (MemberName) $5;
2082 Event e = new EventProperty (
2083 current_class, (Expression) $4, (int) $2, false, name, null,
2084 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2086 if (RootContext.Documentation != null) {
2087 e.DocComment = Lexer.consume_doc_comment ();
2088 Lexer.doc_state = XmlCommentState.Allowed;
2091 current_container.AddEvent (e);
2092 implicit_value_parameter_type = null;
2095 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2096 MemberName mn = (MemberName) $5;
2098 if (mn.Left != null)
2099 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2101 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2103 if (RootContext.Documentation != null)
2104 Lexer.doc_state = XmlCommentState.Allowed;
2108 event_accessor_declarations
2109 : add_accessor_declaration remove_accessor_declaration
2111 $$ = new Pair ($1, $2);
2113 | remove_accessor_declaration add_accessor_declaration
2115 $$ = new Pair ($2, $1);
2117 | add_accessor_declaration { $$ = null; }
2118 | remove_accessor_declaration { $$ = null; }
2121 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2127 add_accessor_declaration
2128 : opt_attributes ADD
2130 Parameter [] args = new Parameter [1];
2131 Parameter implicit_value_parameter = new Parameter (
2132 implicit_value_parameter_type, "value",
2133 Parameter.Modifier.NONE, null);
2135 args [0] = implicit_value_parameter;
2137 current_local_parameters = new Parameters (args, null, lexer.Location);
2138 lexer.EventParsing = false;
2142 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2143 lexer.EventParsing = true;
2145 | opt_attributes ADD error {
2146 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2149 | opt_attributes modifiers ADD {
2150 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2155 remove_accessor_declaration
2156 : opt_attributes REMOVE
2158 Parameter [] args = new Parameter [1];
2159 Parameter implicit_value_parameter = new Parameter (
2160 implicit_value_parameter_type, "value",
2161 Parameter.Modifier.NONE, null);
2163 args [0] = implicit_value_parameter;
2165 current_local_parameters = new Parameters (args, null, lexer.Location);
2166 lexer.EventParsing = false;
2170 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2171 lexer.EventParsing = true;
2173 | opt_attributes REMOVE error {
2174 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2177 | opt_attributes modifiers REMOVE {
2178 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
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 has_get = has_set = false;
2200 parsing_indexer = false;
2207 // The signature is computed from the signature of the indexer. Look
2208 // at section 3.6 on the spec
2209 Location loc = (Location) $5;
2211 IndexerDeclaration decl = (IndexerDeclaration) $3;
2212 Pair pair = (Pair) $7;
2213 Accessor get_block = (Accessor) pair.First;
2214 Accessor set_block = (Accessor) pair.Second;
2217 if (decl.interface_type != null)
2218 name = new MemberName (decl.interface_type,
2219 TypeContainer.DefaultIndexerName);
2221 name = new MemberName (TypeContainer.DefaultIndexerName);
2223 indexer = new Indexer (current_class, decl.type, name,
2224 (int) $2, false, decl.param_list, (Attributes) $1,
2225 get_block, set_block, loc);
2226 if (RootContext.Documentation != null)
2227 indexer.DocComment = ConsumeStoredComment ();
2229 current_container.AddIndexer (indexer);
2231 current_local_parameters = null;
2232 implicit_value_parameter_type = null;
2233 indexer_parameters = null;
2238 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2240 Parameters pars = (Parameters) $4;
2241 if (pars.HasArglist) {
2242 // "__arglist is not valid in this context"
2243 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2244 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2245 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2247 if (RootContext.Documentation != null) {
2248 tmpComment = Lexer.consume_doc_comment ();
2249 Lexer.doc_state = XmlCommentState.Allowed;
2252 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2254 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2256 Parameters pars = (Parameters) $6;
2258 if (pars.HasArglist) {
2259 // "__arglist is not valid in this context"
2260 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2261 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2262 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2265 MemberName name = (MemberName) $2;
2266 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2268 if (RootContext.Documentation != null) {
2269 tmpComment = Lexer.consume_doc_comment ();
2270 Lexer.doc_state = XmlCommentState.Allowed;
2281 if (RootContext.Documentation != null)
2282 enumTypeComment = Lexer.consume_doc_comment ();
2287 bool partial = (bool) $3;
2290 Report.Error (267, lexer.Location, "The partial modifier can only appear before a 'class', 'struct', or 'interface'");
2291 break; // assumes that the parser put us in a switch
2294 Location enum_location = lexer.Location;
2296 MemberName name = MakeName (new MemberName ((string) $5));
2297 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2298 name, (Attributes) $1, enum_location);
2300 if (RootContext.Documentation != null)
2301 e.DocComment = enumTypeComment;
2303 foreach (VariableDeclaration ev in (ArrayList) $8) {
2304 e.AddEnumMember (ev.identifier,
2305 (Expression) ev.expression_or_array_initializer,
2306 ev.Location, ev.OptAttributes,
2310 current_container.AddEnum (e);
2311 RootContext.Tree.RecordDecl (name, e);
2318 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2319 | COLON type { $$ = $2; }
2325 if (RootContext.Documentation != null)
2326 Lexer.doc_state = XmlCommentState.Allowed;
2328 opt_enum_member_declarations
2330 // here will be evaluated after CLOSE_BLACE is consumed.
2331 if (RootContext.Documentation != null)
2332 Lexer.doc_state = XmlCommentState.Allowed;
2340 opt_enum_member_declarations
2341 : /* empty */ { $$ = new ArrayList (4); }
2342 | enum_member_declarations opt_comma { $$ = $1; }
2345 enum_member_declarations
2346 : enum_member_declaration
2348 ArrayList l = new ArrayList (4);
2353 | enum_member_declarations COMMA enum_member_declaration
2355 ArrayList l = (ArrayList) $1;
2363 enum_member_declaration
2364 : opt_attributes IDENTIFIER
2366 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2368 if (RootContext.Documentation != null) {
2369 vd.DocComment = Lexer.consume_doc_comment ();
2370 Lexer.doc_state = XmlCommentState.Allowed;
2375 | opt_attributes IDENTIFIER
2377 $$ = lexer.Location;
2378 if (RootContext.Documentation != null) {
2379 tmpComment = Lexer.consume_doc_comment ();
2380 Lexer.doc_state = XmlCommentState.NotAllowed;
2385 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2387 if (RootContext.Documentation != null)
2388 vd.DocComment = ConsumeStoredComment ();
2394 delegate_declaration
2397 DELEGATE type member_name
2398 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2401 Location l = lexer.Location;
2402 MemberName name = MakeName ((MemberName) $5);
2403 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2404 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2406 if (RootContext.Documentation != null) {
2407 del.DocComment = Lexer.consume_doc_comment ();
2408 Lexer.doc_state = XmlCommentState.Allowed;
2411 current_container.AddDelegate (del);
2412 RootContext.Tree.RecordDecl (name, del);
2417 namespace_or_type_name
2419 | namespace_or_type_name DOT IDENTIFIER {
2420 $$ = new MemberName ((MemberName) $1, (string) $3);
2426 $$ = new MemberName ((string) $1);
2431 * Before you think of adding a return_type, notice that we have been
2432 * using two rules in the places where it matters (one rule using type
2433 * and another identical one that uses VOID as the return type). This
2434 * gets rid of a shift/reduce couple
2437 : namespace_or_type_name
2439 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2451 // Note that here only unmanaged types are allowed but we
2452 // can't perform checks during this phase - we do it during
2453 // semantic analysis.
2455 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2459 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2465 | non_expression_type rank_specifier
2467 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2469 | non_expression_type STAR
2471 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2473 | expression rank_specifiers
2475 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2479 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2483 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2484 // through this different path
2486 | multiplicative_expression STAR
2488 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2495 ArrayList types = new ArrayList (4);
2500 | type_list COMMA type
2502 ArrayList types = (ArrayList) $1;
2510 * replaces all the productions for isolating the various
2511 * simple types, but we need this to reuse it easily in local_variable_type
2514 : OBJECT { $$ = TypeManager.system_object_expr; }
2515 | STRING { $$ = TypeManager.system_string_expr; }
2516 | BOOL { $$ = TypeManager.system_boolean_expr; }
2517 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2518 | FLOAT { $$ = TypeManager.system_single_expr; }
2519 | DOUBLE { $$ = TypeManager.system_double_expr; }
2524 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2525 | BYTE { $$ = TypeManager.system_byte_expr; }
2526 | SHORT { $$ = TypeManager.system_int16_expr; }
2527 | USHORT { $$ = TypeManager.system_uint16_expr; }
2528 | INT { $$ = TypeManager.system_int32_expr; }
2529 | UINT { $$ = TypeManager.system_uint32_expr; }
2530 | LONG { $$ = TypeManager.system_int64_expr; }
2531 | ULONG { $$ = TypeManager.system_uint64_expr; }
2532 | CHAR { $$ = TypeManager.system_char_expr; }
2533 | VOID { $$ = TypeManager.system_void_expr; }
2537 : type rank_specifiers
2539 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2544 // Expressions, section 7.5
2554 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2556 | parenthesized_expression
2558 | invocation_expression
2562 | post_increment_expression
2563 | post_decrement_expression
2567 | checked_expression
2568 | unchecked_expression
2569 | pointer_member_access
2570 | anonymous_method_expression
2577 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2578 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2579 | NULL { $$ = NullLiteral.Null; }
2583 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2584 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2585 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2590 object v = lexer.Value;
2596 $$ = IntLiteral.Zero;
2598 $$ = IntLiteral.One;
2600 $$ = new IntLiteral (i);
2601 } else if (v is uint)
2602 $$ = new UIntLiteral ((UInt32) v);
2604 $$ = new LongLiteral ((Int64) v);
2605 else if (v is ulong)
2606 $$ = new ULongLiteral ((UInt64) v);
2608 Console.WriteLine ("OOPS. Unexpected result from scanner");
2613 : TRUE { $$ = new BoolLiteral (true); }
2614 | FALSE { $$ = new BoolLiteral (false); }
2617 parenthesized_expression_0
2618 : OPEN_PARENS expression CLOSE_PARENS
2621 lexer.Deambiguate_CloseParens ();
2622 // After this, the next token returned is one of
2623 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2624 // or CLOSE_PARENS_MINUS.
2628 parenthesized_expression
2629 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2633 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2635 // If a parenthesized expression is followed by a minus, we need to wrap
2636 // the expression inside a ParenthesizedExpression for the CS0075 check
2637 // in Binary.DoResolve().
2638 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2643 : primary_expression DOT IDENTIFIER
2645 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2647 | predefined_type DOT IDENTIFIER
2649 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2657 invocation_expression
2658 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2661 Location l = lexer.Location;
2662 Report.Error (1, l, "Parse error");
2664 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2666 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2668 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2670 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2672 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2674 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2676 ArrayList args = new ArrayList (1);
2678 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2680 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2682 ArrayList args = ((ArrayList) $4);
2684 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2689 : /* empty */ { $$ = null; }
2696 ArrayList list = new ArrayList (4);
2700 | argument_list COMMA argument
2702 ArrayList list = (ArrayList) $1;
2706 | argument_list error {
2707 CheckToken (1026, yyToken, ", or ) expected");
2714 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2716 | non_simple_argument
2723 : REF variable_reference
2725 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2727 | OUT variable_reference
2729 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2731 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2733 ArrayList list = (ArrayList) $3;
2734 Argument[] args = new Argument [list.Count];
2735 list.CopyTo (args, 0);
2737 Expression expr = new Arglist (args, lexer.Location);
2738 $$ = new Argument (expr, Argument.AType.Expression);
2742 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2747 : expression { note ("section 5.4"); $$ = $1; }
2751 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2753 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2755 | primary_expression rank_specifiers
2757 // So the super-trick is that primary_expression
2758 // can only be either a SimpleName or a MemberAccess.
2759 // The MemberAccess case arises when you have a fully qualified type-name like :
2761 // SimpleName is when you have
2764 Expression expr = (Expression) $1;
2765 if (expr is ComposedCast){
2766 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2767 } else if (!(expr is SimpleName || expr is MemberAccess)){
2768 Error_ExpectingTypeName (lexer.Location, expr);
2769 $$ = TypeManager.system_object_expr;
2772 // So we extract the string corresponding to the SimpleName
2775 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2783 ArrayList list = new ArrayList (4);
2787 | expression_list COMMA expression
2789 ArrayList list = (ArrayList) $1;
2798 $$ = new This (current_block, lexer.Location);
2803 : BASE DOT IDENTIFIER
2805 $$ = new BaseAccess ((string) $3, lexer.Location);
2807 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2809 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2812 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2817 post_increment_expression
2818 : primary_expression OP_INC
2820 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2821 (Expression) $1, lexer.Location);
2825 post_decrement_expression
2826 : primary_expression OP_DEC
2828 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2829 (Expression) $1, lexer.Location);
2834 : object_or_delegate_creation_expression
2835 | array_creation_expression
2838 object_or_delegate_creation_expression
2839 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2841 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2845 array_creation_expression
2846 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2848 opt_array_initializer
2850 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2852 | NEW type rank_specifiers array_initializer
2854 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2858 Report.Error (1031, lexer.Location, "Type expected");
2863 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2879 : rank_specifier opt_rank_specifier
2881 $$ = (string) $2 + (string) $1;
2886 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2888 $$ = "[" + (string) $2 + "]";
2908 | dim_separators COMMA
2910 $$ = (string) $1 + ",";
2914 opt_array_initializer
2926 : OPEN_BRACE CLOSE_BRACE
2928 ArrayList list = new ArrayList (4);
2931 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2933 $$ = (ArrayList) $2;
2937 variable_initializer_list
2938 : variable_initializer
2940 ArrayList list = new ArrayList (4);
2944 | variable_initializer_list COMMA variable_initializer
2946 ArrayList list = (ArrayList) $1;
2953 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2955 $$ = new TypeOfVoid (lexer.Location);
2957 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2959 $$ = new TypeOf ((Expression) $3, lexer.Location);
2964 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2965 $$ = new SizeOf ((Expression) $3, lexer.Location);
2970 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2972 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2976 unchecked_expression
2977 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2979 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2983 pointer_member_access
2984 : primary_expression OP_PTR IDENTIFIER
2988 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2989 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2993 anonymous_method_expression
2994 : DELEGATE opt_anonymous_method_signature _mark_
2996 oob_stack.Push (current_local_parameters);
2997 current_local_parameters = (Parameters)$2;
2999 // Force the next block to be created as a ToplevelBlock
3000 oob_stack.Push (current_block);
3001 oob_stack.Push (top_current_block);
3002 current_block = null;
3006 Location loc = (Location) $3;
3007 top_current_block = (Block) oob_stack.Pop ();
3008 current_block = (Block) oob_stack.Pop ();
3009 if (RootContext.Version == LanguageVersion.ISO_1){
3010 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3013 ToplevelBlock anon_block = (ToplevelBlock) $5;
3015 anon_block.Parent = current_block;
3016 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3019 current_local_parameters = (Parameters) oob_stack.Pop ();
3023 opt_anonymous_method_signature
3024 : /* empty */ { $$ = null; }
3025 | anonymous_method_signature
3028 anonymous_method_signature
3029 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3032 $$ = Parameters.EmptyReadOnlyParameters;
3034 ArrayList par_list = (ArrayList) $2;
3035 Parameter [] pars = new Parameter [par_list.Count];
3036 par_list.CopyTo (pars);
3037 $$ = new Parameters (pars, null, lexer.Location);
3042 opt_anonymous_method_parameter_list
3043 : /* empty */ { $$ = null; }
3044 | anonymous_method_parameter_list { $$ = $1; }
3047 anonymous_method_parameter_list
3048 : anonymous_method_parameter
3050 ArrayList a = new ArrayList (4);
3054 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3056 ArrayList a = (ArrayList) $1;
3062 anonymous_method_parameter
3063 : opt_parameter_modifier type IDENTIFIER {
3064 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3066 | PARAMS type IDENTIFIER {
3067 Report.Error (1670, lexer.Location, "params modifier not allowed in anonymous method declaration");
3073 : primary_expression
3074 | BANG prefixed_unary_expression
3076 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3078 | TILDE prefixed_unary_expression
3080 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3086 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3088 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3090 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3092 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3098 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3100 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3105 // The idea to split this out is from Rhys' grammar
3106 // to solve the problem with casts.
3108 prefixed_unary_expression
3110 | PLUS prefixed_unary_expression
3112 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3114 | MINUS prefixed_unary_expression
3116 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3118 | OP_INC prefixed_unary_expression
3120 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3121 (Expression) $2, lexer.Location);
3123 | OP_DEC prefixed_unary_expression
3125 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3126 (Expression) $2, lexer.Location);
3128 | STAR prefixed_unary_expression
3130 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3132 | BITWISE_AND prefixed_unary_expression
3134 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3138 pre_increment_expression
3139 : OP_INC prefixed_unary_expression
3141 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3142 (Expression) $2, lexer.Location);
3146 pre_decrement_expression
3147 : OP_DEC prefixed_unary_expression
3149 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3150 (Expression) $2, lexer.Location);
3154 multiplicative_expression
3155 : prefixed_unary_expression
3156 | multiplicative_expression STAR prefixed_unary_expression
3158 $$ = new Binary (Binary.Operator.Multiply,
3159 (Expression) $1, (Expression) $3, lexer.Location);
3161 | multiplicative_expression DIV prefixed_unary_expression
3163 $$ = new Binary (Binary.Operator.Division,
3164 (Expression) $1, (Expression) $3, lexer.Location);
3166 | multiplicative_expression PERCENT prefixed_unary_expression
3168 $$ = new Binary (Binary.Operator.Modulus,
3169 (Expression) $1, (Expression) $3, lexer.Location);
3174 : multiplicative_expression
3175 | additive_expression PLUS multiplicative_expression
3177 $$ = new Binary (Binary.Operator.Addition,
3178 (Expression) $1, (Expression) $3, lexer.Location);
3180 | additive_expression MINUS multiplicative_expression
3182 $$ = new Binary (Binary.Operator.Subtraction,
3183 (Expression) $1, (Expression) $3, lexer.Location);
3188 : additive_expression
3189 | shift_expression OP_SHIFT_LEFT additive_expression
3191 $$ = new Binary (Binary.Operator.LeftShift,
3192 (Expression) $1, (Expression) $3, lexer.Location);
3194 | shift_expression OP_SHIFT_RIGHT additive_expression
3196 $$ = new Binary (Binary.Operator.RightShift,
3197 (Expression) $1, (Expression) $3, lexer.Location);
3201 relational_expression
3203 | relational_expression OP_LT shift_expression
3205 $$ = new Binary (Binary.Operator.LessThan,
3206 (Expression) $1, (Expression) $3, lexer.Location);
3208 | relational_expression OP_GT shift_expression
3210 $$ = new Binary (Binary.Operator.GreaterThan,
3211 (Expression) $1, (Expression) $3, lexer.Location);
3213 | relational_expression OP_LE shift_expression
3215 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3216 (Expression) $1, (Expression) $3, lexer.Location);
3218 | relational_expression OP_GE shift_expression
3220 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3221 (Expression) $1, (Expression) $3, lexer.Location);
3223 | relational_expression IS type
3225 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3227 | relational_expression AS type
3229 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3234 : relational_expression
3235 | equality_expression OP_EQ relational_expression
3237 $$ = new Binary (Binary.Operator.Equality,
3238 (Expression) $1, (Expression) $3, lexer.Location);
3240 | equality_expression OP_NE relational_expression
3242 $$ = new Binary (Binary.Operator.Inequality,
3243 (Expression) $1, (Expression) $3, lexer.Location);
3248 : equality_expression
3249 | and_expression BITWISE_AND equality_expression
3251 $$ = new Binary (Binary.Operator.BitwiseAnd,
3252 (Expression) $1, (Expression) $3, lexer.Location);
3256 exclusive_or_expression
3258 | exclusive_or_expression CARRET and_expression
3260 $$ = new Binary (Binary.Operator.ExclusiveOr,
3261 (Expression) $1, (Expression) $3, lexer.Location);
3265 inclusive_or_expression
3266 : exclusive_or_expression
3267 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3269 $$ = new Binary (Binary.Operator.BitwiseOr,
3270 (Expression) $1, (Expression) $3, lexer.Location);
3274 conditional_and_expression
3275 : inclusive_or_expression
3276 | conditional_and_expression OP_AND inclusive_or_expression
3278 $$ = new Binary (Binary.Operator.LogicalAnd,
3279 (Expression) $1, (Expression) $3, lexer.Location);
3283 conditional_or_expression
3284 : conditional_and_expression
3285 | conditional_or_expression OP_OR conditional_and_expression
3287 $$ = new Binary (Binary.Operator.LogicalOr,
3288 (Expression) $1, (Expression) $3, lexer.Location);
3292 conditional_expression
3293 : conditional_or_expression
3294 | conditional_or_expression INTERR expression COLON expression
3296 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3300 assignment_expression
3301 : prefixed_unary_expression ASSIGN expression
3303 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3305 | prefixed_unary_expression OP_MULT_ASSIGN expression
3307 Location l = lexer.Location;
3309 $$ = new CompoundAssign (
3310 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3312 | prefixed_unary_expression OP_DIV_ASSIGN expression
3314 Location l = lexer.Location;
3316 $$ = new CompoundAssign (
3317 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3319 | prefixed_unary_expression OP_MOD_ASSIGN expression
3321 Location l = lexer.Location;
3323 $$ = new CompoundAssign (
3324 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3326 | prefixed_unary_expression OP_ADD_ASSIGN expression
3328 Location l = lexer.Location;
3330 $$ = new CompoundAssign (
3331 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3333 | prefixed_unary_expression OP_SUB_ASSIGN expression
3335 Location l = lexer.Location;
3337 $$ = new CompoundAssign (
3338 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3340 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3342 Location l = lexer.Location;
3344 $$ = new CompoundAssign (
3345 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3347 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3349 Location l = lexer.Location;
3351 $$ = new CompoundAssign (
3352 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3354 | prefixed_unary_expression OP_AND_ASSIGN expression
3356 Location l = lexer.Location;
3358 $$ = new CompoundAssign (
3359 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3361 | prefixed_unary_expression OP_OR_ASSIGN expression
3363 Location l = lexer.Location;
3365 $$ = new CompoundAssign (
3366 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3368 | prefixed_unary_expression OP_XOR_ASSIGN expression
3370 Location l = lexer.Location;
3372 $$ = new CompoundAssign (
3373 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3378 : conditional_expression
3379 | assignment_expression
3399 MemberName name = MakeName ((MemberName) $5);
3400 bool partial = (bool) $3;
3401 int mod_flags = (int) $2;
3404 ClassPart part = PartialContainer.CreatePart (
3405 current_namespace, current_class, name, mod_flags,
3406 (Attributes) $1, Kind.Class, lexer.Location);
3408 current_container = part.PartialContainer;
3409 current_class = part;
3411 if ((mod_flags & Modifiers.STATIC) != 0) {
3412 current_class = new StaticClass (
3413 current_namespace, current_class, name,
3414 mod_flags, (Attributes) $1, lexer.Location);
3416 current_class = new Class (
3417 current_namespace, current_class, name,
3418 mod_flags, (Attributes) $1, lexer.Location);
3421 current_container.AddClassOrStruct (current_class);
3422 current_container = current_class;
3423 RootContext.Tree.RecordDecl (name, current_class);
3429 if (current_class.Name == "System.Object") {
3430 Report.Error (537, current_class.Location,
3431 "The class System.Object cannot have a base " +
3432 "class or implement an interface.");
3434 current_class.Bases = (ArrayList) $7;
3437 if (RootContext.Documentation != null) {
3438 current_class.DocComment = Lexer.consume_doc_comment ();
3439 Lexer.doc_state = XmlCommentState.Allowed;
3444 if (RootContext.Documentation != null)
3445 Lexer.doc_state = XmlCommentState.Allowed;
3449 $$ = pop_current_class ();
3455 { $$ = (bool) false; }
3457 { $$ = (bool) true; }
3461 : /* empty */ { $$ = (int) 0; }
3467 | modifiers modifier
3472 if ((m1 & m2) != 0) {
3473 Location l = lexer.Location;
3474 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3476 $$ = (int) (m1 | m2);
3481 : NEW { $$ = Modifiers.NEW; }
3482 | PUBLIC { $$ = Modifiers.PUBLIC; }
3483 | PROTECTED { $$ = Modifiers.PROTECTED; }
3484 | INTERNAL { $$ = Modifiers.INTERNAL; }
3485 | PRIVATE { $$ = Modifiers.PRIVATE; }
3486 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3487 | SEALED { $$ = Modifiers.SEALED; }
3488 | STATIC { $$ = Modifiers.STATIC; }
3489 | READONLY { $$ = Modifiers.READONLY; }
3490 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3491 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3492 | EXTERN { $$ = Modifiers.EXTERN; }
3493 | VOLATILE { $$ = Modifiers.VOLATILE; }
3494 | UNSAFE { $$ = Modifiers.UNSAFE; }
3498 : /* empty */ { $$ = null; }
3499 | class_base { $$ = $1; }
3503 : COLON type_list { $$ = $2; }
3511 // A block is "contained" on the following places:
3513 // property_declaration as part of the accessor body (get/set)
3514 // operator_declaration
3515 // constructor_declaration
3516 // destructor_declaration
3517 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3522 if (current_block == null){
3523 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3524 top_current_block = current_block;
3526 current_block = new Block (current_block, lexer.Location, Location.Null);
3529 opt_statement_list CLOSE_BRACE
3531 while (current_block.Implicit)
3532 current_block = current_block.Parent;
3534 current_block.SetEndLocation (lexer.Location);
3535 current_block = current_block.Parent;
3536 if (current_block == null)
3537 top_current_block = null;
3548 | statement_list statement
3552 : declaration_statement
3554 if ($1 != null && (Block) $1 != current_block){
3555 current_block.AddStatement ((Statement) $1);
3556 current_block = (Block) $1;
3559 | valid_declaration_statement
3561 current_block.AddStatement ((Statement) $1);
3566 valid_declaration_statement
3569 | expression_statement
3570 | selection_statement
3571 | iteration_statement
3575 | unchecked_statement
3583 : valid_declaration_statement
3584 | declaration_statement
3586 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3591 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3599 $$ = EmptyStatement.Value;
3606 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3608 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3609 current_block.AddStatement (labeled);
3614 declaration_statement
3615 : local_variable_declaration SEMICOLON
3618 DictionaryEntry de = (DictionaryEntry) $1;
3620 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3624 | local_constant_declaration SEMICOLON
3627 DictionaryEntry de = (DictionaryEntry) $1;
3629 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3635 * The following is from Rhys' grammar:
3636 * > Types in local variable declarations must be recognized as
3637 * > expressions to prevent reduce/reduce errors in the grammar.
3638 * > The expressions are converted into types during semantic analysis.
3641 : primary_expression opt_rank_specifier
3643 // FIXME: Do something smart here regarding the composition of the type.
3645 // Ok, the above "primary_expression" is there to get rid of
3646 // both reduce/reduce and shift/reduces in the grammar, it should
3647 // really just be "type_name". If you use type_name, a reduce/reduce
3648 // creeps up. If you use namespace_or_type_name (which is all we need
3649 // really) two shift/reduces appear.
3652 // So the super-trick is that primary_expression
3653 // can only be either a SimpleName or a MemberAccess.
3654 // The MemberAccess case arises when you have a fully qualified type-name like :
3656 // SimpleName is when you have
3659 Expression expr = (Expression) $1;
3660 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3661 Error_ExpectingTypeName (lexer.Location, expr);
3665 // So we extract the string corresponding to the SimpleName
3669 if ((string) $2 == "")
3672 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3675 | builtin_types opt_rank_specifier
3677 if ((string) $2 == "")
3680 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3684 local_variable_pointer_type
3685 : primary_expression STAR
3687 Expression expr = (Expression) $1;
3688 Location l = lexer.Location;
3690 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3691 Error_ExpectingTypeName (l, expr);
3695 $$ = new ComposedCast ((Expression) $1, "*", l);
3697 | builtin_types STAR
3699 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3703 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3705 | local_variable_pointer_type STAR
3707 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3711 local_variable_declaration
3712 : local_variable_type variable_declarators
3715 $$ = new DictionaryEntry ($1, $2);
3719 | local_variable_pointer_type opt_rank_specifier variable_declarators
3724 if ((string) $2 == "")
3725 t = (Expression) $1;
3727 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3728 $$ = new DictionaryEntry (t, $3);
3734 local_constant_declaration
3735 : CONST local_variable_type constant_declarators
3738 $$ = new DictionaryEntry ($2, $3);
3744 expression_statement
3745 : statement_expression SEMICOLON
3752 // We have to do the wrapping here and not in the case above,
3753 // because statement_expression is used for example in for_statement
3755 statement_expression
3756 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3757 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3758 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3759 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3760 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3761 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3762 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3764 Report.Error (1002, lexer.Location, "Expecting `;'");
3769 object_creation_expression
3770 : object_or_delegate_creation_expression
3771 { note ("complain if this is a delegate maybe?"); }
3780 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3783 Location l = (Location) $3;
3785 $$ = new If ((Expression) $4, (Statement) $6, l);
3787 if (RootContext.WarningLevel >= 4){
3788 if ($6 == EmptyStatement.Value)
3789 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3793 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3794 embedded_statement ELSE embedded_statement
3796 Location l = (Location) $3;
3798 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
3803 : SWITCH OPEN_PARENS _mark_
3805 switch_stack.Push (current_block);
3807 expression CLOSE_PARENS
3810 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
3811 current_block = (Block) switch_stack.Pop ();
3827 Report.Error (1522, lexer.Location, "Empty switch block");
3835 ArrayList sections = new ArrayList (4);
3840 | switch_sections switch_section
3842 ArrayList sections = (ArrayList) $1;
3852 current_block = current_block.CreateSwitchBlock (lexer.Location);
3856 Block topmost = current_block;
3858 while (topmost.Implicit)
3859 topmost = topmost.Parent;
3860 $$ = new SwitchSection ((ArrayList) $1, topmost);
3867 ArrayList labels = new ArrayList (4);
3872 | switch_labels switch_label
3874 ArrayList labels = (ArrayList) ($1);
3882 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3883 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3886 1523, lexer.Location,
3887 "The keyword case or default must precede code in switch block");
3899 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
3901 Location l = (Location) $3;
3902 $$ = new While ((Expression) $4, (Statement) $6, l);
3904 if (RootContext.WarningLevel >= 4){
3905 if ($6 == EmptyStatement.Value)
3906 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3912 : DO embedded_statement
3913 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
3915 Location l = (Location) $5;
3917 $$ = new Do ((Statement) $2, (Expression) $6, l);
3923 opt_for_initializer SEMICOLON _mark_
3925 Block assign_block = new Block (current_block);
3926 current_block = assign_block;
3928 if ($3 is DictionaryEntry){
3929 DictionaryEntry de = (DictionaryEntry) $3;
3931 Expression type = (Expression) de.Key;
3932 ArrayList var_declarators = (ArrayList) de.Value;
3934 foreach (VariableDeclaration decl in var_declarators){
3938 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
3942 Location l = lexer.Location;
3944 if (decl.expression_or_array_initializer is Expression){
3945 expr = (Expression) decl.expression_or_array_initializer;
3946 } else if (decl.expression_or_array_initializer == null) {
3949 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3950 expr = new ArrayCreation (type, "", init, decl.Location);
3953 LocalVariableReference var;
3954 var = new LocalVariableReference (assign_block, decl.identifier, l);
3957 Assign a = new Assign (var, expr, decl.Location);
3959 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3963 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
3964 // This can be referred to as $6 below.
3970 opt_for_condition SEMICOLON
3971 opt_for_iterator CLOSE_PARENS
3974 Location l = (Location) $5;
3976 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
3978 if (RootContext.WarningLevel >= 4){
3979 if ($11 == EmptyStatement.Value)
3980 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3983 current_block.AddStatement (f);
3984 while (current_block.Implicit)
3985 current_block = current_block.Parent;
3987 current_block = current_block.Parent;
3992 : /* empty */ { $$ = EmptyStatement.Value; }
3997 : local_variable_declaration
3998 | statement_expression_list
4002 : /* empty */ { $$ = null; }
4003 | boolean_expression
4007 : /* empty */ { $$ = EmptyStatement.Value; }
4012 : statement_expression_list
4015 statement_expression_list
4016 : statement_expression
4018 // CHANGE: was `null'
4019 Block b = new Block (current_block, Block.Flags.Implicit);
4021 b.AddStatement ((Statement) $1);
4024 | statement_expression_list COMMA statement_expression
4026 Block b = (Block) $1;
4028 b.AddStatement ((Statement) $3);
4034 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4036 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4039 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4040 expression CLOSE_PARENS
4042 Block foreach_block = new Block (current_block);
4043 current_block = foreach_block;
4045 Location l = lexer.Location;
4048 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, l);
4050 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4052 // Get a writable reference to this read-only variable.
4054 // Note that the $$ here refers to the value of _this_ code block,
4055 // not the value of the LHS non-terminal. This can be referred to as $9 below.
4056 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4063 LocalVariableReference v = (LocalVariableReference) $9;
4064 Location l = (Location) $6;
4067 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4068 current_block.AddStatement (f);
4071 while (current_block.Implicit)
4072 current_block = current_block.Parent;
4074 current_block = current_block.Parent;
4080 | continue_statement
4090 $$ = new Break (lexer.Location);
4095 : CONTINUE SEMICOLON
4097 $$ = new Continue (lexer.Location);
4102 : GOTO IDENTIFIER SEMICOLON
4104 $$ = new Goto ((string) $2, lexer.Location);
4106 | GOTO CASE constant_expression SEMICOLON
4108 $$ = new GotoCase ((Expression) $3, lexer.Location);
4110 | GOTO DEFAULT SEMICOLON
4112 $$ = new GotoDefault (lexer.Location);
4117 : RETURN opt_expression SEMICOLON
4119 $$ = new Return ((Expression) $2, lexer.Location);
4124 : THROW opt_expression SEMICOLON
4126 $$ = new Throw ((Expression) $2, lexer.Location);
4131 : IDENTIFIER RETURN expression SEMICOLON
4133 string s = (string) $1;
4135 Report.Error (1003, lexer.Location, "; expected");
4138 if (RootContext.Version == LanguageVersion.ISO_1){
4139 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4142 if (iterator_container == null){
4143 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4146 iterator_container.SetYields ();
4147 $$ = new Yield ((Expression) $3, lexer.Location);
4150 | IDENTIFIER RETURN SEMICOLON
4152 Report.Error (1627, lexer.Location, "Expression expected after yield return");
4155 | IDENTIFIER BREAK SEMICOLON
4157 string s = (string) $1;
4159 Report.Error (1003, lexer.Location, "; expected");
4162 if (RootContext.Version == LanguageVersion.ISO_1){
4163 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4166 if (iterator_container == null){
4167 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4170 iterator_container.SetYields ();
4171 $$ = new YieldBreak (lexer.Location);
4182 : TRY block catch_clauses
4186 ArrayList c = (ArrayList)$3;
4187 for (int i = 0; i < c.Count; ++i) {
4188 Catch cc = (Catch) c [i];
4190 if (i != c.Count - 1)
4191 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4198 // Now s contains the list of specific catch clauses
4199 // and g contains the general one.
4201 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4203 | TRY block opt_catch_clauses FINALLY block
4206 ArrayList s = new ArrayList (4);
4207 ArrayList catch_list = (ArrayList) $3;
4209 if (catch_list != null){
4210 foreach (Catch cc in catch_list) {
4218 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4222 Report.Error (1524, lexer.Location, "Expected catch or finally");
4227 : /* empty */ { $$ = null; }
4234 ArrayList l = new ArrayList (4);
4239 | catch_clauses catch_clause
4241 ArrayList l = (ArrayList) $1;
4249 : /* empty */ { $$ = null; }
4254 : CATCH opt_catch_args
4256 Expression type = null;
4260 DictionaryEntry cc = (DictionaryEntry) $2;
4261 type = (Expression) cc.Key;
4262 id = (string) cc.Value;
4265 ArrayList one = new ArrayList (4);
4266 Location loc = lexer.Location;
4268 one.Add (new VariableDeclaration (id, null, loc));
4270 current_block = new Block (current_block);
4271 Block b = declare_local_variables (type, one, loc);
4276 Expression type = null;
4280 DictionaryEntry cc = (DictionaryEntry) $2;
4281 type = (Expression) cc.Key;
4282 id = (string) cc.Value;
4285 while (current_block.Implicit)
4286 current_block = current_block.Parent;
4287 current_block = current_block.Parent;
4291 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4296 : /* empty */ { $$ = null; }
4301 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4303 $$ = new DictionaryEntry ($2, $3);
4310 $$ = new Checked ((Block) $2);
4317 $$ = new Unchecked ((Block) $2);
4324 if (!RootContext.Unsafe){
4325 Report.Error (227, lexer.Location,
4326 "Unsafe code can only be used if -unsafe is used");
4329 $$ = new Unsafe ((Block) $3);
4335 type fixed_pointer_declarators
4338 ArrayList list = (ArrayList) $4;
4339 Expression type = (Expression) $3;
4340 Location l = lexer.Location;
4341 int top = list.Count;
4343 Block assign_block = new Block (current_block);
4344 current_block = assign_block;
4346 for (int i = 0; i < top; i++){
4347 Pair p = (Pair) list [i];
4350 v = current_block.AddVariable (type, (string) p.First, l);
4354 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4362 Location l = (Location) $6;
4364 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4366 if (RootContext.WarningLevel >= 4){
4367 if ($8 == EmptyStatement.Value)
4368 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4371 current_block.AddStatement (f);
4372 while (current_block.Implicit)
4373 current_block = current_block.Parent;
4375 current_block = current_block.Parent;
4379 fixed_pointer_declarators
4380 : fixed_pointer_declarator {
4381 ArrayList declarators = new ArrayList (4);
4383 declarators.Add ($1);
4386 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4388 ArrayList declarators = (ArrayList) $1;
4390 declarators.Add ($3);
4395 fixed_pointer_declarator
4396 : IDENTIFIER ASSIGN expression
4398 $$ = new Pair ($1, $3);
4402 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4408 : LOCK OPEN_PARENS expression CLOSE_PARENS
4414 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4419 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4421 Block assign_block = new Block (current_block);
4422 current_block = assign_block;
4424 if ($3 is DictionaryEntry){
4425 DictionaryEntry de = (DictionaryEntry) $3;
4426 Location l = lexer.Location;
4428 Expression type = (Expression) de.Key;
4429 ArrayList var_declarators = (ArrayList) de.Value;
4431 ArrayList vars = new ArrayList (4);
4433 foreach (VariableDeclaration decl in var_declarators){
4435 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4438 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4441 if (decl.expression_or_array_initializer is Expression){
4442 expr = (Expression) decl.expression_or_array_initializer;
4444 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4446 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4449 expr = new ArrayCreation (type, "", init, decl.Location);
4452 LocalVariableReference var;
4454 // Get a writable reference to this read-only variable.
4455 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4457 // This is so that it is not a warning on using variables
4460 vars.Add (new DictionaryEntry (var, expr));
4462 // Assign a = new Assign (var, expr, decl.Location);
4463 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4466 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4467 // It can be referred to as $6 below.
4468 $$ = new DictionaryEntry (type, vars);
4475 Using u = new Using ($6, (Statement) $7, (Location) $5);
4476 current_block.AddStatement (u);
4477 while (current_block.Implicit)
4478 current_block = current_block.Parent;
4480 current_block = current_block.Parent;
4484 resource_acquisition
4485 : local_variable_declaration
4489 // Utility rule to save location information
4492 { $$ = lexer.Location; }
4497 // A class used to pass around variable declarations and constants
4499 public class VariableDeclaration {
4500 public string identifier;
4501 public object expression_or_array_initializer;
4502 public Location Location;
4503 public Attributes OptAttributes;
4504 public string DocComment;
4506 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4508 this.identifier = id;
4509 this.expression_or_array_initializer = eoai;
4511 this.OptAttributes = opt_attrs;
4514 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4520 /// Used to pass around interface property information
4522 public class InterfaceAccessorInfo {
4524 public readonly Accessor Get, Set;
4526 public InterfaceAccessorInfo (bool has_get, bool has_set,
4527 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4530 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4532 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4535 Get = new Accessor (null, 0, get_attrs, get_loc);
4537 Set = new Accessor (null, 0, set_attrs, set_loc);
4544 // A class used to hold info about an indexer declarator
4546 public class IndexerDeclaration {
4547 public Expression type;
4548 public MemberName interface_type;
4549 public Parameters param_list;
4551 public IndexerDeclaration (Expression type, MemberName interface_type,
4552 Parameters param_list)
4555 this.interface_type = interface_type;
4556 this.param_list = param_list;
4561 // We use this when we do not have an object in advance that is an IIteratorContainer
4563 public class SimpleIteratorContainer : IIteratorContainer {
4566 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4571 public static SimpleIteratorContainer GetSimple () {
4572 Simple.Yields = false;
4576 public void SetYields () { Yields = true; }
4580 // A class used to hold info about an operator declarator
4582 public class OperatorDeclaration {
4583 public Operator.OpType optype;
4584 public Expression ret_type, arg1type, arg2type;
4585 public string arg1name, arg2name;
4586 public Location location;
4588 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4589 Expression arg1type, string arg1name,
4590 Expression arg2type, string arg2name, Location location)
4593 this.ret_type = ret_type;
4594 this.arg1type = arg1type;
4595 this.arg1name = arg1name;
4596 this.arg2type = arg2type;
4597 this.arg2name = arg2name;
4598 this.location = location;
4603 void Error_ExpectingTypeName (Location l, Expression expr)
4605 if (expr is Invocation){
4606 Report.Error (1002, l, "; expected");
4608 Report.Error (-1, l, "Invalid Type definition");
4612 TypeContainer pop_current_class ()
4614 TypeContainer retval = current_class;
4616 current_class = current_class.Parent;
4617 current_container = current_container.Parent;
4619 if (current_class != current_container) {
4620 if (!(current_class is ClassPart) ||
4621 ((ClassPart) current_class).PartialContainer != current_container)
4622 throw new InternalErrorException ();
4623 } else if (current_container is ClassPart)
4624 current_container = ((ClassPart) current_class).PartialContainer;
4630 // Given the @class_name name, it creates a fully qualified name
4631 // based on the containing declaration space
4634 MakeName (MemberName class_name)
4636 Namespace ns = current_namespace.NS;
4638 if (current_container.Name == ""){
4640 return new MemberName (ns.MemberName, class_name);
4644 return new MemberName (current_container.MemberName, class_name);
4648 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4650 Block implicit_block;
4651 ArrayList inits = null;
4654 // We use the `Used' property to check whether statements
4655 // have been added to the current block. If so, we need
4656 // to create another block to contain the new declaration
4657 // otherwise, as an optimization, we use the same block to
4658 // add the declaration.
4660 // FIXME: A further optimization is to check if the statements
4661 // that were added were added as part of the initialization
4662 // below. In which case, no other statements have been executed
4663 // and we might be able to reduce the number of blocks for
4664 // situations like this:
4666 // int j = 1; int k = j + 1;
4668 if (current_block.Used)
4669 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4671 implicit_block = current_block;
4673 foreach (VariableDeclaration decl in variable_declarators){
4675 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4676 if (decl.expression_or_array_initializer != null){
4678 inits = new ArrayList (4);
4685 return implicit_block;
4687 foreach (VariableDeclaration decl in inits){
4691 if (decl.expression_or_array_initializer is Expression){
4692 expr = (Expression) decl.expression_or_array_initializer;
4695 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4697 expr = new ArrayCreation (type, "", init, decl.Location);
4700 LocalVariableReference var;
4701 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4703 assign = new Assign (var, expr, decl.Location);
4705 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4708 return implicit_block;
4711 Block declare_local_constants (Expression type, ArrayList declarators)
4713 Block implicit_block;
4715 if (current_block.Used)
4716 implicit_block = new Block (current_block, Block.Flags.Implicit);
4718 implicit_block = current_block;
4720 foreach (VariableDeclaration decl in declarators){
4721 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4724 return implicit_block;
4727 void CheckAttributeTarget (string a)
4731 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4735 Location l = lexer.Location;
4736 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4742 void CheckUnaryOperator (Operator.OpType op)
4746 case Operator.OpType.LogicalNot:
4747 case Operator.OpType.OnesComplement:
4748 case Operator.OpType.Increment:
4749 case Operator.OpType.Decrement:
4750 case Operator.OpType.True:
4751 case Operator.OpType.False:
4752 case Operator.OpType.Addition:
4753 case Operator.OpType.Subtraction:
4758 Location l = lexer.Location;
4759 Report.Error (1019, l, "Overloadable unary operator expected");
4765 void CheckBinaryOperator (Operator.OpType op)
4769 case Operator.OpType.Addition:
4770 case Operator.OpType.Subtraction:
4771 case Operator.OpType.Multiply:
4772 case Operator.OpType.Division:
4773 case Operator.OpType.Modulus:
4774 case Operator.OpType.BitwiseAnd:
4775 case Operator.OpType.BitwiseOr:
4776 case Operator.OpType.ExclusiveOr:
4777 case Operator.OpType.LeftShift:
4778 case Operator.OpType.RightShift:
4779 case Operator.OpType.Equality:
4780 case Operator.OpType.Inequality:
4781 case Operator.OpType.GreaterThan:
4782 case Operator.OpType.LessThan:
4783 case Operator.OpType.GreaterThanOrEqual:
4784 case Operator.OpType.LessThanOrEqual:
4788 Location l = lexer.Location;
4789 Report.Error (1020, l, "Overloadable binary operator expected");
4795 void syntax_error (Location l, string msg)
4797 Report.Error (1003, l, "Syntax error, " + msg);
4800 void note (string s)
4802 // Used to put annotations
4807 public Tokenizer Lexer {
4813 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4815 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4816 this.name = file.Name;
4818 current_container = RootContext.Tree.Types;
4819 // TODO: Make RootContext.Tree.Types a PartialContainer.
4820 current_class = current_container;
4821 current_container.NamespaceEntry = current_namespace;
4822 oob_stack = new Stack ();
4823 switch_stack = new Stack ();
4825 lexer = new Tokenizer (reader, file, defines);
4828 public void parse ()
4831 if (yacc_verbose_flag > 1)
4832 yyparse (lexer, new yydebug.yyDebugSimple ());
4835 Tokenizer tokenizer = lexer as Tokenizer;
4836 tokenizer.cleanup ();
4837 } catch (Exception e){
4839 // Removed for production use, use parser verbose to get the output.
4841 // Console.WriteLine (e);
4842 Report.Error (-25, lexer.Location, "Parsing error");
4843 if (yacc_verbose_flag > 0)
4844 Console.WriteLine (e);
4847 RootContext.Tree.Types.NamespaceEntry = null;
4850 void CheckToken (int error, int yyToken, string msg)
4852 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4853 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4856 Report.Error (error, lexer.Location, msg);
4859 void CheckIdentifierToken (int yyToken)
4861 CheckToken (1041, yyToken, "Identifier expected");
4864 string ConsumeStoredComment ()
4866 string s = tmpComment;
4868 Lexer.doc_state = XmlCommentState.Allowed;