1 //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Declaration portions of the Parser interfaces.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Parse/Parser.h"
15 #include "clang/Parse/DeclSpec.h"
16 #include "clang/Parse/Scope.h"
17 #include "llvm/ADT/SmallSet.h"
18 using namespace clang
;
20 //===----------------------------------------------------------------------===//
21 // C99 6.7: Declarations.
22 //===----------------------------------------------------------------------===//
25 /// type-name: [C99 6.7.6]
26 /// specifier-qualifier-list abstract-declarator[opt]
27 Parser::TypeTy
*Parser::ParseTypeName() {
28 // Parse the common declaration-specifiers piece.
30 ParseSpecifierQualifierList(DS
);
32 // Parse the abstract-declarator, if present.
33 Declarator
DeclaratorInfo(DS
, Declarator::TypeNameContext
);
34 ParseDeclarator(DeclaratorInfo
);
36 return Actions
.ActOnTypeName(CurScope
, DeclaratorInfo
).Val
;
39 /// ParseAttributes - Parse a non-empty attributes list.
43 /// attributes attribute
46 /// '__attribute__' '(' '(' attribute-list ')' ')'
48 /// [GNU] attribute-list:
50 /// attribute_list ',' attrib
55 /// attrib-name '(' identifier ')'
56 /// attrib-name '(' identifier ',' nonempty-expr-list ')'
57 /// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
59 /// [GNU] attrib-name:
65 /// FIXME: The GCC grammar/code for this construct implies we need two
66 /// token lookahead. Comment from gcc: "If they start with an identifier
67 /// which is followed by a comma or close parenthesis, then the arguments
68 /// start with that identifier; otherwise they are an expression list."
70 /// At the moment, I am not doing 2 token lookahead. I am also unaware of
71 /// any attributes that don't work (based on my limited testing). Most
72 /// attributes are very simple in practice. Until we find a bug, I don't see
73 /// a pressing need to implement the 2 token lookahead.
75 AttributeList
*Parser::ParseAttributes() {
76 assert(Tok
.is(tok::kw___attribute
) && "Not an attribute list!");
78 AttributeList
*CurrAttr
= 0;
80 while (Tok
.is(tok::kw___attribute
)) {
82 if (ExpectAndConsume(tok::l_paren
, diag::err_expected_lparen_after
,
84 SkipUntil(tok::r_paren
, true); // skip until ) or ;
87 if (ExpectAndConsume(tok::l_paren
, diag::err_expected_lparen_after
, "(")) {
88 SkipUntil(tok::r_paren
, true); // skip until ) or ;
91 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
92 while (Tok
.is(tok::identifier
) || isDeclarationSpecifier() ||
95 if (Tok
.is(tok::comma
)) {
96 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
100 // we have an identifier or declaration specifier (const, int, etc.)
101 IdentifierInfo
*AttrName
= Tok
.getIdentifierInfo();
102 SourceLocation AttrNameLoc
= ConsumeToken();
104 // check if we have a "paramterized" attribute
105 if (Tok
.is(tok::l_paren
)) {
106 ConsumeParen(); // ignore the left paren loc for now
108 if (Tok
.is(tok::identifier
)) {
109 IdentifierInfo
*ParmName
= Tok
.getIdentifierInfo();
110 SourceLocation ParmLoc
= ConsumeToken();
112 if (Tok
.is(tok::r_paren
)) {
113 // __attribute__(( mode(byte) ))
114 ConsumeParen(); // ignore the right paren loc for now
115 CurrAttr
= new AttributeList(AttrName
, AttrNameLoc
,
116 ParmName
, ParmLoc
, 0, 0, CurrAttr
);
117 } else if (Tok
.is(tok::comma
)) {
119 // __attribute__(( format(printf, 1, 2) ))
120 llvm::SmallVector
<ExprTy
*, 8> ArgExprs
;
121 bool ArgExprsOk
= true;
123 // now parse the non-empty comma separated list of expressions
125 ExprResult ArgExpr
= ParseAssignmentExpression();
126 if (ArgExpr
.isInvalid
) {
128 SkipUntil(tok::r_paren
);
131 ArgExprs
.push_back(ArgExpr
.Val
);
133 if (Tok
.isNot(tok::comma
))
135 ConsumeToken(); // Eat the comma, move to the next argument
137 if (ArgExprsOk
&& Tok
.is(tok::r_paren
)) {
138 ConsumeParen(); // ignore the right paren loc for now
139 CurrAttr
= new AttributeList(AttrName
, AttrNameLoc
, ParmName
,
140 ParmLoc
, &ArgExprs
[0], ArgExprs
.size(), CurrAttr
);
143 } else { // not an identifier
144 // parse a possibly empty comma separated list of expressions
145 if (Tok
.is(tok::r_paren
)) {
146 // __attribute__(( nonnull() ))
147 ConsumeParen(); // ignore the right paren loc for now
148 CurrAttr
= new AttributeList(AttrName
, AttrNameLoc
,
149 0, SourceLocation(), 0, 0, CurrAttr
);
151 // __attribute__(( aligned(16) ))
152 llvm::SmallVector
<ExprTy
*, 8> ArgExprs
;
153 bool ArgExprsOk
= true;
155 // now parse the list of expressions
157 ExprResult ArgExpr
= ParseAssignmentExpression();
158 if (ArgExpr
.isInvalid
) {
160 SkipUntil(tok::r_paren
);
163 ArgExprs
.push_back(ArgExpr
.Val
);
165 if (Tok
.isNot(tok::comma
))
167 ConsumeToken(); // Eat the comma, move to the next argument
170 if (ArgExprsOk
&& Tok
.is(tok::r_paren
)) {
171 ConsumeParen(); // ignore the right paren loc for now
172 CurrAttr
= new AttributeList(AttrName
, AttrNameLoc
, 0,
173 SourceLocation(), &ArgExprs
[0], ArgExprs
.size(),
179 CurrAttr
= new AttributeList(AttrName
, AttrNameLoc
,
180 0, SourceLocation(), 0, 0, CurrAttr
);
183 if (ExpectAndConsume(tok::r_paren
, diag::err_expected_rparen
))
184 SkipUntil(tok::r_paren
, false);
185 if (ExpectAndConsume(tok::r_paren
, diag::err_expected_rparen
))
186 SkipUntil(tok::r_paren
, false);
191 /// ParseDeclaration - Parse a full 'declaration', which consists of
192 /// declaration-specifiers, some number of declarators, and a semicolon.
193 /// 'Context' should be a Declarator::TheContext value.
195 /// declaration: [C99 6.7]
196 /// block-declaration ->
197 /// simple-declaration
199 /// [C++] namespace-definition
200 /// others... [FIXME]
202 Parser::DeclTy
*Parser::ParseDeclaration(unsigned Context
) {
203 switch (Tok
.getKind()) {
204 case tok::kw_namespace
:
205 return ParseNamespace(Context
);
207 return ParseSimpleDeclaration(Context
);
211 /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
212 /// declaration-specifiers init-declarator-list[opt] ';'
213 ///[C90/C++]init-declarator-list ';' [TODO]
214 /// [OMP] threadprivate-directive [TODO]
215 Parser::DeclTy
*Parser::ParseSimpleDeclaration(unsigned Context
) {
216 // Parse the common declaration-specifiers piece.
218 ParseDeclarationSpecifiers(DS
);
220 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
221 // declaration-specifiers init-declarator-list[opt] ';'
222 if (Tok
.is(tok::semi
)) {
224 return Actions
.ParsedFreeStandingDeclSpec(CurScope
, DS
);
227 Declarator
DeclaratorInfo(DS
, (Declarator::TheContext
)Context
);
228 ParseDeclarator(DeclaratorInfo
);
230 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo
);
234 /// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
235 /// parsing 'declaration-specifiers declarator'. This method is split out this
236 /// way to handle the ambiguity between top-level function-definitions and
239 /// init-declarator-list: [C99 6.7]
241 /// init-declarator-list ',' init-declarator
242 /// init-declarator: [C99 6.7]
244 /// declarator '=' initializer
245 /// [GNU] declarator simple-asm-expr[opt] attributes[opt]
246 /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
248 Parser::DeclTy
*Parser::
249 ParseInitDeclaratorListAfterFirstDeclarator(Declarator
&D
) {
251 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
252 // that they can be chained properly if the actions want this.
253 Parser::DeclTy
*LastDeclInGroup
= 0;
255 // At this point, we know that it is not a function definition. Parse the
256 // rest of the init-declarator-list.
258 // If a simple-asm-expr is present, parse it.
259 if (Tok
.is(tok::kw_asm
))
262 // If attributes are present, parse them.
263 if (Tok
.is(tok::kw___attribute
))
264 D
.AddAttributes(ParseAttributes());
266 // Inform the current actions module that we just parsed this declarator.
267 // FIXME: pass asm & attributes.
268 LastDeclInGroup
= Actions
.ActOnDeclarator(CurScope
, D
, LastDeclInGroup
);
270 // Parse declarator '=' initializer.
272 if (Tok
.is(tok::equal
)) {
274 Init
= ParseInitializer();
275 if (Init
.isInvalid
) {
276 SkipUntil(tok::semi
);
279 Actions
.AddInitializerToDecl(LastDeclInGroup
, Init
.Val
);
282 // If we don't have a comma, it is either the end of the list (a ';') or an
284 if (Tok
.isNot(tok::comma
))
287 // Consume the comma.
290 // Parse the next declarator.
295 if (Tok
.is(tok::semi
)) {
297 return Actions
.FinalizeDeclaratorGroup(CurScope
, LastDeclInGroup
);
299 // If this is an ObjC2 for-each loop, this is a successful declarator
300 // parse. The syntax for these looks like:
301 // 'for' '(' declaration 'in' expr ')' statement
302 if (D
.getContext() == Declarator::ForContext
&& isTokIdentifier_in()) {
303 return Actions
.FinalizeDeclaratorGroup(CurScope
, LastDeclInGroup
);
305 Diag(Tok
, diag::err_parse_error
);
306 // Skip to end of block or statement
307 SkipUntil(tok::r_brace
, true, true);
308 if (Tok
.is(tok::semi
))
313 /// ParseSpecifierQualifierList
314 /// specifier-qualifier-list:
315 /// type-specifier specifier-qualifier-list[opt]
316 /// type-qualifier specifier-qualifier-list[opt]
317 /// [GNU] attributes specifier-qualifier-list[opt]
319 void Parser::ParseSpecifierQualifierList(DeclSpec
&DS
) {
320 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
321 /// parse declaration-specifiers and complain about extra stuff.
322 ParseDeclarationSpecifiers(DS
);
324 // Validate declspec for type-name.
325 unsigned Specs
= DS
.getParsedSpecifiers();
326 if (Specs
== DeclSpec::PQ_None
&& !DS
.getNumProtocolQualifiers())
327 Diag(Tok
, diag::err_typename_requires_specqual
);
329 // Issue diagnostic and remove storage class if present.
330 if (Specs
& DeclSpec::PQ_StorageClassSpecifier
) {
331 if (DS
.getStorageClassSpecLoc().isValid())
332 Diag(DS
.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass
);
334 Diag(DS
.getThreadSpecLoc(), diag::err_typename_invalid_storageclass
);
335 DS
.ClearStorageClassSpecs();
338 // Issue diagnostic and remove function specfier if present.
339 if (Specs
& DeclSpec::PQ_FunctionSpecifier
) {
340 Diag(DS
.getInlineSpecLoc(), diag::err_typename_invalid_functionspec
);
341 DS
.ClearFunctionSpecs();
345 /// ParseDeclarationSpecifiers
346 /// declaration-specifiers: [C99 6.7]
347 /// storage-class-specifier declaration-specifiers[opt]
348 /// type-specifier declaration-specifiers[opt]
349 /// type-qualifier declaration-specifiers[opt]
350 /// [C99] function-specifier declaration-specifiers[opt]
351 /// [GNU] attributes declaration-specifiers[opt]
353 /// storage-class-specifier: [C99 6.7.1]
360 /// type-specifier: [C99 6.7.2]
370 /// struct-or-union-specifier
376 /// [C99] '_Imaginary' // Removed in TC2?
377 /// [GNU] '_Decimal32'
378 /// [GNU] '_Decimal64'
379 /// [GNU] '_Decimal128'
380 /// [GNU] typeof-specifier
381 /// [OBJC] class-name objc-protocol-refs[opt] [TODO]
382 /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
387 /// function-specifier: [C99 6.7.4]
390 void Parser::ParseDeclarationSpecifiers(DeclSpec
&DS
) {
391 DS
.SetRangeStart(Tok
.getLocation());
393 int isInvalid
= false;
394 const char *PrevSpec
= 0;
395 SourceLocation Loc
= Tok
.getLocation();
397 switch (Tok
.getKind()) {
400 // If this is not a declaration specifier token, we're done reading decl
401 // specifiers. First verify that DeclSpec's are consistent.
402 DS
.Finish(Diags
, PP
.getSourceManager(), getLang());
406 case tok::identifier
: {
407 // This identifier can only be a typedef name if we haven't already seen
408 // a type-specifier. Without this check we misparse:
409 // typedef int X; struct Y { short X; }; as 'short int'.
410 if (DS
.hasTypeSpecifier())
411 goto DoneWithDeclSpec
;
413 // It has to be available as a typedef too!
414 TypeTy
*TypeRep
= Actions
.isTypeName(*Tok
.getIdentifierInfo(), CurScope
);
416 goto DoneWithDeclSpec
;
418 isInvalid
= DS
.SetTypeSpecType(DeclSpec::TST_typedef
, Loc
, PrevSpec
,
423 DS
.SetRangeEnd(Tok
.getLocation());
424 ConsumeToken(); // The identifier
426 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
427 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
428 // Objective-C interface. If we don't have Objective-C or a '<', this is
429 // just a normal reference to a typedef name.
430 if (!Tok
.is(tok::less
) || !getLang().ObjC1
)
433 SourceLocation EndProtoLoc
;
434 llvm::SmallVector
<DeclTy
*, 8> ProtocolDecl
;
435 ParseObjCProtocolReferences(ProtocolDecl
, false, EndProtoLoc
);
436 DS
.setProtocolQualifiers(&ProtocolDecl
[0], ProtocolDecl
.size());
438 DS
.SetRangeEnd(EndProtoLoc
);
440 // Do not allow any other declspecs after the protocol qualifier list
441 // "<foo,bar>short" is not allowed.
442 goto DoneWithDeclSpec
;
444 // GNU attributes support.
445 case tok::kw___attribute
:
446 DS
.AddAttributes(ParseAttributes());
449 // storage-class-specifier
450 case tok::kw_typedef
:
451 isInvalid
= DS
.SetStorageClassSpec(DeclSpec::SCS_typedef
, Loc
, PrevSpec
);
454 if (DS
.isThreadSpecified())
455 Diag(Tok
, diag::ext_thread_before
, "extern");
456 isInvalid
= DS
.SetStorageClassSpec(DeclSpec::SCS_extern
, Loc
, PrevSpec
);
458 case tok::kw___private_extern__
:
459 isInvalid
= DS
.SetStorageClassSpec(DeclSpec::SCS_private_extern
, Loc
,
463 if (DS
.isThreadSpecified())
464 Diag(Tok
, diag::ext_thread_before
, "static");
465 isInvalid
= DS
.SetStorageClassSpec(DeclSpec::SCS_static
, Loc
, PrevSpec
);
468 isInvalid
= DS
.SetStorageClassSpec(DeclSpec::SCS_auto
, Loc
, PrevSpec
);
470 case tok::kw_register
:
471 isInvalid
= DS
.SetStorageClassSpec(DeclSpec::SCS_register
, Loc
, PrevSpec
);
473 case tok::kw___thread
:
474 isInvalid
= DS
.SetStorageClassSpecThread(Loc
, PrevSpec
)*2;
479 isInvalid
= DS
.SetTypeSpecWidth(DeclSpec::TSW_short
, Loc
, PrevSpec
);
482 if (DS
.getTypeSpecWidth() != DeclSpec::TSW_long
)
483 isInvalid
= DS
.SetTypeSpecWidth(DeclSpec::TSW_long
, Loc
, PrevSpec
);
485 isInvalid
= DS
.SetTypeSpecWidth(DeclSpec::TSW_longlong
, Loc
, PrevSpec
);
488 isInvalid
= DS
.SetTypeSpecSign(DeclSpec::TSS_signed
, Loc
, PrevSpec
);
490 case tok::kw_unsigned
:
491 isInvalid
= DS
.SetTypeSpecSign(DeclSpec::TSS_unsigned
, Loc
, PrevSpec
);
493 case tok::kw__Complex
:
494 isInvalid
= DS
.SetTypeSpecComplex(DeclSpec::TSC_complex
, Loc
, PrevSpec
);
496 case tok::kw__Imaginary
:
497 isInvalid
= DS
.SetTypeSpecComplex(DeclSpec::TSC_imaginary
, Loc
, PrevSpec
);
500 isInvalid
= DS
.SetTypeSpecType(DeclSpec::TST_void
, Loc
, PrevSpec
);
503 isInvalid
= DS
.SetTypeSpecType(DeclSpec::TST_char
, Loc
, PrevSpec
);
506 isInvalid
= DS
.SetTypeSpecType(DeclSpec::TST_int
, Loc
, PrevSpec
);
509 isInvalid
= DS
.SetTypeSpecType(DeclSpec::TST_float
, Loc
, PrevSpec
);
512 isInvalid
= DS
.SetTypeSpecType(DeclSpec::TST_double
, Loc
, PrevSpec
);
514 case tok::kw_bool
: // [C++ 2.11p1]
516 isInvalid
= DS
.SetTypeSpecType(DeclSpec::TST_bool
, Loc
, PrevSpec
);
518 case tok::kw__Decimal32
:
519 isInvalid
= DS
.SetTypeSpecType(DeclSpec::TST_decimal32
, Loc
, PrevSpec
);
521 case tok::kw__Decimal64
:
522 isInvalid
= DS
.SetTypeSpecType(DeclSpec::TST_decimal64
, Loc
, PrevSpec
);
524 case tok::kw__Decimal128
:
525 isInvalid
= DS
.SetTypeSpecType(DeclSpec::TST_decimal128
, Loc
, PrevSpec
);
531 ParseClassSpecifier(DS
);
534 ParseEnumSpecifier(DS
);
537 // GNU typeof support.
539 ParseTypeofSpecifier(DS
);
544 isInvalid
= DS
.SetTypeQual(DeclSpec::TQ_const
, Loc
, PrevSpec
,
547 case tok::kw_volatile
:
548 isInvalid
= DS
.SetTypeQual(DeclSpec::TQ_volatile
, Loc
, PrevSpec
,
551 case tok::kw_restrict
:
552 isInvalid
= DS
.SetTypeQual(DeclSpec::TQ_restrict
, Loc
, PrevSpec
,
556 // function-specifier
558 isInvalid
= DS
.SetFunctionSpecInline(Loc
, PrevSpec
);
562 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
563 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
564 // but we support it.
565 if (DS
.hasTypeSpecifier() || !getLang().ObjC1
)
566 goto DoneWithDeclSpec
;
569 SourceLocation EndProtoLoc
;
570 llvm::SmallVector
<DeclTy
*, 8> ProtocolDecl
;
571 ParseObjCProtocolReferences(ProtocolDecl
, false, EndProtoLoc
);
572 DS
.setProtocolQualifiers(&ProtocolDecl
[0], ProtocolDecl
.size());
573 DS
.SetRangeEnd(EndProtoLoc
);
575 Diag(Loc
, diag::warn_objc_protocol_qualifier_missing_id
,
576 SourceRange(Loc
, EndProtoLoc
));
577 // Do not allow any other declspecs after the protocol qualifier list
578 // "<foo,bar>short" is not allowed.
579 goto DoneWithDeclSpec
;
582 // If the specifier combination wasn't legal, issue a diagnostic.
584 assert(PrevSpec
&& "Method did not return previous specifier!");
585 if (isInvalid
== 1) // Error.
586 Diag(Tok
, diag::err_invalid_decl_spec_combination
, PrevSpec
);
588 Diag(Tok
, diag::ext_duplicate_declspec
, PrevSpec
);
590 DS
.SetRangeEnd(Tok
.getLocation());
595 /// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
596 /// the first token has already been read and has been turned into an instance
597 /// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
598 /// otherwise it returns false and fills in Decl.
599 bool Parser::ParseTag(DeclTy
*&Decl
, unsigned TagType
, SourceLocation StartLoc
){
600 AttributeList
*Attr
= 0;
601 // If attributes exist after tag, parse them.
602 if (Tok
.is(tok::kw___attribute
))
603 Attr
= ParseAttributes();
605 // Must have either 'struct name' or 'struct {...}'.
606 if (Tok
.isNot(tok::identifier
) && Tok
.isNot(tok::l_brace
)) {
607 Diag(Tok
, diag::err_expected_ident_lbrace
);
609 // Skip the rest of this declarator, up until the comma or semicolon.
610 SkipUntil(tok::comma
, true);
614 // If an identifier is present, consume and remember it.
615 IdentifierInfo
*Name
= 0;
616 SourceLocation NameLoc
;
617 if (Tok
.is(tok::identifier
)) {
618 Name
= Tok
.getIdentifierInfo();
619 NameLoc
= ConsumeToken();
622 // There are three options here. If we have 'struct foo;', then this is a
623 // forward declaration. If we have 'struct foo {...' then this is a
624 // definition. Otherwise we have something like 'struct foo xyz', a reference.
626 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
627 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
628 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
631 if (Tok
.is(tok::l_brace
))
632 TK
= Action::TK_Definition
;
633 else if (Tok
.is(tok::semi
))
634 TK
= Action::TK_Declaration
;
636 TK
= Action::TK_Reference
;
637 Decl
= Actions
.ActOnTag(CurScope
, TagType
, TK
, StartLoc
, Name
, NameLoc
, Attr
);
641 /// ParseStructDeclaration - Parse a struct declaration without the terminating
644 /// struct-declaration:
645 /// specifier-qualifier-list struct-declarator-list
646 /// [GNU] __extension__ struct-declaration
647 /// [GNU] specifier-qualifier-list
648 /// struct-declarator-list:
649 /// struct-declarator
650 /// struct-declarator-list ',' struct-declarator
651 /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
652 /// struct-declarator:
654 /// [GNU] declarator attributes[opt]
655 /// declarator[opt] ':' constant-expression
656 /// [GNU] declarator[opt] ':' constant-expression attributes[opt]
659 ParseStructDeclaration(DeclSpec
&DS
,
660 llvm::SmallVectorImpl
<FieldDeclarator
> &Fields
) {
661 // FIXME: When __extension__ is specified, disable extension diagnostics.
662 while (Tok
.is(tok::kw___extension__
))
665 // Parse the common specifier-qualifiers-list piece.
666 SourceLocation DSStart
= Tok
.getLocation();
667 ParseSpecifierQualifierList(DS
);
668 // TODO: Does specifier-qualifier list correctly check that *something* is
671 // If there are no declarators, issue a warning.
672 if (Tok
.is(tok::semi
)) {
673 Diag(DSStart
, diag::w_no_declarators
);
677 // Read struct-declarators until we find the semicolon.
678 Fields
.push_back(FieldDeclarator(DS
));
680 FieldDeclarator
&DeclaratorInfo
= Fields
.back();
682 /// struct-declarator: declarator
683 /// struct-declarator: declarator[opt] ':' constant-expression
684 if (Tok
.isNot(tok::colon
))
685 ParseDeclarator(DeclaratorInfo
.D
);
687 if (Tok
.is(tok::colon
)) {
689 ExprResult Res
= ParseConstantExpression();
691 SkipUntil(tok::semi
, true, true);
693 DeclaratorInfo
.BitfieldSize
= Res
.Val
;
696 // If attributes exist after the declarator, parse them.
697 if (Tok
.is(tok::kw___attribute
))
698 DeclaratorInfo
.D
.AddAttributes(ParseAttributes());
700 // If we don't have a comma, it is either the end of the list (a ';')
701 // or an error, bail out.
702 if (Tok
.isNot(tok::comma
))
705 // Consume the comma.
708 // Parse the next declarator.
709 Fields
.push_back(FieldDeclarator(DS
));
711 // Attributes are only allowed on the second declarator.
712 if (Tok
.is(tok::kw___attribute
))
713 Fields
.back().D
.AddAttributes(ParseAttributes());
717 /// ParseStructUnionBody
719 /// struct-declaration-list
721 /// [GNU] "struct-declaration-list" without terminatoring ';'
722 /// struct-declaration-list:
723 /// struct-declaration
724 /// struct-declaration-list struct-declaration
725 /// [OBC] '@' 'defs' '(' class-name ')'
727 void Parser::ParseStructUnionBody(SourceLocation RecordLoc
,
728 unsigned TagType
, DeclTy
*TagDecl
) {
729 SourceLocation LBraceLoc
= ConsumeBrace();
731 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
733 if (Tok
.is(tok::r_brace
) && !getLang().CPlusPlus
)
734 Diag(Tok
, diag::ext_empty_struct_union_enum
,
735 DeclSpec::getSpecifierName((DeclSpec::TST
)TagType
));
737 llvm::SmallVector
<DeclTy
*, 32> FieldDecls
;
738 llvm::SmallVector
<FieldDeclarator
, 8> FieldDeclarators
;
740 // While we still have something to read, read the declarations in the struct.
741 while (Tok
.isNot(tok::r_brace
) && Tok
.isNot(tok::eof
)) {
742 // Each iteration of this loop reads one struct-declaration.
744 // Check for extraneous top-level semicolon.
745 if (Tok
.is(tok::semi
)) {
746 Diag(Tok
, diag::ext_extra_struct_semi
);
751 // Parse all the comma separated declarators.
753 FieldDeclarators
.clear();
754 if (!Tok
.is(tok::at
)) {
755 ParseStructDeclaration(DS
, FieldDeclarators
);
757 // Convert them all to fields.
758 for (unsigned i
= 0, e
= FieldDeclarators
.size(); i
!= e
; ++i
) {
759 FieldDeclarator
&FD
= FieldDeclarators
[i
];
760 // Install the declarator into the current TagDecl.
761 DeclTy
*Field
= Actions
.ActOnField(CurScope
,
762 DS
.getSourceRange().getBegin(),
763 FD
.D
, FD
.BitfieldSize
);
764 FieldDecls
.push_back(Field
);
766 } else { // Handle @defs
768 if (!Tok
.isObjCAtKeyword(tok::objc_defs
)) {
769 Diag(Tok
, diag::err_unexpected_at
);
770 SkipUntil(tok::semi
, true, true);
774 ExpectAndConsume(tok::l_paren
, diag::err_expected_lparen
);
775 if (!Tok
.is(tok::identifier
)) {
776 Diag(Tok
, diag::err_expected_ident
);
777 SkipUntil(tok::semi
, true, true);
780 llvm::SmallVector
<DeclTy
*, 16> Fields
;
781 Actions
.ActOnDefs(CurScope
, Tok
.getLocation(), Tok
.getIdentifierInfo(),
783 FieldDecls
.insert(FieldDecls
.end(), Fields
.begin(), Fields
.end());
785 ExpectAndConsume(tok::r_paren
, diag::err_expected_rparen
);
788 if (Tok
.is(tok::semi
)) {
790 } else if (Tok
.is(tok::r_brace
)) {
791 Diag(Tok
.getLocation(), diag::ext_expected_semi_decl_list
);
794 Diag(Tok
, diag::err_expected_semi_decl_list
);
795 // Skip to end of block or statement
796 SkipUntil(tok::r_brace
, true, true);
800 SourceLocation RBraceLoc
= MatchRHSPunctuation(tok::r_brace
, LBraceLoc
);
802 Actions
.ActOnFields(CurScope
,
803 RecordLoc
,TagDecl
,&FieldDecls
[0],FieldDecls
.size(),
804 LBraceLoc
, RBraceLoc
);
806 AttributeList
*AttrList
= 0;
807 // If attributes exist after struct contents, parse them.
808 if (Tok
.is(tok::kw___attribute
))
809 AttrList
= ParseAttributes(); // FIXME: where should I put them?
813 /// ParseEnumSpecifier
814 /// enum-specifier: [C99 6.7.2.2]
815 /// 'enum' identifier[opt] '{' enumerator-list '}'
816 /// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
817 /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
818 /// '}' attributes[opt]
819 /// 'enum' identifier
820 /// [GNU] 'enum' attributes[opt] identifier
821 void Parser::ParseEnumSpecifier(DeclSpec
&DS
) {
822 assert(Tok
.is(tok::kw_enum
) && "Not an enum specifier");
823 SourceLocation StartLoc
= ConsumeToken();
825 // Parse the tag portion of this.
827 if (ParseTag(TagDecl
, DeclSpec::TST_enum
, StartLoc
))
830 if (Tok
.is(tok::l_brace
))
831 ParseEnumBody(StartLoc
, TagDecl
);
833 // TODO: semantic analysis on the declspec for enums.
834 const char *PrevSpec
= 0;
835 if (DS
.SetTypeSpecType(DeclSpec::TST_enum
, StartLoc
, PrevSpec
, TagDecl
))
836 Diag(StartLoc
, diag::err_invalid_decl_spec_combination
, PrevSpec
);
839 /// ParseEnumBody - Parse a {} enclosed enumerator-list.
842 /// enumerator-list ',' enumerator
844 /// enumeration-constant
845 /// enumeration-constant '=' constant-expression
846 /// enumeration-constant:
849 void Parser::ParseEnumBody(SourceLocation StartLoc
, DeclTy
*EnumDecl
) {
850 SourceLocation LBraceLoc
= ConsumeBrace();
852 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
853 if (Tok
.is(tok::r_brace
) && !getLang().CPlusPlus
)
854 Diag(Tok
, diag::ext_empty_struct_union_enum
, "enum");
856 llvm::SmallVector
<DeclTy
*, 32> EnumConstantDecls
;
858 DeclTy
*LastEnumConstDecl
= 0;
860 // Parse the enumerator-list.
861 while (Tok
.is(tok::identifier
)) {
862 IdentifierInfo
*Ident
= Tok
.getIdentifierInfo();
863 SourceLocation IdentLoc
= ConsumeToken();
865 SourceLocation EqualLoc
;
866 ExprTy
*AssignedVal
= 0;
867 if (Tok
.is(tok::equal
)) {
868 EqualLoc
= ConsumeToken();
869 ExprResult Res
= ParseConstantExpression();
871 SkipUntil(tok::comma
, tok::r_brace
, true, true);
873 AssignedVal
= Res
.Val
;
876 // Install the enumerator constant into EnumDecl.
877 DeclTy
*EnumConstDecl
= Actions
.ActOnEnumConstant(CurScope
, EnumDecl
,
880 EqualLoc
, AssignedVal
);
881 EnumConstantDecls
.push_back(EnumConstDecl
);
882 LastEnumConstDecl
= EnumConstDecl
;
884 if (Tok
.isNot(tok::comma
))
886 SourceLocation CommaLoc
= ConsumeToken();
888 if (Tok
.isNot(tok::identifier
) && !getLang().C99
)
889 Diag(CommaLoc
, diag::ext_c99_enumerator_list_comma
);
893 MatchRHSPunctuation(tok::r_brace
, LBraceLoc
);
895 Actions
.ActOnEnumBody(StartLoc
, EnumDecl
, &EnumConstantDecls
[0],
896 EnumConstantDecls
.size());
898 DeclTy
*AttrList
= 0;
899 // If attributes exist after the identifier list, parse them.
900 if (Tok
.is(tok::kw___attribute
))
901 AttrList
= ParseAttributes(); // FIXME: where do they do?
904 /// isTypeSpecifierQualifier - Return true if the current token could be the
905 /// start of a type-qualifier-list.
906 bool Parser::isTypeQualifier() const {
907 switch (Tok
.getKind()) {
908 default: return false;
911 case tok::kw_volatile
:
912 case tok::kw_restrict
:
917 /// isTypeSpecifierQualifier - Return true if the current token could be the
918 /// start of a specifier-qualifier-list.
919 bool Parser::isTypeSpecifierQualifier() const {
920 switch (Tok
.getKind()) {
921 default: return false;
922 // GNU attributes support.
923 case tok::kw___attribute
:
924 // GNU typeof support.
926 // GNU bizarre protocol extension. FIXME: make an extension?
933 case tok::kw_unsigned
:
934 case tok::kw__Complex
:
935 case tok::kw__Imaginary
:
943 case tok::kw__Decimal32
:
944 case tok::kw__Decimal64
:
945 case tok::kw__Decimal128
:
947 // struct-or-union-specifier (C99) or class-specifier (C++)
956 case tok::kw_volatile
:
957 case tok::kw_restrict
:
961 case tok::identifier
:
962 return Actions
.isTypeName(*Tok
.getIdentifierInfo(), CurScope
) != 0;
966 /// isDeclarationSpecifier() - Return true if the current token is part of a
967 /// declaration specifier.
968 bool Parser::isDeclarationSpecifier() const {
969 switch (Tok
.getKind()) {
970 default: return false;
971 // storage-class-specifier
972 case tok::kw_typedef
:
974 case tok::kw___private_extern__
:
977 case tok::kw_register
:
978 case tok::kw___thread
:
984 case tok::kw_unsigned
:
985 case tok::kw__Complex
:
986 case tok::kw__Imaginary
:
994 case tok::kw__Decimal32
:
995 case tok::kw__Decimal64
:
996 case tok::kw__Decimal128
:
998 // struct-or-union-specifier (C99) or class-specifier (C++)
1000 case tok::kw_struct
:
1007 case tok::kw_volatile
:
1008 case tok::kw_restrict
:
1010 // function-specifier
1011 case tok::kw_inline
:
1013 // GNU typeof support.
1014 case tok::kw_typeof
:
1017 case tok::kw___attribute
:
1020 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1022 return getLang().ObjC1
;
1025 case tok::identifier
:
1026 return Actions
.isTypeName(*Tok
.getIdentifierInfo(), CurScope
) != 0;
1031 /// ParseTypeQualifierListOpt
1032 /// type-qualifier-list: [C99 6.7.5]
1034 /// [GNU] attributes
1035 /// type-qualifier-list type-qualifier
1036 /// [GNU] type-qualifier-list attributes
1038 void Parser::ParseTypeQualifierListOpt(DeclSpec
&DS
) {
1040 int isInvalid
= false;
1041 const char *PrevSpec
= 0;
1042 SourceLocation Loc
= Tok
.getLocation();
1044 switch (Tok
.getKind()) {
1046 // If this is not a type-qualifier token, we're done reading type
1047 // qualifiers. First verify that DeclSpec's are consistent.
1048 DS
.Finish(Diags
, PP
.getSourceManager(), getLang());
1051 isInvalid
= DS
.SetTypeQual(DeclSpec::TQ_const
, Loc
, PrevSpec
,
1054 case tok::kw_volatile
:
1055 isInvalid
= DS
.SetTypeQual(DeclSpec::TQ_volatile
, Loc
, PrevSpec
,
1058 case tok::kw_restrict
:
1059 isInvalid
= DS
.SetTypeQual(DeclSpec::TQ_restrict
, Loc
, PrevSpec
,
1062 case tok::kw___attribute
:
1063 DS
.AddAttributes(ParseAttributes());
1064 continue; // do *not* consume the next token!
1067 // If the specifier combination wasn't legal, issue a diagnostic.
1069 assert(PrevSpec
&& "Method did not return previous specifier!");
1070 if (isInvalid
== 1) // Error.
1071 Diag(Tok
, diag::err_invalid_decl_spec_combination
, PrevSpec
);
1073 Diag(Tok
, diag::ext_duplicate_declspec
, PrevSpec
);
1080 /// ParseDeclarator - Parse and verify a newly-initialized declarator.
1082 void Parser::ParseDeclarator(Declarator
&D
) {
1083 /// This implements the 'declarator' production in the C grammar, then checks
1084 /// for well-formedness and issues diagnostics.
1085 ParseDeclaratorInternal(D
);
1088 /// ParseDeclaratorInternal
1089 /// declarator: [C99 6.7.5]
1090 /// pointer[opt] direct-declarator
1091 /// [C++] '&' declarator [C++ 8p4, dcl.decl]
1092 /// [GNU] '&' restrict[opt] attributes[opt] declarator
1094 /// pointer: [C99 6.7.5]
1095 /// '*' type-qualifier-list[opt]
1096 /// '*' type-qualifier-list[opt] pointer
1098 void Parser::ParseDeclaratorInternal(Declarator
&D
) {
1099 tok::TokenKind Kind
= Tok
.getKind();
1101 // Not a pointer or C++ reference.
1102 if (Kind
!= tok::star
&& (Kind
!= tok::amp
|| !getLang().CPlusPlus
))
1103 return ParseDirectDeclarator(D
);
1105 // Otherwise, '*' -> pointer or '&' -> reference.
1106 SourceLocation Loc
= ConsumeToken(); // Eat the * or &.
1108 if (Kind
== tok::star
) {
1112 ParseTypeQualifierListOpt(DS
);
1114 // Recursively parse the declarator.
1115 ParseDeclaratorInternal(D
);
1117 // Remember that we parsed a pointer type, and remember the type-quals.
1118 D
.AddTypeInfo(DeclaratorChunk::getPointer(DS
.getTypeQualifiers(), Loc
,
1119 DS
.TakeAttributes()));
1124 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1125 // cv-qualifiers are introduced through the use of a typedef or of a
1126 // template type argument, in which case the cv-qualifiers are ignored.
1128 // [GNU] Retricted references are allowed.
1129 // [GNU] Attributes on references are allowed.
1130 ParseTypeQualifierListOpt(DS
);
1132 if (DS
.getTypeQualifiers() != DeclSpec::TQ_unspecified
) {
1133 if (DS
.getTypeQualifiers() & DeclSpec::TQ_const
)
1134 Diag(DS
.getConstSpecLoc(),
1135 diag::err_invalid_reference_qualifier_application
,
1137 if (DS
.getTypeQualifiers() & DeclSpec::TQ_volatile
)
1138 Diag(DS
.getVolatileSpecLoc(),
1139 diag::err_invalid_reference_qualifier_application
,
1143 // Recursively parse the declarator.
1144 ParseDeclaratorInternal(D
);
1146 // Remember that we parsed a reference type. It doesn't have type-quals.
1147 D
.AddTypeInfo(DeclaratorChunk::getReference(DS
.getTypeQualifiers(), Loc
,
1148 DS
.TakeAttributes()));
1152 /// ParseDirectDeclarator
1153 /// direct-declarator: [C99 6.7.5]
1155 /// '(' declarator ')'
1156 /// [GNU] '(' attributes declarator ')'
1157 /// [C90] direct-declarator '[' constant-expression[opt] ']'
1158 /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1159 /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1160 /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1161 /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1162 /// direct-declarator '(' parameter-type-list ')'
1163 /// direct-declarator '(' identifier-list[opt] ')'
1164 /// [GNU] direct-declarator '(' parameter-forward-declarations
1165 /// parameter-type-list[opt] ')'
1167 void Parser::ParseDirectDeclarator(Declarator
&D
) {
1168 // Parse the first direct-declarator seen.
1169 if (Tok
.is(tok::identifier
) && D
.mayHaveIdentifier()) {
1170 assert(Tok
.getIdentifierInfo() && "Not an identifier?");
1171 D
.SetIdentifier(Tok
.getIdentifierInfo(), Tok
.getLocation());
1173 } else if (Tok
.is(tok::l_paren
)) {
1174 // direct-declarator: '(' declarator ')'
1175 // direct-declarator: '(' attributes declarator ')'
1176 // Example: 'char (*X)' or 'int (*XX)(void)'
1177 ParseParenDeclarator(D
);
1178 } else if (D
.mayOmitIdentifier()) {
1179 // This could be something simple like "int" (in which case the declarator
1180 // portion is empty), if an abstract-declarator is allowed.
1181 D
.SetIdentifier(0, Tok
.getLocation());
1183 // Expected identifier or '('.
1184 Diag(Tok
, diag::err_expected_ident_lparen
);
1185 D
.SetIdentifier(0, Tok
.getLocation());
1188 assert(D
.isPastIdentifier() &&
1189 "Haven't past the location of the identifier yet?");
1192 if (Tok
.is(tok::l_paren
)) {
1193 ParseFunctionDeclarator(ConsumeParen(), D
);
1194 } else if (Tok
.is(tok::l_square
)) {
1195 ParseBracketDeclarator(D
);
1202 /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1203 /// only called before the identifier, so these are most likely just grouping
1204 /// parens for precedence. If we find that these are actually function
1205 /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1207 /// direct-declarator:
1208 /// '(' declarator ')'
1209 /// [GNU] '(' attributes declarator ')'
1211 void Parser::ParseParenDeclarator(Declarator
&D
) {
1212 SourceLocation StartLoc
= ConsumeParen();
1213 assert(!D
.isPastIdentifier() && "Should be called before passing identifier");
1215 // If we haven't past the identifier yet (or where the identifier would be
1216 // stored, if this is an abstract declarator), then this is probably just
1217 // grouping parens. However, if this could be an abstract-declarator, then
1218 // this could also be the start of function arguments (consider 'void()').
1221 if (!D
.mayOmitIdentifier()) {
1222 // If this can't be an abstract-declarator, this *must* be a grouping
1223 // paren, because we haven't seen the identifier yet.
1225 } else if (Tok
.is(tok::r_paren
) || // 'int()' is a function.
1226 isDeclarationSpecifier()) { // 'int(int)' is a function.
1227 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1228 // considered to be a type, not a K&R identifier-list.
1231 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1235 // If this is a grouping paren, handle:
1236 // direct-declarator: '(' declarator ')'
1237 // direct-declarator: '(' attributes declarator ')'
1239 if (Tok
.is(tok::kw___attribute
))
1240 D
.AddAttributes(ParseAttributes());
1242 ParseDeclaratorInternal(D
);
1244 MatchRHSPunctuation(tok::r_paren
, StartLoc
);
1248 // Okay, if this wasn't a grouping paren, it must be the start of a function
1249 // argument list. Recognize that this declarator will never have an
1250 // identifier (and remember where it would have been), then fall through to
1251 // the handling of argument lists.
1252 D
.SetIdentifier(0, Tok
.getLocation());
1254 ParseFunctionDeclarator(StartLoc
, D
);
1257 /// ParseFunctionDeclarator - We are after the identifier and have parsed the
1258 /// declarator D up to a paren, which indicates that we are parsing function
1261 /// This method also handles this portion of the grammar:
1262 /// parameter-type-list: [C99 6.7.5]
1264 /// parameter-list ',' '...'
1266 /// parameter-list: [C99 6.7.5]
1267 /// parameter-declaration
1268 /// parameter-list ',' parameter-declaration
1270 /// parameter-declaration: [C99 6.7.5]
1271 /// declaration-specifiers declarator
1272 /// [C++] declaration-specifiers declarator '=' assignment-expression
1273 /// [GNU] declaration-specifiers declarator attributes
1274 /// declaration-specifiers abstract-declarator[opt]
1275 /// [C++] declaration-specifiers abstract-declarator[opt]
1276 /// '=' assignment-expression
1277 /// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1279 void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc
, Declarator
&D
) {
1280 // lparen is already consumed!
1281 assert(D
.isPastIdentifier() && "Should not call before identifier!");
1283 // Okay, this is the parameter list of a function definition, or it is an
1284 // identifier list of a K&R-style function.
1286 if (Tok
.is(tok::r_paren
)) {
1287 // Remember that we parsed a function type, and remember the attributes.
1288 // int() -> no prototype, no '...'.
1289 D
.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1291 /*arglist*/ 0, 0, LParenLoc
));
1293 ConsumeParen(); // Eat the closing ')'.
1295 } else if (Tok
.is(tok::identifier
) &&
1296 // K&R identifier lists can't have typedefs as identifiers, per
1298 !Actions
.isTypeName(*Tok
.getIdentifierInfo(), CurScope
)) {
1299 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1300 // normal declarators, not for abstract-declarators.
1301 return ParseFunctionDeclaratorIdentifierList(LParenLoc
, D
);
1304 // Finally, a normal, non-empty parameter type list.
1306 // Build up an array of information about the parsed arguments.
1307 llvm::SmallVector
<DeclaratorChunk::ParamInfo
, 16> ParamInfo
;
1309 // Enter function-declaration scope, limiting any declarators to the
1310 // function prototype scope, including parameter declarators.
1311 EnterScope(Scope::FnScope
|Scope::DeclScope
);
1313 bool IsVariadic
= false;
1315 if (Tok
.is(tok::ellipsis
)) {
1318 // Check to see if this is "void(...)" which is not allowed.
1319 if (ParamInfo
.empty()) {
1320 // Otherwise, parse parameter type list. If it starts with an
1321 // ellipsis, diagnose the malformed function.
1322 Diag(Tok
, diag::err_ellipsis_first_arg
);
1323 IsVariadic
= false; // Treat this like 'void()'.
1326 ConsumeToken(); // Consume the ellipsis.
1330 SourceLocation DSStart
= Tok
.getLocation();
1332 // Parse the declaration-specifiers.
1334 ParseDeclarationSpecifiers(DS
);
1336 // Parse the declarator. This is "PrototypeContext", because we must
1337 // accept either 'declarator' or 'abstract-declarator' here.
1338 Declarator
ParmDecl(DS
, Declarator::PrototypeContext
);
1339 ParseDeclarator(ParmDecl
);
1341 // Parse GNU attributes, if present.
1342 if (Tok
.is(tok::kw___attribute
))
1343 ParmDecl
.AddAttributes(ParseAttributes());
1345 // Remember this parsed parameter in ParamInfo.
1346 IdentifierInfo
*ParmII
= ParmDecl
.getIdentifier();
1348 // If no parameter was specified, verify that *something* was specified,
1349 // otherwise we have a missing type and identifier.
1350 if (DS
.getParsedSpecifiers() == DeclSpec::PQ_None
&&
1351 ParmDecl
.getIdentifier() == 0 && ParmDecl
.getNumTypeObjects() == 0) {
1352 // Completely missing, emit error.
1353 Diag(DSStart
, diag::err_missing_param
);
1355 // Otherwise, we have something. Add it and let semantic analysis try
1356 // to grok it and add the result to the ParamInfo we are building.
1358 // Inform the actions module about the parameter declarator, so it gets
1359 // added to the current scope.
1360 DeclTy
*Param
= Actions
.ActOnParamDeclarator(CurScope
, ParmDecl
);
1362 // Parse the default argument, if any. We parse the default
1363 // arguments in all dialects; the semantic analysis in
1364 // ActOnParamDefaultArgument will reject the default argument in
1366 if (Tok
.is(tok::equal
)) {
1367 SourceLocation EqualLoc
= Tok
.getLocation();
1372 // Parse the default argument
1373 ExprResult DefArgResult
= ParseAssignmentExpression();
1374 if (DefArgResult
.isInvalid
) {
1375 SkipUntil(tok::comma
, tok::r_paren
, true, true);
1377 // Inform the actions module about the default argument
1378 Actions
.ActOnParamDefaultArgument(Param
, EqualLoc
, DefArgResult
.Val
);
1382 ParamInfo
.push_back(DeclaratorChunk::ParamInfo(ParmII
,
1383 ParmDecl
.getIdentifierLoc(), Param
));
1386 // If the next token is a comma, consume it and keep reading arguments.
1387 if (Tok
.isNot(tok::comma
)) break;
1389 // Consume the comma.
1393 // Leave prototype scope.
1396 // Remember that we parsed a function type, and remember the attributes.
1397 D
.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic
,
1398 &ParamInfo
[0], ParamInfo
.size(),
1401 // If we have the closing ')', eat it and we're done.
1402 MatchRHSPunctuation(tok::r_paren
, LParenLoc
);
1405 /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1406 /// we found a K&R-style identifier list instead of a type argument list. The
1407 /// current token is known to be the first identifier in the list.
1409 /// identifier-list: [C99 6.7.5]
1411 /// identifier-list ',' identifier
1413 void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc
,
1415 // Build up an array of information about the parsed arguments.
1416 llvm::SmallVector
<DeclaratorChunk::ParamInfo
, 16> ParamInfo
;
1417 llvm::SmallSet
<const IdentifierInfo
*, 16> ParamsSoFar
;
1419 // If there was no identifier specified for the declarator, either we are in
1420 // an abstract-declarator, or we are in a parameter declarator which was found
1421 // to be abstract. In abstract-declarators, identifier lists are not valid:
1423 if (!D
.getIdentifier())
1424 Diag(Tok
, diag::ext_ident_list_in_param
);
1426 // Tok is known to be the first identifier in the list. Remember this
1427 // identifier in ParamInfo.
1428 ParamsSoFar
.insert(Tok
.getIdentifierInfo());
1429 ParamInfo
.push_back(DeclaratorChunk::ParamInfo(Tok
.getIdentifierInfo(),
1430 Tok
.getLocation(), 0));
1432 ConsumeToken(); // eat the first identifier.
1434 while (Tok
.is(tok::comma
)) {
1438 // If this isn't an identifier, report the error and skip until ')'.
1439 if (Tok
.isNot(tok::identifier
)) {
1440 Diag(Tok
, diag::err_expected_ident
);
1441 SkipUntil(tok::r_paren
);
1445 IdentifierInfo
*ParmII
= Tok
.getIdentifierInfo();
1447 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1448 if (Actions
.isTypeName(*ParmII
, CurScope
))
1449 Diag(Tok
, diag::err_unexpected_typedef_ident
, ParmII
->getName());
1451 // Verify that the argument identifier has not already been mentioned.
1452 if (!ParamsSoFar
.insert(ParmII
)) {
1453 Diag(Tok
.getLocation(), diag::err_param_redefinition
, ParmII
->getName());
1455 // Remember this identifier in ParamInfo.
1456 ParamInfo
.push_back(DeclaratorChunk::ParamInfo(ParmII
,
1457 Tok
.getLocation(), 0));
1460 // Eat the identifier.
1464 // Remember that we parsed a function type, and remember the attributes. This
1465 // function type is always a K&R style function type, which is not varargs and
1466 // has no prototype.
1467 D
.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1468 &ParamInfo
[0], ParamInfo
.size(),
1471 // If we have the closing ')', eat it and we're done.
1472 MatchRHSPunctuation(tok::r_paren
, LParenLoc
);
1475 /// [C90] direct-declarator '[' constant-expression[opt] ']'
1476 /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1477 /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1478 /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1479 /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1480 void Parser::ParseBracketDeclarator(Declarator
&D
) {
1481 SourceLocation StartLoc
= ConsumeBracket();
1483 // If valid, this location is the position where we read the 'static' keyword.
1484 SourceLocation StaticLoc
;
1485 if (Tok
.is(tok::kw_static
))
1486 StaticLoc
= ConsumeToken();
1488 // If there is a type-qualifier-list, read it now.
1490 ParseTypeQualifierListOpt(DS
);
1492 // If we haven't already read 'static', check to see if there is one after the
1493 // type-qualifier-list.
1494 if (!StaticLoc
.isValid() && Tok
.is(tok::kw_static
))
1495 StaticLoc
= ConsumeToken();
1497 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1498 bool isStar
= false;
1499 ExprResult
NumElements(false);
1501 // Handle the case where we have '[*]' as the array size. However, a leading
1502 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1503 // the the token after the star is a ']'. Since stars in arrays are
1504 // infrequent, use of lookahead is not costly here.
1505 if (Tok
.is(tok::star
) && GetLookAheadToken(1).is(tok::r_square
)) {
1506 ConsumeToken(); // Eat the '*'.
1508 if (StaticLoc
.isValid())
1509 Diag(StaticLoc
, diag::err_unspecified_vla_size_with_static
);
1510 StaticLoc
= SourceLocation(); // Drop the static.
1512 } else if (Tok
.isNot(tok::r_square
)) {
1513 // Parse the assignment-expression now.
1514 NumElements
= ParseAssignmentExpression();
1517 // If there was an error parsing the assignment-expression, recover.
1518 if (NumElements
.isInvalid
) {
1519 // If the expression was invalid, skip it.
1520 SkipUntil(tok::r_square
);
1524 MatchRHSPunctuation(tok::r_square
, StartLoc
);
1526 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1527 // it was not a constant expression.
1528 if (!getLang().C99
) {
1529 // TODO: check C90 array constant exprness.
1530 if (isStar
|| StaticLoc
.isValid() ||
1531 0/*TODO: NumElts is not a C90 constantexpr */)
1532 Diag(StartLoc
, diag::ext_c99_array_usage
);
1535 // Remember that we parsed a pointer type, and remember the type-quals.
1536 D
.AddTypeInfo(DeclaratorChunk::getArray(DS
.getTypeQualifiers(),
1537 StaticLoc
.isValid(), isStar
,
1538 NumElements
.Val
, StartLoc
));
1541 /// [GNU] typeof-specifier:
1542 /// typeof ( expressions )
1543 /// typeof ( type-name )
1545 void Parser::ParseTypeofSpecifier(DeclSpec
&DS
) {
1546 assert(Tok
.is(tok::kw_typeof
) && "Not a typeof specifier");
1547 const IdentifierInfo
*BuiltinII
= Tok
.getIdentifierInfo();
1548 SourceLocation StartLoc
= ConsumeToken();
1550 if (Tok
.isNot(tok::l_paren
)) {
1551 Diag(Tok
, diag::err_expected_lparen_after
, BuiltinII
->getName());
1554 SourceLocation LParenLoc
= ConsumeParen(), RParenLoc
;
1556 if (isTypeSpecifierQualifier()) {
1557 TypeTy
*Ty
= ParseTypeName();
1559 assert(Ty
&& "Parser::ParseTypeofSpecifier(): missing type");
1561 if (Tok
.isNot(tok::r_paren
)) {
1562 MatchRHSPunctuation(tok::r_paren
, LParenLoc
);
1565 RParenLoc
= ConsumeParen();
1566 const char *PrevSpec
= 0;
1567 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1568 if (DS
.SetTypeSpecType(DeclSpec::TST_typeofType
, StartLoc
, PrevSpec
, Ty
))
1569 Diag(StartLoc
, diag::err_invalid_decl_spec_combination
, PrevSpec
);
1570 } else { // we have an expression.
1571 ExprResult Result
= ParseExpression();
1573 if (Result
.isInvalid
|| Tok
.isNot(tok::r_paren
)) {
1574 MatchRHSPunctuation(tok::r_paren
, LParenLoc
);
1577 RParenLoc
= ConsumeParen();
1578 const char *PrevSpec
= 0;
1579 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1580 if (DS
.SetTypeSpecType(DeclSpec::TST_typeofExpr
, StartLoc
, PrevSpec
,
1582 Diag(StartLoc
, diag::err_invalid_decl_spec_combination
, PrevSpec
);