blob: 73a09ee39c943b7c88c5bd2a35ec58913ced52ec [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redl66df3ef2008-12-02 14:43:59 +000031Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattner4b009652007-07-25 00:24:17 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Douglas Gregor10a18fc2009-01-26 22:44:13 +000040 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Chris Lattner4b009652007-07-25 00:24:17 +000041}
42
43/// ParseAttributes - Parse a non-empty attributes list.
44///
45/// [GNU] attributes:
46/// attribute
47/// attributes attribute
48///
49/// [GNU] attribute:
50/// '__attribute__' '(' '(' attribute-list ')' ')'
51///
52/// [GNU] attribute-list:
53/// attrib
54/// attribute_list ',' attrib
55///
56/// [GNU] attrib:
57/// empty
58/// attrib-name
59/// attrib-name '(' identifier ')'
60/// attrib-name '(' identifier ',' nonempty-expr-list ')'
61/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
62///
63/// [GNU] attrib-name:
64/// identifier
65/// typespec
66/// typequal
67/// storageclass
68///
69/// FIXME: The GCC grammar/code for this construct implies we need two
70/// token lookahead. Comment from gcc: "If they start with an identifier
71/// which is followed by a comma or close parenthesis, then the arguments
72/// start with that identifier; otherwise they are an expression list."
73///
74/// At the moment, I am not doing 2 token lookahead. I am also unaware of
75/// any attributes that don't work (based on my limited testing). Most
76/// attributes are very simple in practice. Until we find a bug, I don't see
77/// a pressing need to implement the 2 token lookahead.
78
Sebastian Redl0c986032009-02-09 18:23:29 +000079AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000080 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000081
82 AttributeList *CurrAttr = 0;
83
Chris Lattner34a01ad2007-10-09 17:33:22 +000084 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000085 ConsumeToken();
86 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
87 "attribute")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000096 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
97 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000098
Chris Lattner34a01ad2007-10-09 17:33:22 +000099 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000100 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
101 ConsumeToken();
102 continue;
103 }
104 // we have an identifier or declaration specifier (const, int, etc.)
105 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
106 SourceLocation AttrNameLoc = ConsumeToken();
107
108 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000109 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000110 ConsumeParen(); // ignore the left paren loc for now
111
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
114 SourceLocation ParmLoc = ConsumeToken();
115
Chris Lattner34a01ad2007-10-09 17:33:22 +0000116 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000117 // __attribute__(( mode(byte) ))
118 ConsumeParen(); // ignore the right paren loc for now
119 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
120 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000121 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000122 ConsumeToken();
123 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000124 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000125 bool ArgExprsOk = true;
126
127 // now parse the non-empty comma separated list of expressions
128 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000129 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000130 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000131 ArgExprsOk = false;
132 SkipUntil(tok::r_paren);
133 break;
134 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000135 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000136 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000137 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000138 break;
139 ConsumeToken(); // Eat the comma, move to the next argument
140 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000141 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000142 ConsumeParen(); // ignore the right paren loc for now
143 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000144 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000145 }
146 }
147 } else { // not an identifier
148 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000149 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000150 // __attribute__(( nonnull() ))
151 ConsumeParen(); // ignore the right paren loc for now
152 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
153 0, SourceLocation(), 0, 0, CurrAttr);
154 } else {
155 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000156 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000157 bool ArgExprsOk = true;
158
159 // now parse the list of expressions
160 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000161 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000162 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000163 ArgExprsOk = false;
164 SkipUntil(tok::r_paren);
165 break;
166 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000167 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000168 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000169 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000170 break;
171 ConsumeToken(); // Eat the comma, move to the next argument
172 }
173 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000174 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000175 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000176 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
177 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000178 CurrAttr);
179 }
180 }
181 }
182 } else {
183 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
184 0, SourceLocation(), 0, 0, CurrAttr);
185 }
186 }
187 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Chris Lattner4b009652007-07-25 00:24:17 +0000188 SkipUntil(tok::r_paren, false);
Sebastian Redl0c986032009-02-09 18:23:29 +0000189 SourceLocation Loc = Tok.getLocation();;
190 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
191 SkipUntil(tok::r_paren, false);
192 }
193 if (EndLoc)
194 *EndLoc = Loc;
Chris Lattner4b009652007-07-25 00:24:17 +0000195 }
196 return CurrAttr;
197}
198
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000199/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
200/// routine is called to skip/ignore tokens that comprise the MS declspec.
201void Parser::FuzzyParseMicrosoftDeclSpec() {
202 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
203 ConsumeToken();
204 if (Tok.is(tok::l_paren)) {
205 unsigned short savedParenCount = ParenCount;
206 do {
207 ConsumeAnyToken();
208 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
209 }
210 return;
211}
212
Chris Lattner4b009652007-07-25 00:24:17 +0000213/// ParseDeclaration - Parse a full 'declaration', which consists of
214/// declaration-specifiers, some number of declarators, and a semicolon.
215/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000216///
217/// declaration: [C99 6.7]
218/// block-declaration ->
219/// simple-declaration
220/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000221/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000222/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000223/// [C++] using-directive
224/// [C++] using-declaration [TODO]
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000225/// others... [FIXME]
226///
Chris Lattner4b009652007-07-25 00:24:17 +0000227Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000228 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000229 case tok::kw_export:
230 case tok::kw_template:
231 return ParseTemplateDeclaration(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000232 case tok::kw_namespace:
233 return ParseNamespace(Context);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000234 case tok::kw_using:
235 return ParseUsingDirectiveOrDeclaration(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000236 default:
237 return ParseSimpleDeclaration(Context);
238 }
239}
240
241/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
242/// declaration-specifiers init-declarator-list[opt] ';'
243///[C90/C++]init-declarator-list ';' [TODO]
244/// [OMP] threadprivate-directive [TODO]
245Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000246 // Parse the common declaration-specifiers piece.
247 DeclSpec DS;
248 ParseDeclarationSpecifiers(DS);
249
250 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
251 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000252 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000253 ConsumeToken();
254 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
255 }
256
257 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
258 ParseDeclarator(DeclaratorInfo);
259
260 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
261}
262
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000263
Chris Lattner4b009652007-07-25 00:24:17 +0000264/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
265/// parsing 'declaration-specifiers declarator'. This method is split out this
266/// way to handle the ambiguity between top-level function-definitions and
267/// declarations.
268///
Chris Lattner4b009652007-07-25 00:24:17 +0000269/// init-declarator-list: [C99 6.7]
270/// init-declarator
271/// init-declarator-list ',' init-declarator
272/// init-declarator: [C99 6.7]
273/// declarator
274/// declarator '=' initializer
275/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
276/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000277/// [C++] declarator initializer[opt]
278///
279/// [C++] initializer:
280/// [C++] '=' initializer-clause
281/// [C++] '(' expression-list ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000282///
283Parser::DeclTy *Parser::
284ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
285
286 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
287 // that they can be chained properly if the actions want this.
288 Parser::DeclTy *LastDeclInGroup = 0;
289
290 // At this point, we know that it is not a function definition. Parse the
291 // rest of the init-declarator-list.
292 while (1) {
293 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000294 if (Tok.is(tok::kw_asm)) {
Sebastian Redl0c986032009-02-09 18:23:29 +0000295 SourceLocation Loc;
296 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000297 if (AsmLabel.isInvalid()) {
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000298 SkipUntil(tok::semi);
299 return 0;
300 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000301
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000302 D.setAsmLabel(AsmLabel.release());
Sebastian Redl0c986032009-02-09 18:23:29 +0000303 D.SetRangeEnd(Loc);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000304 }
Chris Lattner4b009652007-07-25 00:24:17 +0000305
306 // If attributes are present, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000307 if (Tok.is(tok::kw___attribute)) {
308 SourceLocation Loc;
309 AttributeList *AttrList = ParseAttributes(&Loc);
310 D.AddAttributes(AttrList, Loc);
311 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000312
313 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000314 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000315
Chris Lattner4b009652007-07-25 00:24:17 +0000316 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000317 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000318 ConsumeToken();
Sebastian Redl39d4f022008-12-11 22:51:44 +0000319 OwningExprResult Init(ParseInitializer());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000320 if (Init.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000321 SkipUntil(tok::semi);
322 return 0;
323 }
Sebastian Redl81db6682009-02-05 15:02:23 +0000324 Actions.AddInitializerToDecl(LastDeclInGroup, move(Init));
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000325 } else if (Tok.is(tok::l_paren)) {
326 // Parse C++ direct initializer: '(' expression-list ')'
327 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000328 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000329 CommaLocsTy CommaLocs;
330
331 bool InvalidExpr = false;
332 if (ParseExpressionList(Exprs, CommaLocs)) {
333 SkipUntil(tok::r_paren);
334 InvalidExpr = true;
335 }
336 // Match the ')'.
337 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
338
339 if (!InvalidExpr) {
340 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
341 "Unexpected number of commas!");
342 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000343 Exprs.take(), Exprs.size(),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000344 &CommaLocs[0], RParenLoc);
345 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000346 } else {
347 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Chris Lattner4b009652007-07-25 00:24:17 +0000348 }
349
Chris Lattner4b009652007-07-25 00:24:17 +0000350 // If we don't have a comma, it is either the end of the list (a ';') or an
351 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000352 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000353 break;
354
355 // Consume the comma.
356 ConsumeToken();
357
358 // Parse the next declarator.
359 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000360
361 // Accept attributes in an init-declarator. In the first declarator in a
362 // declaration, these would be part of the declspec. In subsequent
363 // declarators, they become part of the declarator itself, so that they
364 // don't apply to declarators after *this* one. Examples:
365 // short __attribute__((common)) var; -> declspec
366 // short var __attribute__((common)); -> declarator
367 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000368 if (Tok.is(tok::kw___attribute)) {
369 SourceLocation Loc;
370 AttributeList *AttrList = ParseAttributes(&Loc);
371 D.AddAttributes(AttrList, Loc);
372 }
Chris Lattner926cf542008-10-20 04:57:38 +0000373
Chris Lattner4b009652007-07-25 00:24:17 +0000374 ParseDeclarator(D);
375 }
376
Chris Lattner34a01ad2007-10-09 17:33:22 +0000377 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000378 ConsumeToken();
Fariborz Jahanianc1509b02009-01-17 00:00:40 +0000379 // for(is key; in keys) is error.
380 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
381 Diag(Tok, diag::err_parse_error);
382 return 0;
383 }
Chris Lattner4b009652007-07-25 00:24:17 +0000384 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
385 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000386 // If this is an ObjC2 for-each loop, this is a successful declarator
387 // parse. The syntax for these looks like:
388 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000389 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000390 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
391 }
Chris Lattner4b009652007-07-25 00:24:17 +0000392 Diag(Tok, diag::err_parse_error);
393 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000394 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000395 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000396 ConsumeToken();
397 return 0;
398}
399
400/// ParseSpecifierQualifierList
401/// specifier-qualifier-list:
402/// type-specifier specifier-qualifier-list[opt]
403/// type-qualifier specifier-qualifier-list[opt]
404/// [GNU] attributes specifier-qualifier-list[opt]
405///
406void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
407 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
408 /// parse declaration-specifiers and complain about extra stuff.
409 ParseDeclarationSpecifiers(DS);
410
411 // Validate declspec for type-name.
412 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000413 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000414 Diag(Tok, diag::err_typename_requires_specqual);
415
416 // Issue diagnostic and remove storage class if present.
417 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
418 if (DS.getStorageClassSpecLoc().isValid())
419 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
420 else
421 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
422 DS.ClearStorageClassSpecs();
423 }
424
425 // Issue diagnostic and remove function specfier if present.
426 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000427 if (DS.isInlineSpecified())
428 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
429 if (DS.isVirtualSpecified())
430 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
431 if (DS.isExplicitSpecified())
432 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000433 DS.ClearFunctionSpecs();
434 }
435}
436
437/// ParseDeclarationSpecifiers
438/// declaration-specifiers: [C99 6.7]
439/// storage-class-specifier declaration-specifiers[opt]
440/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000441/// [C99] function-specifier declaration-specifiers[opt]
442/// [GNU] attributes declaration-specifiers[opt]
443///
444/// storage-class-specifier: [C99 6.7.1]
445/// 'typedef'
446/// 'extern'
447/// 'static'
448/// 'auto'
449/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000450/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000451/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000452/// function-specifier: [C99 6.7.4]
453/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000454/// [C++] 'virtual'
455/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000456///
Douglas Gregor52473432008-12-24 02:52:09 +0000457void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Chris Lattner712f9a32009-01-05 00:07:25 +0000458 TemplateParameterLists *TemplateParams){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000459 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000460 while (1) {
461 int isInvalid = false;
462 const char *PrevSpec = 0;
463 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000464
Chris Lattner4b009652007-07-25 00:24:17 +0000465 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000466 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000467 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000468 // If this is not a declaration specifier token, we're done reading decl
469 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000470 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000471 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000472
473 case tok::coloncolon: // ::foo::bar
474 // Annotate C++ scope specifiers. If we get one, loop.
475 if (TryAnnotateCXXScopeToken())
476 continue;
477 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000478
479 case tok::annot_cxxscope: {
480 if (DS.hasTypeSpecifier())
481 goto DoneWithDeclSpec;
482
483 // We are looking for a qualified typename.
484 if (NextToken().isNot(tok::identifier))
485 goto DoneWithDeclSpec;
486
487 CXXScopeSpec SS;
488 SS.setScopeRep(Tok.getAnnotationValue());
489 SS.setRange(Tok.getAnnotationRange());
490
491 // If the next token is the name of the class type that the C++ scope
492 // denotes, followed by a '(', then this is a constructor declaration.
493 // We're done with the decl-specifiers.
494 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
495 CurScope, &SS) &&
496 GetLookAheadToken(2).is(tok::l_paren))
497 goto DoneWithDeclSpec;
498
Douglas Gregor1075a162009-02-04 17:00:24 +0000499 Token Next = NextToken();
500 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
501 Next.getLocation(), CurScope, &SS);
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000502 if (TypeRep == 0)
503 goto DoneWithDeclSpec;
504
505 ConsumeToken(); // The C++ scope.
506
Douglas Gregora60c62e2009-02-09 15:09:02 +0000507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000508 TypeRep);
509 if (isInvalid)
510 break;
511
512 DS.SetRangeEnd(Tok.getLocation());
513 ConsumeToken(); // The typename.
514
515 continue;
516 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000517
518 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000519 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerc297b722009-01-21 19:48:37 +0000520 Tok.getAnnotationValue());
521 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
522 ConsumeToken(); // The typename
523
524 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
525 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
526 // Objective-C interface. If we don't have Objective-C or a '<', this is
527 // just a normal reference to a typedef name.
528 if (!Tok.is(tok::less) || !getLang().ObjC1)
529 continue;
530
531 SourceLocation EndProtoLoc;
532 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
533 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
534 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
535
536 DS.SetRangeEnd(EndProtoLoc);
537 continue;
538 }
539
Chris Lattnerfda18db2008-07-26 01:18:38 +0000540 // typedef-name
541 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000542 // In C++, check to see if this is a scope specifier like foo::bar::, if
543 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000544 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
545 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000546
Chris Lattnerfda18db2008-07-26 01:18:38 +0000547 // This identifier can only be a typedef name if we haven't already seen
548 // a type-specifier. Without this check we misparse:
549 // typedef int X; struct Y { short X; }; as 'short int'.
550 if (DS.hasTypeSpecifier())
551 goto DoneWithDeclSpec;
552
553 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000554 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
555 Tok.getLocation(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000556 if (TypeRep == 0)
557 goto DoneWithDeclSpec;
558
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000559 // C++: If the identifier is actually the name of the class type
560 // being defined and the next token is a '(', then this is a
561 // constructor declaration. We're done with the decl-specifiers
562 // and will treat this token as an identifier.
563 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000564 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000565 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
566 NextToken().getKind() == tok::l_paren)
567 goto DoneWithDeclSpec;
568
Douglas Gregora60c62e2009-02-09 15:09:02 +0000569 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000570 TypeRep);
571 if (isInvalid)
572 break;
573
574 DS.SetRangeEnd(Tok.getLocation());
575 ConsumeToken(); // The identifier
576
577 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
578 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
579 // Objective-C interface. If we don't have Objective-C or a '<', this is
580 // just a normal reference to a typedef name.
581 if (!Tok.is(tok::less) || !getLang().ObjC1)
582 continue;
583
584 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000585 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000586 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000587 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000588
589 DS.SetRangeEnd(EndProtoLoc);
590
Steve Narofff7683302008-09-22 10:28:57 +0000591 // Need to support trailing type qualifiers (e.g. "id<p> const").
592 // If a type specifier follows, it will be diagnosed elsewhere.
593 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000594 }
Chris Lattner4b009652007-07-25 00:24:17 +0000595 // GNU attributes support.
596 case tok::kw___attribute:
597 DS.AddAttributes(ParseAttributes());
598 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000599
600 // Microsoft declspec support.
601 case tok::kw___declspec:
602 if (!PP.getLangOptions().Microsoft)
603 goto DoneWithDeclSpec;
604 FuzzyParseMicrosoftDeclSpec();
605 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000606
Steve Naroffedd04d52008-12-25 14:16:32 +0000607 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000608 case tok::kw___forceinline:
609 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000610 case tok::kw___cdecl:
611 case tok::kw___stdcall:
612 case tok::kw___fastcall:
613 if (!PP.getLangOptions().Microsoft)
614 goto DoneWithDeclSpec;
615 // Just ignore it.
616 break;
617
Chris Lattner4b009652007-07-25 00:24:17 +0000618 // storage-class-specifier
619 case tok::kw_typedef:
620 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
621 break;
622 case tok::kw_extern:
623 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000624 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000625 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
626 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000627 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000628 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
629 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000630 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000631 case tok::kw_static:
632 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000633 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000634 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
635 break;
636 case tok::kw_auto:
637 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
638 break;
639 case tok::kw_register:
640 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
641 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000642 case tok::kw_mutable:
643 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
644 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000645 case tok::kw___thread:
646 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
647 break;
648
Chris Lattner4b009652007-07-25 00:24:17 +0000649 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000650
Chris Lattner4b009652007-07-25 00:24:17 +0000651 // function-specifier
652 case tok::kw_inline:
653 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
654 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000655 case tok::kw_virtual:
656 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
657 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000658 case tok::kw_explicit:
659 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
660 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000661
662 // type-specifier
663 case tok::kw_short:
664 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
665 break;
666 case tok::kw_long:
667 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
668 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
669 else
670 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
671 break;
672 case tok::kw_signed:
673 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
674 break;
675 case tok::kw_unsigned:
676 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
677 break;
678 case tok::kw__Complex:
679 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
680 break;
681 case tok::kw__Imaginary:
682 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
683 break;
684 case tok::kw_void:
685 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
686 break;
687 case tok::kw_char:
688 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
689 break;
690 case tok::kw_int:
691 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
692 break;
693 case tok::kw_float:
694 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
695 break;
696 case tok::kw_double:
697 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
698 break;
699 case tok::kw_wchar_t:
700 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
701 break;
702 case tok::kw_bool:
703 case tok::kw__Bool:
704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
705 break;
706 case tok::kw__Decimal32:
707 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
708 break;
709 case tok::kw__Decimal64:
710 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
711 break;
712 case tok::kw__Decimal128:
713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
714 break;
715
716 // class-specifier:
717 case tok::kw_class:
718 case tok::kw_struct:
719 case tok::kw_union:
720 ParseClassSpecifier(DS, TemplateParams);
721 continue;
722
723 // enum-specifier:
724 case tok::kw_enum:
725 ParseEnumSpecifier(DS);
726 continue;
727
728 // cv-qualifier:
729 case tok::kw_const:
730 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
731 break;
732 case tok::kw_volatile:
733 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
734 getLang())*2;
735 break;
736 case tok::kw_restrict:
737 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
738 getLang())*2;
739 break;
740
741 // GNU typeof support.
742 case tok::kw_typeof:
743 ParseTypeofSpecifier(DS);
744 continue;
745
Steve Naroff5f0466b2008-06-05 00:02:44 +0000746 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000747 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000748 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
749 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000750 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000751 goto DoneWithDeclSpec;
752
753 {
754 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000755 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000756 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000757 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000758 DS.SetRangeEnd(EndProtoLoc);
759
Chris Lattnerf006a222008-11-18 07:48:38 +0000760 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
761 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000762 // Need to support trailing type qualifiers (e.g. "id<p> const").
763 // If a type specifier follows, it will be diagnosed elsewhere.
764 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000765 }
Chris Lattner4b009652007-07-25 00:24:17 +0000766 }
767 // If the specifier combination wasn't legal, issue a diagnostic.
768 if (isInvalid) {
769 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000770 // Pick between error or extwarn.
771 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
772 : diag::ext_duplicate_declspec;
773 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000774 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000775 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000776 ConsumeToken();
777 }
778}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000779
Chris Lattnerd706dc82009-01-06 06:59:53 +0000780/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000781/// primarily follow the C++ grammar with additions for C99 and GNU,
782/// which together subsume the C grammar. Note that the C++
783/// type-specifier also includes the C type-qualifier (for const,
784/// volatile, and C99 restrict). Returns true if a type-specifier was
785/// found (and parsed), false otherwise.
786///
787/// type-specifier: [C++ 7.1.5]
788/// simple-type-specifier
789/// class-specifier
790/// enum-specifier
791/// elaborated-type-specifier [TODO]
792/// cv-qualifier
793///
794/// cv-qualifier: [C++ 7.1.5.1]
795/// 'const'
796/// 'volatile'
797/// [C99] 'restrict'
798///
799/// simple-type-specifier: [ C++ 7.1.5.2]
800/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
801/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
802/// 'char'
803/// 'wchar_t'
804/// 'bool'
805/// 'short'
806/// 'int'
807/// 'long'
808/// 'signed'
809/// 'unsigned'
810/// 'float'
811/// 'double'
812/// 'void'
813/// [C99] '_Bool'
814/// [C99] '_Complex'
815/// [C99] '_Imaginary' // Removed in TC2?
816/// [GNU] '_Decimal32'
817/// [GNU] '_Decimal64'
818/// [GNU] '_Decimal128'
819/// [GNU] typeof-specifier
820/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
821/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000822bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
823 const char *&PrevSpec,
824 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000825 SourceLocation Loc = Tok.getLocation();
826
827 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000828 case tok::identifier: // foo::bar
829 // Annotate typenames and C++ scope specifiers. If we get one, just
830 // recurse to handle whatever we get.
831 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000832 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000833 // Otherwise, not a type specifier.
834 return false;
835 case tok::coloncolon: // ::foo::bar
836 if (NextToken().is(tok::kw_new) || // ::new
837 NextToken().is(tok::kw_delete)) // ::delete
838 return false;
839
840 // Annotate typenames and C++ scope specifiers. If we get one, just
841 // recurse to handle whatever we get.
842 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000843 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000844 // Otherwise, not a type specifier.
845 return false;
846
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000847 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000848 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000849 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000850 Tok.getAnnotationValue());
851 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
852 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000853
854 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
855 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
856 // Objective-C interface. If we don't have Objective-C or a '<', this is
857 // just a normal reference to a typedef name.
858 if (!Tok.is(tok::less) || !getLang().ObjC1)
859 return true;
860
861 SourceLocation EndProtoLoc;
862 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
863 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
864 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
865
866 DS.SetRangeEnd(EndProtoLoc);
867 return true;
868 }
869
870 case tok::kw_short:
871 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
872 break;
873 case tok::kw_long:
874 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
875 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
876 else
877 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
878 break;
879 case tok::kw_signed:
880 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
881 break;
882 case tok::kw_unsigned:
883 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
884 break;
885 case tok::kw__Complex:
886 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
887 break;
888 case tok::kw__Imaginary:
889 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
890 break;
891 case tok::kw_void:
892 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
893 break;
894 case tok::kw_char:
895 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
896 break;
897 case tok::kw_int:
898 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
899 break;
900 case tok::kw_float:
901 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
902 break;
903 case tok::kw_double:
904 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
905 break;
906 case tok::kw_wchar_t:
907 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
908 break;
909 case tok::kw_bool:
910 case tok::kw__Bool:
911 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
912 break;
913 case tok::kw__Decimal32:
914 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
915 break;
916 case tok::kw__Decimal64:
917 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
918 break;
919 case tok::kw__Decimal128:
920 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
921 break;
922
923 // class-specifier:
924 case tok::kw_class:
925 case tok::kw_struct:
926 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000927 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000928 return true;
929
930 // enum-specifier:
931 case tok::kw_enum:
932 ParseEnumSpecifier(DS);
933 return true;
934
935 // cv-qualifier:
936 case tok::kw_const:
937 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
938 getLang())*2;
939 break;
940 case tok::kw_volatile:
941 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
942 getLang())*2;
943 break;
944 case tok::kw_restrict:
945 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
946 getLang())*2;
947 break;
948
949 // GNU typeof support.
950 case tok::kw_typeof:
951 ParseTypeofSpecifier(DS);
952 return true;
953
Steve Naroffedd04d52008-12-25 14:16:32 +0000954 case tok::kw___cdecl:
955 case tok::kw___stdcall:
956 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +0000957 if (!PP.getLangOptions().Microsoft) return false;
958 ConsumeToken();
959 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +0000960
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000961 default:
962 // Not a type-specifier; do nothing.
963 return false;
964 }
965
966 // If the specifier combination wasn't legal, issue a diagnostic.
967 if (isInvalid) {
968 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000969 // Pick between error or extwarn.
970 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
971 : diag::ext_duplicate_declspec;
972 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000973 }
974 DS.SetRangeEnd(Tok.getLocation());
975 ConsumeToken(); // whatever we parsed above.
976 return true;
977}
Chris Lattner4b009652007-07-25 00:24:17 +0000978
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000979/// ParseStructDeclaration - Parse a struct declaration without the terminating
980/// semicolon.
981///
Chris Lattner4b009652007-07-25 00:24:17 +0000982/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000983/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000984/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000985/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000986/// struct-declarator-list:
987/// struct-declarator
988/// struct-declarator-list ',' struct-declarator
989/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
990/// struct-declarator:
991/// declarator
992/// [GNU] declarator attributes[opt]
993/// declarator[opt] ':' constant-expression
994/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
995///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000996void Parser::
997ParseStructDeclaration(DeclSpec &DS,
998 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000999 if (Tok.is(tok::kw___extension__)) {
1000 // __extension__ silences extension warnings in the subexpression.
1001 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001002 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001003 return ParseStructDeclaration(DS, Fields);
1004 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001005
1006 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001007 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001008 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001009
Douglas Gregorb748fc52009-01-12 22:49:06 +00001010 // If there are no declarators, this is a free-standing declaration
1011 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001012 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001013 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001014 return;
1015 }
1016
1017 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001018 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001019 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001020 FieldDeclarator &DeclaratorInfo = Fields.back();
1021
Steve Naroffa9adf112007-08-20 22:28:22 +00001022 /// struct-declarator: declarator
1023 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001024 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001025 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001026
Chris Lattner34a01ad2007-10-09 17:33:22 +00001027 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001028 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001029 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001030 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001031 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001032 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001033 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001034 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001035
Steve Naroffa9adf112007-08-20 22:28:22 +00001036 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001037 if (Tok.is(tok::kw___attribute)) {
1038 SourceLocation Loc;
1039 AttributeList *AttrList = ParseAttributes(&Loc);
1040 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1041 }
1042
Steve Naroffa9adf112007-08-20 22:28:22 +00001043 // If we don't have a comma, it is either the end of the list (a ';')
1044 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001045 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001046 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001047
Steve Naroffa9adf112007-08-20 22:28:22 +00001048 // Consume the comma.
1049 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001050
Steve Naroffa9adf112007-08-20 22:28:22 +00001051 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001052 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001053
Steve Naroffa9adf112007-08-20 22:28:22 +00001054 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001055 if (Tok.is(tok::kw___attribute)) {
1056 SourceLocation Loc;
1057 AttributeList *AttrList = ParseAttributes(&Loc);
1058 Fields.back().D.AddAttributes(AttrList, Loc);
1059 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001060 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001061}
1062
1063/// ParseStructUnionBody
1064/// struct-contents:
1065/// struct-declaration-list
1066/// [EXT] empty
1067/// [GNU] "struct-declaration-list" without terminatoring ';'
1068/// struct-declaration-list:
1069/// struct-declaration
1070/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001071/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001072///
Chris Lattner4b009652007-07-25 00:24:17 +00001073void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1074 unsigned TagType, DeclTy *TagDecl) {
1075 SourceLocation LBraceLoc = ConsumeBrace();
1076
Douglas Gregorcab994d2009-01-09 22:42:13 +00001077 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001078 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1079
Chris Lattner4b009652007-07-25 00:24:17 +00001080 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1081 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001082 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001083 Diag(Tok, diag::ext_empty_struct_union_enum)
1084 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001085
1086 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001087 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1088
Chris Lattner4b009652007-07-25 00:24:17 +00001089 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001090 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001091 // Each iteration of this loop reads one struct-declaration.
1092
1093 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001094 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001095 Diag(Tok, diag::ext_extra_struct_semi);
1096 ConsumeToken();
1097 continue;
1098 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001099
1100 // Parse all the comma separated declarators.
1101 DeclSpec DS;
1102 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001103 if (!Tok.is(tok::at)) {
1104 ParseStructDeclaration(DS, FieldDeclarators);
1105
1106 // Convert them all to fields.
1107 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1108 FieldDeclarator &FD = FieldDeclarators[i];
1109 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +00001110 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +00001111 DS.getSourceRange().getBegin(),
1112 FD.D, FD.BitfieldSize);
1113 FieldDecls.push_back(Field);
1114 }
1115 } else { // Handle @defs
1116 ConsumeToken();
1117 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1118 Diag(Tok, diag::err_unexpected_at);
1119 SkipUntil(tok::semi, true, true);
1120 continue;
1121 }
1122 ConsumeToken();
1123 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1124 if (!Tok.is(tok::identifier)) {
1125 Diag(Tok, diag::err_expected_ident);
1126 SkipUntil(tok::semi, true, true);
1127 continue;
1128 }
1129 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001130 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1131 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001132 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1133 ConsumeToken();
1134 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1135 }
Chris Lattner4b009652007-07-25 00:24:17 +00001136
Chris Lattner34a01ad2007-10-09 17:33:22 +00001137 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001138 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001139 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001140 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001141 break;
1142 } else {
1143 Diag(Tok, diag::err_expected_semi_decl_list);
1144 // Skip to end of block or statement
1145 SkipUntil(tok::r_brace, true, true);
1146 }
1147 }
1148
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001149 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001150
Chris Lattner4b009652007-07-25 00:24:17 +00001151 AttributeList *AttrList = 0;
1152 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001153 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001154 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001155
1156 Actions.ActOnFields(CurScope,
1157 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1158 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001159 AttrList);
1160 StructScope.Exit();
1161 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001162}
1163
1164
1165/// ParseEnumSpecifier
1166/// enum-specifier: [C99 6.7.2.2]
1167/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001168///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001169/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1170/// '}' attributes[opt]
1171/// 'enum' identifier
1172/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001173///
1174/// [C++] elaborated-type-specifier:
1175/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1176///
Chris Lattner4b009652007-07-25 00:24:17 +00001177void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001178 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001179 SourceLocation StartLoc = ConsumeToken();
1180
1181 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001182
1183 AttributeList *Attr = 0;
1184 // If attributes exist after tag, parse them.
1185 if (Tok.is(tok::kw___attribute))
1186 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001187
1188 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001189 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001190 if (Tok.isNot(tok::identifier)) {
1191 Diag(Tok, diag::err_expected_ident);
1192 if (Tok.isNot(tok::l_brace)) {
1193 // Has no name and is not a definition.
1194 // Skip the rest of this declarator, up until the comma or semicolon.
1195 SkipUntil(tok::comma, true);
1196 return;
1197 }
1198 }
1199 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001200
1201 // Must have either 'enum name' or 'enum {...}'.
1202 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1203 Diag(Tok, diag::err_expected_ident_lbrace);
1204
1205 // Skip the rest of this declarator, up until the comma or semicolon.
1206 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001207 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001208 }
1209
1210 // If an identifier is present, consume and remember it.
1211 IdentifierInfo *Name = 0;
1212 SourceLocation NameLoc;
1213 if (Tok.is(tok::identifier)) {
1214 Name = Tok.getIdentifierInfo();
1215 NameLoc = ConsumeToken();
1216 }
1217
1218 // There are three options here. If we have 'enum foo;', then this is a
1219 // forward declaration. If we have 'enum foo {...' then this is a
1220 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1221 //
1222 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1223 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1224 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1225 //
1226 Action::TagKind TK;
1227 if (Tok.is(tok::l_brace))
1228 TK = Action::TK_Definition;
1229 else if (Tok.is(tok::semi))
1230 TK = Action::TK_Declaration;
1231 else
1232 TK = Action::TK_Reference;
1233 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregord406b032009-02-06 22:42:48 +00001234 SS, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +00001235
Chris Lattner34a01ad2007-10-09 17:33:22 +00001236 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001237 ParseEnumBody(StartLoc, TagDecl);
1238
1239 // TODO: semantic analysis on the declspec for enums.
1240 const char *PrevSpec = 0;
1241 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001242 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001243}
1244
1245/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1246/// enumerator-list:
1247/// enumerator
1248/// enumerator-list ',' enumerator
1249/// enumerator:
1250/// enumeration-constant
1251/// enumeration-constant '=' constant-expression
1252/// enumeration-constant:
1253/// identifier
1254///
1255void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001256 // Enter the scope of the enum body and start the definition.
1257 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001258 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001259
Chris Lattner4b009652007-07-25 00:24:17 +00001260 SourceLocation LBraceLoc = ConsumeBrace();
1261
Chris Lattnerc9a92452007-08-27 17:24:30 +00001262 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001263 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001264 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001265
1266 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1267
1268 DeclTy *LastEnumConstDecl = 0;
1269
1270 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001271 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001272 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1273 SourceLocation IdentLoc = ConsumeToken();
1274
1275 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001276 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001277 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001278 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001279 AssignedVal = ParseConstantExpression();
1280 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001281 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001282 }
1283
1284 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001285 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001286 LastEnumConstDecl,
1287 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001288 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001289 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001290 EnumConstantDecls.push_back(EnumConstDecl);
1291 LastEnumConstDecl = EnumConstDecl;
1292
Chris Lattner34a01ad2007-10-09 17:33:22 +00001293 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001294 break;
1295 SourceLocation CommaLoc = ConsumeToken();
1296
Chris Lattner34a01ad2007-10-09 17:33:22 +00001297 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001298 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1299 }
1300
1301 // Eat the }.
1302 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1303
Steve Naroff0acc9c92007-09-15 18:49:24 +00001304 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001305 EnumConstantDecls.size());
1306
1307 DeclTy *AttrList = 0;
1308 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001309 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001310 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001311
1312 EnumScope.Exit();
1313 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001314}
1315
1316/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001317/// start of a type-qualifier-list.
1318bool Parser::isTypeQualifier() const {
1319 switch (Tok.getKind()) {
1320 default: return false;
1321 // type-qualifier
1322 case tok::kw_const:
1323 case tok::kw_volatile:
1324 case tok::kw_restrict:
1325 return true;
1326 }
1327}
1328
1329/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001330/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001331bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001332 switch (Tok.getKind()) {
1333 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001334
1335 case tok::identifier: // foo::bar
1336 // Annotate typenames and C++ scope specifiers. If we get one, just
1337 // recurse to handle whatever we get.
1338 if (TryAnnotateTypeOrScopeToken())
1339 return isTypeSpecifierQualifier();
1340 // Otherwise, not a type specifier.
1341 return false;
1342 case tok::coloncolon: // ::foo::bar
1343 if (NextToken().is(tok::kw_new) || // ::new
1344 NextToken().is(tok::kw_delete)) // ::delete
1345 return false;
1346
1347 // Annotate typenames and C++ scope specifiers. If we get one, just
1348 // recurse to handle whatever we get.
1349 if (TryAnnotateTypeOrScopeToken())
1350 return isTypeSpecifierQualifier();
1351 // Otherwise, not a type specifier.
1352 return false;
1353
Chris Lattner4b009652007-07-25 00:24:17 +00001354 // GNU attributes support.
1355 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001356 // GNU typeof support.
1357 case tok::kw_typeof:
1358
Chris Lattner4b009652007-07-25 00:24:17 +00001359 // type-specifiers
1360 case tok::kw_short:
1361 case tok::kw_long:
1362 case tok::kw_signed:
1363 case tok::kw_unsigned:
1364 case tok::kw__Complex:
1365 case tok::kw__Imaginary:
1366 case tok::kw_void:
1367 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001368 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001369 case tok::kw_int:
1370 case tok::kw_float:
1371 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001372 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001373 case tok::kw__Bool:
1374 case tok::kw__Decimal32:
1375 case tok::kw__Decimal64:
1376 case tok::kw__Decimal128:
1377
Chris Lattner2e78db32008-04-13 18:59:07 +00001378 // struct-or-union-specifier (C99) or class-specifier (C++)
1379 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001380 case tok::kw_struct:
1381 case tok::kw_union:
1382 // enum-specifier
1383 case tok::kw_enum:
1384
1385 // type-qualifier
1386 case tok::kw_const:
1387 case tok::kw_volatile:
1388 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001389
1390 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001391 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001392 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001393
1394 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1395 case tok::less:
1396 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001397
1398 case tok::kw___cdecl:
1399 case tok::kw___stdcall:
1400 case tok::kw___fastcall:
1401 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001402 }
1403}
1404
1405/// isDeclarationSpecifier() - Return true if the current token is part of a
1406/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001407bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001408 switch (Tok.getKind()) {
1409 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001410
1411 case tok::identifier: // foo::bar
1412 // Annotate typenames and C++ scope specifiers. If we get one, just
1413 // recurse to handle whatever we get.
1414 if (TryAnnotateTypeOrScopeToken())
1415 return isDeclarationSpecifier();
1416 // Otherwise, not a declaration specifier.
1417 return false;
1418 case tok::coloncolon: // ::foo::bar
1419 if (NextToken().is(tok::kw_new) || // ::new
1420 NextToken().is(tok::kw_delete)) // ::delete
1421 return false;
1422
1423 // Annotate typenames and C++ scope specifiers. If we get one, just
1424 // recurse to handle whatever we get.
1425 if (TryAnnotateTypeOrScopeToken())
1426 return isDeclarationSpecifier();
1427 // Otherwise, not a declaration specifier.
1428 return false;
1429
Chris Lattner4b009652007-07-25 00:24:17 +00001430 // storage-class-specifier
1431 case tok::kw_typedef:
1432 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001433 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001434 case tok::kw_static:
1435 case tok::kw_auto:
1436 case tok::kw_register:
1437 case tok::kw___thread:
1438
1439 // type-specifiers
1440 case tok::kw_short:
1441 case tok::kw_long:
1442 case tok::kw_signed:
1443 case tok::kw_unsigned:
1444 case tok::kw__Complex:
1445 case tok::kw__Imaginary:
1446 case tok::kw_void:
1447 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001448 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001449 case tok::kw_int:
1450 case tok::kw_float:
1451 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001452 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001453 case tok::kw__Bool:
1454 case tok::kw__Decimal32:
1455 case tok::kw__Decimal64:
1456 case tok::kw__Decimal128:
1457
Chris Lattner2e78db32008-04-13 18:59:07 +00001458 // struct-or-union-specifier (C99) or class-specifier (C++)
1459 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001460 case tok::kw_struct:
1461 case tok::kw_union:
1462 // enum-specifier
1463 case tok::kw_enum:
1464
1465 // type-qualifier
1466 case tok::kw_const:
1467 case tok::kw_volatile:
1468 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001469
Chris Lattner4b009652007-07-25 00:24:17 +00001470 // function-specifier
1471 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001472 case tok::kw_virtual:
1473 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001474
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001475 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001476 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001477
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001478 // GNU typeof support.
1479 case tok::kw_typeof:
1480
1481 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001482 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001483 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001484
1485 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1486 case tok::less:
1487 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001488
Steve Naroffab1a3632009-01-06 19:34:12 +00001489 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001490 case tok::kw___cdecl:
1491 case tok::kw___stdcall:
1492 case tok::kw___fastcall:
1493 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001494 }
1495}
1496
1497
1498/// ParseTypeQualifierListOpt
1499/// type-qualifier-list: [C99 6.7.5]
1500/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001501/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001502/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001503/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001504///
Chris Lattner460696f2008-12-18 07:02:59 +00001505void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001506 while (1) {
1507 int isInvalid = false;
1508 const char *PrevSpec = 0;
1509 SourceLocation Loc = Tok.getLocation();
1510
1511 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001512 case tok::kw_const:
1513 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1514 getLang())*2;
1515 break;
1516 case tok::kw_volatile:
1517 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1518 getLang())*2;
1519 break;
1520 case tok::kw_restrict:
1521 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1522 getLang())*2;
1523 break;
Steve Naroffad620402008-12-25 14:41:26 +00001524 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001525 case tok::kw___cdecl:
1526 case tok::kw___stdcall:
1527 case tok::kw___fastcall:
1528 if (!PP.getLangOptions().Microsoft)
1529 goto DoneWithTypeQuals;
1530 // Just ignore it.
1531 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001532 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001533 if (AttributesAllowed) {
1534 DS.AddAttributes(ParseAttributes());
1535 continue; // do *not* consume the next token!
1536 }
1537 // otherwise, FALL THROUGH!
1538 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001539 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001540 // If this is not a type-qualifier token, we're done reading type
1541 // qualifiers. First verify that DeclSpec's are consistent.
1542 DS.Finish(Diags, PP.getSourceManager(), getLang());
1543 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001544 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001545
Chris Lattner4b009652007-07-25 00:24:17 +00001546 // If the specifier combination wasn't legal, issue a diagnostic.
1547 if (isInvalid) {
1548 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001549 // Pick between error or extwarn.
1550 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1551 : diag::ext_duplicate_declspec;
1552 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001553 }
1554 ConsumeToken();
1555 }
1556}
1557
1558
1559/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1560///
1561void Parser::ParseDeclarator(Declarator &D) {
1562 /// This implements the 'declarator' production in the C grammar, then checks
1563 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001564 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001565}
1566
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001567/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1568/// is parsed by the function passed to it. Pass null, and the direct-declarator
1569/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001570/// ptr-operator production.
1571///
Sebastian Redl75555032009-01-24 21:16:55 +00001572/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1573/// [C] pointer[opt] direct-declarator
1574/// [C++] direct-declarator
1575/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001576///
1577/// pointer: [C99 6.7.5]
1578/// '*' type-qualifier-list[opt]
1579/// '*' type-qualifier-list[opt] pointer
1580///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001581/// ptr-operator:
1582/// '*' cv-qualifier-seq[opt]
1583/// '&'
1584/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001585/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001586void Parser::ParseDeclaratorInternal(Declarator &D,
1587 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001588
Sebastian Redl75555032009-01-24 21:16:55 +00001589 // C++ member pointers start with a '::' or a nested-name.
1590 // Member pointers get special handling, since there's no place for the
1591 // scope spec in the generic path below.
1592 if ((Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1593 Tok.is(tok::annot_cxxscope)) && getLang().CPlusPlus) {
1594 CXXScopeSpec SS;
1595 if (ParseOptionalCXXScopeSpecifier(SS)) {
1596 if(Tok.isNot(tok::star)) {
1597 // The scope spec really belongs to the direct-declarator.
1598 D.getCXXScopeSpec() = SS;
1599 if (DirectDeclParser)
1600 (this->*DirectDeclParser)(D);
1601 return;
1602 }
1603
1604 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001605 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001606 DeclSpec DS;
1607 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001608 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001609
1610 // Recurse to parse whatever is left.
1611 ParseDeclaratorInternal(D, DirectDeclParser);
1612
1613 // Sema will have to catch (syntactically invalid) pointers into global
1614 // scope. It has to catch pointers into namespace scope anyway.
1615 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001616 Loc, DS.TakeAttributes()),
1617 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001618 return;
1619 }
1620 }
1621
1622 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001623 // Not a pointer, C++ reference, or block.
1624 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001625 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001626 if (DirectDeclParser)
1627 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001628 return;
1629 }
Sebastian Redl75555032009-01-24 21:16:55 +00001630
Steve Naroffdc22f212008-08-28 10:07:06 +00001631 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Sebastian Redl0c986032009-02-09 18:23:29 +00001632 SourceLocation Loc = ConsumeToken(); // Eat the *, ^ or &.
1633 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001634
Steve Naroffdc22f212008-08-28 10:07:06 +00001635 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001636 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001637 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001638
Chris Lattner4b009652007-07-25 00:24:17 +00001639 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001640 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001641
Chris Lattner4b009652007-07-25 00:24:17 +00001642 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001643 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001644 if (Kind == tok::star)
1645 // Remember that we parsed a pointer type, and remember the type-quals.
1646 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001647 DS.TakeAttributes()),
1648 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001649 else
1650 // Remember that we parsed a Block type, and remember the type-quals.
1651 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001652 Loc),
1653 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001654 } else {
1655 // Is a reference
1656 DeclSpec DS;
1657
1658 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1659 // cv-qualifiers are introduced through the use of a typedef or of a
1660 // template type argument, in which case the cv-qualifiers are ignored.
1661 //
1662 // [GNU] Retricted references are allowed.
1663 // [GNU] Attributes on references are allowed.
1664 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001665 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001666
1667 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1668 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1669 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001670 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001671 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1672 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001673 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001674 }
1675
1676 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001677 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001678
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001679 if (D.getNumTypeObjects() > 0) {
1680 // C++ [dcl.ref]p4: There shall be no references to references.
1681 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1682 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001683 if (const IdentifierInfo *II = D.getIdentifier())
1684 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1685 << II;
1686 else
1687 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1688 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001689
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001690 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001691 // can go ahead and build the (technically ill-formed)
1692 // declarator: reference collapsing will take care of it.
1693 }
1694 }
1695
Chris Lattner4b009652007-07-25 00:24:17 +00001696 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001697 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001698 DS.TakeAttributes()),
1699 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001700 }
1701}
1702
1703/// ParseDirectDeclarator
1704/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001705/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001706/// '(' declarator ')'
1707/// [GNU] '(' attributes declarator ')'
1708/// [C90] direct-declarator '[' constant-expression[opt] ']'
1709/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1710/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1711/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1712/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1713/// direct-declarator '(' parameter-type-list ')'
1714/// direct-declarator '(' identifier-list[opt] ')'
1715/// [GNU] direct-declarator '(' parameter-forward-declarations
1716/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001717/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1718/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001719/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001720///
1721/// declarator-id: [C++ 8]
1722/// id-expression
1723/// '::'[opt] nested-name-specifier[opt] type-name
1724///
1725/// id-expression: [C++ 5.1]
1726/// unqualified-id
1727/// qualified-id [TODO]
1728///
1729/// unqualified-id: [C++ 5.1]
1730/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001731/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001732/// conversion-function-id [TODO]
1733/// '~' class-name
1734/// template-id [TODO]
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001735///
Chris Lattner4b009652007-07-25 00:24:17 +00001736void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001737 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001738
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001739 if (getLang().CPlusPlus) {
1740 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001741 // ParseDeclaratorInternal might already have parsed the scope.
1742 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1743 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001744 if (afterCXXScope) {
1745 // Change the declaration context for name lookup, until this function
1746 // is exited (and the declarator has been parsed).
1747 DeclScopeObj.EnterDeclaratorScope();
1748 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001749
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001750 if (Tok.is(tok::identifier)) {
1751 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001752
1753 // If this identifier is followed by a '<', we may have a template-id.
1754 DeclTy *Template;
Douglas Gregor279272e2009-02-04 19:02:06 +00001755 if (getLang().CPlusPlus && NextToken().is(tok::less) &&
Douglas Gregor2fa10442008-12-18 19:37:40 +00001756 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1757 CurScope))) {
1758 IdentifierInfo *II = Tok.getIdentifierInfo();
1759 AnnotateTemplateIdToken(Template, 0);
1760 // FIXME: Set the declarator to a template-id. How? I don't
1761 // know... for now, just use the identifier.
1762 D.SetIdentifier(II, Tok.getLocation());
1763 }
1764 // If this identifier is the name of the current class, it's a
1765 // constructor name.
Sebastian Redl0c986032009-02-09 18:23:29 +00001766 else if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001767 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001768 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001769 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001770 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001771 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001772 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1773 ConsumeToken();
1774 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001775 } else if (Tok.is(tok::kw_operator)) {
1776 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001777 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001778
Douglas Gregor853dd392008-12-26 15:00:45 +00001779 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001780 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1781 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001782 } else {
1783 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001784 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1785 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1786 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001787 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001788 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001789 }
1790 goto PastIdentifier;
1791 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001792 // This should be a C++ destructor.
1793 SourceLocation TildeLoc = ConsumeToken();
1794 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001795 // FIXME: Inaccurate.
1796 SourceLocation NameLoc = Tok.getLocation();
1797 if (TypeTy *Type = ParseClassName()) {
1798 D.setDestructor(Type, TildeLoc, NameLoc);
1799 } else {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001800 D.SetIdentifier(0, TildeLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001801 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001802 } else {
1803 Diag(Tok, diag::err_expected_class_name);
1804 D.SetIdentifier(0, TildeLoc);
1805 }
1806 goto PastIdentifier;
1807 }
1808
1809 // If we reached this point, token is not identifier and not '~'.
1810
1811 if (afterCXXScope) {
1812 Diag(Tok, diag::err_expected_unqualified_id);
1813 D.SetIdentifier(0, Tok.getLocation());
1814 D.setInvalidType(true);
1815 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001816 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001817 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001818 }
1819
1820 // If we reached this point, we are either in C/ObjC or the token didn't
1821 // satisfy any of the C++-specific checks.
1822
1823 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1824 assert(!getLang().CPlusPlus &&
1825 "There's a C++-specific check for tok::identifier above");
1826 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1827 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1828 ConsumeToken();
1829 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001830 // direct-declarator: '(' declarator ')'
1831 // direct-declarator: '(' attributes declarator ')'
1832 // Example: 'char (*X)' or 'int (*XX)(void)'
1833 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001834 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001835 // This could be something simple like "int" (in which case the declarator
1836 // portion is empty), if an abstract-declarator is allowed.
1837 D.SetIdentifier(0, Tok.getLocation());
1838 } else {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001839 if (getLang().CPlusPlus)
1840 Diag(Tok, diag::err_expected_unqualified_id);
1841 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001842 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001843 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001844 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001845 }
1846
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001847 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001848 assert(D.isPastIdentifier() &&
1849 "Haven't past the location of the identifier yet?");
1850
1851 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001852 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001853 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1854 // In such a case, check if we actually have a function declarator; if it
1855 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001856 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1857 // When not in file scope, warn for ambiguous function declarators, just
1858 // in case the author intended it as a variable definition.
1859 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1860 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1861 break;
1862 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001863 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001864 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001865 ParseBracketDeclarator(D);
1866 } else {
1867 break;
1868 }
1869 }
1870}
1871
Chris Lattnera0d056d2008-04-06 05:45:57 +00001872/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1873/// only called before the identifier, so these are most likely just grouping
1874/// parens for precedence. If we find that these are actually function
1875/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1876///
1877/// direct-declarator:
1878/// '(' declarator ')'
1879/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001880/// direct-declarator '(' parameter-type-list ')'
1881/// direct-declarator '(' identifier-list[opt] ')'
1882/// [GNU] direct-declarator '(' parameter-forward-declarations
1883/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001884///
1885void Parser::ParseParenDeclarator(Declarator &D) {
1886 SourceLocation StartLoc = ConsumeParen();
1887 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1888
Chris Lattner1f185292008-10-20 02:05:46 +00001889 // Eat any attributes before we look at whether this is a grouping or function
1890 // declarator paren. If this is a grouping paren, the attribute applies to
1891 // the type being built up, for example:
1892 // int (__attribute__(()) *x)(long y)
1893 // If this ends up not being a grouping paren, the attribute applies to the
1894 // first argument, for example:
1895 // int (__attribute__(()) int x)
1896 // In either case, we need to eat any attributes to be able to determine what
1897 // sort of paren this is.
1898 //
1899 AttributeList *AttrList = 0;
1900 bool RequiresArg = false;
1901 if (Tok.is(tok::kw___attribute)) {
1902 AttrList = ParseAttributes();
1903
1904 // We require that the argument list (if this is a non-grouping paren) be
1905 // present even if the attribute list was empty.
1906 RequiresArg = true;
1907 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001908 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00001909 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1910 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00001911 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00001912
Chris Lattnera0d056d2008-04-06 05:45:57 +00001913 // If we haven't past the identifier yet (or where the identifier would be
1914 // stored, if this is an abstract declarator), then this is probably just
1915 // grouping parens. However, if this could be an abstract-declarator, then
1916 // this could also be the start of function arguments (consider 'void()').
1917 bool isGrouping;
1918
1919 if (!D.mayOmitIdentifier()) {
1920 // If this can't be an abstract-declarator, this *must* be a grouping
1921 // paren, because we haven't seen the identifier yet.
1922 isGrouping = true;
1923 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001924 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001925 isDeclarationSpecifier()) { // 'int(int)' is a function.
1926 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1927 // considered to be a type, not a K&R identifier-list.
1928 isGrouping = false;
1929 } else {
1930 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1931 isGrouping = true;
1932 }
1933
1934 // If this is a grouping paren, handle:
1935 // direct-declarator: '(' declarator ')'
1936 // direct-declarator: '(' attributes declarator ')'
1937 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001938 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001939 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001940 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00001941 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001942
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001943 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001944 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00001945 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001946
1947 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00001948 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001949 return;
1950 }
1951
1952 // Okay, if this wasn't a grouping paren, it must be the start of a function
1953 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001954 // identifier (and remember where it would have been), then call into
1955 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001956 D.SetIdentifier(0, Tok.getLocation());
1957
Chris Lattner1f185292008-10-20 02:05:46 +00001958 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001959}
1960
1961/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1962/// declarator D up to a paren, which indicates that we are parsing function
1963/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001964///
Chris Lattner1f185292008-10-20 02:05:46 +00001965/// If AttrList is non-null, then the caller parsed those arguments immediately
1966/// after the open paren - they should be considered to be the first argument of
1967/// a parameter. If RequiresArg is true, then the first argument of the
1968/// function is required to be present and required to not be an identifier
1969/// list.
1970///
Chris Lattner4b009652007-07-25 00:24:17 +00001971/// This method also handles this portion of the grammar:
1972/// parameter-type-list: [C99 6.7.5]
1973/// parameter-list
1974/// parameter-list ',' '...'
1975///
1976/// parameter-list: [C99 6.7.5]
1977/// parameter-declaration
1978/// parameter-list ',' parameter-declaration
1979///
1980/// parameter-declaration: [C99 6.7.5]
1981/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001982/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001983/// [GNU] declaration-specifiers declarator attributes
1984/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001985/// [C++] declaration-specifiers abstract-declarator[opt]
1986/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001987/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1988///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001989/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1990/// and "exception-specification[opt]"(TODO).
1991///
Chris Lattner1f185292008-10-20 02:05:46 +00001992void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1993 AttributeList *AttrList,
1994 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001995 // lparen is already consumed!
1996 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001997
Chris Lattner1f185292008-10-20 02:05:46 +00001998 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001999 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002000 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002001 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002002 delete AttrList;
2003 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002004
Sebastian Redl0c986032009-02-09 18:23:29 +00002005 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002006
2007 // cv-qualifier-seq[opt].
2008 DeclSpec DS;
2009 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002010 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002011 if (!DS.getSourceRange().getEnd().isInvalid())
2012 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002013
2014 // Parse exception-specification[opt].
2015 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002016 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002017 }
2018
Chris Lattner9f7564b2008-04-06 06:57:35 +00002019 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002020 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002021 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002022 /*variadic*/ false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002023 /*arglist*/ 0, 0,
2024 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002025 LParenLoc, D),
2026 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002027 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002028 }
2029
2030 // Alternatively, this parameter list may be an identifier list form for a
2031 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002032 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002033 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002034 // K&R identifier lists can't have typedefs as identifiers, per
2035 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002036 if (RequiresArg) {
2037 Diag(Tok, diag::err_argument_required_after_attribute);
2038 delete AttrList;
2039 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002040 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2041 // normal declarators, not for abstract-declarators.
2042 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002043 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002044 }
2045
2046 // Finally, a normal, non-empty parameter type list.
2047
2048 // Build up an array of information about the parsed arguments.
2049 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002050
2051 // Enter function-declaration scope, limiting any declarators to the
2052 // function prototype scope, including parameter declarators.
Douglas Gregorcab994d2009-01-09 22:42:13 +00002053 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002054
2055 bool IsVariadic = false;
2056 while (1) {
2057 if (Tok.is(tok::ellipsis)) {
2058 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00002059
Chris Lattner9f7564b2008-04-06 06:57:35 +00002060 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002061 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002062 // Otherwise, parse parameter type list. If it starts with an
2063 // ellipsis, diagnose the malformed function.
2064 Diag(Tok, diag::err_ellipsis_first_arg);
2065 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00002066 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00002067
Chris Lattner9f7564b2008-04-06 06:57:35 +00002068 ConsumeToken(); // Consume the ellipsis.
2069 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002070 }
2071
Chris Lattner9f7564b2008-04-06 06:57:35 +00002072 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002073
Chris Lattner9f7564b2008-04-06 06:57:35 +00002074 // Parse the declaration-specifiers.
2075 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002076
2077 // If the caller parsed attributes for the first argument, add them now.
2078 if (AttrList) {
2079 DS.AddAttributes(AttrList);
2080 AttrList = 0; // Only apply the attributes to the first parameter.
2081 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002082 ParseDeclarationSpecifiers(DS);
2083
2084 // Parse the declarator. This is "PrototypeContext", because we must
2085 // accept either 'declarator' or 'abstract-declarator' here.
2086 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2087 ParseDeclarator(ParmDecl);
2088
2089 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002090 if (Tok.is(tok::kw___attribute)) {
2091 SourceLocation Loc;
2092 AttributeList *AttrList = ParseAttributes(&Loc);
2093 ParmDecl.AddAttributes(AttrList, Loc);
2094 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002095
Chris Lattner9f7564b2008-04-06 06:57:35 +00002096 // Remember this parsed parameter in ParamInfo.
2097 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2098
Douglas Gregor605de8d2008-12-16 21:30:33 +00002099 // DefArgToks is used when the parsing of default arguments needs
2100 // to be delayed.
2101 CachedTokens *DefArgToks = 0;
2102
Chris Lattner9f7564b2008-04-06 06:57:35 +00002103 // If no parameter was specified, verify that *something* was specified,
2104 // otherwise we have a missing type and identifier.
2105 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
2106 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
2107 // Completely missing, emit error.
2108 Diag(DSStart, diag::err_missing_param);
2109 } else {
2110 // Otherwise, we have something. Add it and let semantic analysis try
2111 // to grok it and add the result to the ParamInfo we are building.
2112
2113 // Inform the actions module about the parameter declarator, so it gets
2114 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00002115 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2116
2117 // Parse the default argument, if any. We parse the default
2118 // arguments in all dialects; the semantic analysis in
2119 // ActOnParamDefaultArgument will reject the default argument in
2120 // C.
2121 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002122 SourceLocation EqualLoc = Tok.getLocation();
2123
Chris Lattner3e254fb2008-04-08 04:40:51 +00002124 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002125 if (D.getContext() == Declarator::MemberContext) {
2126 // If we're inside a class definition, cache the tokens
2127 // corresponding to the default argument. We'll actually parse
2128 // them when we see the end of the class definition.
2129 // FIXME: Templates will require something similar.
2130 // FIXME: Can we use a smart pointer for Toks?
2131 DefArgToks = new CachedTokens;
2132
2133 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2134 tok::semi, false)) {
2135 delete DefArgToks;
2136 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002137 Actions.ActOnParamDefaultArgumentError(Param);
2138 } else
2139 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002140 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002141 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002142 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002143
2144 OwningExprResult DefArgResult(ParseAssignmentExpression());
2145 if (DefArgResult.isInvalid()) {
2146 Actions.ActOnParamDefaultArgumentError(Param);
2147 SkipUntil(tok::comma, tok::r_paren, true, true);
2148 } else {
2149 // Inform the actions module about the default argument
2150 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
2151 DefArgResult.release());
2152 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002153 }
2154 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002155
2156 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002157 ParmDecl.getIdentifierLoc(), Param,
2158 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002159 }
2160
2161 // If the next token is a comma, consume it and keep reading arguments.
2162 if (Tok.isNot(tok::comma)) break;
2163
2164 // Consume the comma.
2165 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002166 }
2167
Chris Lattner9f7564b2008-04-06 06:57:35 +00002168 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002169 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002170
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002171 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002172 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002173
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002174 DeclSpec DS;
2175 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002176 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002177 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002178 if (!DS.getSourceRange().getEnd().isInvalid())
2179 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002180
2181 // Parse exception-specification[opt].
2182 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002183 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002184 }
2185
Chris Lattner4b009652007-07-25 00:24:17 +00002186 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002187 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
2188 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002189 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002190 LParenLoc, D),
2191 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002192}
2193
Chris Lattner35d9c912008-04-06 06:34:08 +00002194/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2195/// we found a K&R-style identifier list instead of a type argument list. The
2196/// current token is known to be the first identifier in the list.
2197///
2198/// identifier-list: [C99 6.7.5]
2199/// identifier
2200/// identifier-list ',' identifier
2201///
2202void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2203 Declarator &D) {
2204 // Build up an array of information about the parsed arguments.
2205 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2206 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2207
2208 // If there was no identifier specified for the declarator, either we are in
2209 // an abstract-declarator, or we are in a parameter declarator which was found
2210 // to be abstract. In abstract-declarators, identifier lists are not valid:
2211 // diagnose this.
2212 if (!D.getIdentifier())
2213 Diag(Tok, diag::ext_ident_list_in_param);
2214
2215 // Tok is known to be the first identifier in the list. Remember this
2216 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002217 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002218 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2219 Tok.getLocation(), 0));
2220
Chris Lattner113a56b2008-04-06 06:39:19 +00002221 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002222
2223 while (Tok.is(tok::comma)) {
2224 // Eat the comma.
2225 ConsumeToken();
2226
Chris Lattner113a56b2008-04-06 06:39:19 +00002227 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002228 if (Tok.isNot(tok::identifier)) {
2229 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002230 SkipUntil(tok::r_paren);
2231 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002232 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002233
Chris Lattner35d9c912008-04-06 06:34:08 +00002234 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002235
2236 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002237 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002238 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002239
2240 // Verify that the argument identifier has not already been mentioned.
2241 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002242 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002243 } else {
2244 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002245 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2246 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00002247 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002248
2249 // Eat the identifier.
2250 ConsumeToken();
2251 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002252
2253 // If we have the closing ')', eat it and we're done.
2254 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2255
Chris Lattner113a56b2008-04-06 06:39:19 +00002256 // Remember that we parsed a function type, and remember the attributes. This
2257 // function type is always a K&R style function type, which is not varargs and
2258 // has no prototype.
2259 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
2260 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002261 /*TypeQuals*/0, LParenLoc, D),
2262 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002263}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002264
Chris Lattner4b009652007-07-25 00:24:17 +00002265/// [C90] direct-declarator '[' constant-expression[opt] ']'
2266/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2267/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2268/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2269/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2270void Parser::ParseBracketDeclarator(Declarator &D) {
2271 SourceLocation StartLoc = ConsumeBracket();
2272
Chris Lattner1525c3a2008-12-18 07:27:21 +00002273 // C array syntax has many features, but by-far the most common is [] and [4].
2274 // This code does a fast path to handle some of the most obvious cases.
2275 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002276 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002277 // Remember that we parsed the empty array type.
2278 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002279 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2280 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002281 return;
2282 } else if (Tok.getKind() == tok::numeric_constant &&
2283 GetLookAheadToken(1).is(tok::r_square)) {
2284 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002285 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002286 ConsumeToken();
2287
Sebastian Redl0c986032009-02-09 18:23:29 +00002288 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002289
2290 // If there was an error parsing the assignment-expression, recover.
2291 if (ExprRes.isInvalid())
2292 ExprRes.release(); // Deallocate expr, just use [].
2293
2294 // Remember that we parsed a array type, and remember its features.
2295 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002296 ExprRes.release(), StartLoc),
2297 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002298 return;
2299 }
2300
Chris Lattner4b009652007-07-25 00:24:17 +00002301 // If valid, this location is the position where we read the 'static' keyword.
2302 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002303 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002304 StaticLoc = ConsumeToken();
2305
2306 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002307 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002308 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002309 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002310
2311 // If we haven't already read 'static', check to see if there is one after the
2312 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002313 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002314 StaticLoc = ConsumeToken();
2315
2316 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2317 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002318 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002319
2320 // Handle the case where we have '[*]' as the array size. However, a leading
2321 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2322 // the the token after the star is a ']'. Since stars in arrays are
2323 // infrequent, use of lookahead is not costly here.
2324 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002325 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002326
Chris Lattner306d4df2008-12-18 06:50:14 +00002327 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002328 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002329 StaticLoc = SourceLocation(); // Drop the static.
2330 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002331 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002332 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002333 // Note, in C89, this production uses the constant-expr production instead
2334 // of assignment-expr. The only difference is that assignment-expr allows
2335 // things like '=' and '*='. Sema rejects these in C89 mode because they
2336 // are not i-c-e's, so we don't need to distinguish between the two here.
2337
Chris Lattner4b009652007-07-25 00:24:17 +00002338 // Parse the assignment-expression now.
2339 NumElements = ParseAssignmentExpression();
2340 }
2341
2342 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002343 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002344 // If the expression was invalid, skip it.
2345 SkipUntil(tok::r_square);
2346 return;
2347 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002348
2349 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2350
Chris Lattner1525c3a2008-12-18 07:27:21 +00002351 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002352 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2353 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002354 NumElements.release(), StartLoc),
2355 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002356}
2357
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002358/// [GNU] typeof-specifier:
2359/// typeof ( expressions )
2360/// typeof ( type-name )
2361/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002362///
2363void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002364 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002365 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002366 SourceLocation StartLoc = ConsumeToken();
2367
Chris Lattner34a01ad2007-10-09 17:33:22 +00002368 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002369 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002370 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002371 return;
2372 }
2373
Sebastian Redl14ca7412008-12-11 21:36:32 +00002374 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002375 if (Result.isInvalid())
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002376 return;
2377
2378 const char *PrevSpec = 0;
2379 // Check for duplicate type specifiers.
2380 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002381 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002382 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002383
2384 // FIXME: Not accurate, the range gets one token more than it should.
2385 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002386 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002387 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002388
Steve Naroff7cbb1462007-07-31 12:34:36 +00002389 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2390
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002391 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00002392 TypeTy *Ty = ParseTypeName();
2393
Steve Naroff4c255ab2007-07-31 23:56:32 +00002394 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2395
Chris Lattner34a01ad2007-10-09 17:33:22 +00002396 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002397 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002398 return;
2399 }
2400 RParenLoc = ConsumeParen();
2401 const char *PrevSpec = 0;
2402 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2403 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattnerf006a222008-11-18 07:48:38 +00002404 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002405 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002406 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002407
2408 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002409 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002410 return;
2411 }
2412 RParenLoc = ConsumeParen();
2413 const char *PrevSpec = 0;
2414 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2415 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002416 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002417 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002418 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002419 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002420}
2421
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002422