blob: b667014efc70cb15e3e335b1b8ed1fc4c49a9ce4 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/ADT/SmallSet.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
25/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Douglas Gregor809070a2009-02-18 17:45:20 +000030Action::TypeResult Parser::ParseTypeName() {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
33 ParseSpecifierQualifierList(DS);
34
35 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
38
Chris Lattnereaaebc72009-04-25 08:06:05 +000039 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000040 return true;
41
42 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000043}
44
45/// ParseAttributes - Parse a non-empty attributes list.
46///
47/// [GNU] attributes:
48/// attribute
49/// attributes attribute
50///
51/// [GNU] attribute:
52/// '__attribute__' '(' '(' attribute-list ')' ')'
53///
54/// [GNU] attribute-list:
55/// attrib
56/// attribute_list ',' attrib
57///
58/// [GNU] attrib:
59/// empty
60/// attrib-name
61/// attrib-name '(' identifier ')'
62/// attrib-name '(' identifier ',' nonempty-expr-list ')'
63/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
64///
65/// [GNU] attrib-name:
66/// identifier
67/// typespec
68/// typequal
69/// storageclass
70///
71/// FIXME: The GCC grammar/code for this construct implies we need two
72/// token lookahead. Comment from gcc: "If they start with an identifier
73/// which is followed by a comma or close parenthesis, then the arguments
74/// start with that identifier; otherwise they are an expression list."
75///
76/// At the moment, I am not doing 2 token lookahead. I am also unaware of
77/// any attributes that don't work (based on my limited testing). Most
78/// attributes are very simple in practice. Until we find a bug, I don't see
79/// a pressing need to implement the 2 token lookahead.
80
Sebastian Redlab197ba2009-02-09 18:23:29 +000081AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000082 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000083
84 AttributeList *CurrAttr = 0;
85
Chris Lattner04d66662007-10-09 17:33:22 +000086 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000087 ConsumeToken();
88 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
89 "attribute")) {
90 SkipUntil(tok::r_paren, true); // skip until ) or ;
91 return CurrAttr;
92 }
93 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
94 SkipUntil(tok::r_paren, true); // skip until ) or ;
95 return CurrAttr;
96 }
97 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +000098 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
99 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000100
Chris Lattner04d66662007-10-09 17:33:22 +0000101 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
103 ConsumeToken();
104 continue;
105 }
106 // we have an identifier or declaration specifier (const, int, etc.)
107 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
108 SourceLocation AttrNameLoc = ConsumeToken();
109
110 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000111 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 ConsumeParen(); // ignore the left paren loc for now
113
Chris Lattner04d66662007-10-09 17:33:22 +0000114 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
116 SourceLocation ParmLoc = ConsumeToken();
117
Chris Lattner04d66662007-10-09 17:33:22 +0000118 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 // __attribute__(( mode(byte) ))
120 ConsumeParen(); // ignore the right paren loc for now
121 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
122 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000123 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 ConsumeToken();
125 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000126 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 bool ArgExprsOk = true;
128
129 // now parse the non-empty comma separated list of expressions
130 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000131 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000132 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 ArgExprsOk = false;
134 SkipUntil(tok::r_paren);
135 break;
136 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000137 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 }
Chris Lattner04d66662007-10-09 17:33:22 +0000139 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 break;
141 ConsumeToken(); // Eat the comma, move to the next argument
142 }
Chris Lattner04d66662007-10-09 17:33:22 +0000143 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 ConsumeParen(); // ignore the right paren loc for now
145 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000146 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 }
148 }
149 } else { // not an identifier
150 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000151 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 // __attribute__(( nonnull() ))
153 ConsumeParen(); // ignore the right paren loc for now
154 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
155 0, SourceLocation(), 0, 0, CurrAttr);
156 } else {
157 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000158 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 bool ArgExprsOk = true;
160
161 // now parse the list of expressions
162 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000163 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000164 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 ArgExprsOk = false;
166 SkipUntil(tok::r_paren);
167 break;
168 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000169 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 }
Chris Lattner04d66662007-10-09 17:33:22 +0000171 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 break;
173 ConsumeToken(); // Eat the comma, move to the next argument
174 }
175 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000176 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000178 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
179 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 CurrAttr);
181 }
182 }
183 }
184 } else {
185 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
186 0, SourceLocation(), 0, 0, CurrAttr);
187 }
188 }
189 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 SkipUntil(tok::r_paren, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000191 SourceLocation Loc = Tok.getLocation();;
192 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
193 SkipUntil(tok::r_paren, false);
194 }
195 if (EndLoc)
196 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 }
198 return CurrAttr;
199}
200
Steve Narofff59e17e2008-12-24 20:59:21 +0000201/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
202/// routine is called to skip/ignore tokens that comprise the MS declspec.
203void Parser::FuzzyParseMicrosoftDeclSpec() {
204 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
205 ConsumeToken();
206 if (Tok.is(tok::l_paren)) {
207 unsigned short savedParenCount = ParenCount;
208 do {
209 ConsumeAnyToken();
210 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
211 }
212 return;
213}
214
Reid Spencer5f016e22007-07-11 17:01:13 +0000215/// ParseDeclaration - Parse a full 'declaration', which consists of
216/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000217/// 'Context' should be a Declarator::TheContext value. This returns the
218/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000219///
220/// declaration: [C99 6.7]
221/// block-declaration ->
222/// simple-declaration
223/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000224/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000225/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000226/// [C++] using-directive
227/// [C++] using-declaration [TODO]
Sebastian Redl50de12f2009-03-24 22:27:57 +0000228/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000229/// others... [FIXME]
230///
Chris Lattner97144fc2009-04-02 04:16:50 +0000231Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
232 SourceLocation &DeclEnd) {
Chris Lattner682bf922009-03-29 16:50:03 +0000233 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000234 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000235 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000236 case tok::kw_export:
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000237 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000238 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000239 case tok::kw_namespace:
Chris Lattner97144fc2009-04-02 04:16:50 +0000240 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000241 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000242 case tok::kw_using:
Chris Lattner97144fc2009-04-02 04:16:50 +0000243 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000244 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000245 case tok::kw_static_assert:
Chris Lattner97144fc2009-04-02 04:16:50 +0000246 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000247 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000248 default:
Chris Lattner97144fc2009-04-02 04:16:50 +0000249 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000250 }
Chris Lattner682bf922009-03-29 16:50:03 +0000251
252 // This routine returns a DeclGroup, if the thing we parsed only contains a
253 // single decl, convert it now.
254 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000255}
256
257/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
258/// declaration-specifiers init-declarator-list[opt] ';'
259///[C90/C++]init-declarator-list ';' [TODO]
260/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000261///
262/// If RequireSemi is false, this does not check for a ';' at the end of the
263/// declaration.
264Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000265 SourceLocation &DeclEnd,
Chris Lattnercd147752009-03-29 17:27:48 +0000266 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 // Parse the common declaration-specifiers piece.
268 DeclSpec DS;
269 ParseDeclarationSpecifiers(DS);
270
271 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
272 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000273 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000275 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
276 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 }
278
279 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
280 ParseDeclarator(DeclaratorInfo);
281
Chris Lattner23c4b182009-03-29 17:18:04 +0000282 DeclGroupPtrTy DG =
283 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnercd147752009-03-29 17:27:48 +0000284
Chris Lattner97144fc2009-04-02 04:16:50 +0000285 DeclEnd = Tok.getLocation();
286
Chris Lattnercd147752009-03-29 17:27:48 +0000287 // If the client wants to check what comes after the declaration, just return
288 // immediately without checking anything!
289 if (!RequireSemi) return DG;
Chris Lattner23c4b182009-03-29 17:18:04 +0000290
291 if (Tok.is(tok::semi)) {
292 ConsumeToken();
Chris Lattner23c4b182009-03-29 17:18:04 +0000293 return DG;
294 }
295
Chris Lattner23c4b182009-03-29 17:18:04 +0000296 Diag(Tok, diag::err_expected_semi_declation);
297 // Skip to end of block or statement
298 SkipUntil(tok::r_brace, true, true);
299 if (Tok.is(tok::semi))
300 ConsumeToken();
301 return DG;
Reid Spencer5f016e22007-07-11 17:01:13 +0000302}
303
Douglas Gregor1426e532009-05-12 21:31:51 +0000304/// \brief Parse 'declaration' after parsing 'declaration-specifiers
305/// declarator'. This method parses the remainder of the declaration
306/// (including any attributes or initializer, among other things) and
307/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000308///
Reid Spencer5f016e22007-07-11 17:01:13 +0000309/// init-declarator: [C99 6.7]
310/// declarator
311/// declarator '=' initializer
312/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
313/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000314/// [C++] declarator initializer[opt]
315///
316/// [C++] initializer:
317/// [C++] '=' initializer-clause
318/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000319/// [C++0x] '=' 'default' [TODO]
320/// [C++0x] '=' 'delete'
321///
322/// According to the standard grammar, =default and =delete are function
323/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000324///
Douglas Gregor1426e532009-05-12 21:31:51 +0000325Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D) {
326 // If a simple-asm-expr is present, parse it.
327 if (Tok.is(tok::kw_asm)) {
328 SourceLocation Loc;
329 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
330 if (AsmLabel.isInvalid()) {
331 SkipUntil(tok::semi, true, true);
332 return DeclPtrTy();
333 }
334
335 D.setAsmLabel(AsmLabel.release());
336 D.SetRangeEnd(Loc);
337 }
338
339 // If attributes are present, parse them.
340 if (Tok.is(tok::kw___attribute)) {
341 SourceLocation Loc;
342 AttributeList *AttrList = ParseAttributes(&Loc);
343 D.AddAttributes(AttrList, Loc);
344 }
345
346 // Inform the current actions module that we just parsed this declarator.
347 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
348
349 // Parse declarator '=' initializer.
350 if (Tok.is(tok::equal)) {
351 ConsumeToken();
352 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
353 SourceLocation DelLoc = ConsumeToken();
354 Actions.SetDeclDeleted(ThisDecl, DelLoc);
355 } else {
356 OwningExprResult Init(ParseInitializer());
357 if (Init.isInvalid()) {
358 SkipUntil(tok::semi, true, true);
359 return DeclPtrTy();
360 }
361 Actions.AddInitializerToDecl(ThisDecl, move(Init));
362 }
363 } else if (Tok.is(tok::l_paren)) {
364 // Parse C++ direct initializer: '(' expression-list ')'
365 SourceLocation LParenLoc = ConsumeParen();
366 ExprVector Exprs(Actions);
367 CommaLocsTy CommaLocs;
368
369 if (ParseExpressionList(Exprs, CommaLocs)) {
370 SkipUntil(tok::r_paren);
371 } else {
372 // Match the ')'.
373 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
374
375 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
376 "Unexpected number of commas!");
377 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
378 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000379 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000380 }
381 } else {
382 Actions.ActOnUninitializedDecl(ThisDecl);
383 }
384
385 return ThisDecl;
386}
387
388/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
389/// parsing 'declaration-specifiers declarator'. This method is split out this
390/// way to handle the ambiguity between top-level function-definitions and
391/// declarations.
392///
393/// init-declarator-list: [C99 6.7]
394/// init-declarator
395/// init-declarator-list ',' init-declarator
396///
397/// According to the standard grammar, =default and =delete are function
398/// definitions, but that definitely doesn't fit with the parser here.
399///
Chris Lattner682bf922009-03-29 16:50:03 +0000400Parser::DeclGroupPtrTy Parser::
Reid Spencer5f016e22007-07-11 17:01:13 +0000401ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner682bf922009-03-29 16:50:03 +0000402 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
403 // that we parse together here.
404 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Reid Spencer5f016e22007-07-11 17:01:13 +0000405
406 // At this point, we know that it is not a function definition. Parse the
407 // rest of the init-declarator-list.
408 while (1) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000409 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
410 if (ThisDecl.get())
411 DeclsInGroup.push_back(ThisDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000412
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 // If we don't have a comma, it is either the end of the list (a ';') or an
414 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000415 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 break;
417
418 // Consume the comma.
419 ConsumeToken();
420
421 // Parse the next declarator.
422 D.clear();
Chris Lattneraab740a2008-10-20 04:57:38 +0000423
424 // Accept attributes in an init-declarator. In the first declarator in a
425 // declaration, these would be part of the declspec. In subsequent
426 // declarators, they become part of the declarator itself, so that they
427 // don't apply to declarators after *this* one. Examples:
428 // short __attribute__((common)) var; -> declspec
429 // short var __attribute__((common)); -> declarator
430 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlab197ba2009-02-09 18:23:29 +0000431 if (Tok.is(tok::kw___attribute)) {
432 SourceLocation Loc;
433 AttributeList *AttrList = ParseAttributes(&Loc);
434 D.AddAttributes(AttrList, Loc);
435 }
Chris Lattneraab740a2008-10-20 04:57:38 +0000436
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 ParseDeclarator(D);
438 }
439
Eli Friedmanc1dc6532009-05-29 01:49:24 +0000440 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
441 DeclsInGroup.data(),
Chris Lattner23c4b182009-03-29 17:18:04 +0000442 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000443}
444
445/// ParseSpecifierQualifierList
446/// specifier-qualifier-list:
447/// type-specifier specifier-qualifier-list[opt]
448/// type-qualifier specifier-qualifier-list[opt]
449/// [GNU] attributes specifier-qualifier-list[opt]
450///
451void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
452 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
453 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 ParseDeclarationSpecifiers(DS);
455
456 // Validate declspec for type-name.
457 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000458 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
459 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 Diag(Tok, diag::err_typename_requires_specqual);
461
462 // Issue diagnostic and remove storage class if present.
463 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
464 if (DS.getStorageClassSpecLoc().isValid())
465 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
466 else
467 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
468 DS.ClearStorageClassSpecs();
469 }
470
471 // Issue diagnostic and remove function specfier if present.
472 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000473 if (DS.isInlineSpecified())
474 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
475 if (DS.isVirtualSpecified())
476 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
477 if (DS.isExplicitSpecified())
478 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 DS.ClearFunctionSpecs();
480 }
481}
482
Chris Lattnerc199ab32009-04-12 20:42:31 +0000483/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
484/// specified token is valid after the identifier in a declarator which
485/// immediately follows the declspec. For example, these things are valid:
486///
487/// int x [ 4]; // direct-declarator
488/// int x ( int y); // direct-declarator
489/// int(int x ) // direct-declarator
490/// int x ; // simple-declaration
491/// int x = 17; // init-declarator-list
492/// int x , y; // init-declarator-list
493/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000494/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000495/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000496///
497/// This is not, because 'x' does not immediately follow the declspec (though
498/// ')' happens to be valid anyway).
499/// int (x)
500///
501static bool isValidAfterIdentifierInDeclarator(const Token &T) {
502 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
503 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000504 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000505}
506
Chris Lattnere40c2952009-04-14 21:34:55 +0000507
508/// ParseImplicitInt - This method is called when we have an non-typename
509/// identifier in a declspec (which normally terminates the decl spec) when
510/// the declspec has no type specifier. In this case, the declspec is either
511/// malformed or is "implicit int" (in K&R and C89).
512///
513/// This method handles diagnosing this prettily and returns false if the
514/// declspec is done being processed. If it recovers and thinks there may be
515/// other pieces of declspec after it, it returns true.
516///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000517bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000518 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000519 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000520 assert(Tok.is(tok::identifier) && "should have identifier");
521
Chris Lattnere40c2952009-04-14 21:34:55 +0000522 SourceLocation Loc = Tok.getLocation();
523 // If we see an identifier that is not a type name, we normally would
524 // parse it as the identifer being declared. However, when a typename
525 // is typo'd or the definition is not included, this will incorrectly
526 // parse the typename as the identifier name and fall over misparsing
527 // later parts of the diagnostic.
528 //
529 // As such, we try to do some look-ahead in cases where this would
530 // otherwise be an "implicit-int" case to see if this is invalid. For
531 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
532 // an identifier with implicit int, we'd get a parse error because the
533 // next token is obviously invalid for a type. Parse these as a case
534 // with an invalid type specifier.
535 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
536
537 // Since we know that this either implicit int (which is rare) or an
538 // error, we'd do lookahead to try to do better recovery.
539 if (isValidAfterIdentifierInDeclarator(NextToken())) {
540 // If this token is valid for implicit int, e.g. "static x = 4", then
541 // we just avoid eating the identifier, so it will be parsed as the
542 // identifier in the declarator.
543 return false;
544 }
545
546 // Otherwise, if we don't consume this token, we are going to emit an
547 // error anyway. Try to recover from various common problems. Check
548 // to see if this was a reference to a tag name without a tag specified.
549 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000550 //
551 // C++ doesn't need this, and isTagName doesn't take SS.
552 if (SS == 0) {
553 const char *TagName = 0;
554 tok::TokenKind TagKind = tok::unknown;
Chris Lattnere40c2952009-04-14 21:34:55 +0000555
Chris Lattnere40c2952009-04-14 21:34:55 +0000556 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
557 default: break;
558 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
559 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
560 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
561 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
562 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000563
Chris Lattnerf4382f52009-04-14 22:17:06 +0000564 if (TagName) {
565 Diag(Loc, diag::err_use_of_tag_name_without_tag)
566 << Tok.getIdentifierInfo() << TagName
567 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
568
569 // Parse this as a tag as if the missing tag were present.
570 if (TagKind == tok::kw_enum)
571 ParseEnumSpecifier(Loc, DS, AS);
572 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000573 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000574 return true;
575 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000576 }
577
578 // Since this is almost certainly an invalid type name, emit a
579 // diagnostic that says it, eat the token, and mark the declspec as
580 // invalid.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000581 SourceRange R;
582 if (SS) R = SS->getRange();
583
584 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattnere40c2952009-04-14 21:34:55 +0000585 const char *PrevSpec;
586 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
587 DS.SetRangeEnd(Tok.getLocation());
588 ConsumeToken();
589
590 // TODO: Could inject an invalid typedef decl in an enclosing scope to
591 // avoid rippling error messages on subsequent uses of the same type,
592 // could be useful if #include was forgotten.
593 return false;
594}
595
Reid Spencer5f016e22007-07-11 17:01:13 +0000596/// ParseDeclarationSpecifiers
597/// declaration-specifiers: [C99 6.7]
598/// storage-class-specifier declaration-specifiers[opt]
599/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000600/// [C99] function-specifier declaration-specifiers[opt]
601/// [GNU] attributes declaration-specifiers[opt]
602///
603/// storage-class-specifier: [C99 6.7.1]
604/// 'typedef'
605/// 'extern'
606/// 'static'
607/// 'auto'
608/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000609/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000610/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000611/// function-specifier: [C99 6.7.4]
612/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000613/// [C++] 'virtual'
614/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000615/// 'friend': [C++ dcl.friend]
616
Reid Spencer5f016e22007-07-11 17:01:13 +0000617///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000618void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000619 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerc199ab32009-04-12 20:42:31 +0000620 AccessSpecifier AS) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000621 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 while (1) {
623 int isInvalid = false;
624 const char *PrevSpec = 0;
625 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000626
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000628 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000629 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 // If this is not a declaration specifier token, we're done reading decl
631 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000632 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 return;
Chris Lattner5e02c472009-01-05 00:07:25 +0000634
635 case tok::coloncolon: // ::foo::bar
636 // Annotate C++ scope specifiers. If we get one, loop.
637 if (TryAnnotateCXXScopeToken())
638 continue;
639 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000640
641 case tok::annot_cxxscope: {
642 if (DS.hasTypeSpecifier())
643 goto DoneWithDeclSpec;
644
645 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000646 Token Next = NextToken();
647 if (Next.is(tok::annot_template_id) &&
648 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000649 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000650 // We have a qualified template-id, e.g., N::A<int>
651 CXXScopeSpec SS;
652 ParseOptionalCXXScopeSpecifier(SS);
653 assert(Tok.is(tok::annot_template_id) &&
654 "ParseOptionalCXXScopeSpecifier not working");
655 AnnotateTemplateIdTokenAsType(&SS);
656 continue;
657 }
658
659 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000660 goto DoneWithDeclSpec;
661
662 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000663 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000664 SS.setRange(Tok.getAnnotationRange());
665
666 // If the next token is the name of the class type that the C++ scope
667 // denotes, followed by a '(', then this is a constructor declaration.
668 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000669 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000670 CurScope, &SS) &&
671 GetLookAheadToken(2).is(tok::l_paren))
672 goto DoneWithDeclSpec;
673
Douglas Gregorb696ea32009-02-04 17:00:24 +0000674 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
675 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000676
Chris Lattnerf4382f52009-04-14 22:17:06 +0000677 // If the referenced identifier is not a type, then this declspec is
678 // erroneous: We already checked about that it has no type specifier, and
679 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
680 // typename.
681 if (TypeRep == 0) {
682 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000683 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000684 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000685 }
Douglas Gregore4e5b052009-03-19 00:18:19 +0000686
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000687 ConsumeToken(); // The C++ scope.
688
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000689 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000690 TypeRep);
691 if (isInvalid)
692 break;
693
694 DS.SetRangeEnd(Tok.getLocation());
695 ConsumeToken(); // The typename.
696
697 continue;
698 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000699
700 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000701 if (Tok.getAnnotationValue())
702 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
703 Tok.getAnnotationValue());
704 else
705 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000706 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
707 ConsumeToken(); // The typename
708
709 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
710 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
711 // Objective-C interface. If we don't have Objective-C or a '<', this is
712 // just a normal reference to a typedef name.
713 if (!Tok.is(tok::less) || !getLang().ObjC1)
714 continue;
715
716 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000717 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner80d0c892009-01-21 19:48:37 +0000718 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
719 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
720
721 DS.SetRangeEnd(EndProtoLoc);
722 continue;
723 }
724
Chris Lattner3bd934a2008-07-26 01:18:38 +0000725 // typedef-name
726 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000727 // In C++, check to see if this is a scope specifier like foo::bar::, if
728 // so handle it as such. This is important for ctor parsing.
Chris Lattner837acd02009-01-21 19:19:26 +0000729 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
730 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000731
Chris Lattner3bd934a2008-07-26 01:18:38 +0000732 // This identifier can only be a typedef name if we haven't already seen
733 // a type-specifier. Without this check we misparse:
734 // typedef int X; struct Y { short X; }; as 'short int'.
735 if (DS.hasTypeSpecifier())
736 goto DoneWithDeclSpec;
737
738 // It has to be available as a typedef too!
Douglas Gregorb696ea32009-02-04 17:00:24 +0000739 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
740 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000741
Chris Lattnerc199ab32009-04-12 20:42:31 +0000742 // If this is not a typedef name, don't parse it as part of the declspec,
743 // it must be an implicit int or an error.
744 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000745 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000746 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000747 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000748
Douglas Gregorb48fe382008-10-31 09:07:45 +0000749 // C++: If the identifier is actually the name of the class type
750 // being defined and the next token is a '(', then this is a
751 // constructor declaration. We're done with the decl-specifiers
752 // and will treat this token as an identifier.
Chris Lattnerc199ab32009-04-12 20:42:31 +0000753 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000754 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
755 NextToken().getKind() == tok::l_paren)
756 goto DoneWithDeclSpec;
757
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000758 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner3bd934a2008-07-26 01:18:38 +0000759 TypeRep);
760 if (isInvalid)
761 break;
762
763 DS.SetRangeEnd(Tok.getLocation());
764 ConsumeToken(); // The identifier
765
766 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
767 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
768 // Objective-C interface. If we don't have Objective-C or a '<', this is
769 // just a normal reference to a typedef name.
770 if (!Tok.is(tok::less) || !getLang().ObjC1)
771 continue;
772
773 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000774 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000775 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000776 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000777
778 DS.SetRangeEnd(EndProtoLoc);
779
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000780 // Need to support trailing type qualifiers (e.g. "id<p> const").
781 // If a type specifier follows, it will be diagnosed elsewhere.
782 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000783 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000784
785 // type-name
786 case tok::annot_template_id: {
787 TemplateIdAnnotation *TemplateId
788 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000789 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000790 // This template-id does not refer to a type name, so we're
791 // done with the type-specifiers.
792 goto DoneWithDeclSpec;
793 }
794
795 // Turn the template-id annotation token into a type annotation
796 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000797 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000798 continue;
799 }
800
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 // GNU attributes support.
802 case tok::kw___attribute:
803 DS.AddAttributes(ParseAttributes());
804 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000805
806 // Microsoft declspec support.
807 case tok::kw___declspec:
808 if (!PP.getLangOptions().Microsoft)
809 goto DoneWithDeclSpec;
810 FuzzyParseMicrosoftDeclSpec();
811 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000812
Steve Naroff239f0732008-12-25 14:16:32 +0000813 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000814 case tok::kw___forceinline:
815 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000816 case tok::kw___cdecl:
817 case tok::kw___stdcall:
818 case tok::kw___fastcall:
819 if (!PP.getLangOptions().Microsoft)
820 goto DoneWithDeclSpec;
821 // Just ignore it.
822 break;
823
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 // storage-class-specifier
825 case tok::kw_typedef:
826 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
827 break;
828 case tok::kw_extern:
829 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000830 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
832 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000833 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000834 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
835 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000836 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 case tok::kw_static:
838 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000839 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
841 break;
842 case tok::kw_auto:
843 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
844 break;
845 case tok::kw_register:
846 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
847 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000848 case tok::kw_mutable:
849 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
850 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 case tok::kw___thread:
852 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
853 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000854
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 // function-specifier
856 case tok::kw_inline:
857 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
858 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000859 case tok::kw_virtual:
860 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
861 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000862 case tok::kw_explicit:
863 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
864 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000865
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000866 // friend
867 case tok::kw_friend:
868 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
869 break;
870
Chris Lattner80d0c892009-01-21 19:48:37 +0000871 // type-specifier
872 case tok::kw_short:
873 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
874 break;
875 case tok::kw_long:
876 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
877 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
878 else
879 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
880 break;
881 case tok::kw_signed:
882 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
883 break;
884 case tok::kw_unsigned:
885 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
886 break;
887 case tok::kw__Complex:
888 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
889 break;
890 case tok::kw__Imaginary:
891 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
892 break;
893 case tok::kw_void:
894 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
895 break;
896 case tok::kw_char:
897 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
898 break;
899 case tok::kw_int:
900 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
901 break;
902 case tok::kw_float:
903 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
904 break;
905 case tok::kw_double:
906 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
907 break;
908 case tok::kw_wchar_t:
909 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
910 break;
911 case tok::kw_bool:
912 case tok::kw__Bool:
913 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
914 break;
915 case tok::kw__Decimal32:
916 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
917 break;
918 case tok::kw__Decimal64:
919 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
920 break;
921 case tok::kw__Decimal128:
922 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
923 break;
924
925 // class-specifier:
926 case tok::kw_class:
927 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +0000928 case tok::kw_union: {
929 tok::TokenKind Kind = Tok.getKind();
930 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000931 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +0000932 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +0000933 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000934
935 // enum-specifier:
936 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +0000937 ConsumeToken();
938 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +0000939 continue;
940
941 // cv-qualifier:
942 case tok::kw_const:
943 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
944 break;
945 case tok::kw_volatile:
946 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
947 getLang())*2;
948 break;
949 case tok::kw_restrict:
950 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
951 getLang())*2;
952 break;
953
Douglas Gregord57959a2009-03-27 23:10:48 +0000954 // C++ typename-specifier:
955 case tok::kw_typename:
956 if (TryAnnotateTypeOrScopeToken())
957 continue;
958 break;
959
Chris Lattner80d0c892009-01-21 19:48:37 +0000960 // GNU typeof support.
961 case tok::kw_typeof:
962 ParseTypeofSpecifier(DS);
963 continue;
964
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000965 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000966 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000967 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
968 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000969 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000970 goto DoneWithDeclSpec;
971
972 {
973 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000974 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000975 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000976 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000977 DS.SetRangeEnd(EndProtoLoc);
978
Chris Lattner1ab3b962008-11-18 07:48:38 +0000979 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +0000980 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +0000981 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000982 // Need to support trailing type qualifiers (e.g. "id<p> const").
983 // If a type specifier follows, it will be diagnosed elsewhere.
984 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000985 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 }
987 // If the specifier combination wasn't legal, issue a diagnostic.
988 if (isInvalid) {
989 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000990 // Pick between error or extwarn.
991 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
992 : diag::ext_duplicate_declspec;
993 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000995 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 ConsumeToken();
997 }
998}
Douglas Gregoradcac882008-12-01 23:54:00 +0000999
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001000/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001001/// primarily follow the C++ grammar with additions for C99 and GNU,
1002/// which together subsume the C grammar. Note that the C++
1003/// type-specifier also includes the C type-qualifier (for const,
1004/// volatile, and C99 restrict). Returns true if a type-specifier was
1005/// found (and parsed), false otherwise.
1006///
1007/// type-specifier: [C++ 7.1.5]
1008/// simple-type-specifier
1009/// class-specifier
1010/// enum-specifier
1011/// elaborated-type-specifier [TODO]
1012/// cv-qualifier
1013///
1014/// cv-qualifier: [C++ 7.1.5.1]
1015/// 'const'
1016/// 'volatile'
1017/// [C99] 'restrict'
1018///
1019/// simple-type-specifier: [ C++ 7.1.5.2]
1020/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1021/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1022/// 'char'
1023/// 'wchar_t'
1024/// 'bool'
1025/// 'short'
1026/// 'int'
1027/// 'long'
1028/// 'signed'
1029/// 'unsigned'
1030/// 'float'
1031/// 'double'
1032/// 'void'
1033/// [C99] '_Bool'
1034/// [C99] '_Complex'
1035/// [C99] '_Imaginary' // Removed in TC2?
1036/// [GNU] '_Decimal32'
1037/// [GNU] '_Decimal64'
1038/// [GNU] '_Decimal128'
1039/// [GNU] typeof-specifier
1040/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1041/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001042bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1043 const char *&PrevSpec,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001044 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001045 SourceLocation Loc = Tok.getLocation();
1046
1047 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001048 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001049 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001050 // Annotate typenames and C++ scope specifiers. If we get one, just
1051 // recurse to handle whatever we get.
1052 if (TryAnnotateTypeOrScopeToken())
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001053 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001054 // Otherwise, not a type specifier.
1055 return false;
1056 case tok::coloncolon: // ::foo::bar
1057 if (NextToken().is(tok::kw_new) || // ::new
1058 NextToken().is(tok::kw_delete)) // ::delete
1059 return false;
1060
1061 // Annotate typenames and C++ scope specifiers. If we get one, just
1062 // recurse to handle whatever we get.
1063 if (TryAnnotateTypeOrScopeToken())
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001064 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001065 // Otherwise, not a type specifier.
1066 return false;
1067
Douglas Gregor12e083c2008-11-07 15:42:26 +00001068 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001069 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001070 if (Tok.getAnnotationValue())
1071 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1072 Tok.getAnnotationValue());
1073 else
1074 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001075 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1076 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +00001077
1078 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1079 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1080 // Objective-C interface. If we don't have Objective-C or a '<', this is
1081 // just a normal reference to a typedef name.
1082 if (!Tok.is(tok::less) || !getLang().ObjC1)
1083 return true;
1084
1085 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001086 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001087 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1088 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1089
1090 DS.SetRangeEnd(EndProtoLoc);
1091 return true;
1092 }
1093
1094 case tok::kw_short:
1095 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1096 break;
1097 case tok::kw_long:
1098 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1099 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1100 else
1101 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1102 break;
1103 case tok::kw_signed:
1104 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1105 break;
1106 case tok::kw_unsigned:
1107 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1108 break;
1109 case tok::kw__Complex:
1110 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1111 break;
1112 case tok::kw__Imaginary:
1113 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1114 break;
1115 case tok::kw_void:
1116 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1117 break;
1118 case tok::kw_char:
1119 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1120 break;
1121 case tok::kw_int:
1122 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1123 break;
1124 case tok::kw_float:
1125 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1126 break;
1127 case tok::kw_double:
1128 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1129 break;
1130 case tok::kw_wchar_t:
1131 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1132 break;
1133 case tok::kw_bool:
1134 case tok::kw__Bool:
1135 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1136 break;
1137 case tok::kw__Decimal32:
1138 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1139 break;
1140 case tok::kw__Decimal64:
1141 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1142 break;
1143 case tok::kw__Decimal128:
1144 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1145 break;
1146
1147 // class-specifier:
1148 case tok::kw_class:
1149 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001150 case tok::kw_union: {
1151 tok::TokenKind Kind = Tok.getKind();
1152 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001153 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001154 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001155 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001156
1157 // enum-specifier:
1158 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001159 ConsumeToken();
1160 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001161 return true;
1162
1163 // cv-qualifier:
1164 case tok::kw_const:
1165 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1166 getLang())*2;
1167 break;
1168 case tok::kw_volatile:
1169 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1170 getLang())*2;
1171 break;
1172 case tok::kw_restrict:
1173 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1174 getLang())*2;
1175 break;
1176
1177 // GNU typeof support.
1178 case tok::kw_typeof:
1179 ParseTypeofSpecifier(DS);
1180 return true;
1181
Steve Naroff239f0732008-12-25 14:16:32 +00001182 case tok::kw___cdecl:
1183 case tok::kw___stdcall:
1184 case tok::kw___fastcall:
Chris Lattner837acd02009-01-21 19:19:26 +00001185 if (!PP.getLangOptions().Microsoft) return false;
1186 ConsumeToken();
1187 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001188
Douglas Gregor12e083c2008-11-07 15:42:26 +00001189 default:
1190 // Not a type-specifier; do nothing.
1191 return false;
1192 }
1193
1194 // If the specifier combination wasn't legal, issue a diagnostic.
1195 if (isInvalid) {
1196 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001197 // Pick between error or extwarn.
1198 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1199 : diag::ext_duplicate_declspec;
1200 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001201 }
1202 DS.SetRangeEnd(Tok.getLocation());
1203 ConsumeToken(); // whatever we parsed above.
1204 return true;
1205}
Reid Spencer5f016e22007-07-11 17:01:13 +00001206
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001207/// ParseStructDeclaration - Parse a struct declaration without the terminating
1208/// semicolon.
1209///
Reid Spencer5f016e22007-07-11 17:01:13 +00001210/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001211/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001212/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001213/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001214/// struct-declarator-list:
1215/// struct-declarator
1216/// struct-declarator-list ',' struct-declarator
1217/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1218/// struct-declarator:
1219/// declarator
1220/// [GNU] declarator attributes[opt]
1221/// declarator[opt] ':' constant-expression
1222/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1223///
Chris Lattnere1359422008-04-10 06:46:29 +00001224void Parser::
1225ParseStructDeclaration(DeclSpec &DS,
1226 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001227 if (Tok.is(tok::kw___extension__)) {
1228 // __extension__ silences extension warnings in the subexpression.
1229 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001230 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001231 return ParseStructDeclaration(DS, Fields);
1232 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001233
1234 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001235 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001236 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001237
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001238 // If there are no declarators, this is a free-standing declaration
1239 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001240 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001241 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001242 return;
1243 }
1244
1245 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001246 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001247 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001248 FieldDeclarator &DeclaratorInfo = Fields.back();
1249
Steve Naroff28a7ca82007-08-20 22:28:22 +00001250 /// struct-declarator: declarator
1251 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001252 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001253 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001254
Chris Lattner04d66662007-10-09 17:33:22 +00001255 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001256 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001257 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001258 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001259 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001260 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001261 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001262 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001263
Steve Naroff28a7ca82007-08-20 22:28:22 +00001264 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001265 if (Tok.is(tok::kw___attribute)) {
1266 SourceLocation Loc;
1267 AttributeList *AttrList = ParseAttributes(&Loc);
1268 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1269 }
1270
Steve Naroff28a7ca82007-08-20 22:28:22 +00001271 // If we don't have a comma, it is either the end of the list (a ';')
1272 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001273 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001274 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001275
Steve Naroff28a7ca82007-08-20 22:28:22 +00001276 // Consume the comma.
1277 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001278
Steve Naroff28a7ca82007-08-20 22:28:22 +00001279 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001280 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001281
Steve Naroff28a7ca82007-08-20 22:28:22 +00001282 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001283 if (Tok.is(tok::kw___attribute)) {
1284 SourceLocation Loc;
1285 AttributeList *AttrList = ParseAttributes(&Loc);
1286 Fields.back().D.AddAttributes(AttrList, Loc);
1287 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001288 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001289}
1290
1291/// ParseStructUnionBody
1292/// struct-contents:
1293/// struct-declaration-list
1294/// [EXT] empty
1295/// [GNU] "struct-declaration-list" without terminatoring ';'
1296/// struct-declaration-list:
1297/// struct-declaration
1298/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001299/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001300///
Reid Spencer5f016e22007-07-11 17:01:13 +00001301void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001302 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001303 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1304 PP.getSourceManager(),
1305 "parsing struct/union body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001306
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 SourceLocation LBraceLoc = ConsumeBrace();
1308
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001309 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001310 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1311
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1313 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001314 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001315 Diag(Tok, diag::ext_empty_struct_union_enum)
1316 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001317
Chris Lattnerb28317a2009-03-28 19:18:32 +00001318 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001319 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1320
Reid Spencer5f016e22007-07-11 17:01:13 +00001321 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001322 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 // Each iteration of this loop reads one struct-declaration.
1324
1325 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001326 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001327 Diag(Tok, diag::ext_extra_struct_semi)
1328 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 ConsumeToken();
1330 continue;
1331 }
Chris Lattnere1359422008-04-10 06:46:29 +00001332
1333 // Parse all the comma separated declarators.
1334 DeclSpec DS;
1335 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001336 if (!Tok.is(tok::at)) {
1337 ParseStructDeclaration(DS, FieldDeclarators);
1338
1339 // Convert them all to fields.
1340 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1341 FieldDeclarator &FD = FieldDeclarators[i];
1342 // Install the declarator into the current TagDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001343 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1344 DS.getSourceRange().getBegin(),
1345 FD.D, FD.BitfieldSize);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001346 FieldDecls.push_back(Field);
1347 }
1348 } else { // Handle @defs
1349 ConsumeToken();
1350 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1351 Diag(Tok, diag::err_unexpected_at);
1352 SkipUntil(tok::semi, true, true);
1353 continue;
1354 }
1355 ConsumeToken();
1356 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1357 if (!Tok.is(tok::identifier)) {
1358 Diag(Tok, diag::err_expected_ident);
1359 SkipUntil(tok::semi, true, true);
1360 continue;
1361 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001362 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001363 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1364 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001365 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1366 ConsumeToken();
1367 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1368 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001369
Chris Lattner04d66662007-10-09 17:33:22 +00001370 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001371 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001372 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001373 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 break;
1375 } else {
1376 Diag(Tok, diag::err_expected_semi_decl_list);
1377 // Skip to end of block or statement
1378 SkipUntil(tok::r_brace, true, true);
1379 }
1380 }
1381
Steve Naroff60fccee2007-10-29 21:38:07 +00001382 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001383
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 AttributeList *AttrList = 0;
1385 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001386 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001387 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001388
1389 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001390 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001391 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001392 AttrList);
1393 StructScope.Exit();
1394 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001395}
1396
1397
1398/// ParseEnumSpecifier
1399/// enum-specifier: [C99 6.7.2.2]
1400/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001401///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001402/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1403/// '}' attributes[opt]
1404/// 'enum' identifier
1405/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001406///
1407/// [C++] elaborated-type-specifier:
1408/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1409///
Chris Lattner4c97d762009-04-12 21:49:30 +00001410void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1411 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001412 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001413
1414 AttributeList *Attr = 0;
1415 // If attributes exist after tag, parse them.
1416 if (Tok.is(tok::kw___attribute))
1417 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001418
1419 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001420 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001421 if (Tok.isNot(tok::identifier)) {
1422 Diag(Tok, diag::err_expected_ident);
1423 if (Tok.isNot(tok::l_brace)) {
1424 // Has no name and is not a definition.
1425 // Skip the rest of this declarator, up until the comma or semicolon.
1426 SkipUntil(tok::comma, true);
1427 return;
1428 }
1429 }
1430 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001431
1432 // Must have either 'enum name' or 'enum {...}'.
1433 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1434 Diag(Tok, diag::err_expected_ident_lbrace);
1435
1436 // Skip the rest of this declarator, up until the comma or semicolon.
1437 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001438 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001439 }
1440
1441 // If an identifier is present, consume and remember it.
1442 IdentifierInfo *Name = 0;
1443 SourceLocation NameLoc;
1444 if (Tok.is(tok::identifier)) {
1445 Name = Tok.getIdentifierInfo();
1446 NameLoc = ConsumeToken();
1447 }
1448
1449 // There are three options here. If we have 'enum foo;', then this is a
1450 // forward declaration. If we have 'enum foo {...' then this is a
1451 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1452 //
1453 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1454 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1455 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1456 //
1457 Action::TagKind TK;
1458 if (Tok.is(tok::l_brace))
1459 TK = Action::TK_Definition;
1460 else if (Tok.is(tok::semi))
1461 TK = Action::TK_Declaration;
1462 else
1463 TK = Action::TK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001464 bool Owned = false;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001465 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001466 StartLoc, SS, Name, NameLoc, Attr, AS,
1467 Owned);
Reid Spencer5f016e22007-07-11 17:01:13 +00001468
Chris Lattner04d66662007-10-09 17:33:22 +00001469 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001470 ParseEnumBody(StartLoc, TagDecl);
1471
1472 // TODO: semantic analysis on the declspec for enums.
1473 const char *PrevSpec = 0;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001474 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
Douglas Gregor402abb52009-05-28 23:31:59 +00001475 TagDecl.getAs<void>(), Owned))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001476 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001477}
1478
1479/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1480/// enumerator-list:
1481/// enumerator
1482/// enumerator-list ',' enumerator
1483/// enumerator:
1484/// enumeration-constant
1485/// enumeration-constant '=' constant-expression
1486/// enumeration-constant:
1487/// identifier
1488///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001489void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001490 // Enter the scope of the enum body and start the definition.
1491 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001492 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001493
Reid Spencer5f016e22007-07-11 17:01:13 +00001494 SourceLocation LBraceLoc = ConsumeBrace();
1495
Chris Lattner7946dd32007-08-27 17:24:30 +00001496 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001497 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001498 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001499
Chris Lattnerb28317a2009-03-28 19:18:32 +00001500 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001501
Chris Lattnerb28317a2009-03-28 19:18:32 +00001502 DeclPtrTy LastEnumConstDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001503
1504 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001505 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001506 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1507 SourceLocation IdentLoc = ConsumeToken();
1508
1509 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001510 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001511 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001512 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001513 AssignedVal = ParseConstantExpression();
1514 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001515 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001516 }
1517
1518 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001519 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1520 LastEnumConstDecl,
1521 IdentLoc, Ident,
1522 EqualLoc,
1523 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 EnumConstantDecls.push_back(EnumConstDecl);
1525 LastEnumConstDecl = EnumConstDecl;
1526
Chris Lattner04d66662007-10-09 17:33:22 +00001527 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 break;
1529 SourceLocation CommaLoc = ConsumeToken();
1530
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001531 if (Tok.isNot(tok::identifier) &&
1532 !(getLang().C99 || getLang().CPlusPlus0x))
1533 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1534 << getLang().CPlusPlus
1535 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001536 }
1537
1538 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001539 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001540
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001541 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001542 EnumConstantDecls.data(), EnumConstantDecls.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001543
Chris Lattnerb28317a2009-03-28 19:18:32 +00001544 Action::AttrTy *AttrList = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001545 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001546 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001547 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001548
1549 EnumScope.Exit();
1550 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001551}
1552
1553/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001554/// start of a type-qualifier-list.
1555bool Parser::isTypeQualifier() const {
1556 switch (Tok.getKind()) {
1557 default: return false;
1558 // type-qualifier
1559 case tok::kw_const:
1560 case tok::kw_volatile:
1561 case tok::kw_restrict:
1562 return true;
1563 }
1564}
1565
1566/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001567/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001568bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001569 switch (Tok.getKind()) {
1570 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001571
1572 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001573 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001574 // Annotate typenames and C++ scope specifiers. If we get one, just
1575 // recurse to handle whatever we get.
1576 if (TryAnnotateTypeOrScopeToken())
1577 return isTypeSpecifierQualifier();
1578 // Otherwise, not a type specifier.
1579 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001580
Chris Lattner166a8fc2009-01-04 23:41:41 +00001581 case tok::coloncolon: // ::foo::bar
1582 if (NextToken().is(tok::kw_new) || // ::new
1583 NextToken().is(tok::kw_delete)) // ::delete
1584 return false;
1585
1586 // Annotate typenames and C++ scope specifiers. If we get one, just
1587 // recurse to handle whatever we get.
1588 if (TryAnnotateTypeOrScopeToken())
1589 return isTypeSpecifierQualifier();
1590 // Otherwise, not a type specifier.
1591 return false;
1592
Reid Spencer5f016e22007-07-11 17:01:13 +00001593 // GNU attributes support.
1594 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001595 // GNU typeof support.
1596 case tok::kw_typeof:
1597
Reid Spencer5f016e22007-07-11 17:01:13 +00001598 // type-specifiers
1599 case tok::kw_short:
1600 case tok::kw_long:
1601 case tok::kw_signed:
1602 case tok::kw_unsigned:
1603 case tok::kw__Complex:
1604 case tok::kw__Imaginary:
1605 case tok::kw_void:
1606 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001607 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001608 case tok::kw_int:
1609 case tok::kw_float:
1610 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001611 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001612 case tok::kw__Bool:
1613 case tok::kw__Decimal32:
1614 case tok::kw__Decimal64:
1615 case tok::kw__Decimal128:
1616
Chris Lattner99dc9142008-04-13 18:59:07 +00001617 // struct-or-union-specifier (C99) or class-specifier (C++)
1618 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001619 case tok::kw_struct:
1620 case tok::kw_union:
1621 // enum-specifier
1622 case tok::kw_enum:
1623
1624 // type-qualifier
1625 case tok::kw_const:
1626 case tok::kw_volatile:
1627 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001628
1629 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001630 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001631 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001632
1633 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1634 case tok::less:
1635 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001636
1637 case tok::kw___cdecl:
1638 case tok::kw___stdcall:
1639 case tok::kw___fastcall:
1640 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001641 }
1642}
1643
1644/// isDeclarationSpecifier() - Return true if the current token is part of a
1645/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001646bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 switch (Tok.getKind()) {
1648 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001649
1650 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001651 // Unfortunate hack to support "Class.factoryMethod" notation.
1652 if (getLang().ObjC1 && NextToken().is(tok::period))
1653 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001654 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001655
Douglas Gregord57959a2009-03-27 23:10:48 +00001656 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001657 // Annotate typenames and C++ scope specifiers. If we get one, just
1658 // recurse to handle whatever we get.
1659 if (TryAnnotateTypeOrScopeToken())
1660 return isDeclarationSpecifier();
1661 // Otherwise, not a declaration specifier.
1662 return false;
1663 case tok::coloncolon: // ::foo::bar
1664 if (NextToken().is(tok::kw_new) || // ::new
1665 NextToken().is(tok::kw_delete)) // ::delete
1666 return false;
1667
1668 // Annotate typenames and C++ scope specifiers. If we get one, just
1669 // recurse to handle whatever we get.
1670 if (TryAnnotateTypeOrScopeToken())
1671 return isDeclarationSpecifier();
1672 // Otherwise, not a declaration specifier.
1673 return false;
1674
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 // storage-class-specifier
1676 case tok::kw_typedef:
1677 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001678 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001679 case tok::kw_static:
1680 case tok::kw_auto:
1681 case tok::kw_register:
1682 case tok::kw___thread:
1683
1684 // type-specifiers
1685 case tok::kw_short:
1686 case tok::kw_long:
1687 case tok::kw_signed:
1688 case tok::kw_unsigned:
1689 case tok::kw__Complex:
1690 case tok::kw__Imaginary:
1691 case tok::kw_void:
1692 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001693 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001694 case tok::kw_int:
1695 case tok::kw_float:
1696 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001697 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 case tok::kw__Bool:
1699 case tok::kw__Decimal32:
1700 case tok::kw__Decimal64:
1701 case tok::kw__Decimal128:
1702
Chris Lattner99dc9142008-04-13 18:59:07 +00001703 // struct-or-union-specifier (C99) or class-specifier (C++)
1704 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001705 case tok::kw_struct:
1706 case tok::kw_union:
1707 // enum-specifier
1708 case tok::kw_enum:
1709
1710 // type-qualifier
1711 case tok::kw_const:
1712 case tok::kw_volatile:
1713 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001714
Reid Spencer5f016e22007-07-11 17:01:13 +00001715 // function-specifier
1716 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001717 case tok::kw_virtual:
1718 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001719
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001720 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001721 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001722
Chris Lattner1ef08762007-08-09 17:01:07 +00001723 // GNU typeof support.
1724 case tok::kw_typeof:
1725
1726 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001727 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001728 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001729
1730 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1731 case tok::less:
1732 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001733
Steve Naroff47f52092009-01-06 19:34:12 +00001734 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001735 case tok::kw___cdecl:
1736 case tok::kw___stdcall:
1737 case tok::kw___fastcall:
1738 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001739 }
1740}
1741
1742
1743/// ParseTypeQualifierListOpt
1744/// type-qualifier-list: [C99 6.7.5]
1745/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001746/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001747/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001748/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001749///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001750void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001751 while (1) {
1752 int isInvalid = false;
1753 const char *PrevSpec = 0;
1754 SourceLocation Loc = Tok.getLocation();
1755
1756 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001757 case tok::kw_const:
1758 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1759 getLang())*2;
1760 break;
1761 case tok::kw_volatile:
1762 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1763 getLang())*2;
1764 break;
1765 case tok::kw_restrict:
1766 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1767 getLang())*2;
1768 break;
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001769 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001770 case tok::kw___cdecl:
1771 case tok::kw___stdcall:
1772 case tok::kw___fastcall:
1773 if (!PP.getLangOptions().Microsoft)
1774 goto DoneWithTypeQuals;
1775 // Just ignore it.
1776 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001778 if (AttributesAllowed) {
1779 DS.AddAttributes(ParseAttributes());
1780 continue; // do *not* consume the next token!
1781 }
1782 // otherwise, FALL THROUGH!
1783 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001784 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001785 // If this is not a type-qualifier token, we're done reading type
1786 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001787 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001788 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001789 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001790
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 // If the specifier combination wasn't legal, issue a diagnostic.
1792 if (isInvalid) {
1793 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001794 // Pick between error or extwarn.
1795 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1796 : diag::ext_duplicate_declspec;
1797 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 }
1799 ConsumeToken();
1800 }
1801}
1802
1803
1804/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1805///
1806void Parser::ParseDeclarator(Declarator &D) {
1807 /// This implements the 'declarator' production in the C grammar, then checks
1808 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001809 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001810}
1811
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001812/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1813/// is parsed by the function passed to it. Pass null, and the direct-declarator
1814/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001815/// ptr-operator production.
1816///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001817/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1818/// [C] pointer[opt] direct-declarator
1819/// [C++] direct-declarator
1820/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001821///
1822/// pointer: [C99 6.7.5]
1823/// '*' type-qualifier-list[opt]
1824/// '*' type-qualifier-list[opt] pointer
1825///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001826/// ptr-operator:
1827/// '*' cv-qualifier-seq[opt]
1828/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00001829/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001830/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00001831/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001832/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001833void Parser::ParseDeclaratorInternal(Declarator &D,
1834 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001835
Sebastian Redlf30208a2009-01-24 21:16:55 +00001836 // C++ member pointers start with a '::' or a nested-name.
1837 // Member pointers get special handling, since there's no place for the
1838 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00001839 if (getLang().CPlusPlus &&
1840 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1841 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001842 CXXScopeSpec SS;
1843 if (ParseOptionalCXXScopeSpecifier(SS)) {
1844 if(Tok.isNot(tok::star)) {
1845 // The scope spec really belongs to the direct-declarator.
1846 D.getCXXScopeSpec() = SS;
1847 if (DirectDeclParser)
1848 (this->*DirectDeclParser)(D);
1849 return;
1850 }
1851
1852 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001853 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001854 DeclSpec DS;
1855 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001856 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001857
1858 // Recurse to parse whatever is left.
1859 ParseDeclaratorInternal(D, DirectDeclParser);
1860
1861 // Sema will have to catch (syntactically invalid) pointers into global
1862 // scope. It has to catch pointers into namespace scope anyway.
1863 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001864 Loc, DS.TakeAttributes()),
1865 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00001866 return;
1867 }
1868 }
1869
1870 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001871 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00001872 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00001873 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00001874 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00001875 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001876 if (DirectDeclParser)
1877 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001878 return;
1879 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001880
Sebastian Redl05532f22009-03-15 22:02:01 +00001881 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1882 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00001883 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001884 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001885
Chris Lattner9af55002009-03-27 04:18:06 +00001886 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00001887 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001888 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001889
Reid Spencer5f016e22007-07-11 17:01:13 +00001890 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001891 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001892
Reid Spencer5f016e22007-07-11 17:01:13 +00001893 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001894 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001895 if (Kind == tok::star)
1896 // Remember that we parsed a pointer type, and remember the type-quals.
1897 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001898 DS.TakeAttributes()),
1899 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00001900 else
1901 // Remember that we parsed a Block type, and remember the type-quals.
1902 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00001903 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001904 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001905 } else {
1906 // Is a reference
1907 DeclSpec DS;
1908
Sebastian Redl743de1f2009-03-23 00:00:23 +00001909 // Complain about rvalue references in C++03, but then go on and build
1910 // the declarator.
1911 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1912 Diag(Loc, diag::err_rvalue_reference);
1913
Reid Spencer5f016e22007-07-11 17:01:13 +00001914 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1915 // cv-qualifiers are introduced through the use of a typedef or of a
1916 // template type argument, in which case the cv-qualifiers are ignored.
1917 //
1918 // [GNU] Retricted references are allowed.
1919 // [GNU] Attributes on references are allowed.
1920 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001921 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001922
1923 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1924 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1925 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001926 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001927 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1928 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001929 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001930 }
1931
1932 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001933 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001934
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001935 if (D.getNumTypeObjects() > 0) {
1936 // C++ [dcl.ref]p4: There shall be no references to references.
1937 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1938 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001939 if (const IdentifierInfo *II = D.getIdentifier())
1940 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1941 << II;
1942 else
1943 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1944 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001945
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001946 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001947 // can go ahead and build the (technically ill-formed)
1948 // declarator: reference collapsing will take care of it.
1949 }
1950 }
1951
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001953 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00001954 DS.TakeAttributes(),
1955 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001956 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001957 }
1958}
1959
1960/// ParseDirectDeclarator
1961/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001962/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001963/// '(' declarator ')'
1964/// [GNU] '(' attributes declarator ')'
1965/// [C90] direct-declarator '[' constant-expression[opt] ']'
1966/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1967/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1968/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1969/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1970/// direct-declarator '(' parameter-type-list ')'
1971/// direct-declarator '(' identifier-list[opt] ')'
1972/// [GNU] direct-declarator '(' parameter-forward-declarations
1973/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001974/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1975/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001976/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001977///
1978/// declarator-id: [C++ 8]
1979/// id-expression
1980/// '::'[opt] nested-name-specifier[opt] type-name
1981///
1982/// id-expression: [C++ 5.1]
1983/// unqualified-id
1984/// qualified-id [TODO]
1985///
1986/// unqualified-id: [C++ 5.1]
1987/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001988/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001989/// conversion-function-id [TODO]
1990/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00001991/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001992///
Reid Spencer5f016e22007-07-11 17:01:13 +00001993void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001994 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001995
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001996 if (getLang().CPlusPlus) {
1997 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001998 // ParseDeclaratorInternal might already have parsed the scope.
1999 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2000 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002001 if (afterCXXScope) {
2002 // Change the declaration context for name lookup, until this function
2003 // is exited (and the declarator has been parsed).
2004 DeclScopeObj.EnterDeclaratorScope();
2005 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002006
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002007 if (Tok.is(tok::identifier)) {
2008 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlsson4649cac2009-04-30 22:41:11 +00002009
2010 // If this identifier is the name of the current class, it's a
2011 // constructor name.
2012 if (!D.getDeclSpec().hasTypeSpecifier() &&
2013 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
2014 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
2015 Tok.getLocation(), CurScope),
2016 Tok.getLocation());
2017 // This is a normal identifier.
2018 } else
2019 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002020 ConsumeToken();
2021 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00002022 } else if (Tok.is(tok::annot_template_id)) {
2023 TemplateIdAnnotation *TemplateId
2024 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2025
2026 // FIXME: Could this template-id name a constructor?
2027
2028 // FIXME: This is an egregious hack, where we silently ignore
2029 // the specialization (which should be a function template
2030 // specialization name) and use the name instead. This hack
2031 // will go away when we have support for function
2032 // specializations.
2033 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2034 TemplateId->Destroy();
2035 ConsumeToken();
2036 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00002037 } else if (Tok.is(tok::kw_operator)) {
2038 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002039 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002040
Douglas Gregor70316a02008-12-26 15:00:45 +00002041 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00002042 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2043 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00002044 } else {
2045 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00002046 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2047 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2048 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00002049 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002050 }
Douglas Gregor70316a02008-12-26 15:00:45 +00002051 }
2052 goto PastIdentifier;
2053 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002054 // This should be a C++ destructor.
2055 SourceLocation TildeLoc = ConsumeToken();
2056 if (Tok.is(tok::identifier)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002057 // FIXME: Inaccurate.
2058 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00002059 SourceLocation EndLoc;
Douglas Gregor31a19b62009-04-01 21:51:26 +00002060 TypeResult Type = ParseClassName(EndLoc);
2061 if (Type.isInvalid())
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002062 D.SetIdentifier(0, TildeLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002063 else
2064 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002065 } else {
2066 Diag(Tok, diag::err_expected_class_name);
2067 D.SetIdentifier(0, TildeLoc);
2068 }
2069 goto PastIdentifier;
2070 }
2071
2072 // If we reached this point, token is not identifier and not '~'.
2073
2074 if (afterCXXScope) {
2075 Diag(Tok, diag::err_expected_unqualified_id);
2076 D.SetIdentifier(0, Tok.getLocation());
2077 D.setInvalidType(true);
2078 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002079 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002080 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002081 }
2082
2083 // If we reached this point, we are either in C/ObjC or the token didn't
2084 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002085 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2086 assert(!getLang().CPlusPlus &&
2087 "There's a C++-specific check for tok::identifier above");
2088 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2089 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2090 ConsumeToken();
2091 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002092 // direct-declarator: '(' declarator ')'
2093 // direct-declarator: '(' attributes declarator ')'
2094 // Example: 'char (*X)' or 'int (*XX)(void)'
2095 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002096 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002097 // This could be something simple like "int" (in which case the declarator
2098 // portion is empty), if an abstract-declarator is allowed.
2099 D.SetIdentifier(0, Tok.getLocation());
2100 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002101 if (D.getContext() == Declarator::MemberContext)
2102 Diag(Tok, diag::err_expected_member_name_or_semi)
2103 << D.getDeclSpec().getSourceRange();
2104 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002105 Diag(Tok, diag::err_expected_unqualified_id);
2106 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002107 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002108 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002109 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 }
2111
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002112 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002113 assert(D.isPastIdentifier() &&
2114 "Haven't past the location of the identifier yet?");
2115
2116 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002117 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002118 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2119 // In such a case, check if we actually have a function declarator; if it
2120 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002121 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2122 // When not in file scope, warn for ambiguous function declarators, just
2123 // in case the author intended it as a variable definition.
2124 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2125 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2126 break;
2127 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002128 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002129 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002130 ParseBracketDeclarator(D);
2131 } else {
2132 break;
2133 }
2134 }
2135}
2136
Chris Lattneref4715c2008-04-06 05:45:57 +00002137/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2138/// only called before the identifier, so these are most likely just grouping
2139/// parens for precedence. If we find that these are actually function
2140/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2141///
2142/// direct-declarator:
2143/// '(' declarator ')'
2144/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002145/// direct-declarator '(' parameter-type-list ')'
2146/// direct-declarator '(' identifier-list[opt] ')'
2147/// [GNU] direct-declarator '(' parameter-forward-declarations
2148/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002149///
2150void Parser::ParseParenDeclarator(Declarator &D) {
2151 SourceLocation StartLoc = ConsumeParen();
2152 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2153
Chris Lattner7399ee02008-10-20 02:05:46 +00002154 // Eat any attributes before we look at whether this is a grouping or function
2155 // declarator paren. If this is a grouping paren, the attribute applies to
2156 // the type being built up, for example:
2157 // int (__attribute__(()) *x)(long y)
2158 // If this ends up not being a grouping paren, the attribute applies to the
2159 // first argument, for example:
2160 // int (__attribute__(()) int x)
2161 // In either case, we need to eat any attributes to be able to determine what
2162 // sort of paren this is.
2163 //
2164 AttributeList *AttrList = 0;
2165 bool RequiresArg = false;
2166 if (Tok.is(tok::kw___attribute)) {
2167 AttrList = ParseAttributes();
2168
2169 // We require that the argument list (if this is a non-grouping paren) be
2170 // present even if the attribute list was empty.
2171 RequiresArg = true;
2172 }
Steve Naroff239f0732008-12-25 14:16:32 +00002173 // Eat any Microsoft extensions.
Douglas Gregor5a2f5d32009-01-10 00:48:18 +00002174 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2175 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff239f0732008-12-25 14:16:32 +00002176 ConsumeToken();
Chris Lattner7399ee02008-10-20 02:05:46 +00002177
Chris Lattneref4715c2008-04-06 05:45:57 +00002178 // If we haven't past the identifier yet (or where the identifier would be
2179 // stored, if this is an abstract declarator), then this is probably just
2180 // grouping parens. However, if this could be an abstract-declarator, then
2181 // this could also be the start of function arguments (consider 'void()').
2182 bool isGrouping;
2183
2184 if (!D.mayOmitIdentifier()) {
2185 // If this can't be an abstract-declarator, this *must* be a grouping
2186 // paren, because we haven't seen the identifier yet.
2187 isGrouping = true;
2188 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002189 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002190 isDeclarationSpecifier()) { // 'int(int)' is a function.
2191 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2192 // considered to be a type, not a K&R identifier-list.
2193 isGrouping = false;
2194 } else {
2195 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2196 isGrouping = true;
2197 }
2198
2199 // If this is a grouping paren, handle:
2200 // direct-declarator: '(' declarator ')'
2201 // direct-declarator: '(' attributes declarator ')'
2202 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002203 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002204 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002205 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002206 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002207
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002208 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002209 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002210 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002211
2212 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002213 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002214 return;
2215 }
2216
2217 // Okay, if this wasn't a grouping paren, it must be the start of a function
2218 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002219 // identifier (and remember where it would have been), then call into
2220 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002221 D.SetIdentifier(0, Tok.getLocation());
2222
Chris Lattner7399ee02008-10-20 02:05:46 +00002223 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002224}
2225
2226/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2227/// declarator D up to a paren, which indicates that we are parsing function
2228/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002229///
Chris Lattner7399ee02008-10-20 02:05:46 +00002230/// If AttrList is non-null, then the caller parsed those arguments immediately
2231/// after the open paren - they should be considered to be the first argument of
2232/// a parameter. If RequiresArg is true, then the first argument of the
2233/// function is required to be present and required to not be an identifier
2234/// list.
2235///
Reid Spencer5f016e22007-07-11 17:01:13 +00002236/// This method also handles this portion of the grammar:
2237/// parameter-type-list: [C99 6.7.5]
2238/// parameter-list
2239/// parameter-list ',' '...'
2240///
2241/// parameter-list: [C99 6.7.5]
2242/// parameter-declaration
2243/// parameter-list ',' parameter-declaration
2244///
2245/// parameter-declaration: [C99 6.7.5]
2246/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002247/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002248/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002249/// declaration-specifiers abstract-declarator[opt]
2250/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002251/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002252/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2253///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002254/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002255/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002256///
Chris Lattner7399ee02008-10-20 02:05:46 +00002257void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2258 AttributeList *AttrList,
2259 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002260 // lparen is already consumed!
2261 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00002262
Chris Lattner7399ee02008-10-20 02:05:46 +00002263 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002264 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002265 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002266 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002267 delete AttrList;
2268 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002269
Sebastian Redlab197ba2009-02-09 18:23:29 +00002270 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002271
2272 // cv-qualifier-seq[opt].
2273 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002274 bool hasExceptionSpec = false;
2275 bool hasAnyExceptionSpec = false;
2276 // FIXME: Does an empty vector ever allocate? Exception specifications are
2277 // extremely rare, so we want something like a SmallVector<TypeTy*, 0>. :-)
2278 std::vector<TypeTy*> Exceptions;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002279 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002280 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002281 if (!DS.getSourceRange().getEnd().isInvalid())
2282 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002283
2284 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002285 if (Tok.is(tok::kw_throw)) {
2286 hasExceptionSpec = true;
2287 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2288 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002289 }
2290
Chris Lattnerf97409f2008-04-06 06:57:35 +00002291 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002292 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002293 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002294 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002295 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002296 /*arglist*/ 0, 0,
2297 DS.getTypeQualifiers(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002298 hasExceptionSpec,
2299 hasAnyExceptionSpec,
2300 Exceptions.empty() ? 0 :
2301 &Exceptions[0],
2302 Exceptions.size(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002303 LParenLoc, D),
2304 Loc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002305 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00002306 }
2307
2308 // Alternatively, this parameter list may be an identifier list form for a
2309 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002310 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002311 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002312 // K&R identifier lists can't have typedefs as identifiers, per
2313 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002314 if (RequiresArg) {
2315 Diag(Tok, diag::err_argument_required_after_attribute);
2316 delete AttrList;
2317 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002318 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2319 // normal declarators, not for abstract-declarators.
2320 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002321 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002322 }
2323
2324 // Finally, a normal, non-empty parameter type list.
2325
2326 // Build up an array of information about the parsed arguments.
2327 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002328
2329 // Enter function-declaration scope, limiting any declarators to the
2330 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002331 ParseScope PrototypeScope(this,
2332 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002333
2334 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002335 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002336 while (1) {
2337 if (Tok.is(tok::ellipsis)) {
2338 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002339 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002340 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002341 }
2342
Chris Lattnerf97409f2008-04-06 06:57:35 +00002343 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002344
Chris Lattnerf97409f2008-04-06 06:57:35 +00002345 // Parse the declaration-specifiers.
2346 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002347
2348 // If the caller parsed attributes for the first argument, add them now.
2349 if (AttrList) {
2350 DS.AddAttributes(AttrList);
2351 AttrList = 0; // Only apply the attributes to the first parameter.
2352 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002353 ParseDeclarationSpecifiers(DS);
2354
Chris Lattnerf97409f2008-04-06 06:57:35 +00002355 // Parse the declarator. This is "PrototypeContext", because we must
2356 // accept either 'declarator' or 'abstract-declarator' here.
2357 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2358 ParseDeclarator(ParmDecl);
2359
2360 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002361 if (Tok.is(tok::kw___attribute)) {
2362 SourceLocation Loc;
2363 AttributeList *AttrList = ParseAttributes(&Loc);
2364 ParmDecl.AddAttributes(AttrList, Loc);
2365 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002366
Chris Lattnerf97409f2008-04-06 06:57:35 +00002367 // Remember this parsed parameter in ParamInfo.
2368 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2369
Douglas Gregor72b505b2008-12-16 21:30:33 +00002370 // DefArgToks is used when the parsing of default arguments needs
2371 // to be delayed.
2372 CachedTokens *DefArgToks = 0;
2373
Chris Lattnerf97409f2008-04-06 06:57:35 +00002374 // If no parameter was specified, verify that *something* was specified,
2375 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002376 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2377 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002378 // Completely missing, emit error.
2379 Diag(DSStart, diag::err_missing_param);
2380 } else {
2381 // Otherwise, we have something. Add it and let semantic analysis try
2382 // to grok it and add the result to the ParamInfo we are building.
2383
2384 // Inform the actions module about the parameter declarator, so it gets
2385 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002386 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002387
2388 // Parse the default argument, if any. We parse the default
2389 // arguments in all dialects; the semantic analysis in
2390 // ActOnParamDefaultArgument will reject the default argument in
2391 // C.
2392 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002393 SourceLocation EqualLoc = Tok.getLocation();
2394
Chris Lattner04421082008-04-08 04:40:51 +00002395 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002396 if (D.getContext() == Declarator::MemberContext) {
2397 // If we're inside a class definition, cache the tokens
2398 // corresponding to the default argument. We'll actually parse
2399 // them when we see the end of the class definition.
2400 // FIXME: Templates will require something similar.
2401 // FIXME: Can we use a smart pointer for Toks?
2402 DefArgToks = new CachedTokens;
2403
2404 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2405 tok::semi, false)) {
2406 delete DefArgToks;
2407 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002408 Actions.ActOnParamDefaultArgumentError(Param);
2409 } else
2410 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner04421082008-04-08 04:40:51 +00002411 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002412 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002413 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002414
2415 OwningExprResult DefArgResult(ParseAssignmentExpression());
2416 if (DefArgResult.isInvalid()) {
2417 Actions.ActOnParamDefaultArgumentError(Param);
2418 SkipUntil(tok::comma, tok::r_paren, true, true);
2419 } else {
2420 // Inform the actions module about the default argument
2421 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002422 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002423 }
Chris Lattner04421082008-04-08 04:40:51 +00002424 }
2425 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002426
2427 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002428 ParmDecl.getIdentifierLoc(), Param,
2429 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002430 }
2431
2432 // If the next token is a comma, consume it and keep reading arguments.
2433 if (Tok.isNot(tok::comma)) break;
2434
2435 // Consume the comma.
2436 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002437 }
2438
Chris Lattnerf97409f2008-04-06 06:57:35 +00002439 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002440 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002441
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002442 // If we have the closing ')', eat it.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002443 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002444
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002445 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002446 bool hasExceptionSpec = false;
2447 bool hasAnyExceptionSpec = false;
2448 // FIXME: Does an empty vector ever allocate? Exception specifications are
2449 // extremely rare, so we want something like a SmallVector<TypeTy*, 0>. :-)
2450 std::vector<TypeTy*> Exceptions;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002451 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002452 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002453 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002454 if (!DS.getSourceRange().getEnd().isInvalid())
2455 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002456
2457 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002458 if (Tok.is(tok::kw_throw)) {
2459 hasExceptionSpec = true;
2460 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2461 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002462 }
2463
Reid Spencer5f016e22007-07-11 17:01:13 +00002464 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002465 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002466 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002467 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002468 DS.getTypeQualifiers(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002469 hasExceptionSpec,
2470 hasAnyExceptionSpec,
2471 Exceptions.empty() ? 0 :
2472 &Exceptions[0],
2473 Exceptions.size(), LParenLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002474 Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002475}
2476
Chris Lattner66d28652008-04-06 06:34:08 +00002477/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2478/// we found a K&R-style identifier list instead of a type argument list. The
2479/// current token is known to be the first identifier in the list.
2480///
2481/// identifier-list: [C99 6.7.5]
2482/// identifier
2483/// identifier-list ',' identifier
2484///
2485void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2486 Declarator &D) {
2487 // Build up an array of information about the parsed arguments.
2488 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2489 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2490
2491 // If there was no identifier specified for the declarator, either we are in
2492 // an abstract-declarator, or we are in a parameter declarator which was found
2493 // to be abstract. In abstract-declarators, identifier lists are not valid:
2494 // diagnose this.
2495 if (!D.getIdentifier())
2496 Diag(Tok, diag::ext_ident_list_in_param);
2497
2498 // Tok is known to be the first identifier in the list. Remember this
2499 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002500 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002501 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002502 Tok.getLocation(),
2503 DeclPtrTy()));
Chris Lattner66d28652008-04-06 06:34:08 +00002504
Chris Lattner50c64772008-04-06 06:39:19 +00002505 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002506
2507 while (Tok.is(tok::comma)) {
2508 // Eat the comma.
2509 ConsumeToken();
2510
Chris Lattner50c64772008-04-06 06:39:19 +00002511 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002512 if (Tok.isNot(tok::identifier)) {
2513 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002514 SkipUntil(tok::r_paren);
2515 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002516 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002517
Chris Lattner66d28652008-04-06 06:34:08 +00002518 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002519
2520 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002521 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002522 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002523
2524 // Verify that the argument identifier has not already been mentioned.
2525 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002526 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002527 } else {
2528 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002529 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002530 Tok.getLocation(),
2531 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002532 }
Chris Lattner66d28652008-04-06 06:34:08 +00002533
2534 // Eat the identifier.
2535 ConsumeToken();
2536 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002537
2538 // If we have the closing ')', eat it and we're done.
2539 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2540
Chris Lattner50c64772008-04-06 06:39:19 +00002541 // Remember that we parsed a function type, and remember the attributes. This
2542 // function type is always a K&R style function type, which is not varargs and
2543 // has no prototype.
2544 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002545 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002546 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002547 /*TypeQuals*/0,
2548 /*exception*/false, false, 0, 0,
2549 LParenLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002550 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002551}
Chris Lattneref4715c2008-04-06 05:45:57 +00002552
Reid Spencer5f016e22007-07-11 17:01:13 +00002553/// [C90] direct-declarator '[' constant-expression[opt] ']'
2554/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2555/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2556/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2557/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2558void Parser::ParseBracketDeclarator(Declarator &D) {
2559 SourceLocation StartLoc = ConsumeBracket();
2560
Chris Lattner378c7e42008-12-18 07:27:21 +00002561 // C array syntax has many features, but by-far the most common is [] and [4].
2562 // This code does a fast path to handle some of the most obvious cases.
2563 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002564 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002565 // Remember that we parsed the empty array type.
2566 OwningExprResult NumElements(Actions);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002567 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2568 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002569 return;
2570 } else if (Tok.getKind() == tok::numeric_constant &&
2571 GetLookAheadToken(1).is(tok::r_square)) {
2572 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002573 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002574 ConsumeToken();
2575
Sebastian Redlab197ba2009-02-09 18:23:29 +00002576 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002577
2578 // If there was an error parsing the assignment-expression, recover.
2579 if (ExprRes.isInvalid())
2580 ExprRes.release(); // Deallocate expr, just use [].
2581
2582 // Remember that we parsed a array type, and remember its features.
2583 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002584 ExprRes.release(), StartLoc),
2585 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002586 return;
2587 }
2588
Reid Spencer5f016e22007-07-11 17:01:13 +00002589 // If valid, this location is the position where we read the 'static' keyword.
2590 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002591 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002592 StaticLoc = ConsumeToken();
2593
2594 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002595 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002596 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002597 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002598
2599 // If we haven't already read 'static', check to see if there is one after the
2600 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002601 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002602 StaticLoc = ConsumeToken();
2603
2604 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2605 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002606 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002607
2608 // Handle the case where we have '[*]' as the array size. However, a leading
2609 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2610 // the the token after the star is a ']'. Since stars in arrays are
2611 // infrequent, use of lookahead is not costly here.
2612 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002613 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002614
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002615 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002616 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002617 StaticLoc = SourceLocation(); // Drop the static.
2618 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002619 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002620 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002621 // Note, in C89, this production uses the constant-expr production instead
2622 // of assignment-expr. The only difference is that assignment-expr allows
2623 // things like '=' and '*='. Sema rejects these in C89 mode because they
2624 // are not i-c-e's, so we don't need to distinguish between the two here.
2625
Reid Spencer5f016e22007-07-11 17:01:13 +00002626 // Parse the assignment-expression now.
2627 NumElements = ParseAssignmentExpression();
2628 }
2629
2630 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002631 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002632 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002633 // If the expression was invalid, skip it.
2634 SkipUntil(tok::r_square);
2635 return;
2636 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002637
2638 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2639
Chris Lattner378c7e42008-12-18 07:27:21 +00002640 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002641 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2642 StaticLoc.isValid(), isStar,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002643 NumElements.release(), StartLoc),
2644 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002645}
2646
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002647/// [GNU] typeof-specifier:
2648/// typeof ( expressions )
2649/// typeof ( type-name )
2650/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002651///
2652void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002653 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002654 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002655 SourceLocation StartLoc = ConsumeToken();
2656
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002657 bool isCastExpr;
2658 TypeTy *CastTy;
2659 SourceRange CastRange;
2660 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2661 isCastExpr,
2662 CastTy,
2663 CastRange);
2664
2665 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002666 // FIXME: Not accurate, the range gets one token more than it should.
2667 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002668 else
2669 DS.SetRangeEnd(CastRange.getEnd());
2670
2671 if (isCastExpr) {
2672 if (!CastTy) {
2673 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002674 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002675 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002676
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002677 const char *PrevSpec = 0;
2678 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2679 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2680 CastTy))
2681 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2682 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002683 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002684
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002685 // If we get here, the operand to the typeof was an expresion.
2686 if (Operand.isInvalid()) {
2687 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002688 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002689 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002690
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002691 const char *PrevSpec = 0;
2692 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2693 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
2694 Operand.release()))
2695 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002696}