blob: 7856e62f04fead246dab4dfcdcffefbfcecd0525 [file] [log] [blame]
Shih-wei Liaof8fd82b2010-02-10 11:10:31 -08001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
15#include "clang/Parse/ParseDiagnostic.h"
16#include "clang/Parse/Scope.h"
17#include "clang/Parse/Template.h"
18#include "RAIIObjectsForParser.h"
19#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
29///
30/// Called type-id in C++.
31Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
32 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39 if (Range)
40 *Range = DeclaratorInfo.getSourceRange();
41
42 if (DeclaratorInfo.isInvalidType())
43 return true;
44
45 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
46}
47
48/// ParseGNUAttributes - Parse a non-empty attributes list.
49///
50/// [GNU] attributes:
51/// attribute
52/// attributes attribute
53///
54/// [GNU] attribute:
55/// '__attribute__' '(' '(' attribute-list ')' ')'
56///
57/// [GNU] attribute-list:
58/// attrib
59/// attribute_list ',' attrib
60///
61/// [GNU] attrib:
62/// empty
63/// attrib-name
64/// attrib-name '(' identifier ')'
65/// attrib-name '(' identifier ',' nonempty-expr-list ')'
66/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
67///
68/// [GNU] attrib-name:
69/// identifier
70/// typespec
71/// typequal
72/// storageclass
73///
74/// FIXME: The GCC grammar/code for this construct implies we need two
75/// token lookahead. Comment from gcc: "If they start with an identifier
76/// which is followed by a comma or close parenthesis, then the arguments
77/// start with that identifier; otherwise they are an expression list."
78///
79/// At the moment, I am not doing 2 token lookahead. I am also unaware of
80/// any attributes that don't work (based on my limited testing). Most
81/// attributes are very simple in practice. Until we find a bug, I don't see
82/// a pressing need to implement the 2 token lookahead.
83
84AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
85 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
86
87 AttributeList *CurrAttr = 0;
88
89 while (Tok.is(tok::kw___attribute)) {
90 ConsumeToken();
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
92 "attribute")) {
93 SkipUntil(tok::r_paren, true); // skip until ) or ;
94 return CurrAttr;
95 }
96 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
97 SkipUntil(tok::r_paren, true); // skip until ) or ;
98 return CurrAttr;
99 }
100 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
101 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
102 Tok.is(tok::comma)) {
103
104 if (Tok.is(tok::comma)) {
105 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
106 ConsumeToken();
107 continue;
108 }
109 // we have an identifier or declaration specifier (const, int, etc.)
110 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
111 SourceLocation AttrNameLoc = ConsumeToken();
112
113 // check if we have a "paramterized" attribute
114 if (Tok.is(tok::l_paren)) {
115 ConsumeParen(); // ignore the left paren loc for now
116
117 if (Tok.is(tok::identifier)) {
118 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
119 SourceLocation ParmLoc = ConsumeToken();
120
121 if (Tok.is(tok::r_paren)) {
122 // __attribute__(( mode(byte) ))
123 ConsumeParen(); // ignore the right paren loc for now
124 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
125 ParmName, ParmLoc, 0, 0, CurrAttr);
126 } else if (Tok.is(tok::comma)) {
127 ConsumeToken();
128 // __attribute__(( format(printf, 1, 2) ))
129 ExprVector ArgExprs(Actions);
130 bool ArgExprsOk = true;
131
132 // now parse the non-empty comma separated list of expressions
133 while (1) {
134 OwningExprResult ArgExpr(ParseAssignmentExpression());
135 if (ArgExpr.isInvalid()) {
136 ArgExprsOk = false;
137 SkipUntil(tok::r_paren);
138 break;
139 } else {
140 ArgExprs.push_back(ArgExpr.release());
141 }
142 if (Tok.isNot(tok::comma))
143 break;
144 ConsumeToken(); // Eat the comma, move to the next argument
145 }
146 if (ArgExprsOk && Tok.is(tok::r_paren)) {
147 ConsumeParen(); // ignore the right paren loc for now
148 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
149 AttrNameLoc, ParmName, ParmLoc,
150 ArgExprs.take(), ArgExprs.size(),
151 CurrAttr);
152 }
153 }
154 } else { // not an identifier
155 switch (Tok.getKind()) {
156 case tok::r_paren:
157 // parse a possibly empty comma separated list of expressions
158 // __attribute__(( nonnull() ))
159 ConsumeParen(); // ignore the right paren loc for now
160 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
161 0, SourceLocation(), 0, 0, CurrAttr);
162 break;
163 case tok::kw_char:
164 case tok::kw_wchar_t:
165 case tok::kw_char16_t:
166 case tok::kw_char32_t:
167 case tok::kw_bool:
168 case tok::kw_short:
169 case tok::kw_int:
170 case tok::kw_long:
171 case tok::kw_signed:
172 case tok::kw_unsigned:
173 case tok::kw_float:
174 case tok::kw_double:
175 case tok::kw_void:
176 case tok::kw_typeof:
177 // If it's a builtin type name, eat it and expect a rparen
178 // __attribute__(( vec_type_hint(char) ))
179 ConsumeToken();
180 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
181 0, SourceLocation(), 0, 0, CurrAttr);
182 if (Tok.is(tok::r_paren))
183 ConsumeParen();
184 break;
185 default:
186 // __attribute__(( aligned(16) ))
187 ExprVector ArgExprs(Actions);
188 bool ArgExprsOk = true;
189
190 // now parse the list of expressions
191 while (1) {
192 OwningExprResult ArgExpr(ParseAssignmentExpression());
193 if (ArgExpr.isInvalid()) {
194 ArgExprsOk = false;
195 SkipUntil(tok::r_paren);
196 break;
197 } else {
198 ArgExprs.push_back(ArgExpr.release());
199 }
200 if (Tok.isNot(tok::comma))
201 break;
202 ConsumeToken(); // Eat the comma, move to the next argument
203 }
204 // Match the ')'.
205 if (ArgExprsOk && Tok.is(tok::r_paren)) {
206 ConsumeParen(); // ignore the right paren loc for now
207 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
208 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
209 ArgExprs.size(),
210 CurrAttr);
211 }
212 break;
213 }
214 }
215 } else {
216 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
217 0, SourceLocation(), 0, 0, CurrAttr);
218 }
219 }
220 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
221 SkipUntil(tok::r_paren, false);
222 SourceLocation Loc = Tok.getLocation();
223 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
224 SkipUntil(tok::r_paren, false);
225 }
226 if (EndLoc)
227 *EndLoc = Loc;
228 }
229 return CurrAttr;
230}
231
232/// ParseMicrosoftDeclSpec - Parse an __declspec construct
233///
234/// [MS] decl-specifier:
235/// __declspec ( extended-decl-modifier-seq )
236///
237/// [MS] extended-decl-modifier-seq:
238/// extended-decl-modifier[opt]
239/// extended-decl-modifier extended-decl-modifier-seq
240
241AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
242 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
243
244 ConsumeToken();
245 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
246 "declspec")) {
247 SkipUntil(tok::r_paren, true); // skip until ) or ;
248 return CurrAttr;
249 }
250 while (Tok.getIdentifierInfo()) {
251 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
252 SourceLocation AttrNameLoc = ConsumeToken();
253 if (Tok.is(tok::l_paren)) {
254 ConsumeParen();
255 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
256 // correctly.
257 OwningExprResult ArgExpr(ParseAssignmentExpression());
258 if (!ArgExpr.isInvalid()) {
259 ExprTy* ExprList = ArgExpr.take();
260 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
261 SourceLocation(), &ExprList, 1,
262 CurrAttr, true);
263 }
264 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
265 SkipUntil(tok::r_paren, false);
266 } else {
267 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
268 0, SourceLocation(), 0, 0, CurrAttr, true);
269 }
270 }
271 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
272 SkipUntil(tok::r_paren, false);
273 return CurrAttr;
274}
275
276AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
277 // Treat these like attributes
278 // FIXME: Allow Sema to distinguish between these and real attributes!
279 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
280 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
281 Tok.is(tok::kw___w64)) {
282 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
283 SourceLocation AttrNameLoc = ConsumeToken();
284 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
285 // FIXME: Support these properly!
286 continue;
287 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
288 SourceLocation(), 0, 0, CurrAttr, true);
289 }
290 return CurrAttr;
291}
292
293/// ParseDeclaration - Parse a full 'declaration', which consists of
294/// declaration-specifiers, some number of declarators, and a semicolon.
295/// 'Context' should be a Declarator::TheContext value. This returns the
296/// location of the semicolon in DeclEnd.
297///
298/// declaration: [C99 6.7]
299/// block-declaration ->
300/// simple-declaration
301/// others [FIXME]
302/// [C++] template-declaration
303/// [C++] namespace-definition
304/// [C++] using-directive
305/// [C++] using-declaration
306/// [C++0x] static_assert-declaration
307/// others... [FIXME]
308///
309Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
310 SourceLocation &DeclEnd,
311 CXX0XAttributeList Attr) {
312 DeclPtrTy SingleDecl;
313 switch (Tok.getKind()) {
314 case tok::kw_template:
315 case tok::kw_export:
316 if (Attr.HasAttr)
317 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
318 << Attr.Range;
319 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
320 break;
321 case tok::kw_namespace:
322 if (Attr.HasAttr)
323 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
324 << Attr.Range;
325 SingleDecl = ParseNamespace(Context, DeclEnd);
326 break;
327 case tok::kw_using:
328 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
329 break;
330 case tok::kw_static_assert:
331 if (Attr.HasAttr)
332 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
333 << Attr.Range;
334 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
335 break;
336 default:
337 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList);
338 }
339
340 // This routine returns a DeclGroup, if the thing we parsed only contains a
341 // single decl, convert it now.
342 return Actions.ConvertDeclToDeclGroup(SingleDecl);
343}
344
345/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
346/// declaration-specifiers init-declarator-list[opt] ';'
347///[C90/C++]init-declarator-list ';' [TODO]
348/// [OMP] threadprivate-directive [TODO]
349///
350/// If RequireSemi is false, this does not check for a ';' at the end of the
351/// declaration.
352Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
353 SourceLocation &DeclEnd,
354 AttributeList *Attr) {
355 // Parse the common declaration-specifiers piece.
356 ParsingDeclSpec DS(*this);
357 if (Attr)
358 DS.AddAttributes(Attr);
359 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
360 getDeclSpecContextFromDeclaratorContext(Context));
361
362 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
363 // declaration-specifiers init-declarator-list[opt] ';'
364 if (Tok.is(tok::semi)) {
365 ConsumeToken();
366 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
367 DS.complete(TheDecl);
368 return Actions.ConvertDeclToDeclGroup(TheDecl);
369 }
370
371 DeclGroupPtrTy DG = ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false,
372 &DeclEnd);
373 return DG;
374}
375
376/// ParseDeclGroup - Having concluded that this is either a function
377/// definition or a group of object declarations, actually parse the
378/// result.
379Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
380 unsigned Context,
381 bool AllowFunctionDefinitions,
382 SourceLocation *DeclEnd) {
383 // Parse the first declarator.
384 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
385 ParseDeclarator(D);
386
387 // Bail out if the first declarator didn't seem well-formed.
388 if (!D.hasName() && !D.mayOmitIdentifier()) {
389 // Skip until ; or }.
390 SkipUntil(tok::r_brace, true, true);
391 if (Tok.is(tok::semi))
392 ConsumeToken();
393 return DeclGroupPtrTy();
394 }
395
396 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
397 if (isDeclarationAfterDeclarator()) {
398 // Fall though. We have to check this first, though, because
399 // __attribute__ might be the start of a function definition in
400 // (extended) K&R C.
401 } else if (isStartOfFunctionDefinition()) {
402 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
403 Diag(Tok, diag::err_function_declared_typedef);
404
405 // Recover by treating the 'typedef' as spurious.
406 DS.ClearStorageClassSpecs();
407 }
408
409 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
410 return Actions.ConvertDeclToDeclGroup(TheDecl);
411 } else {
412 Diag(Tok, diag::err_expected_fn_body);
413 SkipUntil(tok::semi);
414 return DeclGroupPtrTy();
415 }
416 }
417
418 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
419 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
420 D.complete(FirstDecl);
421 if (FirstDecl.get())
422 DeclsInGroup.push_back(FirstDecl);
423
424 // If we don't have a comma, it is either the end of the list (a ';') or an
425 // error, bail out.
426 while (Tok.is(tok::comma)) {
427 // Consume the comma.
428 ConsumeToken();
429
430 // Parse the next declarator.
431 D.clear();
432
433 // Accept attributes in an init-declarator. In the first declarator in a
434 // declaration, these would be part of the declspec. In subsequent
435 // declarators, they become part of the declarator itself, so that they
436 // don't apply to declarators after *this* one. Examples:
437 // short __attribute__((common)) var; -> declspec
438 // short var __attribute__((common)); -> declarator
439 // short x, __attribute__((common)) var; -> declarator
440 if (Tok.is(tok::kw___attribute)) {
441 SourceLocation Loc;
442 AttributeList *AttrList = ParseGNUAttributes(&Loc);
443 D.AddAttributes(AttrList, Loc);
444 }
445
446 ParseDeclarator(D);
447
448 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
449 D.complete(ThisDecl);
450 if (ThisDecl.get())
451 DeclsInGroup.push_back(ThisDecl);
452 }
453
454 if (DeclEnd)
455 *DeclEnd = Tok.getLocation();
456
457 if (Context != Declarator::ForContext &&
458 ExpectAndConsume(tok::semi,
459 Context == Declarator::FileContext
460 ? diag::err_invalid_token_after_toplevel_declarator
461 : diag::err_expected_semi_declaration)) {
462 SkipUntil(tok::r_brace, true, true);
463 if (Tok.is(tok::semi))
464 ConsumeToken();
465 }
466
467 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
468 DeclsInGroup.data(),
469 DeclsInGroup.size());
470}
471
472/// \brief Parse 'declaration' after parsing 'declaration-specifiers
473/// declarator'. This method parses the remainder of the declaration
474/// (including any attributes or initializer, among other things) and
475/// finalizes the declaration.
476///
477/// init-declarator: [C99 6.7]
478/// declarator
479/// declarator '=' initializer
480/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
481/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
482/// [C++] declarator initializer[opt]
483///
484/// [C++] initializer:
485/// [C++] '=' initializer-clause
486/// [C++] '(' expression-list ')'
487/// [C++0x] '=' 'default' [TODO]
488/// [C++0x] '=' 'delete'
489///
490/// According to the standard grammar, =default and =delete are function
491/// definitions, but that definitely doesn't fit with the parser here.
492///
493Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
494 const ParsedTemplateInfo &TemplateInfo) {
495 // If a simple-asm-expr is present, parse it.
496 if (Tok.is(tok::kw_asm)) {
497 SourceLocation Loc;
498 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
499 if (AsmLabel.isInvalid()) {
500 SkipUntil(tok::semi, true, true);
501 return DeclPtrTy();
502 }
503
504 D.setAsmLabel(AsmLabel.release());
505 D.SetRangeEnd(Loc);
506 }
507
508 // If attributes are present, parse them.
509 if (Tok.is(tok::kw___attribute)) {
510 SourceLocation Loc;
511 AttributeList *AttrList = ParseGNUAttributes(&Loc);
512 D.AddAttributes(AttrList, Loc);
513 }
514
515 // Inform the current actions module that we just parsed this declarator.
516 DeclPtrTy ThisDecl;
517 switch (TemplateInfo.Kind) {
518 case ParsedTemplateInfo::NonTemplate:
519 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
520 break;
521
522 case ParsedTemplateInfo::Template:
523 case ParsedTemplateInfo::ExplicitSpecialization:
524 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
525 Action::MultiTemplateParamsArg(Actions,
526 TemplateInfo.TemplateParams->data(),
527 TemplateInfo.TemplateParams->size()),
528 D);
529 break;
530
531 case ParsedTemplateInfo::ExplicitInstantiation: {
532 Action::DeclResult ThisRes
533 = Actions.ActOnExplicitInstantiation(CurScope,
534 TemplateInfo.ExternLoc,
535 TemplateInfo.TemplateLoc,
536 D);
537 if (ThisRes.isInvalid()) {
538 SkipUntil(tok::semi, true, true);
539 return DeclPtrTy();
540 }
541
542 ThisDecl = ThisRes.get();
543 break;
544 }
545 }
546
547 // Parse declarator '=' initializer.
548 if (Tok.is(tok::equal)) {
549 ConsumeToken();
550 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
551 SourceLocation DelLoc = ConsumeToken();
552 Actions.SetDeclDeleted(ThisDecl, DelLoc);
553 } else {
554 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
555 EnterScope(0);
556 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
557 }
558
559 OwningExprResult Init(ParseInitializer());
560
561 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
562 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
563 ExitScope();
564 }
565
566 if (Init.isInvalid()) {
567 SkipUntil(tok::semi, true, true);
568 return DeclPtrTy();
569 }
570 Actions.AddInitializerToDecl(ThisDecl, move(Init));
571 }
572 } else if (Tok.is(tok::l_paren)) {
573 // Parse C++ direct initializer: '(' expression-list ')'
574 SourceLocation LParenLoc = ConsumeParen();
575 ExprVector Exprs(Actions);
576 CommaLocsTy CommaLocs;
577
578 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
579 EnterScope(0);
580 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
581 }
582
583 if (ParseExpressionList(Exprs, CommaLocs)) {
584 SkipUntil(tok::r_paren);
585
586 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
587 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
588 ExitScope();
589 }
590 } else {
591 // Match the ')'.
592 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
593
594 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
595 "Unexpected number of commas!");
596
597 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
598 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
599 ExitScope();
600 }
601
602 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
603 move_arg(Exprs),
604 CommaLocs.data(), RParenLoc);
605 }
606 } else {
607 bool TypeContainsUndeducedAuto =
608 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
609 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
610 }
611
612 return ThisDecl;
613}
614
615/// ParseSpecifierQualifierList
616/// specifier-qualifier-list:
617/// type-specifier specifier-qualifier-list[opt]
618/// type-qualifier specifier-qualifier-list[opt]
619/// [GNU] attributes specifier-qualifier-list[opt]
620///
621void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
622 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
623 /// parse declaration-specifiers and complain about extra stuff.
624 ParseDeclarationSpecifiers(DS);
625
626 // Validate declspec for type-name.
627 unsigned Specs = DS.getParsedSpecifiers();
628 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
629 !DS.getAttributes())
630 Diag(Tok, diag::err_typename_requires_specqual);
631
632 // Issue diagnostic and remove storage class if present.
633 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
634 if (DS.getStorageClassSpecLoc().isValid())
635 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
636 else
637 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
638 DS.ClearStorageClassSpecs();
639 }
640
641 // Issue diagnostic and remove function specfier if present.
642 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
643 if (DS.isInlineSpecified())
644 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
645 if (DS.isVirtualSpecified())
646 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
647 if (DS.isExplicitSpecified())
648 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
649 DS.ClearFunctionSpecs();
650 }
651}
652
653/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
654/// specified token is valid after the identifier in a declarator which
655/// immediately follows the declspec. For example, these things are valid:
656///
657/// int x [ 4]; // direct-declarator
658/// int x ( int y); // direct-declarator
659/// int(int x ) // direct-declarator
660/// int x ; // simple-declaration
661/// int x = 17; // init-declarator-list
662/// int x , y; // init-declarator-list
663/// int x __asm__ ("foo"); // init-declarator-list
664/// int x : 4; // struct-declarator
665/// int x { 5}; // C++'0x unified initializers
666///
667/// This is not, because 'x' does not immediately follow the declspec (though
668/// ')' happens to be valid anyway).
669/// int (x)
670///
671static bool isValidAfterIdentifierInDeclarator(const Token &T) {
672 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
673 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
674 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
675}
676
677
678/// ParseImplicitInt - This method is called when we have an non-typename
679/// identifier in a declspec (which normally terminates the decl spec) when
680/// the declspec has no type specifier. In this case, the declspec is either
681/// malformed or is "implicit int" (in K&R and C89).
682///
683/// This method handles diagnosing this prettily and returns false if the
684/// declspec is done being processed. If it recovers and thinks there may be
685/// other pieces of declspec after it, it returns true.
686///
687bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
688 const ParsedTemplateInfo &TemplateInfo,
689 AccessSpecifier AS) {
690 assert(Tok.is(tok::identifier) && "should have identifier");
691
692 SourceLocation Loc = Tok.getLocation();
693 // If we see an identifier that is not a type name, we normally would
694 // parse it as the identifer being declared. However, when a typename
695 // is typo'd or the definition is not included, this will incorrectly
696 // parse the typename as the identifier name and fall over misparsing
697 // later parts of the diagnostic.
698 //
699 // As such, we try to do some look-ahead in cases where this would
700 // otherwise be an "implicit-int" case to see if this is invalid. For
701 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
702 // an identifier with implicit int, we'd get a parse error because the
703 // next token is obviously invalid for a type. Parse these as a case
704 // with an invalid type specifier.
705 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
706
707 // Since we know that this either implicit int (which is rare) or an
708 // error, we'd do lookahead to try to do better recovery.
709 if (isValidAfterIdentifierInDeclarator(NextToken())) {
710 // If this token is valid for implicit int, e.g. "static x = 4", then
711 // we just avoid eating the identifier, so it will be parsed as the
712 // identifier in the declarator.
713 return false;
714 }
715
716 // Otherwise, if we don't consume this token, we are going to emit an
717 // error anyway. Try to recover from various common problems. Check
718 // to see if this was a reference to a tag name without a tag specified.
719 // This is a common problem in C (saying 'foo' instead of 'struct foo').
720 //
721 // C++ doesn't need this, and isTagName doesn't take SS.
722 if (SS == 0) {
723 const char *TagName = 0;
724 tok::TokenKind TagKind = tok::unknown;
725
726 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
727 default: break;
728 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
729 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
730 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
731 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
732 }
733
734 if (TagName) {
735 Diag(Loc, diag::err_use_of_tag_name_without_tag)
736 << Tok.getIdentifierInfo() << TagName
737 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
738
739 // Parse this as a tag as if the missing tag were present.
740 if (TagKind == tok::kw_enum)
741 ParseEnumSpecifier(Loc, DS, AS);
742 else
743 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
744 return true;
745 }
746 }
747
748 // This is almost certainly an invalid type name. Let the action emit a
749 // diagnostic and attempt to recover.
750 Action::TypeTy *T = 0;
751 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
752 CurScope, SS, T)) {
753 // The action emitted a diagnostic, so we don't have to.
754 if (T) {
755 // The action has suggested that the type T could be used. Set that as
756 // the type in the declaration specifiers, consume the would-be type
757 // name token, and we're done.
758 const char *PrevSpec;
759 unsigned DiagID;
760 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
761 false);
762 DS.SetRangeEnd(Tok.getLocation());
763 ConsumeToken();
764
765 // There may be other declaration specifiers after this.
766 return true;
767 }
768
769 // Fall through; the action had no suggestion for us.
770 } else {
771 // The action did not emit a diagnostic, so emit one now.
772 SourceRange R;
773 if (SS) R = SS->getRange();
774 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
775 }
776
777 // Mark this as an error.
778 const char *PrevSpec;
779 unsigned DiagID;
780 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
781 DS.SetRangeEnd(Tok.getLocation());
782 ConsumeToken();
783
784 // TODO: Could inject an invalid typedef decl in an enclosing scope to
785 // avoid rippling error messages on subsequent uses of the same type,
786 // could be useful if #include was forgotten.
787 return false;
788}
789
790/// \brief Determine the declaration specifier context from the declarator
791/// context.
792///
793/// \param Context the declarator context, which is one of the
794/// Declarator::TheContext enumerator values.
795Parser::DeclSpecContext
796Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
797 if (Context == Declarator::MemberContext)
798 return DSC_class;
799 if (Context == Declarator::FileContext)
800 return DSC_top_level;
801 return DSC_normal;
802}
803
804/// ParseDeclarationSpecifiers
805/// declaration-specifiers: [C99 6.7]
806/// storage-class-specifier declaration-specifiers[opt]
807/// type-specifier declaration-specifiers[opt]
808/// [C99] function-specifier declaration-specifiers[opt]
809/// [GNU] attributes declaration-specifiers[opt]
810///
811/// storage-class-specifier: [C99 6.7.1]
812/// 'typedef'
813/// 'extern'
814/// 'static'
815/// 'auto'
816/// 'register'
817/// [C++] 'mutable'
818/// [GNU] '__thread'
819/// function-specifier: [C99 6.7.4]
820/// [C99] 'inline'
821/// [C++] 'virtual'
822/// [C++] 'explicit'
823/// 'friend': [C++ dcl.friend]
824/// 'constexpr': [C++0x dcl.constexpr]
825
826///
827void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
828 const ParsedTemplateInfo &TemplateInfo,
829 AccessSpecifier AS,
830 DeclSpecContext DSContext) {
831 if (Tok.is(tok::code_completion)) {
832 Action::CodeCompletionContext CCC = Action::CCC_Namespace;
833 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
834 CCC = DSContext == DSC_class? Action::CCC_MemberTemplate
835 : Action::CCC_Template;
836 else if (DSContext == DSC_class)
837 CCC = Action::CCC_Class;
838 else if (ObjCImpDecl)
839 CCC = Action::CCC_ObjCImplementation;
840
841 Actions.CodeCompleteOrdinaryName(CurScope, CCC);
842 ConsumeToken();
843 }
844
845 DS.SetRangeStart(Tok.getLocation());
846 while (1) {
847 bool isInvalid = false;
848 const char *PrevSpec = 0;
849 unsigned DiagID = 0;
850
851 SourceLocation Loc = Tok.getLocation();
852
853 switch (Tok.getKind()) {
854 default:
855 DoneWithDeclSpec:
856 // If this is not a declaration specifier token, we're done reading decl
857 // specifiers. First verify that DeclSpec's are consistent.
858 DS.Finish(Diags, PP);
859 return;
860
861 case tok::coloncolon: // ::foo::bar
862 // Annotate C++ scope specifiers. If we get one, loop.
863 if (TryAnnotateCXXScopeToken(true))
864 continue;
865 goto DoneWithDeclSpec;
866
867 case tok::annot_cxxscope: {
868 if (DS.hasTypeSpecifier())
869 goto DoneWithDeclSpec;
870
871 CXXScopeSpec SS;
872 SS.setScopeRep(Tok.getAnnotationValue());
873 SS.setRange(Tok.getAnnotationRange());
874
875 // We are looking for a qualified typename.
876 Token Next = NextToken();
877 if (Next.is(tok::annot_template_id) &&
878 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
879 ->Kind == TNK_Type_template) {
880 // We have a qualified template-id, e.g., N::A<int>
881
882 // C++ [class.qual]p2:
883 // In a lookup in which the constructor is an acceptable lookup
884 // result and the nested-name-specifier nominates a class C:
885 //
886 // - if the name specified after the
887 // nested-name-specifier, when looked up in C, is the
888 // injected-class-name of C (Clause 9), or
889 //
890 // - if the name specified after the nested-name-specifier
891 // is the same as the identifier or the
892 // simple-template-id's template-name in the last
893 // component of the nested-name-specifier,
894 //
895 // the name is instead considered to name the constructor of
896 // class C.
897 //
898 // Thus, if the template-name is actually the constructor
899 // name, then the code is ill-formed; this interpretation is
900 // reinforced by the NAD status of core issue 635.
901 TemplateIdAnnotation *TemplateId
902 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
903 if (DSContext == DSC_top_level && TemplateId->Name &&
904 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
905 if (isConstructorDeclarator()) {
906 // The user meant this to be an out-of-line constructor
907 // definition, but template arguments are not allowed
908 // there. Just allow this as a constructor; we'll
909 // complain about it later.
910 goto DoneWithDeclSpec;
911 }
912
913 // The user meant this to name a type, but it actually names
914 // a constructor with some extraneous template
915 // arguments. Complain, then parse it as a type as the user
916 // intended.
917 Diag(TemplateId->TemplateNameLoc,
918 diag::err_out_of_line_template_id_names_constructor)
919 << TemplateId->Name;
920 }
921
922 DS.getTypeSpecScope() = SS;
923 ConsumeToken(); // The C++ scope.
924 assert(Tok.is(tok::annot_template_id) &&
925 "ParseOptionalCXXScopeSpecifier not working");
926 AnnotateTemplateIdTokenAsType(&SS);
927 continue;
928 }
929
930 if (Next.is(tok::annot_typename)) {
931 DS.getTypeSpecScope() = SS;
932 ConsumeToken(); // The C++ scope.
933 if (Tok.getAnnotationValue())
934 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
935 PrevSpec, DiagID,
936 Tok.getAnnotationValue());
937 else
938 DS.SetTypeSpecError();
939 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
940 ConsumeToken(); // The typename
941 }
942
943 if (Next.isNot(tok::identifier))
944 goto DoneWithDeclSpec;
945
946 // If we're in a context where the identifier could be a class name,
947 // check whether this is a constructor declaration.
948 if (DSContext == DSC_top_level &&
949 Actions.isCurrentClassName(*Next.getIdentifierInfo(), CurScope,
950 &SS)) {
951 if (isConstructorDeclarator())
952 goto DoneWithDeclSpec;
953
954 // As noted in C++ [class.qual]p2 (cited above), when the name
955 // of the class is qualified in a context where it could name
956 // a constructor, its a constructor name. However, we've
957 // looked at the declarator, and the user probably meant this
958 // to be a type. Complain that it isn't supposed to be treated
959 // as a type, then proceed to parse it as a type.
960 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
961 << Next.getIdentifierInfo();
962 }
963
964 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
965 Next.getLocation(), CurScope, &SS);
966
967 // If the referenced identifier is not a type, then this declspec is
968 // erroneous: We already checked about that it has no type specifier, and
969 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
970 // typename.
971 if (TypeRep == 0) {
972 ConsumeToken(); // Eat the scope spec so the identifier is current.
973 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
974 goto DoneWithDeclSpec;
975 }
976
977 DS.getTypeSpecScope() = SS;
978 ConsumeToken(); // The C++ scope.
979
980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
981 DiagID, TypeRep);
982 if (isInvalid)
983 break;
984
985 DS.SetRangeEnd(Tok.getLocation());
986 ConsumeToken(); // The typename.
987
988 continue;
989 }
990
991 case tok::annot_typename: {
992 if (Tok.getAnnotationValue())
993 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
994 DiagID, Tok.getAnnotationValue());
995 else
996 DS.SetTypeSpecError();
997 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
998 ConsumeToken(); // The typename
999
1000 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1001 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1002 // Objective-C interface. If we don't have Objective-C or a '<', this is
1003 // just a normal reference to a typedef name.
1004 if (!Tok.is(tok::less) || !getLang().ObjC1)
1005 continue;
1006
1007 SourceLocation LAngleLoc, EndProtoLoc;
1008 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
1009 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1010 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1011 LAngleLoc, EndProtoLoc);
1012 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1013 ProtocolLocs.data(), LAngleLoc);
1014
1015 DS.SetRangeEnd(EndProtoLoc);
1016 continue;
1017 }
1018
1019 // typedef-name
1020 case tok::identifier: {
1021 // In C++, check to see if this is a scope specifier like foo::bar::, if
1022 // so handle it as such. This is important for ctor parsing.
1023 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
1024 continue;
1025
1026 // This identifier can only be a typedef name if we haven't already seen
1027 // a type-specifier. Without this check we misparse:
1028 // typedef int X; struct Y { short X; }; as 'short int'.
1029 if (DS.hasTypeSpecifier())
1030 goto DoneWithDeclSpec;
1031
1032 // Check for need to substitute AltiVec keyword tokens.
1033 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1034 break;
1035
1036 // It has to be available as a typedef too!
1037 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
1038 Tok.getLocation(), CurScope);
1039
1040 // If this is not a typedef name, don't parse it as part of the declspec,
1041 // it must be an implicit int or an error.
1042 if (TypeRep == 0) {
1043 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
1044 goto DoneWithDeclSpec;
1045 }
1046
1047 // If we're in a context where the identifier could be a class name,
1048 // check whether this is a constructor declaration.
1049 if (getLang().CPlusPlus && DSContext == DSC_class &&
1050 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
1051 isConstructorDeclarator())
1052 goto DoneWithDeclSpec;
1053
1054 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1055 DiagID, TypeRep);
1056 if (isInvalid)
1057 break;
1058
1059 DS.SetRangeEnd(Tok.getLocation());
1060 ConsumeToken(); // The identifier
1061
1062 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1063 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1064 // Objective-C interface. If we don't have Objective-C or a '<', this is
1065 // just a normal reference to a typedef name.
1066 if (!Tok.is(tok::less) || !getLang().ObjC1)
1067 continue;
1068
1069 SourceLocation LAngleLoc, EndProtoLoc;
1070 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
1071 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1072 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1073 LAngleLoc, EndProtoLoc);
1074 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1075 ProtocolLocs.data(), LAngleLoc);
1076
1077 DS.SetRangeEnd(EndProtoLoc);
1078
1079 // Need to support trailing type qualifiers (e.g. "id<p> const").
1080 // If a type specifier follows, it will be diagnosed elsewhere.
1081 continue;
1082 }
1083
1084 // type-name
1085 case tok::annot_template_id: {
1086 TemplateIdAnnotation *TemplateId
1087 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1088 if (TemplateId->Kind != TNK_Type_template) {
1089 // This template-id does not refer to a type name, so we're
1090 // done with the type-specifiers.
1091 goto DoneWithDeclSpec;
1092 }
1093
1094 // If we're in a context where the template-id could be a
1095 // constructor name or specialization, check whether this is a
1096 // constructor declaration.
1097 if (getLang().CPlusPlus && DSContext == DSC_class &&
1098 Actions.isCurrentClassName(*TemplateId->Name, CurScope) &&
1099 isConstructorDeclarator())
1100 goto DoneWithDeclSpec;
1101
1102 // Turn the template-id annotation token into a type annotation
1103 // token, then try again to parse it as a type-specifier.
1104 AnnotateTemplateIdTokenAsType();
1105 continue;
1106 }
1107
1108 // GNU attributes support.
1109 case tok::kw___attribute:
1110 DS.AddAttributes(ParseGNUAttributes());
1111 continue;
1112
1113 // Microsoft declspec support.
1114 case tok::kw___declspec:
1115 DS.AddAttributes(ParseMicrosoftDeclSpec());
1116 continue;
1117
1118 // Microsoft single token adornments.
1119 case tok::kw___forceinline:
1120 // FIXME: Add handling here!
1121 break;
1122
1123 case tok::kw___ptr64:
1124 case tok::kw___w64:
1125 case tok::kw___cdecl:
1126 case tok::kw___stdcall:
1127 case tok::kw___fastcall:
1128 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1129 continue;
1130
1131 // storage-class-specifier
1132 case tok::kw_typedef:
1133 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1134 DiagID);
1135 break;
1136 case tok::kw_extern:
1137 if (DS.isThreadSpecified())
1138 Diag(Tok, diag::ext_thread_before) << "extern";
1139 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1140 DiagID);
1141 break;
1142 case tok::kw___private_extern__:
1143 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
1144 PrevSpec, DiagID);
1145 break;
1146 case tok::kw_static:
1147 if (DS.isThreadSpecified())
1148 Diag(Tok, diag::ext_thread_before) << "static";
1149 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1150 DiagID);
1151 break;
1152 case tok::kw_auto:
1153 if (getLang().CPlusPlus0x)
1154 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1155 DiagID);
1156 else
1157 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1158 DiagID);
1159 break;
1160 case tok::kw_register:
1161 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1162 DiagID);
1163 break;
1164 case tok::kw_mutable:
1165 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1166 DiagID);
1167 break;
1168 case tok::kw___thread:
1169 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
1170 break;
1171
1172 // function-specifier
1173 case tok::kw_inline:
1174 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
1175 break;
1176 case tok::kw_virtual:
1177 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
1178 break;
1179 case tok::kw_explicit:
1180 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
1181 break;
1182
1183 // friend
1184 case tok::kw_friend:
1185 if (DSContext == DSC_class)
1186 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1187 else {
1188 PrevSpec = ""; // not actually used by the diagnostic
1189 DiagID = diag::err_friend_invalid_in_context;
1190 isInvalid = true;
1191 }
1192 break;
1193
1194 // constexpr
1195 case tok::kw_constexpr:
1196 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1197 break;
1198
1199 // type-specifier
1200 case tok::kw_short:
1201 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1202 DiagID);
1203 break;
1204 case tok::kw_long:
1205 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1206 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1207 DiagID);
1208 else
1209 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1210 DiagID);
1211 break;
1212 case tok::kw_signed:
1213 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1214 DiagID);
1215 break;
1216 case tok::kw_unsigned:
1217 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1218 DiagID);
1219 break;
1220 case tok::kw__Complex:
1221 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1222 DiagID);
1223 break;
1224 case tok::kw__Imaginary:
1225 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1226 DiagID);
1227 break;
1228 case tok::kw_void:
1229 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1230 DiagID);
1231 break;
1232 case tok::kw_char:
1233 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1234 DiagID);
1235 break;
1236 case tok::kw_int:
1237 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1238 DiagID);
1239 break;
1240 case tok::kw_float:
1241 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1242 DiagID);
1243 break;
1244 case tok::kw_double:
1245 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1246 DiagID);
1247 break;
1248 case tok::kw_wchar_t:
1249 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1250 DiagID);
1251 break;
1252 case tok::kw_char16_t:
1253 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1254 DiagID);
1255 break;
1256 case tok::kw_char32_t:
1257 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1258 DiagID);
1259 break;
1260 case tok::kw_bool:
1261 case tok::kw__Bool:
1262 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1263 DiagID);
1264 break;
1265 case tok::kw__Decimal32:
1266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1267 DiagID);
1268 break;
1269 case tok::kw__Decimal64:
1270 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1271 DiagID);
1272 break;
1273 case tok::kw__Decimal128:
1274 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1275 DiagID);
1276 break;
1277 case tok::kw___vector:
1278 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1279 break;
1280 case tok::kw___pixel:
1281 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1282 break;
1283
1284 // class-specifier:
1285 case tok::kw_class:
1286 case tok::kw_struct:
1287 case tok::kw_union: {
1288 tok::TokenKind Kind = Tok.getKind();
1289 ConsumeToken();
1290 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
1291 continue;
1292 }
1293
1294 // enum-specifier:
1295 case tok::kw_enum:
1296 ConsumeToken();
1297 ParseEnumSpecifier(Loc, DS, AS);
1298 continue;
1299
1300 // cv-qualifier:
1301 case tok::kw_const:
1302 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1303 getLang());
1304 break;
1305 case tok::kw_volatile:
1306 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1307 getLang());
1308 break;
1309 case tok::kw_restrict:
1310 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1311 getLang());
1312 break;
1313
1314 // C++ typename-specifier:
1315 case tok::kw_typename:
1316 if (TryAnnotateTypeOrScopeToken())
1317 continue;
1318 break;
1319
1320 // GNU typeof support.
1321 case tok::kw_typeof:
1322 ParseTypeofSpecifier(DS);
1323 continue;
1324
1325 case tok::kw_decltype:
1326 ParseDecltypeSpecifier(DS);
1327 continue;
1328
1329 case tok::less:
1330 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
1331 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1332 // but we support it.
1333 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
1334 goto DoneWithDeclSpec;
1335
1336 {
1337 SourceLocation LAngleLoc, EndProtoLoc;
1338 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
1339 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1340 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1341 LAngleLoc, EndProtoLoc);
1342 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1343 ProtocolLocs.data(), LAngleLoc);
1344 DS.SetRangeEnd(EndProtoLoc);
1345
1346 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1347 << CodeModificationHint::CreateInsertion(Loc, "id")
1348 << SourceRange(Loc, EndProtoLoc);
1349 // Need to support trailing type qualifiers (e.g. "id<p> const").
1350 // If a type specifier follows, it will be diagnosed elsewhere.
1351 continue;
1352 }
1353 }
1354 // If the specifier wasn't legal, issue a diagnostic.
1355 if (isInvalid) {
1356 assert(PrevSpec && "Method did not return previous specifier!");
1357 assert(DiagID);
1358 Diag(Tok, DiagID) << PrevSpec;
1359 }
1360 DS.SetRangeEnd(Tok.getLocation());
1361 ConsumeToken();
1362 }
1363}
1364
1365/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
1366/// primarily follow the C++ grammar with additions for C99 and GNU,
1367/// which together subsume the C grammar. Note that the C++
1368/// type-specifier also includes the C type-qualifier (for const,
1369/// volatile, and C99 restrict). Returns true if a type-specifier was
1370/// found (and parsed), false otherwise.
1371///
1372/// type-specifier: [C++ 7.1.5]
1373/// simple-type-specifier
1374/// class-specifier
1375/// enum-specifier
1376/// elaborated-type-specifier [TODO]
1377/// cv-qualifier
1378///
1379/// cv-qualifier: [C++ 7.1.5.1]
1380/// 'const'
1381/// 'volatile'
1382/// [C99] 'restrict'
1383///
1384/// simple-type-specifier: [ C++ 7.1.5.2]
1385/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1386/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1387/// 'char'
1388/// 'wchar_t'
1389/// 'bool'
1390/// 'short'
1391/// 'int'
1392/// 'long'
1393/// 'signed'
1394/// 'unsigned'
1395/// 'float'
1396/// 'double'
1397/// 'void'
1398/// [C99] '_Bool'
1399/// [C99] '_Complex'
1400/// [C99] '_Imaginary' // Removed in TC2?
1401/// [GNU] '_Decimal32'
1402/// [GNU] '_Decimal64'
1403/// [GNU] '_Decimal128'
1404/// [GNU] typeof-specifier
1405/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1406/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
1407/// [C++0x] 'decltype' ( expression )
1408/// [AltiVec] '__vector'
1409bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
1410 const char *&PrevSpec,
1411 unsigned &DiagID,
1412 const ParsedTemplateInfo &TemplateInfo,
1413 bool SuppressDeclarations) {
1414 SourceLocation Loc = Tok.getLocation();
1415
1416 switch (Tok.getKind()) {
1417 case tok::identifier: // foo::bar
1418 // Check for need to substitute AltiVec keyword tokens.
1419 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1420 break;
1421 // Fall through.
1422 case tok::kw_typename: // typename foo::bar
1423 // Annotate typenames and C++ scope specifiers. If we get one, just
1424 // recurse to handle whatever we get.
1425 if (TryAnnotateTypeOrScopeToken())
1426 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1427 TemplateInfo, SuppressDeclarations);
1428 // Otherwise, not a type specifier.
1429 return false;
1430 case tok::coloncolon: // ::foo::bar
1431 if (NextToken().is(tok::kw_new) || // ::new
1432 NextToken().is(tok::kw_delete)) // ::delete
1433 return false;
1434
1435 // Annotate typenames and C++ scope specifiers. If we get one, just
1436 // recurse to handle whatever we get.
1437 if (TryAnnotateTypeOrScopeToken())
1438 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1439 TemplateInfo, SuppressDeclarations);
1440 // Otherwise, not a type specifier.
1441 return false;
1442
1443 // simple-type-specifier:
1444 case tok::annot_typename: {
1445 if (Tok.getAnnotationValue())
1446 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1447 DiagID, Tok.getAnnotationValue());
1448 else
1449 DS.SetTypeSpecError();
1450 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1451 ConsumeToken(); // The typename
1452
1453 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1454 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1455 // Objective-C interface. If we don't have Objective-C or a '<', this is
1456 // just a normal reference to a typedef name.
1457 if (!Tok.is(tok::less) || !getLang().ObjC1)
1458 return true;
1459
1460 SourceLocation LAngleLoc, EndProtoLoc;
1461 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
1462 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1463 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1464 LAngleLoc, EndProtoLoc);
1465 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1466 ProtocolLocs.data(), LAngleLoc);
1467
1468 DS.SetRangeEnd(EndProtoLoc);
1469 return true;
1470 }
1471
1472 case tok::kw_short:
1473 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
1474 break;
1475 case tok::kw_long:
1476 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1477 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1478 DiagID);
1479 else
1480 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1481 DiagID);
1482 break;
1483 case tok::kw_signed:
1484 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
1485 break;
1486 case tok::kw_unsigned:
1487 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1488 DiagID);
1489 break;
1490 case tok::kw__Complex:
1491 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1492 DiagID);
1493 break;
1494 case tok::kw__Imaginary:
1495 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1496 DiagID);
1497 break;
1498 case tok::kw_void:
1499 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
1500 break;
1501 case tok::kw_char:
1502 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
1503 break;
1504 case tok::kw_int:
1505 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
1506 break;
1507 case tok::kw_float:
1508 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
1509 break;
1510 case tok::kw_double:
1511 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
1512 break;
1513 case tok::kw_wchar_t:
1514 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
1515 break;
1516 case tok::kw_char16_t:
1517 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
1518 break;
1519 case tok::kw_char32_t:
1520 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
1521 break;
1522 case tok::kw_bool:
1523 case tok::kw__Bool:
1524 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
1525 break;
1526 case tok::kw__Decimal32:
1527 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1528 DiagID);
1529 break;
1530 case tok::kw__Decimal64:
1531 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1532 DiagID);
1533 break;
1534 case tok::kw__Decimal128:
1535 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1536 DiagID);
1537 break;
1538 case tok::kw___vector:
1539 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1540 break;
1541 case tok::kw___pixel:
1542 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1543 break;
1544
1545 // class-specifier:
1546 case tok::kw_class:
1547 case tok::kw_struct:
1548 case tok::kw_union: {
1549 tok::TokenKind Kind = Tok.getKind();
1550 ConsumeToken();
1551 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1552 SuppressDeclarations);
1553 return true;
1554 }
1555
1556 // enum-specifier:
1557 case tok::kw_enum:
1558 ConsumeToken();
1559 ParseEnumSpecifier(Loc, DS);
1560 return true;
1561
1562 // cv-qualifier:
1563 case tok::kw_const:
1564 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1565 DiagID, getLang());
1566 break;
1567 case tok::kw_volatile:
1568 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1569 DiagID, getLang());
1570 break;
1571 case tok::kw_restrict:
1572 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1573 DiagID, getLang());
1574 break;
1575
1576 // GNU typeof support.
1577 case tok::kw_typeof:
1578 ParseTypeofSpecifier(DS);
1579 return true;
1580
1581 // C++0x decltype support.
1582 case tok::kw_decltype:
1583 ParseDecltypeSpecifier(DS);
1584 return true;
1585
1586 // C++0x auto support.
1587 case tok::kw_auto:
1588 if (!getLang().CPlusPlus0x)
1589 return false;
1590
1591 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
1592 break;
1593 case tok::kw___ptr64:
1594 case tok::kw___w64:
1595 case tok::kw___cdecl:
1596 case tok::kw___stdcall:
1597 case tok::kw___fastcall:
1598 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1599 return true;
1600
1601 default:
1602 // Not a type-specifier; do nothing.
1603 return false;
1604 }
1605
1606 // If the specifier combination wasn't legal, issue a diagnostic.
1607 if (isInvalid) {
1608 assert(PrevSpec && "Method did not return previous specifier!");
1609 // Pick between error or extwarn.
1610 Diag(Tok, DiagID) << PrevSpec;
1611 }
1612 DS.SetRangeEnd(Tok.getLocation());
1613 ConsumeToken(); // whatever we parsed above.
1614 return true;
1615}
1616
1617/// ParseStructDeclaration - Parse a struct declaration without the terminating
1618/// semicolon.
1619///
1620/// struct-declaration:
1621/// specifier-qualifier-list struct-declarator-list
1622/// [GNU] __extension__ struct-declaration
1623/// [GNU] specifier-qualifier-list
1624/// struct-declarator-list:
1625/// struct-declarator
1626/// struct-declarator-list ',' struct-declarator
1627/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1628/// struct-declarator:
1629/// declarator
1630/// [GNU] declarator attributes[opt]
1631/// declarator[opt] ':' constant-expression
1632/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1633///
1634void Parser::
1635ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
1636 if (Tok.is(tok::kw___extension__)) {
1637 // __extension__ silences extension warnings in the subexpression.
1638 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1639 ConsumeToken();
1640 return ParseStructDeclaration(DS, Fields);
1641 }
1642
1643 // Parse the common specifier-qualifiers-list piece.
1644 SourceLocation DSStart = Tok.getLocation();
1645 ParseSpecifierQualifierList(DS);
1646
1647 // If there are no declarators, this is a free-standing declaration
1648 // specifier. Let the actions module cope with it.
1649 if (Tok.is(tok::semi)) {
1650 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
1651 return;
1652 }
1653
1654 // Read struct-declarators until we find the semicolon.
1655 bool FirstDeclarator = true;
1656 while (1) {
1657 ParsingDeclRAIIObject PD(*this);
1658 FieldDeclarator DeclaratorInfo(DS);
1659
1660 // Attributes are only allowed here on successive declarators.
1661 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1662 SourceLocation Loc;
1663 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1664 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1665 }
1666
1667 /// struct-declarator: declarator
1668 /// struct-declarator: declarator[opt] ':' constant-expression
1669 if (Tok.isNot(tok::colon)) {
1670 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1671 ColonProtectionRAIIObject X(*this);
1672 ParseDeclarator(DeclaratorInfo.D);
1673 }
1674
1675 if (Tok.is(tok::colon)) {
1676 ConsumeToken();
1677 OwningExprResult Res(ParseConstantExpression());
1678 if (Res.isInvalid())
1679 SkipUntil(tok::semi, true, true);
1680 else
1681 DeclaratorInfo.BitfieldSize = Res.release();
1682 }
1683
1684 // If attributes exist after the declarator, parse them.
1685 if (Tok.is(tok::kw___attribute)) {
1686 SourceLocation Loc;
1687 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1688 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1689 }
1690
1691 // We're done with this declarator; invoke the callback.
1692 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1693 PD.complete(D);
1694
1695 // If we don't have a comma, it is either the end of the list (a ';')
1696 // or an error, bail out.
1697 if (Tok.isNot(tok::comma))
1698 return;
1699
1700 // Consume the comma.
1701 ConsumeToken();
1702
1703 FirstDeclarator = false;
1704 }
1705}
1706
1707/// ParseStructUnionBody
1708/// struct-contents:
1709/// struct-declaration-list
1710/// [EXT] empty
1711/// [GNU] "struct-declaration-list" without terminatoring ';'
1712/// struct-declaration-list:
1713/// struct-declaration
1714/// struct-declaration-list struct-declaration
1715/// [OBC] '@' 'defs' '(' class-name ')'
1716///
1717void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1718 unsigned TagType, DeclPtrTy TagDecl) {
1719 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1720 PP.getSourceManager(),
1721 "parsing struct/union body");
1722
1723 SourceLocation LBraceLoc = ConsumeBrace();
1724
1725 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
1726 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1727
1728 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1729 // C++.
1730 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
1731 Diag(Tok, diag::ext_empty_struct_union_enum)
1732 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
1733
1734 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
1735
1736 // While we still have something to read, read the declarations in the struct.
1737 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1738 // Each iteration of this loop reads one struct-declaration.
1739
1740 // Check for extraneous top-level semicolon.
1741 if (Tok.is(tok::semi)) {
1742 Diag(Tok, diag::ext_extra_struct_semi)
1743 << CodeModificationHint::CreateRemoval(Tok.getLocation());
1744 ConsumeToken();
1745 continue;
1746 }
1747
1748 // Parse all the comma separated declarators.
1749 DeclSpec DS;
1750
1751 if (!Tok.is(tok::at)) {
1752 struct CFieldCallback : FieldCallback {
1753 Parser &P;
1754 DeclPtrTy TagDecl;
1755 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1756
1757 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1758 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1759 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1760
1761 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
1762 // Install the declarator into the current TagDecl.
1763 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1764 FD.D.getDeclSpec().getSourceRange().getBegin(),
1765 FD.D, FD.BitfieldSize);
1766 FieldDecls.push_back(Field);
1767 return Field;
1768 }
1769 } Callback(*this, TagDecl, FieldDecls);
1770
1771 ParseStructDeclaration(DS, Callback);
1772 } else { // Handle @defs
1773 ConsumeToken();
1774 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1775 Diag(Tok, diag::err_unexpected_at);
1776 SkipUntil(tok::semi, true);
1777 continue;
1778 }
1779 ConsumeToken();
1780 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1781 if (!Tok.is(tok::identifier)) {
1782 Diag(Tok, diag::err_expected_ident);
1783 SkipUntil(tok::semi, true);
1784 continue;
1785 }
1786 llvm::SmallVector<DeclPtrTy, 16> Fields;
1787 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1788 Tok.getIdentifierInfo(), Fields);
1789 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1790 ConsumeToken();
1791 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1792 }
1793
1794 if (Tok.is(tok::semi)) {
1795 ConsumeToken();
1796 } else if (Tok.is(tok::r_brace)) {
1797 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
1798 break;
1799 } else {
1800 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1801 // Skip to end of block or statement to avoid ext-warning on extra ';'.
1802 SkipUntil(tok::r_brace, true, true);
1803 // If we stopped at a ';', eat it.
1804 if (Tok.is(tok::semi)) ConsumeToken();
1805 }
1806 }
1807
1808 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1809
1810 AttributeList *AttrList = 0;
1811 // If attributes exist after struct contents, parse them.
1812 if (Tok.is(tok::kw___attribute))
1813 AttrList = ParseGNUAttributes();
1814
1815 Actions.ActOnFields(CurScope,
1816 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
1817 LBraceLoc, RBraceLoc,
1818 AttrList);
1819 StructScope.Exit();
1820 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
1821}
1822
1823
1824/// ParseEnumSpecifier
1825/// enum-specifier: [C99 6.7.2.2]
1826/// 'enum' identifier[opt] '{' enumerator-list '}'
1827///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
1828/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1829/// '}' attributes[opt]
1830/// 'enum' identifier
1831/// [GNU] 'enum' attributes[opt] identifier
1832///
1833/// [C++] elaborated-type-specifier:
1834/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1835///
1836void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1837 AccessSpecifier AS) {
1838 // Parse the tag portion of this.
1839 if (Tok.is(tok::code_completion)) {
1840 // Code completion for an enum name.
1841 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1842 ConsumeToken();
1843 }
1844
1845 AttributeList *Attr = 0;
1846 // If attributes exist after tag, parse them.
1847 if (Tok.is(tok::kw___attribute))
1848 Attr = ParseGNUAttributes();
1849
1850 CXXScopeSpec SS;
1851 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
1852 if (Tok.isNot(tok::identifier)) {
1853 Diag(Tok, diag::err_expected_ident);
1854 if (Tok.isNot(tok::l_brace)) {
1855 // Has no name and is not a definition.
1856 // Skip the rest of this declarator, up until the comma or semicolon.
1857 SkipUntil(tok::comma, true);
1858 return;
1859 }
1860 }
1861 }
1862
1863 // Must have either 'enum name' or 'enum {...}'.
1864 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1865 Diag(Tok, diag::err_expected_ident_lbrace);
1866
1867 // Skip the rest of this declarator, up until the comma or semicolon.
1868 SkipUntil(tok::comma, true);
1869 return;
1870 }
1871
1872 // If an identifier is present, consume and remember it.
1873 IdentifierInfo *Name = 0;
1874 SourceLocation NameLoc;
1875 if (Tok.is(tok::identifier)) {
1876 Name = Tok.getIdentifierInfo();
1877 NameLoc = ConsumeToken();
1878 }
1879
1880 // There are three options here. If we have 'enum foo;', then this is a
1881 // forward declaration. If we have 'enum foo {...' then this is a
1882 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1883 //
1884 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1885 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1886 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1887 //
1888 Action::TagUseKind TUK;
1889 if (Tok.is(tok::l_brace))
1890 TUK = Action::TUK_Definition;
1891 else if (Tok.is(tok::semi))
1892 TUK = Action::TUK_Declaration;
1893 else
1894 TUK = Action::TUK_Reference;
1895 bool Owned = false;
1896 bool IsDependent = false;
1897 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
1898 StartLoc, SS, Name, NameLoc, Attr, AS,
1899 Action::MultiTemplateParamsArg(Actions),
1900 Owned, IsDependent);
1901 assert(!IsDependent && "didn't expect dependent enum");
1902
1903 if (Tok.is(tok::l_brace))
1904 ParseEnumBody(StartLoc, TagDecl);
1905
1906 // FIXME: The DeclSpec should keep the locations of both the keyword and the
1907 // name (if there is one).
1908 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1909 const char *PrevSpec = 0;
1910 unsigned DiagID;
1911 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
1912 TagDecl.getAs<void>(), Owned))
1913 Diag(StartLoc, DiagID) << PrevSpec;
1914}
1915
1916/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1917/// enumerator-list:
1918/// enumerator
1919/// enumerator-list ',' enumerator
1920/// enumerator:
1921/// enumeration-constant
1922/// enumeration-constant '=' constant-expression
1923/// enumeration-constant:
1924/// identifier
1925///
1926void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
1927 // Enter the scope of the enum body and start the definition.
1928 ParseScope EnumScope(this, Scope::DeclScope);
1929 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
1930
1931 SourceLocation LBraceLoc = ConsumeBrace();
1932
1933 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
1934 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
1935 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
1936
1937 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
1938
1939 DeclPtrTy LastEnumConstDecl;
1940
1941 // Parse the enumerator-list.
1942 while (Tok.is(tok::identifier)) {
1943 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1944 SourceLocation IdentLoc = ConsumeToken();
1945
1946 SourceLocation EqualLoc;
1947 OwningExprResult AssignedVal(Actions);
1948 if (Tok.is(tok::equal)) {
1949 EqualLoc = ConsumeToken();
1950 AssignedVal = ParseConstantExpression();
1951 if (AssignedVal.isInvalid())
1952 SkipUntil(tok::comma, tok::r_brace, true, true);
1953 }
1954
1955 // Install the enumerator constant into EnumDecl.
1956 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1957 LastEnumConstDecl,
1958 IdentLoc, Ident,
1959 EqualLoc,
1960 AssignedVal.release());
1961 EnumConstantDecls.push_back(EnumConstDecl);
1962 LastEnumConstDecl = EnumConstDecl;
1963
1964 if (Tok.isNot(tok::comma))
1965 break;
1966 SourceLocation CommaLoc = ConsumeToken();
1967
1968 if (Tok.isNot(tok::identifier) &&
1969 !(getLang().C99 || getLang().CPlusPlus0x))
1970 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1971 << getLang().CPlusPlus
1972 << CodeModificationHint::CreateRemoval(CommaLoc);
1973 }
1974
1975 // Eat the }.
1976 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1977
1978 AttributeList *Attr = 0;
1979 // If attributes exist after the identifier list, parse them.
1980 if (Tok.is(tok::kw___attribute))
1981 Attr = ParseGNUAttributes(); // FIXME: where do they do?
1982
1983 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1984 EnumConstantDecls.data(), EnumConstantDecls.size(),
1985 CurScope, Attr);
1986
1987 EnumScope.Exit();
1988 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
1989}
1990
1991/// isTypeSpecifierQualifier - Return true if the current token could be the
1992/// start of a type-qualifier-list.
1993bool Parser::isTypeQualifier() const {
1994 switch (Tok.getKind()) {
1995 default: return false;
1996 // type-qualifier
1997 case tok::kw_const:
1998 case tok::kw_volatile:
1999 case tok::kw_restrict:
2000 return true;
2001 }
2002}
2003
2004/// isTypeSpecifierQualifier - Return true if the current token could be the
2005/// start of a specifier-qualifier-list.
2006bool Parser::isTypeSpecifierQualifier() {
2007 switch (Tok.getKind()) {
2008 default: return false;
2009
2010 case tok::identifier: // foo::bar
2011 if (TryAltiVecVectorToken())
2012 return true;
2013 // Fall through.
2014 case tok::kw_typename: // typename T::type
2015 // Annotate typenames and C++ scope specifiers. If we get one, just
2016 // recurse to handle whatever we get.
2017 if (TryAnnotateTypeOrScopeToken())
2018 return isTypeSpecifierQualifier();
2019 // Otherwise, not a type specifier.
2020 return false;
2021
2022 case tok::coloncolon: // ::foo::bar
2023 if (NextToken().is(tok::kw_new) || // ::new
2024 NextToken().is(tok::kw_delete)) // ::delete
2025 return false;
2026
2027 // Annotate typenames and C++ scope specifiers. If we get one, just
2028 // recurse to handle whatever we get.
2029 if (TryAnnotateTypeOrScopeToken())
2030 return isTypeSpecifierQualifier();
2031 // Otherwise, not a type specifier.
2032 return false;
2033
2034 // GNU attributes support.
2035 case tok::kw___attribute:
2036 // GNU typeof support.
2037 case tok::kw_typeof:
2038
2039 // type-specifiers
2040 case tok::kw_short:
2041 case tok::kw_long:
2042 case tok::kw_signed:
2043 case tok::kw_unsigned:
2044 case tok::kw__Complex:
2045 case tok::kw__Imaginary:
2046 case tok::kw_void:
2047 case tok::kw_char:
2048 case tok::kw_wchar_t:
2049 case tok::kw_char16_t:
2050 case tok::kw_char32_t:
2051 case tok::kw_int:
2052 case tok::kw_float:
2053 case tok::kw_double:
2054 case tok::kw_bool:
2055 case tok::kw__Bool:
2056 case tok::kw__Decimal32:
2057 case tok::kw__Decimal64:
2058 case tok::kw__Decimal128:
2059 case tok::kw___vector:
2060
2061 // struct-or-union-specifier (C99) or class-specifier (C++)
2062 case tok::kw_class:
2063 case tok::kw_struct:
2064 case tok::kw_union:
2065 // enum-specifier
2066 case tok::kw_enum:
2067
2068 // type-qualifier
2069 case tok::kw_const:
2070 case tok::kw_volatile:
2071 case tok::kw_restrict:
2072
2073 // typedef-name
2074 case tok::annot_typename:
2075 return true;
2076
2077 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2078 case tok::less:
2079 return getLang().ObjC1;
2080
2081 case tok::kw___cdecl:
2082 case tok::kw___stdcall:
2083 case tok::kw___fastcall:
2084 case tok::kw___w64:
2085 case tok::kw___ptr64:
2086 return true;
2087 }
2088}
2089
2090/// isDeclarationSpecifier() - Return true if the current token is part of a
2091/// declaration specifier.
2092bool Parser::isDeclarationSpecifier() {
2093 switch (Tok.getKind()) {
2094 default: return false;
2095
2096 case tok::identifier: // foo::bar
2097 // Unfortunate hack to support "Class.factoryMethod" notation.
2098 if (getLang().ObjC1 && NextToken().is(tok::period))
2099 return false;
2100 if (TryAltiVecVectorToken())
2101 return true;
2102 // Fall through.
2103
2104 case tok::kw_typename: // typename T::type
2105 // Annotate typenames and C++ scope specifiers. If we get one, just
2106 // recurse to handle whatever we get.
2107 if (TryAnnotateTypeOrScopeToken())
2108 return isDeclarationSpecifier();
2109 // Otherwise, not a declaration specifier.
2110 return false;
2111 case tok::coloncolon: // ::foo::bar
2112 if (NextToken().is(tok::kw_new) || // ::new
2113 NextToken().is(tok::kw_delete)) // ::delete
2114 return false;
2115
2116 // Annotate typenames and C++ scope specifiers. If we get one, just
2117 // recurse to handle whatever we get.
2118 if (TryAnnotateTypeOrScopeToken())
2119 return isDeclarationSpecifier();
2120 // Otherwise, not a declaration specifier.
2121 return false;
2122
2123 // storage-class-specifier
2124 case tok::kw_typedef:
2125 case tok::kw_extern:
2126 case tok::kw___private_extern__:
2127 case tok::kw_static:
2128 case tok::kw_auto:
2129 case tok::kw_register:
2130 case tok::kw___thread:
2131
2132 // type-specifiers
2133 case tok::kw_short:
2134 case tok::kw_long:
2135 case tok::kw_signed:
2136 case tok::kw_unsigned:
2137 case tok::kw__Complex:
2138 case tok::kw__Imaginary:
2139 case tok::kw_void:
2140 case tok::kw_char:
2141 case tok::kw_wchar_t:
2142 case tok::kw_char16_t:
2143 case tok::kw_char32_t:
2144
2145 case tok::kw_int:
2146 case tok::kw_float:
2147 case tok::kw_double:
2148 case tok::kw_bool:
2149 case tok::kw__Bool:
2150 case tok::kw__Decimal32:
2151 case tok::kw__Decimal64:
2152 case tok::kw__Decimal128:
2153 case tok::kw___vector:
2154
2155 // struct-or-union-specifier (C99) or class-specifier (C++)
2156 case tok::kw_class:
2157 case tok::kw_struct:
2158 case tok::kw_union:
2159 // enum-specifier
2160 case tok::kw_enum:
2161
2162 // type-qualifier
2163 case tok::kw_const:
2164 case tok::kw_volatile:
2165 case tok::kw_restrict:
2166
2167 // function-specifier
2168 case tok::kw_inline:
2169 case tok::kw_virtual:
2170 case tok::kw_explicit:
2171
2172 // typedef-name
2173 case tok::annot_typename:
2174
2175 // GNU typeof support.
2176 case tok::kw_typeof:
2177
2178 // GNU attributes.
2179 case tok::kw___attribute:
2180 return true;
2181
2182 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2183 case tok::less:
2184 return getLang().ObjC1;
2185
2186 case tok::kw___declspec:
2187 case tok::kw___cdecl:
2188 case tok::kw___stdcall:
2189 case tok::kw___fastcall:
2190 case tok::kw___w64:
2191 case tok::kw___ptr64:
2192 case tok::kw___forceinline:
2193 return true;
2194 }
2195}
2196
2197bool Parser::isConstructorDeclarator() {
2198 TentativeParsingAction TPA(*this);
2199
2200 // Parse the C++ scope specifier.
2201 CXXScopeSpec SS;
2202 ParseOptionalCXXScopeSpecifier(SS, 0, true);
2203
2204 // Parse the constructor name.
2205 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2206 // We already know that we have a constructor name; just consume
2207 // the token.
2208 ConsumeToken();
2209 } else {
2210 TPA.Revert();
2211 return false;
2212 }
2213
2214 // Current class name must be followed by a left parentheses.
2215 if (Tok.isNot(tok::l_paren)) {
2216 TPA.Revert();
2217 return false;
2218 }
2219 ConsumeParen();
2220
2221 // A right parentheses or ellipsis signals that we have a constructor.
2222 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2223 TPA.Revert();
2224 return true;
2225 }
2226
2227 // If we need to, enter the specified scope.
2228 DeclaratorScopeObj DeclScopeObj(*this, SS);
2229 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(CurScope, SS))
2230 DeclScopeObj.EnterDeclaratorScope();
2231
2232 // Check whether the next token(s) are part of a declaration
2233 // specifier, in which case we have the start of a parameter and,
2234 // therefore, we know that this is a constructor.
2235 bool IsConstructor = isDeclarationSpecifier();
2236 TPA.Revert();
2237 return IsConstructor;
2238}
2239
2240/// ParseTypeQualifierListOpt
2241/// type-qualifier-list: [C99 6.7.5]
2242/// type-qualifier
2243/// [GNU] attributes [ only if AttributesAllowed=true ]
2244/// type-qualifier-list type-qualifier
2245/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
2246/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2247/// if CXX0XAttributesAllowed = true
2248///
2249void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2250 bool CXX0XAttributesAllowed) {
2251 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2252 SourceLocation Loc = Tok.getLocation();
2253 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2254 if (CXX0XAttributesAllowed)
2255 DS.AddAttributes(Attr.AttrList);
2256 else
2257 Diag(Loc, diag::err_attributes_not_allowed);
2258 }
2259
2260 while (1) {
2261 bool isInvalid = false;
2262 const char *PrevSpec = 0;
2263 unsigned DiagID = 0;
2264 SourceLocation Loc = Tok.getLocation();
2265
2266 switch (Tok.getKind()) {
2267 case tok::kw_const:
2268 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2269 getLang());
2270 break;
2271 case tok::kw_volatile:
2272 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2273 getLang());
2274 break;
2275 case tok::kw_restrict:
2276 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2277 getLang());
2278 break;
2279 case tok::kw___w64:
2280 case tok::kw___ptr64:
2281 case tok::kw___cdecl:
2282 case tok::kw___stdcall:
2283 case tok::kw___fastcall:
2284 if (GNUAttributesAllowed) {
2285 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2286 continue;
2287 }
2288 goto DoneWithTypeQuals;
2289 case tok::kw___attribute:
2290 if (GNUAttributesAllowed) {
2291 DS.AddAttributes(ParseGNUAttributes());
2292 continue; // do *not* consume the next token!
2293 }
2294 // otherwise, FALL THROUGH!
2295 default:
2296 DoneWithTypeQuals:
2297 // If this is not a type-qualifier token, we're done reading type
2298 // qualifiers. First verify that DeclSpec's are consistent.
2299 DS.Finish(Diags, PP);
2300 return;
2301 }
2302
2303 // If the specifier combination wasn't legal, issue a diagnostic.
2304 if (isInvalid) {
2305 assert(PrevSpec && "Method did not return previous specifier!");
2306 Diag(Tok, DiagID) << PrevSpec;
2307 }
2308 ConsumeToken();
2309 }
2310}
2311
2312
2313/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2314///
2315void Parser::ParseDeclarator(Declarator &D) {
2316 /// This implements the 'declarator' production in the C grammar, then checks
2317 /// for well-formedness and issues diagnostics.
2318 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
2319}
2320
2321/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2322/// is parsed by the function passed to it. Pass null, and the direct-declarator
2323/// isn't parsed at all, making this function effectively parse the C++
2324/// ptr-operator production.
2325///
2326/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2327/// [C] pointer[opt] direct-declarator
2328/// [C++] direct-declarator
2329/// [C++] ptr-operator declarator
2330///
2331/// pointer: [C99 6.7.5]
2332/// '*' type-qualifier-list[opt]
2333/// '*' type-qualifier-list[opt] pointer
2334///
2335/// ptr-operator:
2336/// '*' cv-qualifier-seq[opt]
2337/// '&'
2338/// [C++0x] '&&'
2339/// [GNU] '&' restrict[opt] attributes[opt]
2340/// [GNU?] '&&' restrict[opt] attributes[opt]
2341/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
2342void Parser::ParseDeclaratorInternal(Declarator &D,
2343 DirectDeclParseFunction DirectDeclParser) {
2344 if (Diags.hasAllExtensionsSilenced())
2345 D.setExtension();
2346 // C++ member pointers start with a '::' or a nested-name.
2347 // Member pointers get special handling, since there's no place for the
2348 // scope spec in the generic path below.
2349 if (getLang().CPlusPlus &&
2350 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2351 Tok.is(tok::annot_cxxscope))) {
2352 CXXScopeSpec SS;
2353 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
2354 if (Tok.isNot(tok::star)) {
2355 // The scope spec really belongs to the direct-declarator.
2356 D.getCXXScopeSpec() = SS;
2357 if (DirectDeclParser)
2358 (this->*DirectDeclParser)(D);
2359 return;
2360 }
2361
2362 SourceLocation Loc = ConsumeToken();
2363 D.SetRangeEnd(Loc);
2364 DeclSpec DS;
2365 ParseTypeQualifierListOpt(DS);
2366 D.ExtendWithDeclSpec(DS);
2367
2368 // Recurse to parse whatever is left.
2369 ParseDeclaratorInternal(D, DirectDeclParser);
2370
2371 // Sema will have to catch (syntactically invalid) pointers into global
2372 // scope. It has to catch pointers into namespace scope anyway.
2373 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
2374 Loc, DS.TakeAttributes()),
2375 /* Don't replace range end. */SourceLocation());
2376 return;
2377 }
2378 }
2379
2380 tok::TokenKind Kind = Tok.getKind();
2381 // Not a pointer, C++ reference, or block.
2382 if (Kind != tok::star && Kind != tok::caret &&
2383 (Kind != tok::amp || !getLang().CPlusPlus) &&
2384 // We parse rvalue refs in C++03, because otherwise the errors are scary.
2385 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
2386 if (DirectDeclParser)
2387 (this->*DirectDeclParser)(D);
2388 return;
2389 }
2390
2391 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2392 // '&&' -> rvalue reference
2393 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
2394 D.SetRangeEnd(Loc);
2395
2396 if (Kind == tok::star || Kind == tok::caret) {
2397 // Is a pointer.
2398 DeclSpec DS;
2399
2400 ParseTypeQualifierListOpt(DS);
2401 D.ExtendWithDeclSpec(DS);
2402
2403 // Recursively parse the declarator.
2404 ParseDeclaratorInternal(D, DirectDeclParser);
2405 if (Kind == tok::star)
2406 // Remember that we parsed a pointer type, and remember the type-quals.
2407 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
2408 DS.TakeAttributes()),
2409 SourceLocation());
2410 else
2411 // Remember that we parsed a Block type, and remember the type-quals.
2412 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
2413 Loc, DS.TakeAttributes()),
2414 SourceLocation());
2415 } else {
2416 // Is a reference
2417 DeclSpec DS;
2418
2419 // Complain about rvalue references in C++03, but then go on and build
2420 // the declarator.
2421 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2422 Diag(Loc, diag::err_rvalue_reference);
2423
2424 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2425 // cv-qualifiers are introduced through the use of a typedef or of a
2426 // template type argument, in which case the cv-qualifiers are ignored.
2427 //
2428 // [GNU] Retricted references are allowed.
2429 // [GNU] Attributes on references are allowed.
2430 // [C++0x] Attributes on references are not allowed.
2431 ParseTypeQualifierListOpt(DS, true, false);
2432 D.ExtendWithDeclSpec(DS);
2433
2434 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2435 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2436 Diag(DS.getConstSpecLoc(),
2437 diag::err_invalid_reference_qualifier_application) << "const";
2438 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2439 Diag(DS.getVolatileSpecLoc(),
2440 diag::err_invalid_reference_qualifier_application) << "volatile";
2441 }
2442
2443 // Recursively parse the declarator.
2444 ParseDeclaratorInternal(D, DirectDeclParser);
2445
2446 if (D.getNumTypeObjects() > 0) {
2447 // C++ [dcl.ref]p4: There shall be no references to references.
2448 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2449 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
2450 if (const IdentifierInfo *II = D.getIdentifier())
2451 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2452 << II;
2453 else
2454 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2455 << "type name";
2456
2457 // Once we've complained about the reference-to-reference, we
2458 // can go ahead and build the (technically ill-formed)
2459 // declarator: reference collapsing will take care of it.
2460 }
2461 }
2462
2463 // Remember that we parsed a reference type. It doesn't have type-quals.
2464 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
2465 DS.TakeAttributes(),
2466 Kind == tok::amp),
2467 SourceLocation());
2468 }
2469}
2470
2471/// ParseDirectDeclarator
2472/// direct-declarator: [C99 6.7.5]
2473/// [C99] identifier
2474/// '(' declarator ')'
2475/// [GNU] '(' attributes declarator ')'
2476/// [C90] direct-declarator '[' constant-expression[opt] ']'
2477/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2478/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2479/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2480/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2481/// direct-declarator '(' parameter-type-list ')'
2482/// direct-declarator '(' identifier-list[opt] ')'
2483/// [GNU] direct-declarator '(' parameter-forward-declarations
2484/// parameter-type-list[opt] ')'
2485/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2486/// cv-qualifier-seq[opt] exception-specification[opt]
2487/// [C++] declarator-id
2488///
2489/// declarator-id: [C++ 8]
2490/// id-expression
2491/// '::'[opt] nested-name-specifier[opt] type-name
2492///
2493/// id-expression: [C++ 5.1]
2494/// unqualified-id
2495/// qualified-id
2496///
2497/// unqualified-id: [C++ 5.1]
2498/// identifier
2499/// operator-function-id
2500/// conversion-function-id
2501/// '~' class-name
2502/// template-id
2503///
2504void Parser::ParseDirectDeclarator(Declarator &D) {
2505 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
2506
2507 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2508 // ParseDeclaratorInternal might already have parsed the scope.
2509 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2510 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2511 true);
2512 if (afterCXXScope) {
2513 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2514 // Change the declaration context for name lookup, until this function
2515 // is exited (and the declarator has been parsed).
2516 DeclScopeObj.EnterDeclaratorScope();
2517 }
2518
2519 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2520 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2521 // We found something that indicates the start of an unqualified-id.
2522 // Parse that unqualified-id.
2523 bool AllowConstructorName
2524 = ((D.getCXXScopeSpec().isSet() &&
2525 D.getContext() == Declarator::FileContext) ||
2526 (!D.getCXXScopeSpec().isSet() &&
2527 D.getContext() == Declarator::MemberContext)) &&
2528 !D.getDeclSpec().hasTypeSpecifier();
2529 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2530 /*EnteringContext=*/true,
2531 /*AllowDestructorName=*/true,
2532 AllowConstructorName,
2533 /*ObjectType=*/0,
2534 D.getName())) {
2535 D.SetIdentifier(0, Tok.getLocation());
2536 D.setInvalidType(true);
2537 } else {
2538 // Parsed the unqualified-id; update range information and move along.
2539 if (D.getSourceRange().getBegin().isInvalid())
2540 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2541 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
2542 }
2543 goto PastIdentifier;
2544 }
2545 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2546 assert(!getLang().CPlusPlus &&
2547 "There's a C++-specific check for tok::identifier above");
2548 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2549 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2550 ConsumeToken();
2551 goto PastIdentifier;
2552 }
2553
2554 if (Tok.is(tok::l_paren)) {
2555 // direct-declarator: '(' declarator ')'
2556 // direct-declarator: '(' attributes declarator ')'
2557 // Example: 'char (*X)' or 'int (*XX)(void)'
2558 ParseParenDeclarator(D);
2559
2560 // If the declarator was parenthesized, we entered the declarator
2561 // scope when parsing the parenthesized declarator, then exited
2562 // the scope already. Re-enter the scope, if we need to.
2563 if (D.getCXXScopeSpec().isSet()) {
2564 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2565 // Change the declaration context for name lookup, until this function
2566 // is exited (and the declarator has been parsed).
2567 DeclScopeObj.EnterDeclaratorScope();
2568 }
2569 } else if (D.mayOmitIdentifier()) {
2570 // This could be something simple like "int" (in which case the declarator
2571 // portion is empty), if an abstract-declarator is allowed.
2572 D.SetIdentifier(0, Tok.getLocation());
2573 } else {
2574 if (D.getContext() == Declarator::MemberContext)
2575 Diag(Tok, diag::err_expected_member_name_or_semi)
2576 << D.getDeclSpec().getSourceRange();
2577 else if (getLang().CPlusPlus)
2578 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
2579 else
2580 Diag(Tok, diag::err_expected_ident_lparen);
2581 D.SetIdentifier(0, Tok.getLocation());
2582 D.setInvalidType(true);
2583 }
2584
2585 PastIdentifier:
2586 assert(D.isPastIdentifier() &&
2587 "Haven't past the location of the identifier yet?");
2588
2589 // Don't parse attributes unless we have an identifier.
2590 if (D.getIdentifier() && getLang().CPlusPlus
2591 && isCXX0XAttributeSpecifier(true)) {
2592 SourceLocation AttrEndLoc;
2593 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2594 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2595 }
2596
2597 while (1) {
2598 if (Tok.is(tok::l_paren)) {
2599 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2600 // In such a case, check if we actually have a function declarator; if it
2601 // is not, the declarator has been fully parsed.
2602 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2603 // When not in file scope, warn for ambiguous function declarators, just
2604 // in case the author intended it as a variable definition.
2605 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2606 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2607 break;
2608 }
2609 ParseFunctionDeclarator(ConsumeParen(), D);
2610 } else if (Tok.is(tok::l_square)) {
2611 ParseBracketDeclarator(D);
2612 } else {
2613 break;
2614 }
2615 }
2616}
2617
2618/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2619/// only called before the identifier, so these are most likely just grouping
2620/// parens for precedence. If we find that these are actually function
2621/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2622///
2623/// direct-declarator:
2624/// '(' declarator ')'
2625/// [GNU] '(' attributes declarator ')'
2626/// direct-declarator '(' parameter-type-list ')'
2627/// direct-declarator '(' identifier-list[opt] ')'
2628/// [GNU] direct-declarator '(' parameter-forward-declarations
2629/// parameter-type-list[opt] ')'
2630///
2631void Parser::ParseParenDeclarator(Declarator &D) {
2632 SourceLocation StartLoc = ConsumeParen();
2633 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2634
2635 // Eat any attributes before we look at whether this is a grouping or function
2636 // declarator paren. If this is a grouping paren, the attribute applies to
2637 // the type being built up, for example:
2638 // int (__attribute__(()) *x)(long y)
2639 // If this ends up not being a grouping paren, the attribute applies to the
2640 // first argument, for example:
2641 // int (__attribute__(()) int x)
2642 // In either case, we need to eat any attributes to be able to determine what
2643 // sort of paren this is.
2644 //
2645 AttributeList *AttrList = 0;
2646 bool RequiresArg = false;
2647 if (Tok.is(tok::kw___attribute)) {
2648 AttrList = ParseGNUAttributes();
2649
2650 // We require that the argument list (if this is a non-grouping paren) be
2651 // present even if the attribute list was empty.
2652 RequiresArg = true;
2653 }
2654 // Eat any Microsoft extensions.
2655 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2656 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2657 Tok.is(tok::kw___ptr64)) {
2658 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2659 }
2660
2661 // If we haven't past the identifier yet (or where the identifier would be
2662 // stored, if this is an abstract declarator), then this is probably just
2663 // grouping parens. However, if this could be an abstract-declarator, then
2664 // this could also be the start of function arguments (consider 'void()').
2665 bool isGrouping;
2666
2667 if (!D.mayOmitIdentifier()) {
2668 // If this can't be an abstract-declarator, this *must* be a grouping
2669 // paren, because we haven't seen the identifier yet.
2670 isGrouping = true;
2671 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
2672 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
2673 isDeclarationSpecifier()) { // 'int(int)' is a function.
2674 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2675 // considered to be a type, not a K&R identifier-list.
2676 isGrouping = false;
2677 } else {
2678 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2679 isGrouping = true;
2680 }
2681
2682 // If this is a grouping paren, handle:
2683 // direct-declarator: '(' declarator ')'
2684 // direct-declarator: '(' attributes declarator ')'
2685 if (isGrouping) {
2686 bool hadGroupingParens = D.hasGroupingParens();
2687 D.setGroupingParens(true);
2688 if (AttrList)
2689 D.AddAttributes(AttrList, SourceLocation());
2690
2691 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
2692 // Match the ')'.
2693 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
2694
2695 D.setGroupingParens(hadGroupingParens);
2696 D.SetRangeEnd(Loc);
2697 return;
2698 }
2699
2700 // Okay, if this wasn't a grouping paren, it must be the start of a function
2701 // argument list. Recognize that this declarator will never have an
2702 // identifier (and remember where it would have been), then call into
2703 // ParseFunctionDeclarator to handle of argument list.
2704 D.SetIdentifier(0, Tok.getLocation());
2705
2706 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
2707}
2708
2709/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2710/// declarator D up to a paren, which indicates that we are parsing function
2711/// arguments.
2712///
2713/// If AttrList is non-null, then the caller parsed those arguments immediately
2714/// after the open paren - they should be considered to be the first argument of
2715/// a parameter. If RequiresArg is true, then the first argument of the
2716/// function is required to be present and required to not be an identifier
2717/// list.
2718///
2719/// This method also handles this portion of the grammar:
2720/// parameter-type-list: [C99 6.7.5]
2721/// parameter-list
2722/// parameter-list ',' '...'
2723/// [C++] parameter-list '...'
2724///
2725/// parameter-list: [C99 6.7.5]
2726/// parameter-declaration
2727/// parameter-list ',' parameter-declaration
2728///
2729/// parameter-declaration: [C99 6.7.5]
2730/// declaration-specifiers declarator
2731/// [C++] declaration-specifiers declarator '=' assignment-expression
2732/// [GNU] declaration-specifiers declarator attributes
2733/// declaration-specifiers abstract-declarator[opt]
2734/// [C++] declaration-specifiers abstract-declarator[opt]
2735/// '=' assignment-expression
2736/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2737///
2738/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
2739/// and "exception-specification[opt]".
2740///
2741void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2742 AttributeList *AttrList,
2743 bool RequiresArg) {
2744 // lparen is already consumed!
2745 assert(D.isPastIdentifier() && "Should not call before identifier!");
2746
2747 // This parameter list may be empty.
2748 if (Tok.is(tok::r_paren)) {
2749 if (RequiresArg) {
2750 Diag(Tok, diag::err_argument_required_after_attribute);
2751 delete AttrList;
2752 }
2753
2754 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2755 SourceLocation EndLoc = RParenLoc;
2756
2757 // cv-qualifier-seq[opt].
2758 DeclSpec DS;
2759 bool hasExceptionSpec = false;
2760 SourceLocation ThrowLoc;
2761 bool hasAnyExceptionSpec = false;
2762 llvm::SmallVector<TypeTy*, 2> Exceptions;
2763 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
2764 if (getLang().CPlusPlus) {
2765 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
2766 if (!DS.getSourceRange().getEnd().isInvalid())
2767 EndLoc = DS.getSourceRange().getEnd();
2768
2769 // Parse exception-specification[opt].
2770 if (Tok.is(tok::kw_throw)) {
2771 hasExceptionSpec = true;
2772 ThrowLoc = Tok.getLocation();
2773 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
2774 hasAnyExceptionSpec);
2775 assert(Exceptions.size() == ExceptionRanges.size() &&
2776 "Produced different number of exception types and ranges.");
2777 }
2778 }
2779
2780 // Remember that we parsed a function type, and remember the attributes.
2781 // int() -> no prototype, no '...'.
2782 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
2783 /*variadic*/ false,
2784 SourceLocation(),
2785 /*arglist*/ 0, 0,
2786 DS.getTypeQualifiers(),
2787 hasExceptionSpec, ThrowLoc,
2788 hasAnyExceptionSpec,
2789 Exceptions.data(),
2790 ExceptionRanges.data(),
2791 Exceptions.size(),
2792 LParenLoc, RParenLoc, D),
2793 EndLoc);
2794 return;
2795 }
2796
2797 // Alternatively, this parameter list may be an identifier list form for a
2798 // K&R-style function: void foo(a,b,c)
2799 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2800 && !TryAltiVecVectorToken()) {
2801 if (!TryAnnotateTypeOrScopeToken()) {
2802 // K&R identifier lists can't have typedefs as identifiers, per
2803 // C99 6.7.5.3p11.
2804 if (RequiresArg) {
2805 Diag(Tok, diag::err_argument_required_after_attribute);
2806 delete AttrList;
2807 }
2808 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2809 // normal declarators, not for abstract-declarators.
2810 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
2811 }
2812 }
2813
2814 // Finally, a normal, non-empty parameter type list.
2815
2816 // Build up an array of information about the parsed arguments.
2817 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2818
2819 // Enter function-declaration scope, limiting any declarators to the
2820 // function prototype scope, including parameter declarators.
2821 ParseScope PrototypeScope(this,
2822 Scope::FunctionPrototypeScope|Scope::DeclScope);
2823
2824 bool IsVariadic = false;
2825 SourceLocation EllipsisLoc;
2826 while (1) {
2827 if (Tok.is(tok::ellipsis)) {
2828 IsVariadic = true;
2829 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2830 break;
2831 }
2832
2833 SourceLocation DSStart = Tok.getLocation();
2834
2835 // Parse the declaration-specifiers.
2836 // Just use the ParsingDeclaration "scope" of the declarator.
2837 DeclSpec DS;
2838
2839 // If the caller parsed attributes for the first argument, add them now.
2840 if (AttrList) {
2841 DS.AddAttributes(AttrList);
2842 AttrList = 0; // Only apply the attributes to the first parameter.
2843 }
2844 ParseDeclarationSpecifiers(DS);
2845
2846 // Parse the declarator. This is "PrototypeContext", because we must
2847 // accept either 'declarator' or 'abstract-declarator' here.
2848 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2849 ParseDeclarator(ParmDecl);
2850
2851 // Parse GNU attributes, if present.
2852 if (Tok.is(tok::kw___attribute)) {
2853 SourceLocation Loc;
2854 AttributeList *AttrList = ParseGNUAttributes(&Loc);
2855 ParmDecl.AddAttributes(AttrList, Loc);
2856 }
2857
2858 // Remember this parsed parameter in ParamInfo.
2859 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2860
2861 // DefArgToks is used when the parsing of default arguments needs
2862 // to be delayed.
2863 CachedTokens *DefArgToks = 0;
2864
2865 // If no parameter was specified, verify that *something* was specified,
2866 // otherwise we have a missing type and identifier.
2867 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2868 ParmDecl.getNumTypeObjects() == 0) {
2869 // Completely missing, emit error.
2870 Diag(DSStart, diag::err_missing_param);
2871 } else {
2872 // Otherwise, we have something. Add it and let semantic analysis try
2873 // to grok it and add the result to the ParamInfo we are building.
2874
2875 // Inform the actions module about the parameter declarator, so it gets
2876 // added to the current scope.
2877 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2878
2879 // Parse the default argument, if any. We parse the default
2880 // arguments in all dialects; the semantic analysis in
2881 // ActOnParamDefaultArgument will reject the default argument in
2882 // C.
2883 if (Tok.is(tok::equal)) {
2884 SourceLocation EqualLoc = Tok.getLocation();
2885
2886 // Parse the default argument
2887 if (D.getContext() == Declarator::MemberContext) {
2888 // If we're inside a class definition, cache the tokens
2889 // corresponding to the default argument. We'll actually parse
2890 // them when we see the end of the class definition.
2891 // FIXME: Templates will require something similar.
2892 // FIXME: Can we use a smart pointer for Toks?
2893 DefArgToks = new CachedTokens;
2894
2895 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2896 tok::semi, false)) {
2897 delete DefArgToks;
2898 DefArgToks = 0;
2899 Actions.ActOnParamDefaultArgumentError(Param);
2900 } else
2901 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
2902 (*DefArgToks)[1].getLocation());
2903 } else {
2904 // Consume the '='.
2905 ConsumeToken();
2906
2907 OwningExprResult DefArgResult(ParseAssignmentExpression());
2908 if (DefArgResult.isInvalid()) {
2909 Actions.ActOnParamDefaultArgumentError(Param);
2910 SkipUntil(tok::comma, tok::r_paren, true, true);
2911 } else {
2912 // Inform the actions module about the default argument
2913 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
2914 move(DefArgResult));
2915 }
2916 }
2917 }
2918
2919 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2920 ParmDecl.getIdentifierLoc(), Param,
2921 DefArgToks));
2922 }
2923
2924 // If the next token is a comma, consume it and keep reading arguments.
2925 if (Tok.isNot(tok::comma)) {
2926 if (Tok.is(tok::ellipsis)) {
2927 IsVariadic = true;
2928 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2929
2930 if (!getLang().CPlusPlus) {
2931 // We have ellipsis without a preceding ',', which is ill-formed
2932 // in C. Complain and provide the fix.
2933 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2934 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2935 }
2936 }
2937
2938 break;
2939 }
2940
2941 // Consume the comma.
2942 ConsumeToken();
2943 }
2944
2945 // Leave prototype scope.
2946 PrototypeScope.Exit();
2947
2948 // If we have the closing ')', eat it.
2949 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2950 SourceLocation EndLoc = RParenLoc;
2951
2952 DeclSpec DS;
2953 bool hasExceptionSpec = false;
2954 SourceLocation ThrowLoc;
2955 bool hasAnyExceptionSpec = false;
2956 llvm::SmallVector<TypeTy*, 2> Exceptions;
2957 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
2958
2959 if (getLang().CPlusPlus) {
2960 // Parse cv-qualifier-seq[opt].
2961 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
2962 if (!DS.getSourceRange().getEnd().isInvalid())
2963 EndLoc = DS.getSourceRange().getEnd();
2964
2965 // Parse exception-specification[opt].
2966 if (Tok.is(tok::kw_throw)) {
2967 hasExceptionSpec = true;
2968 ThrowLoc = Tok.getLocation();
2969 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
2970 hasAnyExceptionSpec);
2971 assert(Exceptions.size() == ExceptionRanges.size() &&
2972 "Produced different number of exception types and ranges.");
2973 }
2974 }
2975
2976 // Remember that we parsed a function type, and remember the attributes.
2977 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
2978 EllipsisLoc,
2979 ParamInfo.data(), ParamInfo.size(),
2980 DS.getTypeQualifiers(),
2981 hasExceptionSpec, ThrowLoc,
2982 hasAnyExceptionSpec,
2983 Exceptions.data(),
2984 ExceptionRanges.data(),
2985 Exceptions.size(),
2986 LParenLoc, RParenLoc, D),
2987 EndLoc);
2988}
2989
2990/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2991/// we found a K&R-style identifier list instead of a type argument list. The
2992/// current token is known to be the first identifier in the list.
2993///
2994/// identifier-list: [C99 6.7.5]
2995/// identifier
2996/// identifier-list ',' identifier
2997///
2998void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2999 Declarator &D) {
3000 // Build up an array of information about the parsed arguments.
3001 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3002 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
3003
3004 // If there was no identifier specified for the declarator, either we are in
3005 // an abstract-declarator, or we are in a parameter declarator which was found
3006 // to be abstract. In abstract-declarators, identifier lists are not valid:
3007 // diagnose this.
3008 if (!D.getIdentifier())
3009 Diag(Tok, diag::ext_ident_list_in_param);
3010
3011 // Tok is known to be the first identifier in the list. Remember this
3012 // identifier in ParamInfo.
3013 ParamsSoFar.insert(Tok.getIdentifierInfo());
3014 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
3015 Tok.getLocation(),
3016 DeclPtrTy()));
3017
3018 ConsumeToken(); // eat the first identifier.
3019
3020 while (Tok.is(tok::comma)) {
3021 // Eat the comma.
3022 ConsumeToken();
3023
3024 // If this isn't an identifier, report the error and skip until ')'.
3025 if (Tok.isNot(tok::identifier)) {
3026 Diag(Tok, diag::err_expected_ident);
3027 SkipUntil(tok::r_paren);
3028 return;
3029 }
3030
3031 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
3032
3033 // Reject 'typedef int y; int test(x, y)', but continue parsing.
3034 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
3035 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
3036
3037 // Verify that the argument identifier has not already been mentioned.
3038 if (!ParamsSoFar.insert(ParmII)) {
3039 Diag(Tok, diag::err_param_redefinition) << ParmII;
3040 } else {
3041 // Remember this identifier in ParamInfo.
3042 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3043 Tok.getLocation(),
3044 DeclPtrTy()));
3045 }
3046
3047 // Eat the identifier.
3048 ConsumeToken();
3049 }
3050
3051 // If we have the closing ')', eat it and we're done.
3052 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3053
3054 // Remember that we parsed a function type, and remember the attributes. This
3055 // function type is always a K&R style function type, which is not varargs and
3056 // has no prototype.
3057 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
3058 SourceLocation(),
3059 &ParamInfo[0], ParamInfo.size(),
3060 /*TypeQuals*/0,
3061 /*exception*/false,
3062 SourceLocation(), false, 0, 0, 0,
3063 LParenLoc, RLoc, D),
3064 RLoc);
3065}
3066
3067/// [C90] direct-declarator '[' constant-expression[opt] ']'
3068/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3069/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3070/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3071/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3072void Parser::ParseBracketDeclarator(Declarator &D) {
3073 SourceLocation StartLoc = ConsumeBracket();
3074
3075 // C array syntax has many features, but by-far the most common is [] and [4].
3076 // This code does a fast path to handle some of the most obvious cases.
3077 if (Tok.getKind() == tok::r_square) {
3078 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3079 //FIXME: Use these
3080 CXX0XAttributeList Attr;
3081 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3082 Attr = ParseCXX0XAttributes();
3083 }
3084
3085 // Remember that we parsed the empty array type.
3086 OwningExprResult NumElements(Actions);
3087 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3088 StartLoc, EndLoc),
3089 EndLoc);
3090 return;
3091 } else if (Tok.getKind() == tok::numeric_constant &&
3092 GetLookAheadToken(1).is(tok::r_square)) {
3093 // [4] is very common. Parse the numeric constant expression.
3094 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
3095 ConsumeToken();
3096
3097 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3098 //FIXME: Use these
3099 CXX0XAttributeList Attr;
3100 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3101 Attr = ParseCXX0XAttributes();
3102 }
3103
3104 // If there was an error parsing the assignment-expression, recover.
3105 if (ExprRes.isInvalid())
3106 ExprRes.release(); // Deallocate expr, just use [].
3107
3108 // Remember that we parsed a array type, and remember its features.
3109 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3110 StartLoc, EndLoc),
3111 EndLoc);
3112 return;
3113 }
3114
3115 // If valid, this location is the position where we read the 'static' keyword.
3116 SourceLocation StaticLoc;
3117 if (Tok.is(tok::kw_static))
3118 StaticLoc = ConsumeToken();
3119
3120 // If there is a type-qualifier-list, read it now.
3121 // Type qualifiers in an array subscript are a C99 feature.
3122 DeclSpec DS;
3123 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
3124
3125 // If we haven't already read 'static', check to see if there is one after the
3126 // type-qualifier-list.
3127 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
3128 StaticLoc = ConsumeToken();
3129
3130 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3131 bool isStar = false;
3132 OwningExprResult NumElements(Actions);
3133
3134 // Handle the case where we have '[*]' as the array size. However, a leading
3135 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3136 // the the token after the star is a ']'. Since stars in arrays are
3137 // infrequent, use of lookahead is not costly here.
3138 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
3139 ConsumeToken(); // Eat the '*'.
3140
3141 if (StaticLoc.isValid()) {
3142 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
3143 StaticLoc = SourceLocation(); // Drop the static.
3144 }
3145 isStar = true;
3146 } else if (Tok.isNot(tok::r_square)) {
3147 // Note, in C89, this production uses the constant-expr production instead
3148 // of assignment-expr. The only difference is that assignment-expr allows
3149 // things like '=' and '*='. Sema rejects these in C89 mode because they
3150 // are not i-c-e's, so we don't need to distinguish between the two here.
3151
3152 // Parse the constant-expression or assignment-expression now (depending
3153 // on dialect).
3154 if (getLang().CPlusPlus)
3155 NumElements = ParseConstantExpression();
3156 else
3157 NumElements = ParseAssignmentExpression();
3158 }
3159
3160 // If there was an error parsing the assignment-expression, recover.
3161 if (NumElements.isInvalid()) {
3162 D.setInvalidType(true);
3163 // If the expression was invalid, skip it.
3164 SkipUntil(tok::r_square);
3165 return;
3166 }
3167
3168 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3169
3170 //FIXME: Use these
3171 CXX0XAttributeList Attr;
3172 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3173 Attr = ParseCXX0XAttributes();
3174 }
3175
3176 // Remember that we parsed a array type, and remember its features.
3177 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3178 StaticLoc.isValid(), isStar,
3179 NumElements.release(),
3180 StartLoc, EndLoc),
3181 EndLoc);
3182}
3183
3184/// [GNU] typeof-specifier:
3185/// typeof ( expressions )
3186/// typeof ( type-name )
3187/// [GNU/C++] typeof unary-expression
3188///
3189void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
3190 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
3191 Token OpTok = Tok;
3192 SourceLocation StartLoc = ConsumeToken();
3193
3194 const bool hasParens = Tok.is(tok::l_paren);
3195
3196 bool isCastExpr;
3197 TypeTy *CastTy;
3198 SourceRange CastRange;
3199 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3200 isCastExpr,
3201 CastTy,
3202 CastRange);
3203 if (hasParens)
3204 DS.setTypeofParensRange(CastRange);
3205
3206 if (CastRange.getEnd().isInvalid())
3207 // FIXME: Not accurate, the range gets one token more than it should.
3208 DS.SetRangeEnd(Tok.getLocation());
3209 else
3210 DS.SetRangeEnd(CastRange.getEnd());
3211
3212 if (isCastExpr) {
3213 if (!CastTy) {
3214 DS.SetTypeSpecError();
3215 return;
3216 }
3217
3218 const char *PrevSpec = 0;
3219 unsigned DiagID;
3220 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3221 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
3222 DiagID, CastTy))
3223 Diag(StartLoc, DiagID) << PrevSpec;
3224 return;
3225 }
3226
3227 // If we get here, the operand to the typeof was an expresion.
3228 if (Operand.isInvalid()) {
3229 DS.SetTypeSpecError();
3230 return;
3231 }
3232
3233 const char *PrevSpec = 0;
3234 unsigned DiagID;
3235 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3236 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
3237 DiagID, Operand.release()))
3238 Diag(StartLoc, DiagID) << PrevSpec;
3239}