blob: 2c26b13dc14fedfccdf83147c39b10073ede5aee [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++.
Douglas Gregor6c0f4062009-02-18 17:45:20 +000031Action::TypeResult 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 Gregor6c0f4062009-02-18 17:45:20 +000040 if (DeclaratorInfo.getInvalidType())
41 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattner4b009652007-07-25 00:24:17 +000044}
45
46/// ParseAttributes - Parse a non-empty attributes list.
47///
48/// [GNU] attributes:
49/// attribute
50/// attributes attribute
51///
52/// [GNU] attribute:
53/// '__attribute__' '(' '(' attribute-list ')' ')'
54///
55/// [GNU] attribute-list:
56/// attrib
57/// attribute_list ',' attrib
58///
59/// [GNU] attrib:
60/// empty
61/// attrib-name
62/// attrib-name '(' identifier ')'
63/// attrib-name '(' identifier ',' nonempty-expr-list ')'
64/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
65///
66/// [GNU] attrib-name:
67/// identifier
68/// typespec
69/// typequal
70/// storageclass
71///
72/// FIXME: The GCC grammar/code for this construct implies we need two
73/// token lookahead. Comment from gcc: "If they start with an identifier
74/// which is followed by a comma or close parenthesis, then the arguments
75/// start with that identifier; otherwise they are an expression list."
76///
77/// At the moment, I am not doing 2 token lookahead. I am also unaware of
78/// any attributes that don't work (based on my limited testing). Most
79/// attributes are very simple in practice. Until we find a bug, I don't see
80/// a pressing need to implement the 2 token lookahead.
81
Sebastian Redl0c986032009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000084
85 AttributeList *CurrAttr = 0;
86
Chris Lattner34a01ad2007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000088 ConsumeToken();
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
90 "attribute")) {
91 SkipUntil(tok::r_paren, true); // skip until ) or ;
92 return CurrAttr;
93 }
94 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
96 return CurrAttr;
97 }
98 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000101
Chris Lattner34a01ad2007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000103 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
104 ConsumeToken();
105 continue;
106 }
107 // we have an identifier or declaration specifier (const, int, etc.)
108 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
109 SourceLocation AttrNameLoc = ConsumeToken();
110
111 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 ConsumeParen(); // ignore the left paren loc for now
114
Chris Lattner34a01ad2007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner34a01ad2007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000120 // __attribute__(( mode(byte) ))
121 ConsumeParen(); // ignore the right paren loc for now
122 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
123 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000139 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000153 // __attribute__(( nonnull() ))
154 ConsumeParen(); // ignore the right paren loc for now
155 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
156 0, SourceLocation(), 0, 0, CurrAttr);
157 } else {
158 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000171 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000181 CurrAttr);
182 }
183 }
184 }
185 } else {
186 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
187 0, SourceLocation(), 0, 0, CurrAttr);
188 }
189 }
190 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Chris Lattner4b009652007-07-25 00:24:17 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redl0c986032009-02-09 18:23:29 +0000192 SourceLocation Loc = Tok.getLocation();;
193 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
194 SkipUntil(tok::r_paren, false);
195 }
196 if (EndLoc)
197 *EndLoc = Loc;
Chris Lattner4b009652007-07-25 00:24:17 +0000198 }
199 return CurrAttr;
200}
201
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000202/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
203/// routine is called to skip/ignore tokens that comprise the MS declspec.
204void Parser::FuzzyParseMicrosoftDeclSpec() {
205 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
206 ConsumeToken();
207 if (Tok.is(tok::l_paren)) {
208 unsigned short savedParenCount = ParenCount;
209 do {
210 ConsumeAnyToken();
211 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
212 }
213 return;
214}
215
Chris Lattner4b009652007-07-25 00:24:17 +0000216/// ParseDeclaration - Parse a full 'declaration', which consists of
217/// declaration-specifiers, some number of declarators, and a semicolon.
218/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000219///
220/// declaration: [C99 6.7]
221/// block-declaration ->
222/// simple-declaration
223/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000224/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000225/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000226/// [C++] using-directive
227/// [C++] using-declaration [TODO]
Sebastian Redla8cecf62009-03-24 22:27:57 +0000228/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000229/// others... [FIXME]
230///
Chris Lattner4b009652007-07-25 00:24:17 +0000231Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000232 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000233 case tok::kw_export:
234 case tok::kw_template:
Douglas Gregora08b6c72009-02-17 23:15:12 +0000235 return ParseTemplateDeclarationOrSpecialization(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000236 case tok::kw_namespace:
237 return ParseNamespace(Context);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000238 case tok::kw_using:
239 return ParseUsingDirectiveOrDeclaration(Context);
Anders Carlssonab041982009-03-11 16:27:10 +0000240 case tok::kw_static_assert:
241 return ParseStaticAssertDeclaration();
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000242 default:
243 return ParseSimpleDeclaration(Context);
244 }
245}
246
247/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
248/// declaration-specifiers init-declarator-list[opt] ';'
249///[C90/C++]init-declarator-list ';' [TODO]
250/// [OMP] threadprivate-directive [TODO]
251Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000252 // Parse the common declaration-specifiers piece.
253 DeclSpec DS;
254 ParseDeclarationSpecifiers(DS);
255
256 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
257 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000258 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000259 ConsumeToken();
260 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
261 }
262
263 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
264 ParseDeclarator(DeclaratorInfo);
265
266 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
267}
268
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000269
Chris Lattner4b009652007-07-25 00:24:17 +0000270/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
271/// parsing 'declaration-specifiers declarator'. This method is split out this
272/// way to handle the ambiguity between top-level function-definitions and
273/// declarations.
274///
Chris Lattner4b009652007-07-25 00:24:17 +0000275/// init-declarator-list: [C99 6.7]
276/// init-declarator
277/// init-declarator-list ',' init-declarator
278/// init-declarator: [C99 6.7]
279/// declarator
280/// declarator '=' initializer
281/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
282/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000283/// [C++] declarator initializer[opt]
284///
285/// [C++] initializer:
286/// [C++] '=' initializer-clause
287/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000288/// [C++0x] '=' 'default' [TODO]
289/// [C++0x] '=' 'delete'
290///
291/// According to the standard grammar, =default and =delete are function
292/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000293///
294Parser::DeclTy *Parser::
295ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
296
297 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
298 // that they can be chained properly if the actions want this.
299 Parser::DeclTy *LastDeclInGroup = 0;
300
301 // At this point, we know that it is not a function definition. Parse the
302 // rest of the init-declarator-list.
303 while (1) {
304 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000305 if (Tok.is(tok::kw_asm)) {
Sebastian Redl0c986032009-02-09 18:23:29 +0000306 SourceLocation Loc;
307 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000308 if (AsmLabel.isInvalid()) {
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000309 SkipUntil(tok::semi);
310 return 0;
311 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000312
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000313 D.setAsmLabel(AsmLabel.release());
Sebastian Redl0c986032009-02-09 18:23:29 +0000314 D.SetRangeEnd(Loc);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000315 }
Chris Lattner4b009652007-07-25 00:24:17 +0000316
317 // If attributes are present, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000318 if (Tok.is(tok::kw___attribute)) {
319 SourceLocation Loc;
320 AttributeList *AttrList = ParseAttributes(&Loc);
321 D.AddAttributes(AttrList, Loc);
322 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000323
324 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000325 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000326
Chris Lattner4b009652007-07-25 00:24:17 +0000327 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000328 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000329 ConsumeToken();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000330 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
331 SourceLocation DelLoc = ConsumeToken();
332 Actions.SetDeclDeleted(LastDeclInGroup, DelLoc);
333 } else {
334 OwningExprResult Init(ParseInitializer());
335 if (Init.isInvalid()) {
336 SkipUntil(tok::semi);
337 return 0;
338 }
339 Actions.AddInitializerToDecl(LastDeclInGroup, move(Init));
Chris Lattner4b009652007-07-25 00:24:17 +0000340 }
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000341 } else if (Tok.is(tok::l_paren)) {
342 // Parse C++ direct initializer: '(' expression-list ')'
343 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000344 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000345 CommaLocsTy CommaLocs;
346
347 bool InvalidExpr = false;
348 if (ParseExpressionList(Exprs, CommaLocs)) {
349 SkipUntil(tok::r_paren);
350 InvalidExpr = true;
351 }
352 // Match the ')'.
353 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
354
355 if (!InvalidExpr) {
356 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
357 "Unexpected number of commas!");
358 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000359 move_arg(Exprs),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000360 &CommaLocs[0], RParenLoc);
361 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000362 } else {
363 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Chris Lattner4b009652007-07-25 00:24:17 +0000364 }
365
Chris Lattner4b009652007-07-25 00:24:17 +0000366 // If we don't have a comma, it is either the end of the list (a ';') or an
367 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000368 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000369 break;
370
371 // Consume the comma.
372 ConsumeToken();
373
374 // Parse the next declarator.
375 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000376
377 // Accept attributes in an init-declarator. In the first declarator in a
378 // declaration, these would be part of the declspec. In subsequent
379 // declarators, they become part of the declarator itself, so that they
380 // don't apply to declarators after *this* one. Examples:
381 // short __attribute__((common)) var; -> declspec
382 // short var __attribute__((common)); -> declarator
383 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000384 if (Tok.is(tok::kw___attribute)) {
385 SourceLocation Loc;
386 AttributeList *AttrList = ParseAttributes(&Loc);
387 D.AddAttributes(AttrList, Loc);
388 }
Chris Lattner926cf542008-10-20 04:57:38 +0000389
Chris Lattner4b009652007-07-25 00:24:17 +0000390 ParseDeclarator(D);
391 }
392
Chris Lattner34a01ad2007-10-09 17:33:22 +0000393 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000394 ConsumeToken();
Fariborz Jahanianc1509b02009-01-17 00:00:40 +0000395 // for(is key; in keys) is error.
396 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
397 Diag(Tok, diag::err_parse_error);
398 return 0;
399 }
Chris Lattner4b009652007-07-25 00:24:17 +0000400 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
401 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000402 // If this is an ObjC2 for-each loop, this is a successful declarator
403 // parse. The syntax for these looks like:
404 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000405 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000406 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
407 }
Chris Lattner4b009652007-07-25 00:24:17 +0000408 Diag(Tok, diag::err_parse_error);
409 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000410 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000411 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000412 ConsumeToken();
413 return 0;
414}
415
416/// ParseSpecifierQualifierList
417/// specifier-qualifier-list:
418/// type-specifier specifier-qualifier-list[opt]
419/// type-qualifier specifier-qualifier-list[opt]
420/// [GNU] attributes specifier-qualifier-list[opt]
421///
422void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
423 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
424 /// parse declaration-specifiers and complain about extra stuff.
425 ParseDeclarationSpecifiers(DS);
426
427 // Validate declspec for type-name.
428 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000429 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000430 Diag(Tok, diag::err_typename_requires_specqual);
431
432 // Issue diagnostic and remove storage class if present.
433 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
434 if (DS.getStorageClassSpecLoc().isValid())
435 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
436 else
437 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
438 DS.ClearStorageClassSpecs();
439 }
440
441 // Issue diagnostic and remove function specfier if present.
442 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000443 if (DS.isInlineSpecified())
444 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
445 if (DS.isVirtualSpecified())
446 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
447 if (DS.isExplicitSpecified())
448 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000449 DS.ClearFunctionSpecs();
450 }
451}
452
453/// ParseDeclarationSpecifiers
454/// declaration-specifiers: [C99 6.7]
455/// storage-class-specifier declaration-specifiers[opt]
456/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000457/// [C99] function-specifier declaration-specifiers[opt]
458/// [GNU] attributes declaration-specifiers[opt]
459///
460/// storage-class-specifier: [C99 6.7.1]
461/// 'typedef'
462/// 'extern'
463/// 'static'
464/// 'auto'
465/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000466/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000467/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000468/// function-specifier: [C99 6.7.4]
469/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000470/// [C++] 'virtual'
471/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000472///
Douglas Gregor52473432008-12-24 02:52:09 +0000473void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000474 TemplateParameterLists *TemplateParams,
475 AccessSpecifier AS){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000476 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000477 while (1) {
478 int isInvalid = false;
479 const char *PrevSpec = 0;
480 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000481
Chris Lattner4b009652007-07-25 00:24:17 +0000482 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000483 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000484 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000485 // If this is not a declaration specifier token, we're done reading decl
486 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000487 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000488 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000489
490 case tok::coloncolon: // ::foo::bar
491 // Annotate C++ scope specifiers. If we get one, loop.
492 if (TryAnnotateCXXScopeToken())
493 continue;
494 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000495
496 case tok::annot_cxxscope: {
497 if (DS.hasTypeSpecifier())
498 goto DoneWithDeclSpec;
499
500 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000501 Token Next = NextToken();
502 if (Next.is(tok::annot_template_id) &&
503 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
504 ->Kind == TNK_Class_template) {
505 // We have a qualified template-id, e.g., N::A<int>
506 CXXScopeSpec SS;
507 ParseOptionalCXXScopeSpecifier(SS);
508 assert(Tok.is(tok::annot_template_id) &&
509 "ParseOptionalCXXScopeSpecifier not working");
510 AnnotateTemplateIdTokenAsType(&SS);
511 continue;
512 }
513
514 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000515 goto DoneWithDeclSpec;
516
517 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000518 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000519 SS.setRange(Tok.getAnnotationRange());
520
521 // If the next token is the name of the class type that the C++ scope
522 // denotes, followed by a '(', then this is a constructor declaration.
523 // We're done with the decl-specifiers.
524 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
525 CurScope, &SS) &&
526 GetLookAheadToken(2).is(tok::l_paren))
527 goto DoneWithDeclSpec;
528
Douglas Gregor1075a162009-02-04 17:00:24 +0000529 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
530 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000531
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000532 if (TypeRep == 0)
533 goto DoneWithDeclSpec;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000534
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000535 ConsumeToken(); // The C++ scope.
536
Douglas Gregora60c62e2009-02-09 15:09:02 +0000537 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000538 TypeRep);
539 if (isInvalid)
540 break;
541
542 DS.SetRangeEnd(Tok.getLocation());
543 ConsumeToken(); // The typename.
544
545 continue;
546 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000547
548 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerc297b722009-01-21 19:48:37 +0000550 Tok.getAnnotationValue());
551 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
552 ConsumeToken(); // The typename
553
554 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
555 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
556 // Objective-C interface. If we don't have Objective-C or a '<', this is
557 // just a normal reference to a typedef name.
558 if (!Tok.is(tok::less) || !getLang().ObjC1)
559 continue;
560
561 SourceLocation EndProtoLoc;
562 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
563 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
564 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
565
566 DS.SetRangeEnd(EndProtoLoc);
567 continue;
568 }
569
Chris Lattnerfda18db2008-07-26 01:18:38 +0000570 // typedef-name
571 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000572 // In C++, check to see if this is a scope specifier like foo::bar::, if
573 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000574 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
575 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000576
Chris Lattnerfda18db2008-07-26 01:18:38 +0000577 // This identifier can only be a typedef name if we haven't already seen
578 // a type-specifier. Without this check we misparse:
579 // typedef int X; struct Y { short X; }; as 'short int'.
580 if (DS.hasTypeSpecifier())
581 goto DoneWithDeclSpec;
582
583 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000584 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
585 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000586
Chris Lattnerfda18db2008-07-26 01:18:38 +0000587 if (TypeRep == 0)
588 goto DoneWithDeclSpec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000589
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000590 // C++: If the identifier is actually the name of the class type
591 // being defined and the next token is a '(', then this is a
592 // constructor declaration. We're done with the decl-specifiers
593 // and will treat this token as an identifier.
594 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000595 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000596 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
597 NextToken().getKind() == tok::l_paren)
598 goto DoneWithDeclSpec;
599
Douglas Gregora60c62e2009-02-09 15:09:02 +0000600 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000601 TypeRep);
602 if (isInvalid)
603 break;
604
605 DS.SetRangeEnd(Tok.getLocation());
606 ConsumeToken(); // The identifier
607
608 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
609 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
610 // Objective-C interface. If we don't have Objective-C or a '<', this is
611 // just a normal reference to a typedef name.
612 if (!Tok.is(tok::less) || !getLang().ObjC1)
613 continue;
614
615 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000616 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000617 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000618 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000619
620 DS.SetRangeEnd(EndProtoLoc);
621
Steve Narofff7683302008-09-22 10:28:57 +0000622 // Need to support trailing type qualifiers (e.g. "id<p> const").
623 // If a type specifier follows, it will be diagnosed elsewhere.
624 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000625 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000626
627 // type-name
628 case tok::annot_template_id: {
629 TemplateIdAnnotation *TemplateId
630 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
631 if (TemplateId->Kind != TNK_Class_template) {
632 // This template-id does not refer to a type name, so we're
633 // done with the type-specifiers.
634 goto DoneWithDeclSpec;
635 }
636
637 // Turn the template-id annotation token into a type annotation
638 // token, then try again to parse it as a type-specifier.
639 if (AnnotateTemplateIdTokenAsType())
640 DS.SetTypeSpecError();
641
642 continue;
643 }
644
Chris Lattner4b009652007-07-25 00:24:17 +0000645 // GNU attributes support.
646 case tok::kw___attribute:
647 DS.AddAttributes(ParseAttributes());
648 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000649
650 // Microsoft declspec support.
651 case tok::kw___declspec:
652 if (!PP.getLangOptions().Microsoft)
653 goto DoneWithDeclSpec;
654 FuzzyParseMicrosoftDeclSpec();
655 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000656
Steve Naroffedd04d52008-12-25 14:16:32 +0000657 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000658 case tok::kw___forceinline:
659 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000660 case tok::kw___cdecl:
661 case tok::kw___stdcall:
662 case tok::kw___fastcall:
663 if (!PP.getLangOptions().Microsoft)
664 goto DoneWithDeclSpec;
665 // Just ignore it.
666 break;
667
Chris Lattner4b009652007-07-25 00:24:17 +0000668 // storage-class-specifier
669 case tok::kw_typedef:
670 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
671 break;
672 case tok::kw_extern:
673 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000674 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000675 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
676 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000677 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000678 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
679 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000680 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000681 case tok::kw_static:
682 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000683 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000684 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
685 break;
686 case tok::kw_auto:
687 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
688 break;
689 case tok::kw_register:
690 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
691 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000692 case tok::kw_mutable:
693 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
694 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000695 case tok::kw___thread:
696 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
697 break;
698
Chris Lattner4b009652007-07-25 00:24:17 +0000699 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000700
Chris Lattner4b009652007-07-25 00:24:17 +0000701 // function-specifier
702 case tok::kw_inline:
703 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
704 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000705 case tok::kw_virtual:
706 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
707 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000708 case tok::kw_explicit:
709 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
710 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000711
712 // type-specifier
713 case tok::kw_short:
714 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
715 break;
716 case tok::kw_long:
717 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
718 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
719 else
720 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
721 break;
722 case tok::kw_signed:
723 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
724 break;
725 case tok::kw_unsigned:
726 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
727 break;
728 case tok::kw__Complex:
729 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
730 break;
731 case tok::kw__Imaginary:
732 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
733 break;
734 case tok::kw_void:
735 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
736 break;
737 case tok::kw_char:
738 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
739 break;
740 case tok::kw_int:
741 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
742 break;
743 case tok::kw_float:
744 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
745 break;
746 case tok::kw_double:
747 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
748 break;
749 case tok::kw_wchar_t:
750 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
751 break;
752 case tok::kw_bool:
753 case tok::kw__Bool:
754 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
755 break;
756 case tok::kw__Decimal32:
757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
758 break;
759 case tok::kw__Decimal64:
760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
761 break;
762 case tok::kw__Decimal128:
763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
764 break;
765
766 // class-specifier:
767 case tok::kw_class:
768 case tok::kw_struct:
769 case tok::kw_union:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000770 ParseClassSpecifier(DS, TemplateParams, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000771 continue;
772
773 // enum-specifier:
774 case tok::kw_enum:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000775 ParseEnumSpecifier(DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000776 continue;
777
778 // cv-qualifier:
779 case tok::kw_const:
780 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
781 break;
782 case tok::kw_volatile:
783 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
784 getLang())*2;
785 break;
786 case tok::kw_restrict:
787 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
788 getLang())*2;
789 break;
790
791 // GNU typeof support.
792 case tok::kw_typeof:
793 ParseTypeofSpecifier(DS);
794 continue;
795
Steve Naroff5f0466b2008-06-05 00:02:44 +0000796 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000797 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000798 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
799 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000800 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000801 goto DoneWithDeclSpec;
802
803 {
804 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000805 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000806 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000807 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000808 DS.SetRangeEnd(EndProtoLoc);
809
Chris Lattnerf006a222008-11-18 07:48:38 +0000810 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
811 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000812 // Need to support trailing type qualifiers (e.g. "id<p> const").
813 // If a type specifier follows, it will be diagnosed elsewhere.
814 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000815 }
Chris Lattner4b009652007-07-25 00:24:17 +0000816 }
817 // If the specifier combination wasn't legal, issue a diagnostic.
818 if (isInvalid) {
819 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000820 // Pick between error or extwarn.
821 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
822 : diag::ext_duplicate_declspec;
823 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000824 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000825 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000826 ConsumeToken();
827 }
828}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000829
Chris Lattnerd706dc82009-01-06 06:59:53 +0000830/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000831/// primarily follow the C++ grammar with additions for C99 and GNU,
832/// which together subsume the C grammar. Note that the C++
833/// type-specifier also includes the C type-qualifier (for const,
834/// volatile, and C99 restrict). Returns true if a type-specifier was
835/// found (and parsed), false otherwise.
836///
837/// type-specifier: [C++ 7.1.5]
838/// simple-type-specifier
839/// class-specifier
840/// enum-specifier
841/// elaborated-type-specifier [TODO]
842/// cv-qualifier
843///
844/// cv-qualifier: [C++ 7.1.5.1]
845/// 'const'
846/// 'volatile'
847/// [C99] 'restrict'
848///
849/// simple-type-specifier: [ C++ 7.1.5.2]
850/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
851/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
852/// 'char'
853/// 'wchar_t'
854/// 'bool'
855/// 'short'
856/// 'int'
857/// 'long'
858/// 'signed'
859/// 'unsigned'
860/// 'float'
861/// 'double'
862/// 'void'
863/// [C99] '_Bool'
864/// [C99] '_Complex'
865/// [C99] '_Imaginary' // Removed in TC2?
866/// [GNU] '_Decimal32'
867/// [GNU] '_Decimal64'
868/// [GNU] '_Decimal128'
869/// [GNU] typeof-specifier
870/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
871/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000872bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
873 const char *&PrevSpec,
874 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000875 SourceLocation Loc = Tok.getLocation();
876
877 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000878 case tok::identifier: // foo::bar
879 // Annotate typenames and C++ scope specifiers. If we get one, just
880 // recurse to handle whatever we get.
881 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000882 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000883 // Otherwise, not a type specifier.
884 return false;
885 case tok::coloncolon: // ::foo::bar
886 if (NextToken().is(tok::kw_new) || // ::new
887 NextToken().is(tok::kw_delete)) // ::delete
888 return false;
889
890 // Annotate typenames and C++ scope specifiers. If we get one, just
891 // recurse to handle whatever we get.
892 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000893 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000894 // Otherwise, not a type specifier.
895 return false;
896
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000897 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000898 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000899 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000900 Tok.getAnnotationValue());
901 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
902 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000903
904 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
905 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
906 // Objective-C interface. If we don't have Objective-C or a '<', this is
907 // just a normal reference to a typedef name.
908 if (!Tok.is(tok::less) || !getLang().ObjC1)
909 return true;
910
911 SourceLocation EndProtoLoc;
912 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
913 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
914 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
915
916 DS.SetRangeEnd(EndProtoLoc);
917 return true;
918 }
919
920 case tok::kw_short:
921 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
922 break;
923 case tok::kw_long:
924 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
925 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
926 else
927 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
928 break;
929 case tok::kw_signed:
930 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
931 break;
932 case tok::kw_unsigned:
933 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
934 break;
935 case tok::kw__Complex:
936 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
937 break;
938 case tok::kw__Imaginary:
939 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
940 break;
941 case tok::kw_void:
942 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
943 break;
944 case tok::kw_char:
945 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
946 break;
947 case tok::kw_int:
948 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
949 break;
950 case tok::kw_float:
951 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
952 break;
953 case tok::kw_double:
954 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
955 break;
956 case tok::kw_wchar_t:
957 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
958 break;
959 case tok::kw_bool:
960 case tok::kw__Bool:
961 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
962 break;
963 case tok::kw__Decimal32:
964 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
965 break;
966 case tok::kw__Decimal64:
967 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
968 break;
969 case tok::kw__Decimal128:
970 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
971 break;
972
973 // class-specifier:
974 case tok::kw_class:
975 case tok::kw_struct:
976 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000977 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000978 return true;
979
980 // enum-specifier:
981 case tok::kw_enum:
982 ParseEnumSpecifier(DS);
983 return true;
984
985 // cv-qualifier:
986 case tok::kw_const:
987 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
988 getLang())*2;
989 break;
990 case tok::kw_volatile:
991 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
992 getLang())*2;
993 break;
994 case tok::kw_restrict:
995 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
996 getLang())*2;
997 break;
998
999 // GNU typeof support.
1000 case tok::kw_typeof:
1001 ParseTypeofSpecifier(DS);
1002 return true;
1003
Steve Naroffedd04d52008-12-25 14:16:32 +00001004 case tok::kw___cdecl:
1005 case tok::kw___stdcall:
1006 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001007 if (!PP.getLangOptions().Microsoft) return false;
1008 ConsumeToken();
1009 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001010
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001011 default:
1012 // Not a type-specifier; do nothing.
1013 return false;
1014 }
1015
1016 // If the specifier combination wasn't legal, issue a diagnostic.
1017 if (isInvalid) {
1018 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001019 // Pick between error or extwarn.
1020 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1021 : diag::ext_duplicate_declspec;
1022 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001023 }
1024 DS.SetRangeEnd(Tok.getLocation());
1025 ConsumeToken(); // whatever we parsed above.
1026 return true;
1027}
Chris Lattner4b009652007-07-25 00:24:17 +00001028
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001029/// ParseStructDeclaration - Parse a struct declaration without the terminating
1030/// semicolon.
1031///
Chris Lattner4b009652007-07-25 00:24:17 +00001032/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001033/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001034/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001035/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001036/// struct-declarator-list:
1037/// struct-declarator
1038/// struct-declarator-list ',' struct-declarator
1039/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1040/// struct-declarator:
1041/// declarator
1042/// [GNU] declarator attributes[opt]
1043/// declarator[opt] ':' constant-expression
1044/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1045///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001046void Parser::
1047ParseStructDeclaration(DeclSpec &DS,
1048 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001049 if (Tok.is(tok::kw___extension__)) {
1050 // __extension__ silences extension warnings in the subexpression.
1051 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001052 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001053 return ParseStructDeclaration(DS, Fields);
1054 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001055
1056 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001057 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001058 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001059
Douglas Gregorb748fc52009-01-12 22:49:06 +00001060 // If there are no declarators, this is a free-standing declaration
1061 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001062 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001063 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001064 return;
1065 }
1066
1067 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001068 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001069 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001070 FieldDeclarator &DeclaratorInfo = Fields.back();
1071
Steve Naroffa9adf112007-08-20 22:28:22 +00001072 /// struct-declarator: declarator
1073 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001074 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001075 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001076
Chris Lattner34a01ad2007-10-09 17:33:22 +00001077 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001078 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001079 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001080 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001081 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001082 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001083 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001084 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001085
Steve Naroffa9adf112007-08-20 22:28:22 +00001086 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001087 if (Tok.is(tok::kw___attribute)) {
1088 SourceLocation Loc;
1089 AttributeList *AttrList = ParseAttributes(&Loc);
1090 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1091 }
1092
Steve Naroffa9adf112007-08-20 22:28:22 +00001093 // If we don't have a comma, it is either the end of the list (a ';')
1094 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001095 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001096 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001097
Steve Naroffa9adf112007-08-20 22:28:22 +00001098 // Consume the comma.
1099 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001100
Steve Naroffa9adf112007-08-20 22:28:22 +00001101 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001102 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001103
Steve Naroffa9adf112007-08-20 22:28:22 +00001104 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001105 if (Tok.is(tok::kw___attribute)) {
1106 SourceLocation Loc;
1107 AttributeList *AttrList = ParseAttributes(&Loc);
1108 Fields.back().D.AddAttributes(AttrList, Loc);
1109 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001110 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001111}
1112
1113/// ParseStructUnionBody
1114/// struct-contents:
1115/// struct-declaration-list
1116/// [EXT] empty
1117/// [GNU] "struct-declaration-list" without terminatoring ';'
1118/// struct-declaration-list:
1119/// struct-declaration
1120/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001121/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001122///
Chris Lattner4b009652007-07-25 00:24:17 +00001123void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1124 unsigned TagType, DeclTy *TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001125 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1126 PP.getSourceManager(),
1127 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001128
Chris Lattner4b009652007-07-25 00:24:17 +00001129 SourceLocation LBraceLoc = ConsumeBrace();
1130
Douglas Gregorcab994d2009-01-09 22:42:13 +00001131 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001132 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1133
Chris Lattner4b009652007-07-25 00:24:17 +00001134 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1135 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001136 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001137 Diag(Tok, diag::ext_empty_struct_union_enum)
1138 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001139
1140 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001141 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1142
Chris Lattner4b009652007-07-25 00:24:17 +00001143 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001144 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001145 // Each iteration of this loop reads one struct-declaration.
1146
1147 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001148 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001149 Diag(Tok, diag::ext_extra_struct_semi);
1150 ConsumeToken();
1151 continue;
1152 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001153
1154 // Parse all the comma separated declarators.
1155 DeclSpec DS;
1156 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001157 if (!Tok.is(tok::at)) {
1158 ParseStructDeclaration(DS, FieldDeclarators);
1159
1160 // Convert them all to fields.
1161 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1162 FieldDeclarator &FD = FieldDeclarators[i];
1163 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +00001164 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +00001165 DS.getSourceRange().getBegin(),
1166 FD.D, FD.BitfieldSize);
1167 FieldDecls.push_back(Field);
1168 }
1169 } else { // Handle @defs
1170 ConsumeToken();
1171 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1172 Diag(Tok, diag::err_unexpected_at);
1173 SkipUntil(tok::semi, true, true);
1174 continue;
1175 }
1176 ConsumeToken();
1177 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1178 if (!Tok.is(tok::identifier)) {
1179 Diag(Tok, diag::err_expected_ident);
1180 SkipUntil(tok::semi, true, true);
1181 continue;
1182 }
1183 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001184 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1185 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001186 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1187 ConsumeToken();
1188 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1189 }
Chris Lattner4b009652007-07-25 00:24:17 +00001190
Chris Lattner34a01ad2007-10-09 17:33:22 +00001191 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001192 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001193 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001194 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001195 break;
1196 } else {
1197 Diag(Tok, diag::err_expected_semi_decl_list);
1198 // Skip to end of block or statement
1199 SkipUntil(tok::r_brace, true, true);
1200 }
1201 }
1202
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001203 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001204
Chris Lattner4b009652007-07-25 00:24:17 +00001205 AttributeList *AttrList = 0;
1206 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001207 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001208 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001209
1210 Actions.ActOnFields(CurScope,
1211 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1212 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001213 AttrList);
1214 StructScope.Exit();
1215 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001216}
1217
1218
1219/// ParseEnumSpecifier
1220/// enum-specifier: [C99 6.7.2.2]
1221/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001222///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001223/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1224/// '}' attributes[opt]
1225/// 'enum' identifier
1226/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001227///
1228/// [C++] elaborated-type-specifier:
1229/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1230///
Douglas Gregor0c793bb2009-03-25 22:00:53 +00001231void Parser::ParseEnumSpecifier(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001232 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001233 SourceLocation StartLoc = ConsumeToken();
1234
1235 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001236
1237 AttributeList *Attr = 0;
1238 // If attributes exist after tag, parse them.
1239 if (Tok.is(tok::kw___attribute))
1240 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001241
1242 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001243 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001244 if (Tok.isNot(tok::identifier)) {
1245 Diag(Tok, diag::err_expected_ident);
1246 if (Tok.isNot(tok::l_brace)) {
1247 // Has no name and is not a definition.
1248 // Skip the rest of this declarator, up until the comma or semicolon.
1249 SkipUntil(tok::comma, true);
1250 return;
1251 }
1252 }
1253 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001254
1255 // Must have either 'enum name' or 'enum {...}'.
1256 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1257 Diag(Tok, diag::err_expected_ident_lbrace);
1258
1259 // Skip the rest of this declarator, up until the comma or semicolon.
1260 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001261 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001262 }
1263
1264 // If an identifier is present, consume and remember it.
1265 IdentifierInfo *Name = 0;
1266 SourceLocation NameLoc;
1267 if (Tok.is(tok::identifier)) {
1268 Name = Tok.getIdentifierInfo();
1269 NameLoc = ConsumeToken();
1270 }
1271
1272 // There are three options here. If we have 'enum foo;', then this is a
1273 // forward declaration. If we have 'enum foo {...' then this is a
1274 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1275 //
1276 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1277 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1278 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1279 //
1280 Action::TagKind TK;
1281 if (Tok.is(tok::l_brace))
1282 TK = Action::TK_Definition;
1283 else if (Tok.is(tok::semi))
1284 TK = Action::TK_Declaration;
1285 else
1286 TK = Action::TK_Reference;
1287 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregor0c793bb2009-03-25 22:00:53 +00001288 SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001289
Chris Lattner34a01ad2007-10-09 17:33:22 +00001290 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001291 ParseEnumBody(StartLoc, TagDecl);
1292
1293 // TODO: semantic analysis on the declspec for enums.
1294 const char *PrevSpec = 0;
1295 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001296 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001297}
1298
1299/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1300/// enumerator-list:
1301/// enumerator
1302/// enumerator-list ',' enumerator
1303/// enumerator:
1304/// enumeration-constant
1305/// enumeration-constant '=' constant-expression
1306/// enumeration-constant:
1307/// identifier
1308///
1309void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001310 // Enter the scope of the enum body and start the definition.
1311 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001312 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001313
Chris Lattner4b009652007-07-25 00:24:17 +00001314 SourceLocation LBraceLoc = ConsumeBrace();
1315
Chris Lattnerc9a92452007-08-27 17:24:30 +00001316 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001317 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001318 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001319
1320 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1321
1322 DeclTy *LastEnumConstDecl = 0;
1323
1324 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001325 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001326 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1327 SourceLocation IdentLoc = ConsumeToken();
1328
1329 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001330 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001331 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001332 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001333 AssignedVal = ParseConstantExpression();
1334 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001335 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001336 }
1337
1338 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001339 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001340 LastEnumConstDecl,
1341 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001342 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001343 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001344 EnumConstantDecls.push_back(EnumConstDecl);
1345 LastEnumConstDecl = EnumConstDecl;
1346
Chris Lattner34a01ad2007-10-09 17:33:22 +00001347 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001348 break;
1349 SourceLocation CommaLoc = ConsumeToken();
1350
Chris Lattner34a01ad2007-10-09 17:33:22 +00001351 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001352 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1353 }
1354
1355 // Eat the }.
1356 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1357
Steve Naroff0acc9c92007-09-15 18:49:24 +00001358 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001359 EnumConstantDecls.size());
1360
1361 DeclTy *AttrList = 0;
1362 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001363 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001364 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001365
1366 EnumScope.Exit();
1367 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001368}
1369
1370/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001371/// start of a type-qualifier-list.
1372bool Parser::isTypeQualifier() const {
1373 switch (Tok.getKind()) {
1374 default: return false;
1375 // type-qualifier
1376 case tok::kw_const:
1377 case tok::kw_volatile:
1378 case tok::kw_restrict:
1379 return true;
1380 }
1381}
1382
1383/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001384/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001385bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001386 switch (Tok.getKind()) {
1387 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001388
1389 case tok::identifier: // foo::bar
1390 // Annotate typenames and C++ scope specifiers. If we get one, just
1391 // recurse to handle whatever we get.
1392 if (TryAnnotateTypeOrScopeToken())
1393 return isTypeSpecifierQualifier();
1394 // Otherwise, not a type specifier.
1395 return false;
1396 case tok::coloncolon: // ::foo::bar
1397 if (NextToken().is(tok::kw_new) || // ::new
1398 NextToken().is(tok::kw_delete)) // ::delete
1399 return false;
1400
1401 // Annotate typenames and C++ scope specifiers. If we get one, just
1402 // recurse to handle whatever we get.
1403 if (TryAnnotateTypeOrScopeToken())
1404 return isTypeSpecifierQualifier();
1405 // Otherwise, not a type specifier.
1406 return false;
1407
Chris Lattner4b009652007-07-25 00:24:17 +00001408 // GNU attributes support.
1409 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001410 // GNU typeof support.
1411 case tok::kw_typeof:
1412
Chris Lattner4b009652007-07-25 00:24:17 +00001413 // type-specifiers
1414 case tok::kw_short:
1415 case tok::kw_long:
1416 case tok::kw_signed:
1417 case tok::kw_unsigned:
1418 case tok::kw__Complex:
1419 case tok::kw__Imaginary:
1420 case tok::kw_void:
1421 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001422 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001423 case tok::kw_int:
1424 case tok::kw_float:
1425 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001426 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001427 case tok::kw__Bool:
1428 case tok::kw__Decimal32:
1429 case tok::kw__Decimal64:
1430 case tok::kw__Decimal128:
1431
Chris Lattner2e78db32008-04-13 18:59:07 +00001432 // struct-or-union-specifier (C99) or class-specifier (C++)
1433 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001434 case tok::kw_struct:
1435 case tok::kw_union:
1436 // enum-specifier
1437 case tok::kw_enum:
1438
1439 // type-qualifier
1440 case tok::kw_const:
1441 case tok::kw_volatile:
1442 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001443
1444 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001445 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001446 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001447
1448 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1449 case tok::less:
1450 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001451
1452 case tok::kw___cdecl:
1453 case tok::kw___stdcall:
1454 case tok::kw___fastcall:
1455 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001456 }
1457}
1458
1459/// isDeclarationSpecifier() - Return true if the current token is part of a
1460/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001461bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001462 switch (Tok.getKind()) {
1463 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001464
1465 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001466 // Unfortunate hack to support "Class.factoryMethod" notation.
1467 if (getLang().ObjC1 && NextToken().is(tok::period))
1468 return false;
1469
Chris Lattnerb75fde62009-01-04 23:41:41 +00001470 // Annotate typenames and C++ scope specifiers. If we get one, just
1471 // recurse to handle whatever we get.
1472 if (TryAnnotateTypeOrScopeToken())
1473 return isDeclarationSpecifier();
1474 // Otherwise, not a declaration specifier.
1475 return false;
1476 case tok::coloncolon: // ::foo::bar
1477 if (NextToken().is(tok::kw_new) || // ::new
1478 NextToken().is(tok::kw_delete)) // ::delete
1479 return false;
1480
1481 // Annotate typenames and C++ scope specifiers. If we get one, just
1482 // recurse to handle whatever we get.
1483 if (TryAnnotateTypeOrScopeToken())
1484 return isDeclarationSpecifier();
1485 // Otherwise, not a declaration specifier.
1486 return false;
1487
Chris Lattner4b009652007-07-25 00:24:17 +00001488 // storage-class-specifier
1489 case tok::kw_typedef:
1490 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001491 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001492 case tok::kw_static:
1493 case tok::kw_auto:
1494 case tok::kw_register:
1495 case tok::kw___thread:
1496
1497 // type-specifiers
1498 case tok::kw_short:
1499 case tok::kw_long:
1500 case tok::kw_signed:
1501 case tok::kw_unsigned:
1502 case tok::kw__Complex:
1503 case tok::kw__Imaginary:
1504 case tok::kw_void:
1505 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001506 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001507 case tok::kw_int:
1508 case tok::kw_float:
1509 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001510 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001511 case tok::kw__Bool:
1512 case tok::kw__Decimal32:
1513 case tok::kw__Decimal64:
1514 case tok::kw__Decimal128:
1515
Chris Lattner2e78db32008-04-13 18:59:07 +00001516 // struct-or-union-specifier (C99) or class-specifier (C++)
1517 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001518 case tok::kw_struct:
1519 case tok::kw_union:
1520 // enum-specifier
1521 case tok::kw_enum:
1522
1523 // type-qualifier
1524 case tok::kw_const:
1525 case tok::kw_volatile:
1526 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001527
Chris Lattner4b009652007-07-25 00:24:17 +00001528 // function-specifier
1529 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001530 case tok::kw_virtual:
1531 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001532
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001533 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001534 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001535
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001536 // GNU typeof support.
1537 case tok::kw_typeof:
1538
1539 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001540 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001541 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001542
1543 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1544 case tok::less:
1545 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001546
Steve Naroffab1a3632009-01-06 19:34:12 +00001547 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001548 case tok::kw___cdecl:
1549 case tok::kw___stdcall:
1550 case tok::kw___fastcall:
1551 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001552 }
1553}
1554
1555
1556/// ParseTypeQualifierListOpt
1557/// type-qualifier-list: [C99 6.7.5]
1558/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001559/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001560/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001561/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001562///
Chris Lattner460696f2008-12-18 07:02:59 +00001563void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001564 while (1) {
1565 int isInvalid = false;
1566 const char *PrevSpec = 0;
1567 SourceLocation Loc = Tok.getLocation();
1568
1569 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001570 case tok::kw_const:
1571 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1572 getLang())*2;
1573 break;
1574 case tok::kw_volatile:
1575 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1576 getLang())*2;
1577 break;
1578 case tok::kw_restrict:
1579 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1580 getLang())*2;
1581 break;
Steve Naroffad620402008-12-25 14:41:26 +00001582 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001583 case tok::kw___cdecl:
1584 case tok::kw___stdcall:
1585 case tok::kw___fastcall:
1586 if (!PP.getLangOptions().Microsoft)
1587 goto DoneWithTypeQuals;
1588 // Just ignore it.
1589 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001590 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001591 if (AttributesAllowed) {
1592 DS.AddAttributes(ParseAttributes());
1593 continue; // do *not* consume the next token!
1594 }
1595 // otherwise, FALL THROUGH!
1596 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001597 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001598 // If this is not a type-qualifier token, we're done reading type
1599 // qualifiers. First verify that DeclSpec's are consistent.
1600 DS.Finish(Diags, PP.getSourceManager(), getLang());
1601 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001602 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001603
Chris Lattner4b009652007-07-25 00:24:17 +00001604 // If the specifier combination wasn't legal, issue a diagnostic.
1605 if (isInvalid) {
1606 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001607 // Pick between error or extwarn.
1608 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1609 : diag::ext_duplicate_declspec;
1610 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001611 }
1612 ConsumeToken();
1613 }
1614}
1615
1616
1617/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1618///
1619void Parser::ParseDeclarator(Declarator &D) {
1620 /// This implements the 'declarator' production in the C grammar, then checks
1621 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001622 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001623}
1624
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001625/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1626/// is parsed by the function passed to it. Pass null, and the direct-declarator
1627/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001628/// ptr-operator production.
1629///
Sebastian Redl75555032009-01-24 21:16:55 +00001630/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1631/// [C] pointer[opt] direct-declarator
1632/// [C++] direct-declarator
1633/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001634///
1635/// pointer: [C99 6.7.5]
1636/// '*' type-qualifier-list[opt]
1637/// '*' type-qualifier-list[opt] pointer
1638///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001639/// ptr-operator:
1640/// '*' cv-qualifier-seq[opt]
1641/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001642/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001643/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001644/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001645/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001646void Parser::ParseDeclaratorInternal(Declarator &D,
1647 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001648
Sebastian Redl75555032009-01-24 21:16:55 +00001649 // C++ member pointers start with a '::' or a nested-name.
1650 // Member pointers get special handling, since there's no place for the
1651 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001652 if (getLang().CPlusPlus &&
1653 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1654 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001655 CXXScopeSpec SS;
1656 if (ParseOptionalCXXScopeSpecifier(SS)) {
1657 if(Tok.isNot(tok::star)) {
1658 // The scope spec really belongs to the direct-declarator.
1659 D.getCXXScopeSpec() = SS;
1660 if (DirectDeclParser)
1661 (this->*DirectDeclParser)(D);
1662 return;
1663 }
1664
1665 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001666 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001667 DeclSpec DS;
1668 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001669 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001670
1671 // Recurse to parse whatever is left.
1672 ParseDeclaratorInternal(D, DirectDeclParser);
1673
1674 // Sema will have to catch (syntactically invalid) pointers into global
1675 // scope. It has to catch pointers into namespace scope anyway.
1676 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001677 Loc, DS.TakeAttributes()),
1678 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001679 return;
1680 }
1681 }
1682
1683 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001684 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001685 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001686 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001687 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001688 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001689 if (DirectDeclParser)
1690 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001691 return;
1692 }
Sebastian Redl75555032009-01-24 21:16:55 +00001693
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001694 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1695 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001696 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001697 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001698
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001699 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001700 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001701 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001702
Chris Lattner4b009652007-07-25 00:24:17 +00001703 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001704 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001705
Chris Lattner4b009652007-07-25 00:24:17 +00001706 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001707 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001708 if (Kind == tok::star)
1709 // Remember that we parsed a pointer type, and remember the type-quals.
1710 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001711 DS.TakeAttributes()),
1712 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001713 else
1714 // Remember that we parsed a Block type, and remember the type-quals.
1715 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001716 Loc),
1717 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001718 } else {
1719 // Is a reference
1720 DeclSpec DS;
1721
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001722 // Complain about rvalue references in C++03, but then go on and build
1723 // the declarator.
1724 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1725 Diag(Loc, diag::err_rvalue_reference);
1726
Chris Lattner4b009652007-07-25 00:24:17 +00001727 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1728 // cv-qualifiers are introduced through the use of a typedef or of a
1729 // template type argument, in which case the cv-qualifiers are ignored.
1730 //
1731 // [GNU] Retricted references are allowed.
1732 // [GNU] Attributes on references are allowed.
1733 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001734 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001735
1736 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1737 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1738 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001739 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001740 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1741 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001742 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001743 }
1744
1745 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001746 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001747
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001748 if (D.getNumTypeObjects() > 0) {
1749 // C++ [dcl.ref]p4: There shall be no references to references.
1750 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1751 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001752 if (const IdentifierInfo *II = D.getIdentifier())
1753 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1754 << II;
1755 else
1756 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1757 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001758
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001759 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001760 // can go ahead and build the (technically ill-formed)
1761 // declarator: reference collapsing will take care of it.
1762 }
1763 }
1764
Chris Lattner4b009652007-07-25 00:24:17 +00001765 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001766 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001767 DS.TakeAttributes(),
1768 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001769 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001770 }
1771}
1772
1773/// ParseDirectDeclarator
1774/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001775/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001776/// '(' declarator ')'
1777/// [GNU] '(' attributes declarator ')'
1778/// [C90] direct-declarator '[' constant-expression[opt] ']'
1779/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1780/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1781/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1782/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1783/// direct-declarator '(' parameter-type-list ')'
1784/// direct-declarator '(' identifier-list[opt] ')'
1785/// [GNU] direct-declarator '(' parameter-forward-declarations
1786/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001787/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1788/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001789/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001790///
1791/// declarator-id: [C++ 8]
1792/// id-expression
1793/// '::'[opt] nested-name-specifier[opt] type-name
1794///
1795/// id-expression: [C++ 5.1]
1796/// unqualified-id
1797/// qualified-id [TODO]
1798///
1799/// unqualified-id: [C++ 5.1]
1800/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001801/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001802/// conversion-function-id [TODO]
1803/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001804/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001805///
Chris Lattner4b009652007-07-25 00:24:17 +00001806void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001807 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001808
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001809 if (getLang().CPlusPlus) {
1810 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001811 // ParseDeclaratorInternal might already have parsed the scope.
1812 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1813 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001814 if (afterCXXScope) {
1815 // Change the declaration context for name lookup, until this function
1816 // is exited (and the declarator has been parsed).
1817 DeclScopeObj.EnterDeclaratorScope();
1818 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001819
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001820 if (Tok.is(tok::identifier)) {
1821 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001822
Douglas Gregor2fa10442008-12-18 19:37:40 +00001823 // If this identifier is the name of the current class, it's a
1824 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001825 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001826 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001827 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001828 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001829 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001830 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001831 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1832 ConsumeToken();
1833 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001834 } else if (Tok.is(tok::annot_template_id)) {
1835 TemplateIdAnnotation *TemplateId
1836 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1837
1838 // FIXME: Could this template-id name a constructor?
1839
1840 // FIXME: This is an egregious hack, where we silently ignore
1841 // the specialization (which should be a function template
1842 // specialization name) and use the name instead. This hack
1843 // will go away when we have support for function
1844 // specializations.
1845 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1846 TemplateId->Destroy();
1847 ConsumeToken();
1848 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001849 } else if (Tok.is(tok::kw_operator)) {
1850 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001851 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001852
Douglas Gregor853dd392008-12-26 15:00:45 +00001853 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001854 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1855 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001856 } else {
1857 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001858 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1859 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1860 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001861 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001862 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001863 }
1864 goto PastIdentifier;
1865 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001866 // This should be a C++ destructor.
1867 SourceLocation TildeLoc = ConsumeToken();
1868 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001869 // FIXME: Inaccurate.
1870 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001871 SourceLocation EndLoc;
1872 if (TypeTy *Type = ParseClassName(EndLoc)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001873 D.setDestructor(Type, TildeLoc, NameLoc);
1874 } else {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001875 D.SetIdentifier(0, TildeLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001876 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001877 } else {
1878 Diag(Tok, diag::err_expected_class_name);
1879 D.SetIdentifier(0, TildeLoc);
1880 }
1881 goto PastIdentifier;
1882 }
1883
1884 // If we reached this point, token is not identifier and not '~'.
1885
1886 if (afterCXXScope) {
1887 Diag(Tok, diag::err_expected_unqualified_id);
1888 D.SetIdentifier(0, Tok.getLocation());
1889 D.setInvalidType(true);
1890 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001891 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001892 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001893 }
1894
1895 // If we reached this point, we are either in C/ObjC or the token didn't
1896 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001897 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1898 assert(!getLang().CPlusPlus &&
1899 "There's a C++-specific check for tok::identifier above");
1900 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1901 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1902 ConsumeToken();
1903 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001904 // direct-declarator: '(' declarator ')'
1905 // direct-declarator: '(' attributes declarator ')'
1906 // Example: 'char (*X)' or 'int (*XX)(void)'
1907 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001908 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001909 // This could be something simple like "int" (in which case the declarator
1910 // portion is empty), if an abstract-declarator is allowed.
1911 D.SetIdentifier(0, Tok.getLocation());
1912 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00001913 if (D.getContext() == Declarator::MemberContext)
1914 Diag(Tok, diag::err_expected_member_name_or_semi)
1915 << D.getDeclSpec().getSourceRange();
1916 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001917 Diag(Tok, diag::err_expected_unqualified_id);
1918 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001919 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001920 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001921 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001922 }
1923
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001924 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001925 assert(D.isPastIdentifier() &&
1926 "Haven't past the location of the identifier yet?");
1927
1928 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001929 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001930 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1931 // In such a case, check if we actually have a function declarator; if it
1932 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001933 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1934 // When not in file scope, warn for ambiguous function declarators, just
1935 // in case the author intended it as a variable definition.
1936 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1937 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1938 break;
1939 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001940 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001941 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001942 ParseBracketDeclarator(D);
1943 } else {
1944 break;
1945 }
1946 }
1947}
1948
Chris Lattnera0d056d2008-04-06 05:45:57 +00001949/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1950/// only called before the identifier, so these are most likely just grouping
1951/// parens for precedence. If we find that these are actually function
1952/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1953///
1954/// direct-declarator:
1955/// '(' declarator ')'
1956/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001957/// direct-declarator '(' parameter-type-list ')'
1958/// direct-declarator '(' identifier-list[opt] ')'
1959/// [GNU] direct-declarator '(' parameter-forward-declarations
1960/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001961///
1962void Parser::ParseParenDeclarator(Declarator &D) {
1963 SourceLocation StartLoc = ConsumeParen();
1964 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1965
Chris Lattner1f185292008-10-20 02:05:46 +00001966 // Eat any attributes before we look at whether this is a grouping or function
1967 // declarator paren. If this is a grouping paren, the attribute applies to
1968 // the type being built up, for example:
1969 // int (__attribute__(()) *x)(long y)
1970 // If this ends up not being a grouping paren, the attribute applies to the
1971 // first argument, for example:
1972 // int (__attribute__(()) int x)
1973 // In either case, we need to eat any attributes to be able to determine what
1974 // sort of paren this is.
1975 //
1976 AttributeList *AttrList = 0;
1977 bool RequiresArg = false;
1978 if (Tok.is(tok::kw___attribute)) {
1979 AttrList = ParseAttributes();
1980
1981 // We require that the argument list (if this is a non-grouping paren) be
1982 // present even if the attribute list was empty.
1983 RequiresArg = true;
1984 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001985 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00001986 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1987 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00001988 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00001989
Chris Lattnera0d056d2008-04-06 05:45:57 +00001990 // If we haven't past the identifier yet (or where the identifier would be
1991 // stored, if this is an abstract declarator), then this is probably just
1992 // grouping parens. However, if this could be an abstract-declarator, then
1993 // this could also be the start of function arguments (consider 'void()').
1994 bool isGrouping;
1995
1996 if (!D.mayOmitIdentifier()) {
1997 // If this can't be an abstract-declarator, this *must* be a grouping
1998 // paren, because we haven't seen the identifier yet.
1999 isGrouping = true;
2000 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002001 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002002 isDeclarationSpecifier()) { // 'int(int)' is a function.
2003 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2004 // considered to be a type, not a K&R identifier-list.
2005 isGrouping = false;
2006 } else {
2007 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2008 isGrouping = true;
2009 }
2010
2011 // If this is a grouping paren, handle:
2012 // direct-declarator: '(' declarator ')'
2013 // direct-declarator: '(' attributes declarator ')'
2014 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002015 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002016 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002017 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002018 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002019
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002020 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002021 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002022 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002023
2024 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002025 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002026 return;
2027 }
2028
2029 // Okay, if this wasn't a grouping paren, it must be the start of a function
2030 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002031 // identifier (and remember where it would have been), then call into
2032 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002033 D.SetIdentifier(0, Tok.getLocation());
2034
Chris Lattner1f185292008-10-20 02:05:46 +00002035 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002036}
2037
2038/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2039/// declarator D up to a paren, which indicates that we are parsing function
2040/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002041///
Chris Lattner1f185292008-10-20 02:05:46 +00002042/// If AttrList is non-null, then the caller parsed those arguments immediately
2043/// after the open paren - they should be considered to be the first argument of
2044/// a parameter. If RequiresArg is true, then the first argument of the
2045/// function is required to be present and required to not be an identifier
2046/// list.
2047///
Chris Lattner4b009652007-07-25 00:24:17 +00002048/// This method also handles this portion of the grammar:
2049/// parameter-type-list: [C99 6.7.5]
2050/// parameter-list
2051/// parameter-list ',' '...'
2052///
2053/// parameter-list: [C99 6.7.5]
2054/// parameter-declaration
2055/// parameter-list ',' parameter-declaration
2056///
2057/// parameter-declaration: [C99 6.7.5]
2058/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002059/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002060/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002061/// declaration-specifiers abstract-declarator[opt]
2062/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002063/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002064/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2065///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002066/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002067/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002068///
Chris Lattner1f185292008-10-20 02:05:46 +00002069void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2070 AttributeList *AttrList,
2071 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002072 // lparen is already consumed!
2073 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002074
Chris Lattner1f185292008-10-20 02:05:46 +00002075 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002076 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002077 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002078 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002079 delete AttrList;
2080 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002081
Sebastian Redl0c986032009-02-09 18:23:29 +00002082 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002083
2084 // cv-qualifier-seq[opt].
2085 DeclSpec DS;
2086 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002087 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002088 if (!DS.getSourceRange().getEnd().isInvalid())
2089 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002090
2091 // Parse exception-specification[opt].
2092 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002093 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002094 }
2095
Chris Lattner9f7564b2008-04-06 06:57:35 +00002096 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002097 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002098 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002099 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002100 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002101 /*arglist*/ 0, 0,
2102 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002103 LParenLoc, D),
2104 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002105 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002106 }
2107
2108 // Alternatively, this parameter list may be an identifier list form for a
2109 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002110 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002111 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002112 // K&R identifier lists can't have typedefs as identifiers, per
2113 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002114 if (RequiresArg) {
2115 Diag(Tok, diag::err_argument_required_after_attribute);
2116 delete AttrList;
2117 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002118 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2119 // normal declarators, not for abstract-declarators.
2120 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002121 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002122 }
2123
2124 // Finally, a normal, non-empty parameter type list.
2125
2126 // Build up an array of information about the parsed arguments.
2127 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002128
2129 // Enter function-declaration scope, limiting any declarators to the
2130 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002131 ParseScope PrototypeScope(this,
2132 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002133
2134 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002135 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002136 while (1) {
2137 if (Tok.is(tok::ellipsis)) {
2138 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002139 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002140 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002141 }
2142
Chris Lattner9f7564b2008-04-06 06:57:35 +00002143 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002144
Chris Lattner9f7564b2008-04-06 06:57:35 +00002145 // Parse the declaration-specifiers.
2146 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002147
2148 // If the caller parsed attributes for the first argument, add them now.
2149 if (AttrList) {
2150 DS.AddAttributes(AttrList);
2151 AttrList = 0; // Only apply the attributes to the first parameter.
2152 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002153 ParseDeclarationSpecifiers(DS);
2154
Chris Lattner9f7564b2008-04-06 06:57:35 +00002155 // Parse the declarator. This is "PrototypeContext", because we must
2156 // accept either 'declarator' or 'abstract-declarator' here.
2157 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2158 ParseDeclarator(ParmDecl);
2159
2160 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002161 if (Tok.is(tok::kw___attribute)) {
2162 SourceLocation Loc;
2163 AttributeList *AttrList = ParseAttributes(&Loc);
2164 ParmDecl.AddAttributes(AttrList, Loc);
2165 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002166
Chris Lattner9f7564b2008-04-06 06:57:35 +00002167 // Remember this parsed parameter in ParamInfo.
2168 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2169
Douglas Gregor605de8d2008-12-16 21:30:33 +00002170 // DefArgToks is used when the parsing of default arguments needs
2171 // to be delayed.
2172 CachedTokens *DefArgToks = 0;
2173
Chris Lattner9f7564b2008-04-06 06:57:35 +00002174 // If no parameter was specified, verify that *something* was specified,
2175 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002176 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2177 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002178 // Completely missing, emit error.
2179 Diag(DSStart, diag::err_missing_param);
2180 } else {
2181 // Otherwise, we have something. Add it and let semantic analysis try
2182 // to grok it and add the result to the ParamInfo we are building.
2183
2184 // Inform the actions module about the parameter declarator, so it gets
2185 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00002186 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2187
2188 // Parse the default argument, if any. We parse the default
2189 // arguments in all dialects; the semantic analysis in
2190 // ActOnParamDefaultArgument will reject the default argument in
2191 // C.
2192 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002193 SourceLocation EqualLoc = Tok.getLocation();
2194
Chris Lattner3e254fb2008-04-08 04:40:51 +00002195 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002196 if (D.getContext() == Declarator::MemberContext) {
2197 // If we're inside a class definition, cache the tokens
2198 // corresponding to the default argument. We'll actually parse
2199 // them when we see the end of the class definition.
2200 // FIXME: Templates will require something similar.
2201 // FIXME: Can we use a smart pointer for Toks?
2202 DefArgToks = new CachedTokens;
2203
2204 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2205 tok::semi, false)) {
2206 delete DefArgToks;
2207 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002208 Actions.ActOnParamDefaultArgumentError(Param);
2209 } else
2210 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002211 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002212 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002213 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002214
2215 OwningExprResult DefArgResult(ParseAssignmentExpression());
2216 if (DefArgResult.isInvalid()) {
2217 Actions.ActOnParamDefaultArgumentError(Param);
2218 SkipUntil(tok::comma, tok::r_paren, true, true);
2219 } else {
2220 // Inform the actions module about the default argument
2221 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002222 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002223 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002224 }
2225 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002226
2227 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002228 ParmDecl.getIdentifierLoc(), Param,
2229 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002230 }
2231
2232 // If the next token is a comma, consume it and keep reading arguments.
2233 if (Tok.isNot(tok::comma)) break;
2234
2235 // Consume the comma.
2236 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002237 }
2238
Chris Lattner9f7564b2008-04-06 06:57:35 +00002239 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002240 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002241
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002242 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002243 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002244
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002245 DeclSpec DS;
2246 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002247 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002248 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002249 if (!DS.getSourceRange().getEnd().isInvalid())
2250 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002251
2252 // Parse exception-specification[opt].
2253 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002254 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002255 }
2256
Chris Lattner4b009652007-07-25 00:24:17 +00002257 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002258 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002259 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002260 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002261 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002262 LParenLoc, D),
2263 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002264}
2265
Chris Lattner35d9c912008-04-06 06:34:08 +00002266/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2267/// we found a K&R-style identifier list instead of a type argument list. The
2268/// current token is known to be the first identifier in the list.
2269///
2270/// identifier-list: [C99 6.7.5]
2271/// identifier
2272/// identifier-list ',' identifier
2273///
2274void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2275 Declarator &D) {
2276 // Build up an array of information about the parsed arguments.
2277 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2278 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2279
2280 // If there was no identifier specified for the declarator, either we are in
2281 // an abstract-declarator, or we are in a parameter declarator which was found
2282 // to be abstract. In abstract-declarators, identifier lists are not valid:
2283 // diagnose this.
2284 if (!D.getIdentifier())
2285 Diag(Tok, diag::ext_ident_list_in_param);
2286
2287 // Tok is known to be the first identifier in the list. Remember this
2288 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002289 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002290 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2291 Tok.getLocation(), 0));
2292
Chris Lattner113a56b2008-04-06 06:39:19 +00002293 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002294
2295 while (Tok.is(tok::comma)) {
2296 // Eat the comma.
2297 ConsumeToken();
2298
Chris Lattner113a56b2008-04-06 06:39:19 +00002299 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002300 if (Tok.isNot(tok::identifier)) {
2301 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002302 SkipUntil(tok::r_paren);
2303 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002304 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002305
Chris Lattner35d9c912008-04-06 06:34:08 +00002306 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002307
2308 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002309 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002310 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002311
2312 // Verify that the argument identifier has not already been mentioned.
2313 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002314 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002315 } else {
2316 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002317 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2318 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00002319 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002320
2321 // Eat the identifier.
2322 ConsumeToken();
2323 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002324
2325 // If we have the closing ')', eat it and we're done.
2326 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2327
Chris Lattner113a56b2008-04-06 06:39:19 +00002328 // Remember that we parsed a function type, and remember the attributes. This
2329 // function type is always a K&R style function type, which is not varargs and
2330 // has no prototype.
2331 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002332 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002333 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002334 /*TypeQuals*/0, LParenLoc, D),
2335 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002336}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002337
Chris Lattner4b009652007-07-25 00:24:17 +00002338/// [C90] direct-declarator '[' constant-expression[opt] ']'
2339/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2340/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2341/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2342/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2343void Parser::ParseBracketDeclarator(Declarator &D) {
2344 SourceLocation StartLoc = ConsumeBracket();
2345
Chris Lattner1525c3a2008-12-18 07:27:21 +00002346 // C array syntax has many features, but by-far the most common is [] and [4].
2347 // This code does a fast path to handle some of the most obvious cases.
2348 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002349 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002350 // Remember that we parsed the empty array type.
2351 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002352 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2353 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002354 return;
2355 } else if (Tok.getKind() == tok::numeric_constant &&
2356 GetLookAheadToken(1).is(tok::r_square)) {
2357 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002358 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002359 ConsumeToken();
2360
Sebastian Redl0c986032009-02-09 18:23:29 +00002361 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002362
2363 // If there was an error parsing the assignment-expression, recover.
2364 if (ExprRes.isInvalid())
2365 ExprRes.release(); // Deallocate expr, just use [].
2366
2367 // Remember that we parsed a array type, and remember its features.
2368 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002369 ExprRes.release(), StartLoc),
2370 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002371 return;
2372 }
2373
Chris Lattner4b009652007-07-25 00:24:17 +00002374 // If valid, this location is the position where we read the 'static' keyword.
2375 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002376 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002377 StaticLoc = ConsumeToken();
2378
2379 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002380 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002381 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002382 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002383
2384 // If we haven't already read 'static', check to see if there is one after the
2385 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002386 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002387 StaticLoc = ConsumeToken();
2388
2389 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2390 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002391 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002392
2393 // Handle the case where we have '[*]' as the array size. However, a leading
2394 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2395 // the the token after the star is a ']'. Since stars in arrays are
2396 // infrequent, use of lookahead is not costly here.
2397 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002398 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002399
Chris Lattner306d4df2008-12-18 06:50:14 +00002400 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002401 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002402 StaticLoc = SourceLocation(); // Drop the static.
2403 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002404 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002405 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002406 // Note, in C89, this production uses the constant-expr production instead
2407 // of assignment-expr. The only difference is that assignment-expr allows
2408 // things like '=' and '*='. Sema rejects these in C89 mode because they
2409 // are not i-c-e's, so we don't need to distinguish between the two here.
2410
Chris Lattner4b009652007-07-25 00:24:17 +00002411 // Parse the assignment-expression now.
2412 NumElements = ParseAssignmentExpression();
2413 }
2414
2415 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002416 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002417 // If the expression was invalid, skip it.
2418 SkipUntil(tok::r_square);
2419 return;
2420 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002421
2422 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2423
Chris Lattner1525c3a2008-12-18 07:27:21 +00002424 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002425 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2426 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002427 NumElements.release(), StartLoc),
2428 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002429}
2430
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002431/// [GNU] typeof-specifier:
2432/// typeof ( expressions )
2433/// typeof ( type-name )
2434/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002435///
2436void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002437 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002438 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002439 SourceLocation StartLoc = ConsumeToken();
2440
Chris Lattner34a01ad2007-10-09 17:33:22 +00002441 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002442 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002443 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002444 return;
2445 }
2446
Sebastian Redl14ca7412008-12-11 21:36:32 +00002447 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002448 if (Result.isInvalid()) {
2449 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002450 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002451 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002452
2453 const char *PrevSpec = 0;
2454 // Check for duplicate type specifiers.
2455 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002456 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002457 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002458
2459 // FIXME: Not accurate, the range gets one token more than it should.
2460 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002461 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002462 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002463
Steve Naroff7cbb1462007-07-31 12:34:36 +00002464 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2465
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002466 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002467 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002468
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002469 assert((Ty.isInvalid() || Ty.get()) &&
2470 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002471
Chris Lattner34a01ad2007-10-09 17:33:22 +00002472 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002473 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002474 return;
2475 }
2476 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002477
2478 if (Ty.isInvalid())
2479 DS.SetTypeSpecError();
2480 else {
2481 const char *PrevSpec = 0;
2482 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2483 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2484 Ty.get()))
2485 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2486 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002487 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002488 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002489
2490 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002491 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002492 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002493 return;
2494 }
2495 RParenLoc = ConsumeParen();
2496 const char *PrevSpec = 0;
2497 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2498 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002499 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002500 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002501 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002502 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002503}
2504
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002505