blob: b1cbc3cceb6d3e7535bda8fee1048e9935febee1 [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
Douglas Gregord3022602009-03-27 23:10:48 +0000791 // C++ typename-specifier:
792 case tok::kw_typename:
793 if (TryAnnotateTypeOrScopeToken())
794 continue;
795 break;
796
Chris Lattnerc297b722009-01-21 19:48:37 +0000797 // GNU typeof support.
798 case tok::kw_typeof:
799 ParseTypeofSpecifier(DS);
800 continue;
801
Steve Naroff5f0466b2008-06-05 00:02:44 +0000802 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000803 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000804 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
805 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000806 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000807 goto DoneWithDeclSpec;
808
809 {
810 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000811 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000812 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000813 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000814 DS.SetRangeEnd(EndProtoLoc);
815
Chris Lattnerf006a222008-11-18 07:48:38 +0000816 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
817 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000818 // Need to support trailing type qualifiers (e.g. "id<p> const").
819 // If a type specifier follows, it will be diagnosed elsewhere.
820 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000821 }
Chris Lattner4b009652007-07-25 00:24:17 +0000822 }
823 // If the specifier combination wasn't legal, issue a diagnostic.
824 if (isInvalid) {
825 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000826 // Pick between error or extwarn.
827 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
828 : diag::ext_duplicate_declspec;
829 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000830 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000831 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000832 ConsumeToken();
833 }
834}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000835
Chris Lattnerd706dc82009-01-06 06:59:53 +0000836/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000837/// primarily follow the C++ grammar with additions for C99 and GNU,
838/// which together subsume the C grammar. Note that the C++
839/// type-specifier also includes the C type-qualifier (for const,
840/// volatile, and C99 restrict). Returns true if a type-specifier was
841/// found (and parsed), false otherwise.
842///
843/// type-specifier: [C++ 7.1.5]
844/// simple-type-specifier
845/// class-specifier
846/// enum-specifier
847/// elaborated-type-specifier [TODO]
848/// cv-qualifier
849///
850/// cv-qualifier: [C++ 7.1.5.1]
851/// 'const'
852/// 'volatile'
853/// [C99] 'restrict'
854///
855/// simple-type-specifier: [ C++ 7.1.5.2]
856/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
857/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
858/// 'char'
859/// 'wchar_t'
860/// 'bool'
861/// 'short'
862/// 'int'
863/// 'long'
864/// 'signed'
865/// 'unsigned'
866/// 'float'
867/// 'double'
868/// 'void'
869/// [C99] '_Bool'
870/// [C99] '_Complex'
871/// [C99] '_Imaginary' // Removed in TC2?
872/// [GNU] '_Decimal32'
873/// [GNU] '_Decimal64'
874/// [GNU] '_Decimal128'
875/// [GNU] typeof-specifier
876/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
877/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000878bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
879 const char *&PrevSpec,
880 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000881 SourceLocation Loc = Tok.getLocation();
882
883 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000884 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +0000885 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +0000886 // Annotate typenames and C++ scope specifiers. If we get one, just
887 // recurse to handle whatever we get.
888 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000889 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000890 // Otherwise, not a type specifier.
891 return false;
892 case tok::coloncolon: // ::foo::bar
893 if (NextToken().is(tok::kw_new) || // ::new
894 NextToken().is(tok::kw_delete)) // ::delete
895 return false;
896
897 // Annotate typenames and C++ scope specifiers. If we get one, just
898 // recurse to handle whatever we get.
899 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000900 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000901 // Otherwise, not a type specifier.
902 return false;
903
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000904 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000905 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000906 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000907 Tok.getAnnotationValue());
908 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
909 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000910
911 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
912 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
913 // Objective-C interface. If we don't have Objective-C or a '<', this is
914 // just a normal reference to a typedef name.
915 if (!Tok.is(tok::less) || !getLang().ObjC1)
916 return true;
917
918 SourceLocation EndProtoLoc;
919 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
920 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
921 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
922
923 DS.SetRangeEnd(EndProtoLoc);
924 return true;
925 }
926
927 case tok::kw_short:
928 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
929 break;
930 case tok::kw_long:
931 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
932 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
933 else
934 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
935 break;
936 case tok::kw_signed:
937 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
938 break;
939 case tok::kw_unsigned:
940 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
941 break;
942 case tok::kw__Complex:
943 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
944 break;
945 case tok::kw__Imaginary:
946 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
947 break;
948 case tok::kw_void:
949 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
950 break;
951 case tok::kw_char:
952 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
953 break;
954 case tok::kw_int:
955 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
956 break;
957 case tok::kw_float:
958 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
959 break;
960 case tok::kw_double:
961 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
962 break;
963 case tok::kw_wchar_t:
964 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
965 break;
966 case tok::kw_bool:
967 case tok::kw__Bool:
968 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
969 break;
970 case tok::kw__Decimal32:
971 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
972 break;
973 case tok::kw__Decimal64:
974 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
975 break;
976 case tok::kw__Decimal128:
977 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
978 break;
979
980 // class-specifier:
981 case tok::kw_class:
982 case tok::kw_struct:
983 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000984 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000985 return true;
986
987 // enum-specifier:
988 case tok::kw_enum:
989 ParseEnumSpecifier(DS);
990 return true;
991
992 // cv-qualifier:
993 case tok::kw_const:
994 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
995 getLang())*2;
996 break;
997 case tok::kw_volatile:
998 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
999 getLang())*2;
1000 break;
1001 case tok::kw_restrict:
1002 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1003 getLang())*2;
1004 break;
1005
1006 // GNU typeof support.
1007 case tok::kw_typeof:
1008 ParseTypeofSpecifier(DS);
1009 return true;
1010
Steve Naroffedd04d52008-12-25 14:16:32 +00001011 case tok::kw___cdecl:
1012 case tok::kw___stdcall:
1013 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001014 if (!PP.getLangOptions().Microsoft) return false;
1015 ConsumeToken();
1016 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001017
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001018 default:
1019 // Not a type-specifier; do nothing.
1020 return false;
1021 }
1022
1023 // If the specifier combination wasn't legal, issue a diagnostic.
1024 if (isInvalid) {
1025 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001026 // Pick between error or extwarn.
1027 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1028 : diag::ext_duplicate_declspec;
1029 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001030 }
1031 DS.SetRangeEnd(Tok.getLocation());
1032 ConsumeToken(); // whatever we parsed above.
1033 return true;
1034}
Chris Lattner4b009652007-07-25 00:24:17 +00001035
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001036/// ParseStructDeclaration - Parse a struct declaration without the terminating
1037/// semicolon.
1038///
Chris Lattner4b009652007-07-25 00:24:17 +00001039/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001040/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001041/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001042/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001043/// struct-declarator-list:
1044/// struct-declarator
1045/// struct-declarator-list ',' struct-declarator
1046/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1047/// struct-declarator:
1048/// declarator
1049/// [GNU] declarator attributes[opt]
1050/// declarator[opt] ':' constant-expression
1051/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1052///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001053void Parser::
1054ParseStructDeclaration(DeclSpec &DS,
1055 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001056 if (Tok.is(tok::kw___extension__)) {
1057 // __extension__ silences extension warnings in the subexpression.
1058 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001059 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001060 return ParseStructDeclaration(DS, Fields);
1061 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001062
1063 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001064 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001065 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001066
Douglas Gregorb748fc52009-01-12 22:49:06 +00001067 // If there are no declarators, this is a free-standing declaration
1068 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001069 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001070 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001071 return;
1072 }
1073
1074 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001075 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001076 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001077 FieldDeclarator &DeclaratorInfo = Fields.back();
1078
Steve Naroffa9adf112007-08-20 22:28:22 +00001079 /// struct-declarator: declarator
1080 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001081 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001082 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001083
Chris Lattner34a01ad2007-10-09 17:33:22 +00001084 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001085 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001086 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001087 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001088 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001089 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001090 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001091 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001092
Steve Naroffa9adf112007-08-20 22:28:22 +00001093 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001094 if (Tok.is(tok::kw___attribute)) {
1095 SourceLocation Loc;
1096 AttributeList *AttrList = ParseAttributes(&Loc);
1097 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1098 }
1099
Steve Naroffa9adf112007-08-20 22:28:22 +00001100 // If we don't have a comma, it is either the end of the list (a ';')
1101 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001102 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001103 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001104
Steve Naroffa9adf112007-08-20 22:28:22 +00001105 // Consume the comma.
1106 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001107
Steve Naroffa9adf112007-08-20 22:28:22 +00001108 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001109 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001110
Steve Naroffa9adf112007-08-20 22:28:22 +00001111 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001112 if (Tok.is(tok::kw___attribute)) {
1113 SourceLocation Loc;
1114 AttributeList *AttrList = ParseAttributes(&Loc);
1115 Fields.back().D.AddAttributes(AttrList, Loc);
1116 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001117 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001118}
1119
1120/// ParseStructUnionBody
1121/// struct-contents:
1122/// struct-declaration-list
1123/// [EXT] empty
1124/// [GNU] "struct-declaration-list" without terminatoring ';'
1125/// struct-declaration-list:
1126/// struct-declaration
1127/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001128/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001129///
Chris Lattner4b009652007-07-25 00:24:17 +00001130void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1131 unsigned TagType, DeclTy *TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001132 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1133 PP.getSourceManager(),
1134 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001135
Chris Lattner4b009652007-07-25 00:24:17 +00001136 SourceLocation LBraceLoc = ConsumeBrace();
1137
Douglas Gregorcab994d2009-01-09 22:42:13 +00001138 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001139 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1140
Chris Lattner4b009652007-07-25 00:24:17 +00001141 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1142 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001143 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001144 Diag(Tok, diag::ext_empty_struct_union_enum)
1145 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001146
1147 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001148 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1149
Chris Lattner4b009652007-07-25 00:24:17 +00001150 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001151 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001152 // Each iteration of this loop reads one struct-declaration.
1153
1154 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001155 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001156 Diag(Tok, diag::ext_extra_struct_semi);
1157 ConsumeToken();
1158 continue;
1159 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001160
1161 // Parse all the comma separated declarators.
1162 DeclSpec DS;
1163 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001164 if (!Tok.is(tok::at)) {
1165 ParseStructDeclaration(DS, FieldDeclarators);
1166
1167 // Convert them all to fields.
1168 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1169 FieldDeclarator &FD = FieldDeclarators[i];
1170 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +00001171 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +00001172 DS.getSourceRange().getBegin(),
1173 FD.D, FD.BitfieldSize);
1174 FieldDecls.push_back(Field);
1175 }
1176 } else { // Handle @defs
1177 ConsumeToken();
1178 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1179 Diag(Tok, diag::err_unexpected_at);
1180 SkipUntil(tok::semi, true, true);
1181 continue;
1182 }
1183 ConsumeToken();
1184 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1185 if (!Tok.is(tok::identifier)) {
1186 Diag(Tok, diag::err_expected_ident);
1187 SkipUntil(tok::semi, true, true);
1188 continue;
1189 }
1190 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001191 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1192 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001193 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1194 ConsumeToken();
1195 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1196 }
Chris Lattner4b009652007-07-25 00:24:17 +00001197
Chris Lattner34a01ad2007-10-09 17:33:22 +00001198 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001199 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001200 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001201 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001202 break;
1203 } else {
1204 Diag(Tok, diag::err_expected_semi_decl_list);
1205 // Skip to end of block or statement
1206 SkipUntil(tok::r_brace, true, true);
1207 }
1208 }
1209
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001210 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001211
Chris Lattner4b009652007-07-25 00:24:17 +00001212 AttributeList *AttrList = 0;
1213 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001214 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001215 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001216
1217 Actions.ActOnFields(CurScope,
1218 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1219 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001220 AttrList);
1221 StructScope.Exit();
1222 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001223}
1224
1225
1226/// ParseEnumSpecifier
1227/// enum-specifier: [C99 6.7.2.2]
1228/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001229///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001230/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1231/// '}' attributes[opt]
1232/// 'enum' identifier
1233/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001234///
1235/// [C++] elaborated-type-specifier:
1236/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1237///
Douglas Gregor0c793bb2009-03-25 22:00:53 +00001238void Parser::ParseEnumSpecifier(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001239 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001240 SourceLocation StartLoc = ConsumeToken();
1241
1242 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001243
1244 AttributeList *Attr = 0;
1245 // If attributes exist after tag, parse them.
1246 if (Tok.is(tok::kw___attribute))
1247 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001248
1249 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001250 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001251 if (Tok.isNot(tok::identifier)) {
1252 Diag(Tok, diag::err_expected_ident);
1253 if (Tok.isNot(tok::l_brace)) {
1254 // Has no name and is not a definition.
1255 // Skip the rest of this declarator, up until the comma or semicolon.
1256 SkipUntil(tok::comma, true);
1257 return;
1258 }
1259 }
1260 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001261
1262 // Must have either 'enum name' or 'enum {...}'.
1263 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1264 Diag(Tok, diag::err_expected_ident_lbrace);
1265
1266 // Skip the rest of this declarator, up until the comma or semicolon.
1267 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001268 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001269 }
1270
1271 // If an identifier is present, consume and remember it.
1272 IdentifierInfo *Name = 0;
1273 SourceLocation NameLoc;
1274 if (Tok.is(tok::identifier)) {
1275 Name = Tok.getIdentifierInfo();
1276 NameLoc = ConsumeToken();
1277 }
1278
1279 // There are three options here. If we have 'enum foo;', then this is a
1280 // forward declaration. If we have 'enum foo {...' then this is a
1281 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1282 //
1283 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1284 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1285 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1286 //
1287 Action::TagKind TK;
1288 if (Tok.is(tok::l_brace))
1289 TK = Action::TK_Definition;
1290 else if (Tok.is(tok::semi))
1291 TK = Action::TK_Declaration;
1292 else
1293 TK = Action::TK_Reference;
1294 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregor0c793bb2009-03-25 22:00:53 +00001295 SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001296
Chris Lattner34a01ad2007-10-09 17:33:22 +00001297 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001298 ParseEnumBody(StartLoc, TagDecl);
1299
1300 // TODO: semantic analysis on the declspec for enums.
1301 const char *PrevSpec = 0;
1302 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001303 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001304}
1305
1306/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1307/// enumerator-list:
1308/// enumerator
1309/// enumerator-list ',' enumerator
1310/// enumerator:
1311/// enumeration-constant
1312/// enumeration-constant '=' constant-expression
1313/// enumeration-constant:
1314/// identifier
1315///
1316void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001317 // Enter the scope of the enum body and start the definition.
1318 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001319 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001320
Chris Lattner4b009652007-07-25 00:24:17 +00001321 SourceLocation LBraceLoc = ConsumeBrace();
1322
Chris Lattnerc9a92452007-08-27 17:24:30 +00001323 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001324 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001325 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001326
1327 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1328
1329 DeclTy *LastEnumConstDecl = 0;
1330
1331 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001332 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001333 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1334 SourceLocation IdentLoc = ConsumeToken();
1335
1336 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001337 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001338 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001339 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001340 AssignedVal = ParseConstantExpression();
1341 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001342 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001343 }
1344
1345 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001346 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001347 LastEnumConstDecl,
1348 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001349 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001350 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001351 EnumConstantDecls.push_back(EnumConstDecl);
1352 LastEnumConstDecl = EnumConstDecl;
1353
Chris Lattner34a01ad2007-10-09 17:33:22 +00001354 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001355 break;
1356 SourceLocation CommaLoc = ConsumeToken();
1357
Chris Lattner34a01ad2007-10-09 17:33:22 +00001358 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001359 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1360 }
1361
1362 // Eat the }.
1363 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1364
Steve Naroff0acc9c92007-09-15 18:49:24 +00001365 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001366 EnumConstantDecls.size());
1367
1368 DeclTy *AttrList = 0;
1369 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001370 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001371 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001372
1373 EnumScope.Exit();
1374 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001375}
1376
1377/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001378/// start of a type-qualifier-list.
1379bool Parser::isTypeQualifier() const {
1380 switch (Tok.getKind()) {
1381 default: return false;
1382 // type-qualifier
1383 case tok::kw_const:
1384 case tok::kw_volatile:
1385 case tok::kw_restrict:
1386 return true;
1387 }
1388}
1389
1390/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001391/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001392bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001393 switch (Tok.getKind()) {
1394 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001395
1396 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001397 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001398 // Annotate typenames and C++ scope specifiers. If we get one, just
1399 // recurse to handle whatever we get.
1400 if (TryAnnotateTypeOrScopeToken())
1401 return isTypeSpecifierQualifier();
1402 // Otherwise, not a type specifier.
1403 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001404
Chris Lattnerb75fde62009-01-04 23:41:41 +00001405 case tok::coloncolon: // ::foo::bar
1406 if (NextToken().is(tok::kw_new) || // ::new
1407 NextToken().is(tok::kw_delete)) // ::delete
1408 return false;
1409
1410 // Annotate typenames and C++ scope specifiers. If we get one, just
1411 // recurse to handle whatever we get.
1412 if (TryAnnotateTypeOrScopeToken())
1413 return isTypeSpecifierQualifier();
1414 // Otherwise, not a type specifier.
1415 return false;
1416
Chris Lattner4b009652007-07-25 00:24:17 +00001417 // GNU attributes support.
1418 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001419 // GNU typeof support.
1420 case tok::kw_typeof:
1421
Chris Lattner4b009652007-07-25 00:24:17 +00001422 // type-specifiers
1423 case tok::kw_short:
1424 case tok::kw_long:
1425 case tok::kw_signed:
1426 case tok::kw_unsigned:
1427 case tok::kw__Complex:
1428 case tok::kw__Imaginary:
1429 case tok::kw_void:
1430 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001431 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001432 case tok::kw_int:
1433 case tok::kw_float:
1434 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001435 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001436 case tok::kw__Bool:
1437 case tok::kw__Decimal32:
1438 case tok::kw__Decimal64:
1439 case tok::kw__Decimal128:
1440
Chris Lattner2e78db32008-04-13 18:59:07 +00001441 // struct-or-union-specifier (C99) or class-specifier (C++)
1442 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001443 case tok::kw_struct:
1444 case tok::kw_union:
1445 // enum-specifier
1446 case tok::kw_enum:
1447
1448 // type-qualifier
1449 case tok::kw_const:
1450 case tok::kw_volatile:
1451 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001452
1453 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001454 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001455 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001456
1457 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1458 case tok::less:
1459 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001460
1461 case tok::kw___cdecl:
1462 case tok::kw___stdcall:
1463 case tok::kw___fastcall:
1464 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001465 }
1466}
1467
1468/// isDeclarationSpecifier() - Return true if the current token is part of a
1469/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001470bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001471 switch (Tok.getKind()) {
1472 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001473
1474 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001475 // Unfortunate hack to support "Class.factoryMethod" notation.
1476 if (getLang().ObjC1 && NextToken().is(tok::period))
1477 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001478 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001479
Douglas Gregord3022602009-03-27 23:10:48 +00001480 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001481 // 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 case tok::coloncolon: // ::foo::bar
1488 if (NextToken().is(tok::kw_new) || // ::new
1489 NextToken().is(tok::kw_delete)) // ::delete
1490 return false;
1491
1492 // Annotate typenames and C++ scope specifiers. If we get one, just
1493 // recurse to handle whatever we get.
1494 if (TryAnnotateTypeOrScopeToken())
1495 return isDeclarationSpecifier();
1496 // Otherwise, not a declaration specifier.
1497 return false;
1498
Chris Lattner4b009652007-07-25 00:24:17 +00001499 // storage-class-specifier
1500 case tok::kw_typedef:
1501 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001502 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001503 case tok::kw_static:
1504 case tok::kw_auto:
1505 case tok::kw_register:
1506 case tok::kw___thread:
1507
1508 // type-specifiers
1509 case tok::kw_short:
1510 case tok::kw_long:
1511 case tok::kw_signed:
1512 case tok::kw_unsigned:
1513 case tok::kw__Complex:
1514 case tok::kw__Imaginary:
1515 case tok::kw_void:
1516 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001517 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001518 case tok::kw_int:
1519 case tok::kw_float:
1520 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001521 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001522 case tok::kw__Bool:
1523 case tok::kw__Decimal32:
1524 case tok::kw__Decimal64:
1525 case tok::kw__Decimal128:
1526
Chris Lattner2e78db32008-04-13 18:59:07 +00001527 // struct-or-union-specifier (C99) or class-specifier (C++)
1528 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001529 case tok::kw_struct:
1530 case tok::kw_union:
1531 // enum-specifier
1532 case tok::kw_enum:
1533
1534 // type-qualifier
1535 case tok::kw_const:
1536 case tok::kw_volatile:
1537 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001538
Chris Lattner4b009652007-07-25 00:24:17 +00001539 // function-specifier
1540 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001541 case tok::kw_virtual:
1542 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001543
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001544 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001545 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001546
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001547 // GNU typeof support.
1548 case tok::kw_typeof:
1549
1550 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001551 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001552 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001553
1554 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1555 case tok::less:
1556 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001557
Steve Naroffab1a3632009-01-06 19:34:12 +00001558 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001559 case tok::kw___cdecl:
1560 case tok::kw___stdcall:
1561 case tok::kw___fastcall:
1562 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001563 }
1564}
1565
1566
1567/// ParseTypeQualifierListOpt
1568/// type-qualifier-list: [C99 6.7.5]
1569/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001570/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001571/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001572/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001573///
Chris Lattner460696f2008-12-18 07:02:59 +00001574void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001575 while (1) {
1576 int isInvalid = false;
1577 const char *PrevSpec = 0;
1578 SourceLocation Loc = Tok.getLocation();
1579
1580 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001581 case tok::kw_const:
1582 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1583 getLang())*2;
1584 break;
1585 case tok::kw_volatile:
1586 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1587 getLang())*2;
1588 break;
1589 case tok::kw_restrict:
1590 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1591 getLang())*2;
1592 break;
Steve Naroffad620402008-12-25 14:41:26 +00001593 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001594 case tok::kw___cdecl:
1595 case tok::kw___stdcall:
1596 case tok::kw___fastcall:
1597 if (!PP.getLangOptions().Microsoft)
1598 goto DoneWithTypeQuals;
1599 // Just ignore it.
1600 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001601 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001602 if (AttributesAllowed) {
1603 DS.AddAttributes(ParseAttributes());
1604 continue; // do *not* consume the next token!
1605 }
1606 // otherwise, FALL THROUGH!
1607 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001608 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001609 // If this is not a type-qualifier token, we're done reading type
1610 // qualifiers. First verify that DeclSpec's are consistent.
1611 DS.Finish(Diags, PP.getSourceManager(), getLang());
1612 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001613 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001614
Chris Lattner4b009652007-07-25 00:24:17 +00001615 // If the specifier combination wasn't legal, issue a diagnostic.
1616 if (isInvalid) {
1617 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001618 // Pick between error or extwarn.
1619 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1620 : diag::ext_duplicate_declspec;
1621 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001622 }
1623 ConsumeToken();
1624 }
1625}
1626
1627
1628/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1629///
1630void Parser::ParseDeclarator(Declarator &D) {
1631 /// This implements the 'declarator' production in the C grammar, then checks
1632 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001633 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001634}
1635
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001636/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1637/// is parsed by the function passed to it. Pass null, and the direct-declarator
1638/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001639/// ptr-operator production.
1640///
Sebastian Redl75555032009-01-24 21:16:55 +00001641/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1642/// [C] pointer[opt] direct-declarator
1643/// [C++] direct-declarator
1644/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001645///
1646/// pointer: [C99 6.7.5]
1647/// '*' type-qualifier-list[opt]
1648/// '*' type-qualifier-list[opt] pointer
1649///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001650/// ptr-operator:
1651/// '*' cv-qualifier-seq[opt]
1652/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001653/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001654/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001655/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001656/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001657void Parser::ParseDeclaratorInternal(Declarator &D,
1658 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001659
Sebastian Redl75555032009-01-24 21:16:55 +00001660 // C++ member pointers start with a '::' or a nested-name.
1661 // Member pointers get special handling, since there's no place for the
1662 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001663 if (getLang().CPlusPlus &&
1664 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1665 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001666 CXXScopeSpec SS;
1667 if (ParseOptionalCXXScopeSpecifier(SS)) {
1668 if(Tok.isNot(tok::star)) {
1669 // The scope spec really belongs to the direct-declarator.
1670 D.getCXXScopeSpec() = SS;
1671 if (DirectDeclParser)
1672 (this->*DirectDeclParser)(D);
1673 return;
1674 }
1675
1676 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001677 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001678 DeclSpec DS;
1679 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001680 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001681
1682 // Recurse to parse whatever is left.
1683 ParseDeclaratorInternal(D, DirectDeclParser);
1684
1685 // Sema will have to catch (syntactically invalid) pointers into global
1686 // scope. It has to catch pointers into namespace scope anyway.
1687 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001688 Loc, DS.TakeAttributes()),
1689 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001690 return;
1691 }
1692 }
1693
1694 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001695 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001696 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001697 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001698 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001699 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001700 if (DirectDeclParser)
1701 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001702 return;
1703 }
Sebastian Redl75555032009-01-24 21:16:55 +00001704
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001705 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1706 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001707 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001708 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001709
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001710 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001711 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001712 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001713
Chris Lattner4b009652007-07-25 00:24:17 +00001714 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001715 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001716
Chris Lattner4b009652007-07-25 00:24:17 +00001717 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001718 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001719 if (Kind == tok::star)
1720 // Remember that we parsed a pointer type, and remember the type-quals.
1721 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001722 DS.TakeAttributes()),
1723 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001724 else
1725 // Remember that we parsed a Block type, and remember the type-quals.
1726 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001727 Loc),
1728 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001729 } else {
1730 // Is a reference
1731 DeclSpec DS;
1732
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001733 // Complain about rvalue references in C++03, but then go on and build
1734 // the declarator.
1735 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1736 Diag(Loc, diag::err_rvalue_reference);
1737
Chris Lattner4b009652007-07-25 00:24:17 +00001738 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1739 // cv-qualifiers are introduced through the use of a typedef or of a
1740 // template type argument, in which case the cv-qualifiers are ignored.
1741 //
1742 // [GNU] Retricted references are allowed.
1743 // [GNU] Attributes on references are allowed.
1744 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001745 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001746
1747 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1748 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1749 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001750 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001751 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1752 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001753 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001754 }
1755
1756 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001757 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001758
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001759 if (D.getNumTypeObjects() > 0) {
1760 // C++ [dcl.ref]p4: There shall be no references to references.
1761 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1762 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001763 if (const IdentifierInfo *II = D.getIdentifier())
1764 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1765 << II;
1766 else
1767 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1768 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001769
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001770 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001771 // can go ahead and build the (technically ill-formed)
1772 // declarator: reference collapsing will take care of it.
1773 }
1774 }
1775
Chris Lattner4b009652007-07-25 00:24:17 +00001776 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001777 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001778 DS.TakeAttributes(),
1779 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001780 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001781 }
1782}
1783
1784/// ParseDirectDeclarator
1785/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001786/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001787/// '(' declarator ')'
1788/// [GNU] '(' attributes declarator ')'
1789/// [C90] direct-declarator '[' constant-expression[opt] ']'
1790/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1791/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1792/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1793/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1794/// direct-declarator '(' parameter-type-list ')'
1795/// direct-declarator '(' identifier-list[opt] ')'
1796/// [GNU] direct-declarator '(' parameter-forward-declarations
1797/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001798/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1799/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001800/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001801///
1802/// declarator-id: [C++ 8]
1803/// id-expression
1804/// '::'[opt] nested-name-specifier[opt] type-name
1805///
1806/// id-expression: [C++ 5.1]
1807/// unqualified-id
1808/// qualified-id [TODO]
1809///
1810/// unqualified-id: [C++ 5.1]
1811/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001812/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001813/// conversion-function-id [TODO]
1814/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001815/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001816///
Chris Lattner4b009652007-07-25 00:24:17 +00001817void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001818 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001819
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001820 if (getLang().CPlusPlus) {
1821 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001822 // ParseDeclaratorInternal might already have parsed the scope.
1823 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1824 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001825 if (afterCXXScope) {
1826 // Change the declaration context for name lookup, until this function
1827 // is exited (and the declarator has been parsed).
1828 DeclScopeObj.EnterDeclaratorScope();
1829 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001830
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001831 if (Tok.is(tok::identifier)) {
1832 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001833
Douglas Gregor2fa10442008-12-18 19:37:40 +00001834 // If this identifier is the name of the current class, it's a
1835 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001836 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001837 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001838 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001839 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001840 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001841 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001842 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1843 ConsumeToken();
1844 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001845 } else if (Tok.is(tok::annot_template_id)) {
1846 TemplateIdAnnotation *TemplateId
1847 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1848
1849 // FIXME: Could this template-id name a constructor?
1850
1851 // FIXME: This is an egregious hack, where we silently ignore
1852 // the specialization (which should be a function template
1853 // specialization name) and use the name instead. This hack
1854 // will go away when we have support for function
1855 // specializations.
1856 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1857 TemplateId->Destroy();
1858 ConsumeToken();
1859 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001860 } else if (Tok.is(tok::kw_operator)) {
1861 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001862 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001863
Douglas Gregor853dd392008-12-26 15:00:45 +00001864 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001865 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1866 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001867 } else {
1868 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001869 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1870 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1871 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001872 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001873 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001874 }
1875 goto PastIdentifier;
1876 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001877 // This should be a C++ destructor.
1878 SourceLocation TildeLoc = ConsumeToken();
1879 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001880 // FIXME: Inaccurate.
1881 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001882 SourceLocation EndLoc;
1883 if (TypeTy *Type = ParseClassName(EndLoc)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001884 D.setDestructor(Type, TildeLoc, NameLoc);
1885 } else {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001886 D.SetIdentifier(0, TildeLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001887 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001888 } else {
1889 Diag(Tok, diag::err_expected_class_name);
1890 D.SetIdentifier(0, TildeLoc);
1891 }
1892 goto PastIdentifier;
1893 }
1894
1895 // If we reached this point, token is not identifier and not '~'.
1896
1897 if (afterCXXScope) {
1898 Diag(Tok, diag::err_expected_unqualified_id);
1899 D.SetIdentifier(0, Tok.getLocation());
1900 D.setInvalidType(true);
1901 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001902 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001903 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001904 }
1905
1906 // If we reached this point, we are either in C/ObjC or the token didn't
1907 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001908 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1909 assert(!getLang().CPlusPlus &&
1910 "There's a C++-specific check for tok::identifier above");
1911 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1912 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1913 ConsumeToken();
1914 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001915 // direct-declarator: '(' declarator ')'
1916 // direct-declarator: '(' attributes declarator ')'
1917 // Example: 'char (*X)' or 'int (*XX)(void)'
1918 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001919 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001920 // This could be something simple like "int" (in which case the declarator
1921 // portion is empty), if an abstract-declarator is allowed.
1922 D.SetIdentifier(0, Tok.getLocation());
1923 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00001924 if (D.getContext() == Declarator::MemberContext)
1925 Diag(Tok, diag::err_expected_member_name_or_semi)
1926 << D.getDeclSpec().getSourceRange();
1927 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001928 Diag(Tok, diag::err_expected_unqualified_id);
1929 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001930 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001931 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001932 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001933 }
1934
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001935 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001936 assert(D.isPastIdentifier() &&
1937 "Haven't past the location of the identifier yet?");
1938
1939 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001940 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001941 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1942 // In such a case, check if we actually have a function declarator; if it
1943 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001944 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1945 // When not in file scope, warn for ambiguous function declarators, just
1946 // in case the author intended it as a variable definition.
1947 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1948 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1949 break;
1950 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001951 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001952 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001953 ParseBracketDeclarator(D);
1954 } else {
1955 break;
1956 }
1957 }
1958}
1959
Chris Lattnera0d056d2008-04-06 05:45:57 +00001960/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1961/// only called before the identifier, so these are most likely just grouping
1962/// parens for precedence. If we find that these are actually function
1963/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1964///
1965/// direct-declarator:
1966/// '(' declarator ')'
1967/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001968/// direct-declarator '(' parameter-type-list ')'
1969/// direct-declarator '(' identifier-list[opt] ')'
1970/// [GNU] direct-declarator '(' parameter-forward-declarations
1971/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001972///
1973void Parser::ParseParenDeclarator(Declarator &D) {
1974 SourceLocation StartLoc = ConsumeParen();
1975 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1976
Chris Lattner1f185292008-10-20 02:05:46 +00001977 // Eat any attributes before we look at whether this is a grouping or function
1978 // declarator paren. If this is a grouping paren, the attribute applies to
1979 // the type being built up, for example:
1980 // int (__attribute__(()) *x)(long y)
1981 // If this ends up not being a grouping paren, the attribute applies to the
1982 // first argument, for example:
1983 // int (__attribute__(()) int x)
1984 // In either case, we need to eat any attributes to be able to determine what
1985 // sort of paren this is.
1986 //
1987 AttributeList *AttrList = 0;
1988 bool RequiresArg = false;
1989 if (Tok.is(tok::kw___attribute)) {
1990 AttrList = ParseAttributes();
1991
1992 // We require that the argument list (if this is a non-grouping paren) be
1993 // present even if the attribute list was empty.
1994 RequiresArg = true;
1995 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001996 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00001997 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1998 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00001999 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002000
Chris Lattnera0d056d2008-04-06 05:45:57 +00002001 // If we haven't past the identifier yet (or where the identifier would be
2002 // stored, if this is an abstract declarator), then this is probably just
2003 // grouping parens. However, if this could be an abstract-declarator, then
2004 // this could also be the start of function arguments (consider 'void()').
2005 bool isGrouping;
2006
2007 if (!D.mayOmitIdentifier()) {
2008 // If this can't be an abstract-declarator, this *must* be a grouping
2009 // paren, because we haven't seen the identifier yet.
2010 isGrouping = true;
2011 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002012 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002013 isDeclarationSpecifier()) { // 'int(int)' is a function.
2014 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2015 // considered to be a type, not a K&R identifier-list.
2016 isGrouping = false;
2017 } else {
2018 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2019 isGrouping = true;
2020 }
2021
2022 // If this is a grouping paren, handle:
2023 // direct-declarator: '(' declarator ')'
2024 // direct-declarator: '(' attributes declarator ')'
2025 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002026 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002027 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002028 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002029 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002030
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002031 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002032 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002033 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002034
2035 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002036 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002037 return;
2038 }
2039
2040 // Okay, if this wasn't a grouping paren, it must be the start of a function
2041 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002042 // identifier (and remember where it would have been), then call into
2043 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002044 D.SetIdentifier(0, Tok.getLocation());
2045
Chris Lattner1f185292008-10-20 02:05:46 +00002046 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002047}
2048
2049/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2050/// declarator D up to a paren, which indicates that we are parsing function
2051/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002052///
Chris Lattner1f185292008-10-20 02:05:46 +00002053/// If AttrList is non-null, then the caller parsed those arguments immediately
2054/// after the open paren - they should be considered to be the first argument of
2055/// a parameter. If RequiresArg is true, then the first argument of the
2056/// function is required to be present and required to not be an identifier
2057/// list.
2058///
Chris Lattner4b009652007-07-25 00:24:17 +00002059/// This method also handles this portion of the grammar:
2060/// parameter-type-list: [C99 6.7.5]
2061/// parameter-list
2062/// parameter-list ',' '...'
2063///
2064/// parameter-list: [C99 6.7.5]
2065/// parameter-declaration
2066/// parameter-list ',' parameter-declaration
2067///
2068/// parameter-declaration: [C99 6.7.5]
2069/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002070/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002071/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002072/// declaration-specifiers abstract-declarator[opt]
2073/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002074/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002075/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2076///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002077/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002078/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002079///
Chris Lattner1f185292008-10-20 02:05:46 +00002080void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2081 AttributeList *AttrList,
2082 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002083 // lparen is already consumed!
2084 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002085
Chris Lattner1f185292008-10-20 02:05:46 +00002086 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002087 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002088 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002089 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002090 delete AttrList;
2091 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002092
Sebastian Redl0c986032009-02-09 18:23:29 +00002093 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002094
2095 // cv-qualifier-seq[opt].
2096 DeclSpec DS;
2097 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002098 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002099 if (!DS.getSourceRange().getEnd().isInvalid())
2100 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002101
2102 // Parse exception-specification[opt].
2103 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002104 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002105 }
2106
Chris Lattner9f7564b2008-04-06 06:57:35 +00002107 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002108 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002109 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002110 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002111 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002112 /*arglist*/ 0, 0,
2113 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002114 LParenLoc, D),
2115 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002116 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002117 }
2118
2119 // Alternatively, this parameter list may be an identifier list form for a
2120 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002121 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002122 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002123 // K&R identifier lists can't have typedefs as identifiers, per
2124 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002125 if (RequiresArg) {
2126 Diag(Tok, diag::err_argument_required_after_attribute);
2127 delete AttrList;
2128 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002129 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2130 // normal declarators, not for abstract-declarators.
2131 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002132 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002133 }
2134
2135 // Finally, a normal, non-empty parameter type list.
2136
2137 // Build up an array of information about the parsed arguments.
2138 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002139
2140 // Enter function-declaration scope, limiting any declarators to the
2141 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002142 ParseScope PrototypeScope(this,
2143 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002144
2145 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002146 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002147 while (1) {
2148 if (Tok.is(tok::ellipsis)) {
2149 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002150 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002151 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002152 }
2153
Chris Lattner9f7564b2008-04-06 06:57:35 +00002154 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002155
Chris Lattner9f7564b2008-04-06 06:57:35 +00002156 // Parse the declaration-specifiers.
2157 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002158
2159 // If the caller parsed attributes for the first argument, add them now.
2160 if (AttrList) {
2161 DS.AddAttributes(AttrList);
2162 AttrList = 0; // Only apply the attributes to the first parameter.
2163 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002164 ParseDeclarationSpecifiers(DS);
2165
Chris Lattner9f7564b2008-04-06 06:57:35 +00002166 // Parse the declarator. This is "PrototypeContext", because we must
2167 // accept either 'declarator' or 'abstract-declarator' here.
2168 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2169 ParseDeclarator(ParmDecl);
2170
2171 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002172 if (Tok.is(tok::kw___attribute)) {
2173 SourceLocation Loc;
2174 AttributeList *AttrList = ParseAttributes(&Loc);
2175 ParmDecl.AddAttributes(AttrList, Loc);
2176 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002177
Chris Lattner9f7564b2008-04-06 06:57:35 +00002178 // Remember this parsed parameter in ParamInfo.
2179 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2180
Douglas Gregor605de8d2008-12-16 21:30:33 +00002181 // DefArgToks is used when the parsing of default arguments needs
2182 // to be delayed.
2183 CachedTokens *DefArgToks = 0;
2184
Chris Lattner9f7564b2008-04-06 06:57:35 +00002185 // If no parameter was specified, verify that *something* was specified,
2186 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002187 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2188 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002189 // Completely missing, emit error.
2190 Diag(DSStart, diag::err_missing_param);
2191 } else {
2192 // Otherwise, we have something. Add it and let semantic analysis try
2193 // to grok it and add the result to the ParamInfo we are building.
2194
2195 // Inform the actions module about the parameter declarator, so it gets
2196 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00002197 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2198
2199 // Parse the default argument, if any. We parse the default
2200 // arguments in all dialects; the semantic analysis in
2201 // ActOnParamDefaultArgument will reject the default argument in
2202 // C.
2203 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002204 SourceLocation EqualLoc = Tok.getLocation();
2205
Chris Lattner3e254fb2008-04-08 04:40:51 +00002206 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002207 if (D.getContext() == Declarator::MemberContext) {
2208 // If we're inside a class definition, cache the tokens
2209 // corresponding to the default argument. We'll actually parse
2210 // them when we see the end of the class definition.
2211 // FIXME: Templates will require something similar.
2212 // FIXME: Can we use a smart pointer for Toks?
2213 DefArgToks = new CachedTokens;
2214
2215 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2216 tok::semi, false)) {
2217 delete DefArgToks;
2218 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002219 Actions.ActOnParamDefaultArgumentError(Param);
2220 } else
2221 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002222 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002223 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002224 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002225
2226 OwningExprResult DefArgResult(ParseAssignmentExpression());
2227 if (DefArgResult.isInvalid()) {
2228 Actions.ActOnParamDefaultArgumentError(Param);
2229 SkipUntil(tok::comma, tok::r_paren, true, true);
2230 } else {
2231 // Inform the actions module about the default argument
2232 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002233 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002234 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002235 }
2236 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002237
2238 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002239 ParmDecl.getIdentifierLoc(), Param,
2240 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002241 }
2242
2243 // If the next token is a comma, consume it and keep reading arguments.
2244 if (Tok.isNot(tok::comma)) break;
2245
2246 // Consume the comma.
2247 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002248 }
2249
Chris Lattner9f7564b2008-04-06 06:57:35 +00002250 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002251 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002252
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002253 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002254 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002255
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002256 DeclSpec DS;
2257 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002258 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002259 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002260 if (!DS.getSourceRange().getEnd().isInvalid())
2261 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002262
2263 // Parse exception-specification[opt].
2264 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002265 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002266 }
2267
Chris Lattner4b009652007-07-25 00:24:17 +00002268 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002269 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002270 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002271 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002272 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002273 LParenLoc, D),
2274 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002275}
2276
Chris Lattner35d9c912008-04-06 06:34:08 +00002277/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2278/// we found a K&R-style identifier list instead of a type argument list. The
2279/// current token is known to be the first identifier in the list.
2280///
2281/// identifier-list: [C99 6.7.5]
2282/// identifier
2283/// identifier-list ',' identifier
2284///
2285void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2286 Declarator &D) {
2287 // Build up an array of information about the parsed arguments.
2288 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2289 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2290
2291 // If there was no identifier specified for the declarator, either we are in
2292 // an abstract-declarator, or we are in a parameter declarator which was found
2293 // to be abstract. In abstract-declarators, identifier lists are not valid:
2294 // diagnose this.
2295 if (!D.getIdentifier())
2296 Diag(Tok, diag::ext_ident_list_in_param);
2297
2298 // Tok is known to be the first identifier in the list. Remember this
2299 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002300 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002301 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2302 Tok.getLocation(), 0));
2303
Chris Lattner113a56b2008-04-06 06:39:19 +00002304 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002305
2306 while (Tok.is(tok::comma)) {
2307 // Eat the comma.
2308 ConsumeToken();
2309
Chris Lattner113a56b2008-04-06 06:39:19 +00002310 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002311 if (Tok.isNot(tok::identifier)) {
2312 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002313 SkipUntil(tok::r_paren);
2314 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002315 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002316
Chris Lattner35d9c912008-04-06 06:34:08 +00002317 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002318
2319 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002320 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002321 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002322
2323 // Verify that the argument identifier has not already been mentioned.
2324 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002325 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002326 } else {
2327 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002328 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2329 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00002330 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002331
2332 // Eat the identifier.
2333 ConsumeToken();
2334 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002335
2336 // If we have the closing ')', eat it and we're done.
2337 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2338
Chris Lattner113a56b2008-04-06 06:39:19 +00002339 // Remember that we parsed a function type, and remember the attributes. This
2340 // function type is always a K&R style function type, which is not varargs and
2341 // has no prototype.
2342 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002343 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002344 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002345 /*TypeQuals*/0, LParenLoc, D),
2346 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002347}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002348
Chris Lattner4b009652007-07-25 00:24:17 +00002349/// [C90] direct-declarator '[' constant-expression[opt] ']'
2350/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2351/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2352/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2353/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2354void Parser::ParseBracketDeclarator(Declarator &D) {
2355 SourceLocation StartLoc = ConsumeBracket();
2356
Chris Lattner1525c3a2008-12-18 07:27:21 +00002357 // C array syntax has many features, but by-far the most common is [] and [4].
2358 // This code does a fast path to handle some of the most obvious cases.
2359 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002360 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002361 // Remember that we parsed the empty array type.
2362 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002363 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2364 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002365 return;
2366 } else if (Tok.getKind() == tok::numeric_constant &&
2367 GetLookAheadToken(1).is(tok::r_square)) {
2368 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002369 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002370 ConsumeToken();
2371
Sebastian Redl0c986032009-02-09 18:23:29 +00002372 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002373
2374 // If there was an error parsing the assignment-expression, recover.
2375 if (ExprRes.isInvalid())
2376 ExprRes.release(); // Deallocate expr, just use [].
2377
2378 // Remember that we parsed a array type, and remember its features.
2379 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002380 ExprRes.release(), StartLoc),
2381 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002382 return;
2383 }
2384
Chris Lattner4b009652007-07-25 00:24:17 +00002385 // If valid, this location is the position where we read the 'static' keyword.
2386 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002387 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002388 StaticLoc = ConsumeToken();
2389
2390 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002391 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002392 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002393 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002394
2395 // If we haven't already read 'static', check to see if there is one after the
2396 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002397 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002398 StaticLoc = ConsumeToken();
2399
2400 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2401 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002402 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002403
2404 // Handle the case where we have '[*]' as the array size. However, a leading
2405 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2406 // the the token after the star is a ']'. Since stars in arrays are
2407 // infrequent, use of lookahead is not costly here.
2408 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002409 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002410
Chris Lattner306d4df2008-12-18 06:50:14 +00002411 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002412 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002413 StaticLoc = SourceLocation(); // Drop the static.
2414 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002415 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002416 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002417 // Note, in C89, this production uses the constant-expr production instead
2418 // of assignment-expr. The only difference is that assignment-expr allows
2419 // things like '=' and '*='. Sema rejects these in C89 mode because they
2420 // are not i-c-e's, so we don't need to distinguish between the two here.
2421
Chris Lattner4b009652007-07-25 00:24:17 +00002422 // Parse the assignment-expression now.
2423 NumElements = ParseAssignmentExpression();
2424 }
2425
2426 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002427 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002428 // If the expression was invalid, skip it.
2429 SkipUntil(tok::r_square);
2430 return;
2431 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002432
2433 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2434
Chris Lattner1525c3a2008-12-18 07:27:21 +00002435 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002436 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2437 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002438 NumElements.release(), StartLoc),
2439 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002440}
2441
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002442/// [GNU] typeof-specifier:
2443/// typeof ( expressions )
2444/// typeof ( type-name )
2445/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002446///
2447void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002448 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002449 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002450 SourceLocation StartLoc = ConsumeToken();
2451
Chris Lattner34a01ad2007-10-09 17:33:22 +00002452 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002453 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002454 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002455 return;
2456 }
2457
Sebastian Redl14ca7412008-12-11 21:36:32 +00002458 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002459 if (Result.isInvalid()) {
2460 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002461 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002462 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002463
2464 const char *PrevSpec = 0;
2465 // Check for duplicate type specifiers.
2466 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002467 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002468 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002469
2470 // FIXME: Not accurate, the range gets one token more than it should.
2471 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002472 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002473 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002474
Steve Naroff7cbb1462007-07-31 12:34:36 +00002475 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2476
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002477 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002478 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002479
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002480 assert((Ty.isInvalid() || Ty.get()) &&
2481 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002482
Chris Lattner34a01ad2007-10-09 17:33:22 +00002483 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002484 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002485 return;
2486 }
2487 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002488
2489 if (Ty.isInvalid())
2490 DS.SetTypeSpecError();
2491 else {
2492 const char *PrevSpec = 0;
2493 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2494 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2495 Ty.get()))
2496 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2497 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002498 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002499 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002500
2501 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002502 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002503 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002504 return;
2505 }
2506 RParenLoc = ConsumeParen();
2507 const char *PrevSpec = 0;
2508 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2509 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002510 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002511 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002512 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002513 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002514}
2515
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002516