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_container, 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;
2280 if (RootContext.Documentation != null)
2281 enumTypeComment = Lexer.consume_doc_comment ();
2286 Location enum_location = lexer.Location;
2288 MemberName name = MakeName (new MemberName ((string) $4));
2289 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2290 name, (Attributes) $1, enum_location);
2292 if (RootContext.Documentation != null)
2293 e.DocComment = enumTypeComment;
2295 foreach (VariableDeclaration ev in (ArrayList) $7) {
2296 e.AddEnumMember (ev.identifier,
2297 (Expression) ev.expression_or_array_initializer,
2298 ev.Location, ev.OptAttributes,
2302 current_container.AddEnum (e);
2303 RootContext.Tree.RecordDecl (name, e);
2310 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2311 | COLON type { $$ = $2; }
2317 if (RootContext.Documentation != null)
2318 Lexer.doc_state = XmlCommentState.Allowed;
2320 opt_enum_member_declarations
2322 // here will be evaluated after CLOSE_BLACE is consumed.
2323 if (RootContext.Documentation != null)
2324 Lexer.doc_state = XmlCommentState.Allowed;
2332 opt_enum_member_declarations
2333 : /* empty */ { $$ = new ArrayList (4); }
2334 | enum_member_declarations opt_comma { $$ = $1; }
2337 enum_member_declarations
2338 : enum_member_declaration
2340 ArrayList l = new ArrayList (4);
2345 | enum_member_declarations COMMA enum_member_declaration
2347 ArrayList l = (ArrayList) $1;
2355 enum_member_declaration
2356 : opt_attributes IDENTIFIER
2358 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2360 if (RootContext.Documentation != null) {
2361 vd.DocComment = Lexer.consume_doc_comment ();
2362 Lexer.doc_state = XmlCommentState.Allowed;
2367 | opt_attributes IDENTIFIER
2369 $$ = lexer.Location;
2370 if (RootContext.Documentation != null) {
2371 tmpComment = Lexer.consume_doc_comment ();
2372 Lexer.doc_state = XmlCommentState.NotAllowed;
2377 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2379 if (RootContext.Documentation != null)
2380 vd.DocComment = ConsumeStoredComment ();
2386 delegate_declaration
2389 DELEGATE type member_name
2390 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2393 Location l = lexer.Location;
2394 MemberName name = MakeName ((MemberName) $5);
2395 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2396 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2398 if (RootContext.Documentation != null) {
2399 del.DocComment = Lexer.consume_doc_comment ();
2400 Lexer.doc_state = XmlCommentState.Allowed;
2403 current_container.AddDelegate (del);
2404 RootContext.Tree.RecordDecl (name, del);
2409 namespace_or_type_name
2411 | namespace_or_type_name DOT IDENTIFIER {
2412 $$ = new MemberName ((MemberName) $1, (string) $3);
2418 $$ = new MemberName ((string) $1);
2423 * Before you think of adding a return_type, notice that we have been
2424 * using two rules in the places where it matters (one rule using type
2425 * and another identical one that uses VOID as the return type). This
2426 * gets rid of a shift/reduce couple
2429 : namespace_or_type_name
2431 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2443 // Note that here only unmanaged types are allowed but we
2444 // can't perform checks during this phase - we do it during
2445 // semantic analysis.
2447 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2451 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2457 | non_expression_type rank_specifier
2459 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2461 | non_expression_type STAR
2463 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2465 | expression rank_specifiers
2467 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2471 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2475 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2476 // through this different path
2478 | multiplicative_expression STAR
2480 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2487 ArrayList types = new ArrayList (4);
2492 | type_list COMMA type
2494 ArrayList types = (ArrayList) $1;
2502 * replaces all the productions for isolating the various
2503 * simple types, but we need this to reuse it easily in local_variable_type
2506 : OBJECT { $$ = TypeManager.system_object_expr; }
2507 | STRING { $$ = TypeManager.system_string_expr; }
2508 | BOOL { $$ = TypeManager.system_boolean_expr; }
2509 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2510 | FLOAT { $$ = TypeManager.system_single_expr; }
2511 | DOUBLE { $$ = TypeManager.system_double_expr; }
2516 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2517 | BYTE { $$ = TypeManager.system_byte_expr; }
2518 | SHORT { $$ = TypeManager.system_int16_expr; }
2519 | USHORT { $$ = TypeManager.system_uint16_expr; }
2520 | INT { $$ = TypeManager.system_int32_expr; }
2521 | UINT { $$ = TypeManager.system_uint32_expr; }
2522 | LONG { $$ = TypeManager.system_int64_expr; }
2523 | ULONG { $$ = TypeManager.system_uint64_expr; }
2524 | CHAR { $$ = TypeManager.system_char_expr; }
2525 | VOID { $$ = TypeManager.system_void_expr; }
2529 : type rank_specifiers
2531 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2536 // Expressions, section 7.5
2546 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2548 | parenthesized_expression
2550 | invocation_expression
2554 | post_increment_expression
2555 | post_decrement_expression
2559 | checked_expression
2560 | unchecked_expression
2561 | pointer_member_access
2562 | anonymous_method_expression
2569 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2570 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2571 | NULL { $$ = NullLiteral.Null; }
2575 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2576 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2577 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2582 object v = lexer.Value;
2588 $$ = IntLiteral.Zero;
2590 $$ = IntLiteral.One;
2592 $$ = new IntLiteral (i);
2593 } else if (v is uint)
2594 $$ = new UIntLiteral ((UInt32) v);
2596 $$ = new LongLiteral ((Int64) v);
2597 else if (v is ulong)
2598 $$ = new ULongLiteral ((UInt64) v);
2600 Console.WriteLine ("OOPS. Unexpected result from scanner");
2605 : TRUE { $$ = new BoolLiteral (true); }
2606 | FALSE { $$ = new BoolLiteral (false); }
2609 parenthesized_expression_0
2610 : OPEN_PARENS expression CLOSE_PARENS
2613 lexer.Deambiguate_CloseParens ();
2614 // After this, the next token returned is one of
2615 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2616 // or CLOSE_PARENS_MINUS.
2620 parenthesized_expression
2621 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2625 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2627 // If a parenthesized expression is followed by a minus, we need to wrap
2628 // the expression inside a ParenthesizedExpression for the CS0075 check
2629 // in Binary.DoResolve().
2630 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2635 : primary_expression DOT IDENTIFIER
2637 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2639 | predefined_type DOT IDENTIFIER
2641 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2649 invocation_expression
2650 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2653 Location l = lexer.Location;
2654 Report.Error (1, l, "Parse error");
2656 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2658 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2660 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2662 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2664 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2666 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2668 ArrayList args = new ArrayList (1);
2670 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2672 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2674 ArrayList args = ((ArrayList) $4);
2676 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2681 : /* empty */ { $$ = null; }
2688 ArrayList list = new ArrayList (4);
2692 | argument_list COMMA argument
2694 ArrayList list = (ArrayList) $1;
2698 | argument_list error {
2699 CheckToken (1026, yyToken, ", or ) expected");
2706 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2708 | non_simple_argument
2715 : REF variable_reference
2717 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2719 | OUT variable_reference
2721 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2723 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2725 ArrayList list = (ArrayList) $3;
2726 Argument[] args = new Argument [list.Count];
2727 list.CopyTo (args, 0);
2729 Expression expr = new Arglist (args, lexer.Location);
2730 $$ = new Argument (expr, Argument.AType.Expression);
2734 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2739 : expression { note ("section 5.4"); $$ = $1; }
2743 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2745 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2747 | primary_expression rank_specifiers
2749 // So the super-trick is that primary_expression
2750 // can only be either a SimpleName or a MemberAccess.
2751 // The MemberAccess case arises when you have a fully qualified type-name like :
2753 // SimpleName is when you have
2756 Expression expr = (Expression) $1;
2757 if (expr is ComposedCast){
2758 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2759 } else if (!(expr is SimpleName || expr is MemberAccess)){
2760 Error_ExpectingTypeName (lexer.Location, expr);
2761 $$ = TypeManager.system_object_expr;
2764 // So we extract the string corresponding to the SimpleName
2767 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2775 ArrayList list = new ArrayList (4);
2779 | expression_list COMMA expression
2781 ArrayList list = (ArrayList) $1;
2790 $$ = new This (current_block, lexer.Location);
2795 : BASE DOT IDENTIFIER
2797 $$ = new BaseAccess ((string) $3, lexer.Location);
2799 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2801 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2804 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2809 post_increment_expression
2810 : primary_expression OP_INC
2812 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2813 (Expression) $1, lexer.Location);
2817 post_decrement_expression
2818 : primary_expression OP_DEC
2820 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2821 (Expression) $1, lexer.Location);
2826 : object_or_delegate_creation_expression
2827 | array_creation_expression
2830 object_or_delegate_creation_expression
2831 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2833 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2837 array_creation_expression
2838 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2840 opt_array_initializer
2842 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2844 | NEW type rank_specifiers array_initializer
2846 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2850 Report.Error (1031, lexer.Location, "Type expected");
2855 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2871 : rank_specifier opt_rank_specifier
2873 $$ = (string) $2 + (string) $1;
2878 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2880 $$ = "[" + (string) $2 + "]";
2900 | dim_separators COMMA
2902 $$ = (string) $1 + ",";
2906 opt_array_initializer
2918 : OPEN_BRACE CLOSE_BRACE
2920 ArrayList list = new ArrayList (4);
2923 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2925 $$ = (ArrayList) $2;
2929 variable_initializer_list
2930 : variable_initializer
2932 ArrayList list = new ArrayList (4);
2936 | variable_initializer_list COMMA variable_initializer
2938 ArrayList list = (ArrayList) $1;
2945 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2947 $$ = new TypeOfVoid (lexer.Location);
2949 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2951 $$ = new TypeOf ((Expression) $3, lexer.Location);
2956 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2957 $$ = new SizeOf ((Expression) $3, lexer.Location);
2962 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2964 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2968 unchecked_expression
2969 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2971 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2975 pointer_member_access
2976 : primary_expression OP_PTR IDENTIFIER
2980 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2981 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2985 anonymous_method_expression
2986 : DELEGATE opt_anonymous_method_signature _mark_
2988 oob_stack.Push (current_local_parameters);
2989 current_local_parameters = (Parameters)$2;
2991 // Force the next block to be created as a ToplevelBlock
2992 oob_stack.Push (current_block);
2993 oob_stack.Push (top_current_block);
2994 current_block = null;
2998 Location loc = (Location) $3;
2999 top_current_block = (Block) oob_stack.Pop ();
3000 current_block = (Block) oob_stack.Pop ();
3001 if (RootContext.Version == LanguageVersion.ISO_1){
3002 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3005 ToplevelBlock anon_block = (ToplevelBlock) $5;
3007 anon_block.Parent = current_block;
3008 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3011 current_local_parameters = (Parameters) oob_stack.Pop ();
3015 opt_anonymous_method_signature
3016 : /* empty */ { $$ = null; }
3017 | anonymous_method_signature
3020 anonymous_method_signature
3021 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3024 $$ = Parameters.EmptyReadOnlyParameters;
3026 ArrayList par_list = (ArrayList) $2;
3027 Parameter [] pars = new Parameter [par_list.Count];
3028 par_list.CopyTo (pars);
3029 $$ = new Parameters (pars, null, lexer.Location);
3034 opt_anonymous_method_parameter_list
3035 : /* empty */ { $$ = null; }
3036 | anonymous_method_parameter_list { $$ = $1; }
3039 anonymous_method_parameter_list
3040 : anonymous_method_parameter
3042 ArrayList a = new ArrayList (4);
3046 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3048 ArrayList a = (ArrayList) $1;
3054 anonymous_method_parameter
3055 : opt_parameter_modifier type IDENTIFIER {
3056 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3058 | PARAMS type IDENTIFIER {
3059 Report.Error (1670, lexer.Location, "params modifier not allowed in anonymous method declaration");
3065 : primary_expression
3066 | BANG prefixed_unary_expression
3068 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3070 | TILDE prefixed_unary_expression
3072 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3078 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3080 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3082 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3084 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3090 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3092 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3097 // The idea to split this out is from Rhys' grammar
3098 // to solve the problem with casts.
3100 prefixed_unary_expression
3102 | PLUS prefixed_unary_expression
3104 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3106 | MINUS prefixed_unary_expression
3108 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3110 | OP_INC prefixed_unary_expression
3112 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3113 (Expression) $2, lexer.Location);
3115 | OP_DEC prefixed_unary_expression
3117 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3118 (Expression) $2, lexer.Location);
3120 | STAR prefixed_unary_expression
3122 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3124 | BITWISE_AND prefixed_unary_expression
3126 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3130 pre_increment_expression
3131 : OP_INC prefixed_unary_expression
3133 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3134 (Expression) $2, lexer.Location);
3138 pre_decrement_expression
3139 : OP_DEC prefixed_unary_expression
3141 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3142 (Expression) $2, lexer.Location);
3146 multiplicative_expression
3147 : prefixed_unary_expression
3148 | multiplicative_expression STAR prefixed_unary_expression
3150 $$ = new Binary (Binary.Operator.Multiply,
3151 (Expression) $1, (Expression) $3, lexer.Location);
3153 | multiplicative_expression DIV prefixed_unary_expression
3155 $$ = new Binary (Binary.Operator.Division,
3156 (Expression) $1, (Expression) $3, lexer.Location);
3158 | multiplicative_expression PERCENT prefixed_unary_expression
3160 $$ = new Binary (Binary.Operator.Modulus,
3161 (Expression) $1, (Expression) $3, lexer.Location);
3166 : multiplicative_expression
3167 | additive_expression PLUS multiplicative_expression
3169 $$ = new Binary (Binary.Operator.Addition,
3170 (Expression) $1, (Expression) $3, lexer.Location);
3172 | additive_expression MINUS multiplicative_expression
3174 $$ = new Binary (Binary.Operator.Subtraction,
3175 (Expression) $1, (Expression) $3, lexer.Location);
3180 : additive_expression
3181 | shift_expression OP_SHIFT_LEFT additive_expression
3183 $$ = new Binary (Binary.Operator.LeftShift,
3184 (Expression) $1, (Expression) $3, lexer.Location);
3186 | shift_expression OP_SHIFT_RIGHT additive_expression
3188 $$ = new Binary (Binary.Operator.RightShift,
3189 (Expression) $1, (Expression) $3, lexer.Location);
3193 relational_expression
3195 | relational_expression OP_LT shift_expression
3197 $$ = new Binary (Binary.Operator.LessThan,
3198 (Expression) $1, (Expression) $3, lexer.Location);
3200 | relational_expression OP_GT shift_expression
3202 $$ = new Binary (Binary.Operator.GreaterThan,
3203 (Expression) $1, (Expression) $3, lexer.Location);
3205 | relational_expression OP_LE shift_expression
3207 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3208 (Expression) $1, (Expression) $3, lexer.Location);
3210 | relational_expression OP_GE shift_expression
3212 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3213 (Expression) $1, (Expression) $3, lexer.Location);
3215 | relational_expression IS type
3217 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3219 | relational_expression AS type
3221 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3226 : relational_expression
3227 | equality_expression OP_EQ relational_expression
3229 $$ = new Binary (Binary.Operator.Equality,
3230 (Expression) $1, (Expression) $3, lexer.Location);
3232 | equality_expression OP_NE relational_expression
3234 $$ = new Binary (Binary.Operator.Inequality,
3235 (Expression) $1, (Expression) $3, lexer.Location);
3240 : equality_expression
3241 | and_expression BITWISE_AND equality_expression
3243 $$ = new Binary (Binary.Operator.BitwiseAnd,
3244 (Expression) $1, (Expression) $3, lexer.Location);
3248 exclusive_or_expression
3250 | exclusive_or_expression CARRET and_expression
3252 $$ = new Binary (Binary.Operator.ExclusiveOr,
3253 (Expression) $1, (Expression) $3, lexer.Location);
3257 inclusive_or_expression
3258 : exclusive_or_expression
3259 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3261 $$ = new Binary (Binary.Operator.BitwiseOr,
3262 (Expression) $1, (Expression) $3, lexer.Location);
3266 conditional_and_expression
3267 : inclusive_or_expression
3268 | conditional_and_expression OP_AND inclusive_or_expression
3270 $$ = new Binary (Binary.Operator.LogicalAnd,
3271 (Expression) $1, (Expression) $3, lexer.Location);
3275 conditional_or_expression
3276 : conditional_and_expression
3277 | conditional_or_expression OP_OR conditional_and_expression
3279 $$ = new Binary (Binary.Operator.LogicalOr,
3280 (Expression) $1, (Expression) $3, lexer.Location);
3284 conditional_expression
3285 : conditional_or_expression
3286 | conditional_or_expression INTERR expression COLON expression
3288 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3292 assignment_expression
3293 : prefixed_unary_expression ASSIGN expression
3295 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3297 | prefixed_unary_expression OP_MULT_ASSIGN expression
3299 Location l = lexer.Location;
3301 $$ = new CompoundAssign (
3302 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3304 | prefixed_unary_expression OP_DIV_ASSIGN expression
3306 Location l = lexer.Location;
3308 $$ = new CompoundAssign (
3309 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3311 | prefixed_unary_expression OP_MOD_ASSIGN expression
3313 Location l = lexer.Location;
3315 $$ = new CompoundAssign (
3316 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3318 | prefixed_unary_expression OP_ADD_ASSIGN expression
3320 Location l = lexer.Location;
3322 $$ = new CompoundAssign (
3323 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3325 | prefixed_unary_expression OP_SUB_ASSIGN expression
3327 Location l = lexer.Location;
3329 $$ = new CompoundAssign (
3330 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3332 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3334 Location l = lexer.Location;
3336 $$ = new CompoundAssign (
3337 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3339 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3341 Location l = lexer.Location;
3343 $$ = new CompoundAssign (
3344 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3346 | prefixed_unary_expression OP_AND_ASSIGN expression
3348 Location l = lexer.Location;
3350 $$ = new CompoundAssign (
3351 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3353 | prefixed_unary_expression OP_OR_ASSIGN expression
3355 Location l = lexer.Location;
3357 $$ = new CompoundAssign (
3358 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3360 | prefixed_unary_expression OP_XOR_ASSIGN expression
3362 Location l = lexer.Location;
3364 $$ = new CompoundAssign (
3365 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3370 : conditional_expression
3371 | assignment_expression
3391 MemberName name = MakeName ((MemberName) $5);
3392 bool partial = (bool) $3;
3393 int mod_flags = (int) $2;
3396 ClassPart part = PartialContainer.CreatePart (
3397 current_namespace, current_class, name, mod_flags,
3398 (Attributes) $1, Kind.Class, lexer.Location);
3400 current_container = part.PartialContainer;
3401 current_class = part;
3403 if ((mod_flags & Modifiers.STATIC) != 0) {
3404 current_class = new StaticClass (
3405 current_namespace, current_class, name,
3406 mod_flags, (Attributes) $1, lexer.Location);
3408 current_class = new Class (
3409 current_namespace, current_class, name,
3410 mod_flags, (Attributes) $1, lexer.Location);
3413 current_container.AddClassOrStruct (current_class);
3414 current_container = current_class;
3415 RootContext.Tree.RecordDecl (name, current_class);
3421 if (current_class.Name == "System.Object") {
3422 Report.Error (537, current_class.Location,
3423 "The class System.Object cannot have a base " +
3424 "class or implement an interface.");
3426 current_class.Bases = (ArrayList) $7;
3429 if (RootContext.Documentation != null) {
3430 current_class.DocComment = Lexer.consume_doc_comment ();
3431 Lexer.doc_state = XmlCommentState.Allowed;
3436 if (RootContext.Documentation != null)
3437 Lexer.doc_state = XmlCommentState.Allowed;
3441 $$ = pop_current_class ();
3447 { $$ = (bool) false; }
3449 { $$ = (bool) true; }
3453 : /* empty */ { $$ = (int) 0; }
3459 | modifiers modifier
3464 if ((m1 & m2) != 0) {
3465 Location l = lexer.Location;
3466 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3468 $$ = (int) (m1 | m2);
3473 : NEW { $$ = Modifiers.NEW; }
3474 | PUBLIC { $$ = Modifiers.PUBLIC; }
3475 | PROTECTED { $$ = Modifiers.PROTECTED; }
3476 | INTERNAL { $$ = Modifiers.INTERNAL; }
3477 | PRIVATE { $$ = Modifiers.PRIVATE; }
3478 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3479 | SEALED { $$ = Modifiers.SEALED; }
3480 | STATIC { $$ = Modifiers.STATIC; }
3481 | READONLY { $$ = Modifiers.READONLY; }
3482 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3483 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3484 | EXTERN { $$ = Modifiers.EXTERN; }
3485 | VOLATILE { $$ = Modifiers.VOLATILE; }
3486 | UNSAFE { $$ = Modifiers.UNSAFE; }
3490 : /* empty */ { $$ = null; }
3491 | class_base { $$ = $1; }
3495 : COLON type_list { $$ = $2; }
3503 // A block is "contained" on the following places:
3505 // property_declaration as part of the accessor body (get/set)
3506 // operator_declaration
3507 // constructor_declaration
3508 // destructor_declaration
3509 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3514 if (current_block == null){
3515 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3516 top_current_block = current_block;
3518 current_block = new Block (current_block, current_local_parameters,
3519 lexer.Location, Location.Null);
3522 opt_statement_list CLOSE_BRACE
3524 while (current_block.Implicit)
3525 current_block = current_block.Parent;
3527 current_block.SetEndLocation (lexer.Location);
3528 current_block = current_block.Parent;
3529 if (current_block == null)
3530 top_current_block = null;
3541 | statement_list statement
3545 : declaration_statement
3547 if ($1 != null && (Block) $1 != current_block){
3548 current_block.AddStatement ((Statement) $1);
3549 current_block = (Block) $1;
3552 | valid_declaration_statement
3554 current_block.AddStatement ((Statement) $1);
3559 valid_declaration_statement
3562 | expression_statement
3563 | selection_statement
3564 | iteration_statement
3568 | unchecked_statement
3576 : valid_declaration_statement
3577 | declaration_statement
3579 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3584 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3592 $$ = EmptyStatement.Value;
3599 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3601 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3602 current_block.AddStatement (labeled);
3607 declaration_statement
3608 : local_variable_declaration SEMICOLON
3611 DictionaryEntry de = (DictionaryEntry) $1;
3613 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3617 | local_constant_declaration SEMICOLON
3620 DictionaryEntry de = (DictionaryEntry) $1;
3622 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3628 * The following is from Rhys' grammar:
3629 * > Types in local variable declarations must be recognized as
3630 * > expressions to prevent reduce/reduce errors in the grammar.
3631 * > The expressions are converted into types during semantic analysis.
3634 : primary_expression opt_rank_specifier
3636 // FIXME: Do something smart here regarding the composition of the type.
3638 // Ok, the above "primary_expression" is there to get rid of
3639 // both reduce/reduce and shift/reduces in the grammar, it should
3640 // really just be "type_name". If you use type_name, a reduce/reduce
3641 // creeps up. If you use namespace_or_type_name (which is all we need
3642 // really) two shift/reduces appear.
3645 // So the super-trick is that primary_expression
3646 // can only be either a SimpleName or a MemberAccess.
3647 // The MemberAccess case arises when you have a fully qualified type-name like :
3649 // SimpleName is when you have
3652 Expression expr = (Expression) $1;
3653 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3654 Error_ExpectingTypeName (lexer.Location, expr);
3658 // So we extract the string corresponding to the SimpleName
3662 if ((string) $2 == "")
3665 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3668 | builtin_types opt_rank_specifier
3670 if ((string) $2 == "")
3673 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3677 local_variable_pointer_type
3678 : primary_expression STAR
3680 Expression expr = (Expression) $1;
3681 Location l = lexer.Location;
3683 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3684 Error_ExpectingTypeName (l, expr);
3688 $$ = new ComposedCast ((Expression) $1, "*", l);
3690 | builtin_types STAR
3692 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3696 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3698 | local_variable_pointer_type STAR
3700 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3704 local_variable_declaration
3705 : local_variable_type variable_declarators
3708 $$ = new DictionaryEntry ($1, $2);
3712 | local_variable_pointer_type opt_rank_specifier variable_declarators
3717 if ((string) $2 == "")
3718 t = (Expression) $1;
3720 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3721 $$ = new DictionaryEntry (t, $3);
3727 local_constant_declaration
3728 : CONST local_variable_type constant_declarators
3731 $$ = new DictionaryEntry ($2, $3);
3737 expression_statement
3738 : statement_expression SEMICOLON
3745 // We have to do the wrapping here and not in the case above,
3746 // because statement_expression is used for example in for_statement
3748 statement_expression
3749 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3750 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3751 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3752 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3753 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3754 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3755 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3757 Report.Error (1002, lexer.Location, "Expecting `;'");
3762 object_creation_expression
3763 : object_or_delegate_creation_expression
3764 { note ("complain if this is a delegate maybe?"); }
3773 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3776 Location l = (Location) $3;
3778 $$ = new If ((Expression) $4, (Statement) $6, l);
3780 if (RootContext.WarningLevel >= 4){
3781 if ($6 == EmptyStatement.Value)
3782 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3786 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3787 embedded_statement ELSE embedded_statement
3789 Location l = (Location) $3;
3791 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
3796 : SWITCH OPEN_PARENS _mark_
3798 switch_stack.Push (current_block);
3800 expression CLOSE_PARENS
3803 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
3804 current_block = (Block) switch_stack.Pop ();
3820 Report.Error (1522, lexer.Location, "Empty switch block");
3828 ArrayList sections = new ArrayList (4);
3833 | switch_sections switch_section
3835 ArrayList sections = (ArrayList) $1;
3845 current_block = current_block.CreateSwitchBlock (lexer.Location);
3849 Block topmost = current_block;
3851 while (topmost.Implicit)
3852 topmost = topmost.Parent;
3853 $$ = new SwitchSection ((ArrayList) $1, topmost);
3860 ArrayList labels = new ArrayList (4);
3865 | switch_labels switch_label
3867 ArrayList labels = (ArrayList) ($1);
3875 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3876 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3879 1523, lexer.Location,
3880 "The keyword case or default must precede code in switch block");
3892 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
3894 Location l = (Location) $3;
3895 $$ = new While ((Expression) $4, (Statement) $6, l);
3897 if (RootContext.WarningLevel >= 4){
3898 if ($6 == EmptyStatement.Value)
3899 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3905 : DO embedded_statement
3906 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
3908 Location l = (Location) $5;
3910 $$ = new Do ((Statement) $2, (Expression) $6, l);
3916 opt_for_initializer SEMICOLON _mark_
3918 Block assign_block = new Block (current_block);
3919 current_block = assign_block;
3921 if ($3 is DictionaryEntry){
3922 DictionaryEntry de = (DictionaryEntry) $3;
3924 Expression type = (Expression) de.Key;
3925 ArrayList var_declarators = (ArrayList) de.Value;
3927 foreach (VariableDeclaration decl in var_declarators){
3931 vi = current_block.AddVariable (
3932 type, decl.identifier, current_local_parameters, decl.Location);
3936 Location l = lexer.Location;
3938 if (decl.expression_or_array_initializer is Expression){
3939 expr = (Expression) decl.expression_or_array_initializer;
3940 } else if (decl.expression_or_array_initializer == null) {
3943 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3944 expr = new ArrayCreation (type, "", init, decl.Location);
3947 LocalVariableReference var;
3948 var = new LocalVariableReference (assign_block, decl.identifier, l);
3951 Assign a = new Assign (var, expr, decl.Location);
3953 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3957 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
3958 // This can be referred to as $6 below.
3964 opt_for_condition SEMICOLON
3965 opt_for_iterator CLOSE_PARENS
3968 Location l = (Location) $5;
3970 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
3972 if (RootContext.WarningLevel >= 4){
3973 if ($11 == EmptyStatement.Value)
3974 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3977 current_block.AddStatement (f);
3978 while (current_block.Implicit)
3979 current_block = current_block.Parent;
3981 current_block = current_block.Parent;
3986 : /* empty */ { $$ = EmptyStatement.Value; }
3991 : local_variable_declaration
3992 | statement_expression_list
3996 : /* empty */ { $$ = null; }
3997 | boolean_expression
4001 : /* empty */ { $$ = EmptyStatement.Value; }
4006 : statement_expression_list
4009 statement_expression_list
4010 : statement_expression
4012 // CHANGE: was `null'
4013 Block b = new Block (current_block, Block.Flags.Implicit);
4015 b.AddStatement ((Statement) $1);
4018 | statement_expression_list COMMA statement_expression
4020 Block b = (Block) $1;
4022 b.AddStatement ((Statement) $3);
4028 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4030 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4033 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4034 expression CLOSE_PARENS
4036 Block foreach_block = new Block (current_block);
4037 current_block = foreach_block;
4039 Location l = lexer.Location;
4042 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
4046 // Get a writable reference to this read-only variable.
4048 // Note that the $$ here refers to the value of _this_ code block,
4049 // not the value of the LHS non-terminal. This can be referred to as $9 below.
4050 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4057 LocalVariableReference v = (LocalVariableReference) $9;
4058 Location l = (Location) $6;
4061 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4062 current_block.AddStatement (f);
4065 while (current_block.Implicit)
4066 current_block = current_block.Parent;
4068 current_block = current_block.Parent;
4074 | continue_statement
4084 $$ = new Break (lexer.Location);
4089 : CONTINUE SEMICOLON
4091 $$ = new Continue (lexer.Location);
4096 : GOTO IDENTIFIER SEMICOLON
4098 $$ = new Goto (current_block, (string) $2, lexer.Location);
4100 | GOTO CASE constant_expression SEMICOLON
4102 $$ = new GotoCase ((Expression) $3, lexer.Location);
4104 | GOTO DEFAULT SEMICOLON
4106 $$ = new GotoDefault (lexer.Location);
4111 : RETURN opt_expression SEMICOLON
4113 $$ = new Return ((Expression) $2, lexer.Location);
4118 : THROW opt_expression SEMICOLON
4120 $$ = new Throw ((Expression) $2, lexer.Location);
4125 : IDENTIFIER RETURN expression SEMICOLON
4127 string s = (string) $1;
4129 Report.Error (1003, lexer.Location, "; expected");
4132 if (RootContext.Version == LanguageVersion.ISO_1){
4133 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4136 if (iterator_container == null){
4137 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4140 iterator_container.SetYields ();
4141 $$ = new Yield ((Expression) $3, lexer.Location);
4144 | IDENTIFIER RETURN SEMICOLON
4146 Report.Error (1627, lexer.Location, "Expression expected after yield return");
4149 | IDENTIFIER BREAK SEMICOLON
4151 string s = (string) $1;
4153 Report.Error (1003, lexer.Location, "; expected");
4156 if (RootContext.Version == LanguageVersion.ISO_1){
4157 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4160 if (iterator_container == null){
4161 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4164 iterator_container.SetYields ();
4165 $$ = new YieldBreak (lexer.Location);
4176 : TRY block catch_clauses
4180 ArrayList c = (ArrayList)$3;
4181 for (int i = 0; i < c.Count; ++i) {
4182 Catch cc = (Catch) c [i];
4184 if (i != c.Count - 1)
4185 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4192 // Now s contains the list of specific catch clauses
4193 // and g contains the general one.
4195 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4197 | TRY block opt_catch_clauses FINALLY block
4200 ArrayList s = new ArrayList (4);
4201 ArrayList catch_list = (ArrayList) $3;
4203 if (catch_list != null){
4204 foreach (Catch cc in catch_list) {
4212 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4216 Report.Error (1524, lexer.Location, "Expected catch or finally");
4221 : /* empty */ { $$ = null; }
4228 ArrayList l = new ArrayList (4);
4233 | catch_clauses catch_clause
4235 ArrayList l = (ArrayList) $1;
4243 : /* empty */ { $$ = null; }
4248 : CATCH opt_catch_args
4250 Expression type = null;
4254 DictionaryEntry cc = (DictionaryEntry) $2;
4255 type = (Expression) cc.Key;
4256 id = (string) cc.Value;
4259 ArrayList one = new ArrayList (4);
4260 Location loc = lexer.Location;
4262 one.Add (new VariableDeclaration (id, null, loc));
4264 current_block = new Block (current_block);
4265 Block b = declare_local_variables (type, one, loc);
4270 Expression type = null;
4274 DictionaryEntry cc = (DictionaryEntry) $2;
4275 type = (Expression) cc.Key;
4276 id = (string) cc.Value;
4279 while (current_block.Implicit)
4280 current_block = current_block.Parent;
4281 current_block = current_block.Parent;
4285 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4290 : /* empty */ { $$ = null; }
4295 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4297 $$ = new DictionaryEntry ($2, $3);
4304 $$ = new Checked ((Block) $2);
4311 $$ = new Unchecked ((Block) $2);
4318 if (!RootContext.Unsafe){
4319 Report.Error (227, lexer.Location,
4320 "Unsafe code can only be used if --unsafe is used");
4323 $$ = new Unsafe ((Block) $3);
4329 type fixed_pointer_declarators
4332 ArrayList list = (ArrayList) $4;
4333 Expression type = (Expression) $3;
4334 Location l = lexer.Location;
4335 int top = list.Count;
4337 Block assign_block = new Block (current_block);
4338 current_block = assign_block;
4340 for (int i = 0; i < top; i++){
4341 Pair p = (Pair) list [i];
4344 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4356 Location l = (Location) $6;
4358 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4360 if (RootContext.WarningLevel >= 4){
4361 if ($8 == EmptyStatement.Value)
4362 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4365 current_block.AddStatement (f);
4366 while (current_block.Implicit)
4367 current_block = current_block.Parent;
4369 current_block = current_block.Parent;
4373 fixed_pointer_declarators
4374 : fixed_pointer_declarator {
4375 ArrayList declarators = new ArrayList (4);
4377 declarators.Add ($1);
4380 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4382 ArrayList declarators = (ArrayList) $1;
4384 declarators.Add ($3);
4389 fixed_pointer_declarator
4390 : IDENTIFIER ASSIGN expression
4392 $$ = new Pair ($1, $3);
4396 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4402 : LOCK OPEN_PARENS expression CLOSE_PARENS
4408 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4413 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4415 Block assign_block = new Block (current_block);
4416 current_block = assign_block;
4418 if ($3 is DictionaryEntry){
4419 DictionaryEntry de = (DictionaryEntry) $3;
4420 Location l = lexer.Location;
4422 Expression type = (Expression) de.Key;
4423 ArrayList var_declarators = (ArrayList) de.Value;
4425 ArrayList vars = new ArrayList (4);
4427 foreach (VariableDeclaration decl in var_declarators){
4429 LocalInfo vi = current_block.AddVariable (
4430 type, decl.identifier,
4431 current_local_parameters, decl.Location);
4437 if (decl.expression_or_array_initializer is Expression){
4438 expr = (Expression) decl.expression_or_array_initializer;
4440 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4442 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4445 expr = new ArrayCreation (type, "", init, decl.Location);
4448 LocalVariableReference var;
4450 // Get a writable reference to this read-only variable.
4451 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4453 // This is so that it is not a warning on using variables
4456 vars.Add (new DictionaryEntry (var, expr));
4458 // Assign a = new Assign (var, expr, decl.Location);
4459 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4462 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4463 // It can be referred to as $6 below.
4464 $$ = new DictionaryEntry (type, vars);
4471 Using u = new Using ($6, (Statement) $7, (Location) $5);
4472 current_block.AddStatement (u);
4473 while (current_block.Implicit)
4474 current_block = current_block.Parent;
4476 current_block = current_block.Parent;
4480 resource_acquisition
4481 : local_variable_declaration
4485 // Utility rule to save location information
4488 { $$ = lexer.Location; }
4493 // A class used to pass around variable declarations and constants
4495 public class VariableDeclaration {
4496 public string identifier;
4497 public object expression_or_array_initializer;
4498 public Location Location;
4499 public Attributes OptAttributes;
4500 public string DocComment;
4502 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4504 this.identifier = id;
4505 this.expression_or_array_initializer = eoai;
4507 this.OptAttributes = opt_attrs;
4510 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4516 /// Used to pass around interface property information
4518 public class InterfaceAccessorInfo {
4520 public readonly Accessor Get, Set;
4522 public InterfaceAccessorInfo (bool has_get, bool has_set,
4523 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4526 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4528 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4531 Get = new Accessor (null, 0, get_attrs, get_loc);
4533 Set = new Accessor (null, 0, set_attrs, set_loc);
4540 // A class used to hold info about an indexer declarator
4542 public class IndexerDeclaration {
4543 public Expression type;
4544 public MemberName interface_type;
4545 public Parameters param_list;
4547 public IndexerDeclaration (Expression type, MemberName interface_type,
4548 Parameters param_list)
4551 this.interface_type = interface_type;
4552 this.param_list = param_list;
4557 // We use this when we do not have an object in advance that is an IIteratorContainer
4559 public class SimpleIteratorContainer : IIteratorContainer {
4562 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4567 public static SimpleIteratorContainer GetSimple () {
4568 Simple.Yields = false;
4572 public void SetYields () { Yields = true; }
4576 // A class used to hold info about an operator declarator
4578 public class OperatorDeclaration {
4579 public Operator.OpType optype;
4580 public Expression ret_type, arg1type, arg2type;
4581 public string arg1name, arg2name;
4582 public Location location;
4584 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4585 Expression arg1type, string arg1name,
4586 Expression arg2type, string arg2name, Location location)
4589 this.ret_type = ret_type;
4590 this.arg1type = arg1type;
4591 this.arg1name = arg1name;
4592 this.arg2type = arg2type;
4593 this.arg2name = arg2name;
4594 this.location = location;
4599 void Error_ExpectingTypeName (Location l, Expression expr)
4601 if (expr is Invocation){
4602 Report.Error (1002, l, "; expected");
4604 Report.Error (-1, l, "Invalid Type definition");
4608 TypeContainer pop_current_class ()
4610 TypeContainer retval = current_class;
4612 current_class = current_class.Parent;
4613 current_container = current_container.Parent;
4615 if (current_class != current_container) {
4616 if (!(current_class is ClassPart) ||
4617 ((ClassPart) current_class).PartialContainer != current_container)
4618 throw new InternalErrorException ();
4619 } else if (current_container is ClassPart)
4620 current_container = ((ClassPart) current_class).PartialContainer;
4626 // Given the @class_name name, it creates a fully qualified name
4627 // based on the containing declaration space
4630 MakeName (MemberName class_name)
4632 Namespace ns = current_namespace.NS;
4634 if (current_container.Name == ""){
4636 return new MemberName (ns.MemberName, class_name);
4640 return new MemberName (current_container.MemberName, class_name);
4644 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4646 Block implicit_block;
4647 ArrayList inits = null;
4650 // We use the `Used' property to check whether statements
4651 // have been added to the current block. If so, we need
4652 // to create another block to contain the new declaration
4653 // otherwise, as an optimization, we use the same block to
4654 // add the declaration.
4656 // FIXME: A further optimization is to check if the statements
4657 // that were added were added as part of the initialization
4658 // below. In which case, no other statements have been executed
4659 // and we might be able to reduce the number of blocks for
4660 // situations like this:
4662 // int j = 1; int k = j + 1;
4664 if (current_block.Used)
4665 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4667 implicit_block = current_block;
4669 foreach (VariableDeclaration decl in variable_declarators){
4671 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4672 if (decl.expression_or_array_initializer != null){
4674 inits = new ArrayList (4);
4681 return implicit_block;
4683 foreach (VariableDeclaration decl in inits){
4687 if (decl.expression_or_array_initializer is Expression){
4688 expr = (Expression) decl.expression_or_array_initializer;
4691 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4693 expr = new ArrayCreation (type, "", init, decl.Location);
4696 LocalVariableReference var;
4697 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4699 assign = new Assign (var, expr, decl.Location);
4701 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4704 return implicit_block;
4707 Block declare_local_constants (Expression type, ArrayList declarators)
4709 Block implicit_block;
4711 if (current_block.Used)
4712 implicit_block = new Block (current_block, Block.Flags.Implicit);
4714 implicit_block = current_block;
4716 foreach (VariableDeclaration decl in declarators){
4717 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4718 current_local_parameters, decl.Location);
4721 return implicit_block;
4724 void CheckAttributeTarget (string a)
4728 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4732 Location l = lexer.Location;
4733 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4739 void CheckUnaryOperator (Operator.OpType op)
4743 case Operator.OpType.LogicalNot:
4744 case Operator.OpType.OnesComplement:
4745 case Operator.OpType.Increment:
4746 case Operator.OpType.Decrement:
4747 case Operator.OpType.True:
4748 case Operator.OpType.False:
4749 case Operator.OpType.Addition:
4750 case Operator.OpType.Subtraction:
4755 Location l = lexer.Location;
4756 Report.Error (1019, l, "Overloadable unary operator expected");
4762 void CheckBinaryOperator (Operator.OpType op)
4766 case Operator.OpType.Addition:
4767 case Operator.OpType.Subtraction:
4768 case Operator.OpType.Multiply:
4769 case Operator.OpType.Division:
4770 case Operator.OpType.Modulus:
4771 case Operator.OpType.BitwiseAnd:
4772 case Operator.OpType.BitwiseOr:
4773 case Operator.OpType.ExclusiveOr:
4774 case Operator.OpType.LeftShift:
4775 case Operator.OpType.RightShift:
4776 case Operator.OpType.Equality:
4777 case Operator.OpType.Inequality:
4778 case Operator.OpType.GreaterThan:
4779 case Operator.OpType.LessThan:
4780 case Operator.OpType.GreaterThanOrEqual:
4781 case Operator.OpType.LessThanOrEqual:
4785 Location l = lexer.Location;
4786 Report.Error (1020, l, "Overloadable binary operator expected");
4792 void syntax_error (Location l, string msg)
4794 Report.Error (1003, l, "Syntax error, " + msg);
4797 void output (string s)
4799 Console.WriteLine (s);
4802 void note (string s)
4804 // Used to put annotations
4809 public Tokenizer Lexer {
4815 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4817 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4818 this.name = file.Name;
4820 current_container = RootContext.Tree.Types;
4821 // TODO: Make RootContext.Tree.Types a PartialContainer.
4822 current_class = current_container;
4823 current_container.NamespaceEntry = current_namespace;
4824 oob_stack = new Stack ();
4825 switch_stack = new Stack ();
4827 lexer = new Tokenizer (reader, file, defines);
4830 public void parse ()
4833 if (yacc_verbose_flag > 1)
4834 yyparse (lexer, new yydebug.yyDebugSimple ());
4837 Tokenizer tokenizer = lexer as Tokenizer;
4838 tokenizer.cleanup ();
4839 } catch (Exception e){
4841 // Removed for production use, use parser verbose to get the output.
4843 // Console.WriteLine (e);
4844 Report.Error (-25, lexer.Location, "Parsing error");
4845 if (yacc_verbose_flag > 0)
4846 Console.WriteLine (e);
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;