blob: 81685eb8ca1320bc0e8a619dafc2494118c26e40 [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,
Chris Lattner712f9a32009-01-05 00:07:25 +0000474 TemplateParameterLists *TemplateParams){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000475 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000476 while (1) {
477 int isInvalid = false;
478 const char *PrevSpec = 0;
479 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000480
Chris Lattner4b009652007-07-25 00:24:17 +0000481 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000482 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000483 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000484 // If this is not a declaration specifier token, we're done reading decl
485 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000486 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000487 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000488
489 case tok::coloncolon: // ::foo::bar
490 // Annotate C++ scope specifiers. If we get one, loop.
491 if (TryAnnotateCXXScopeToken())
492 continue;
493 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000494
495 case tok::annot_cxxscope: {
496 if (DS.hasTypeSpecifier())
497 goto DoneWithDeclSpec;
498
499 // We are looking for a qualified typename.
500 if (NextToken().isNot(tok::identifier))
501 goto DoneWithDeclSpec;
502
503 CXXScopeSpec SS;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000504 SS.setFromAnnotationData(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000505 SS.setRange(Tok.getAnnotationRange());
506
507 // If the next token is the name of the class type that the C++ scope
508 // denotes, followed by a '(', then this is a constructor declaration.
509 // We're done with the decl-specifiers.
510 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
511 CurScope, &SS) &&
512 GetLookAheadToken(2).is(tok::l_paren))
513 goto DoneWithDeclSpec;
514
Douglas Gregor1075a162009-02-04 17:00:24 +0000515 Token Next = NextToken();
516 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
517 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000518
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000519 if (TypeRep == 0)
520 goto DoneWithDeclSpec;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000521
522 CXXScopeSpec::freeAnnotationData(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000523 ConsumeToken(); // The C++ scope.
524
Douglas Gregora60c62e2009-02-09 15:09:02 +0000525 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000526 TypeRep);
527 if (isInvalid)
528 break;
529
530 DS.SetRangeEnd(Tok.getLocation());
531 ConsumeToken(); // The typename.
532
533 continue;
534 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000535
536 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000537 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerc297b722009-01-21 19:48:37 +0000538 Tok.getAnnotationValue());
539 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
540 ConsumeToken(); // The typename
541
542 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
543 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
544 // Objective-C interface. If we don't have Objective-C or a '<', this is
545 // just a normal reference to a typedef name.
546 if (!Tok.is(tok::less) || !getLang().ObjC1)
547 continue;
548
549 SourceLocation EndProtoLoc;
550 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
551 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
552 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
553
554 DS.SetRangeEnd(EndProtoLoc);
555 continue;
556 }
557
Chris Lattnerfda18db2008-07-26 01:18:38 +0000558 // typedef-name
559 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000560 // In C++, check to see if this is a scope specifier like foo::bar::, if
561 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000562 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
563 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000564
Chris Lattnerfda18db2008-07-26 01:18:38 +0000565 // This identifier can only be a typedef name if we haven't already seen
566 // a type-specifier. Without this check we misparse:
567 // typedef int X; struct Y { short X; }; as 'short int'.
568 if (DS.hasTypeSpecifier())
569 goto DoneWithDeclSpec;
570
571 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000572 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
573 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000574
Chris Lattnerfda18db2008-07-26 01:18:38 +0000575 if (TypeRep == 0)
576 goto DoneWithDeclSpec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000577
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000578 // C++: If the identifier is actually the name of the class type
579 // being defined and the next token is a '(', then this is a
580 // constructor declaration. We're done with the decl-specifiers
581 // and will treat this token as an identifier.
582 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000583 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000584 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
585 NextToken().getKind() == tok::l_paren)
586 goto DoneWithDeclSpec;
587
Douglas Gregora60c62e2009-02-09 15:09:02 +0000588 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000589 TypeRep);
590 if (isInvalid)
591 break;
592
593 DS.SetRangeEnd(Tok.getLocation());
594 ConsumeToken(); // The identifier
595
596 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
597 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
598 // Objective-C interface. If we don't have Objective-C or a '<', this is
599 // just a normal reference to a typedef name.
600 if (!Tok.is(tok::less) || !getLang().ObjC1)
601 continue;
602
603 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000604 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000605 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000606 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000607
608 DS.SetRangeEnd(EndProtoLoc);
609
Steve Narofff7683302008-09-22 10:28:57 +0000610 // Need to support trailing type qualifiers (e.g. "id<p> const").
611 // If a type specifier follows, it will be diagnosed elsewhere.
612 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000613 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000614
615 // type-name
616 case tok::annot_template_id: {
617 TemplateIdAnnotation *TemplateId
618 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
619 if (TemplateId->Kind != TNK_Class_template) {
620 // This template-id does not refer to a type name, so we're
621 // done with the type-specifiers.
622 goto DoneWithDeclSpec;
623 }
624
625 // Turn the template-id annotation token into a type annotation
626 // token, then try again to parse it as a type-specifier.
627 if (AnnotateTemplateIdTokenAsType())
628 DS.SetTypeSpecError();
629
630 continue;
631 }
632
Chris Lattner4b009652007-07-25 00:24:17 +0000633 // GNU attributes support.
634 case tok::kw___attribute:
635 DS.AddAttributes(ParseAttributes());
636 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000637
638 // Microsoft declspec support.
639 case tok::kw___declspec:
640 if (!PP.getLangOptions().Microsoft)
641 goto DoneWithDeclSpec;
642 FuzzyParseMicrosoftDeclSpec();
643 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000644
Steve Naroffedd04d52008-12-25 14:16:32 +0000645 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000646 case tok::kw___forceinline:
647 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000648 case tok::kw___cdecl:
649 case tok::kw___stdcall:
650 case tok::kw___fastcall:
651 if (!PP.getLangOptions().Microsoft)
652 goto DoneWithDeclSpec;
653 // Just ignore it.
654 break;
655
Chris Lattner4b009652007-07-25 00:24:17 +0000656 // storage-class-specifier
657 case tok::kw_typedef:
658 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
659 break;
660 case tok::kw_extern:
661 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000662 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000663 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
664 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000665 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000666 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
667 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000668 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000669 case tok::kw_static:
670 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000671 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000672 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
673 break;
674 case tok::kw_auto:
675 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
676 break;
677 case tok::kw_register:
678 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
679 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000680 case tok::kw_mutable:
681 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
682 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000683 case tok::kw___thread:
684 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
685 break;
686
Chris Lattner4b009652007-07-25 00:24:17 +0000687 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000688
Chris Lattner4b009652007-07-25 00:24:17 +0000689 // function-specifier
690 case tok::kw_inline:
691 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
692 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000693 case tok::kw_virtual:
694 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
695 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000696 case tok::kw_explicit:
697 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
698 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000699
700 // type-specifier
701 case tok::kw_short:
702 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
703 break;
704 case tok::kw_long:
705 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
706 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
707 else
708 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
709 break;
710 case tok::kw_signed:
711 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
712 break;
713 case tok::kw_unsigned:
714 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
715 break;
716 case tok::kw__Complex:
717 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
718 break;
719 case tok::kw__Imaginary:
720 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
721 break;
722 case tok::kw_void:
723 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
724 break;
725 case tok::kw_char:
726 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
727 break;
728 case tok::kw_int:
729 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
730 break;
731 case tok::kw_float:
732 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
733 break;
734 case tok::kw_double:
735 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
736 break;
737 case tok::kw_wchar_t:
738 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
739 break;
740 case tok::kw_bool:
741 case tok::kw__Bool:
742 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
743 break;
744 case tok::kw__Decimal32:
745 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
746 break;
747 case tok::kw__Decimal64:
748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
749 break;
750 case tok::kw__Decimal128:
751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
752 break;
753
754 // class-specifier:
755 case tok::kw_class:
756 case tok::kw_struct:
757 case tok::kw_union:
758 ParseClassSpecifier(DS, TemplateParams);
759 continue;
760
761 // enum-specifier:
762 case tok::kw_enum:
763 ParseEnumSpecifier(DS);
764 continue;
765
766 // cv-qualifier:
767 case tok::kw_const:
768 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
769 break;
770 case tok::kw_volatile:
771 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
772 getLang())*2;
773 break;
774 case tok::kw_restrict:
775 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
776 getLang())*2;
777 break;
778
779 // GNU typeof support.
780 case tok::kw_typeof:
781 ParseTypeofSpecifier(DS);
782 continue;
783
Steve Naroff5f0466b2008-06-05 00:02:44 +0000784 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000785 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000786 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
787 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000788 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000789 goto DoneWithDeclSpec;
790
791 {
792 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000793 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000794 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000795 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000796 DS.SetRangeEnd(EndProtoLoc);
797
Chris Lattnerf006a222008-11-18 07:48:38 +0000798 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
799 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000800 // Need to support trailing type qualifiers (e.g. "id<p> const").
801 // If a type specifier follows, it will be diagnosed elsewhere.
802 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000803 }
Chris Lattner4b009652007-07-25 00:24:17 +0000804 }
805 // If the specifier combination wasn't legal, issue a diagnostic.
806 if (isInvalid) {
807 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000808 // Pick between error or extwarn.
809 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
810 : diag::ext_duplicate_declspec;
811 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000812 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000813 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000814 ConsumeToken();
815 }
816}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000817
Chris Lattnerd706dc82009-01-06 06:59:53 +0000818/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000819/// primarily follow the C++ grammar with additions for C99 and GNU,
820/// which together subsume the C grammar. Note that the C++
821/// type-specifier also includes the C type-qualifier (for const,
822/// volatile, and C99 restrict). Returns true if a type-specifier was
823/// found (and parsed), false otherwise.
824///
825/// type-specifier: [C++ 7.1.5]
826/// simple-type-specifier
827/// class-specifier
828/// enum-specifier
829/// elaborated-type-specifier [TODO]
830/// cv-qualifier
831///
832/// cv-qualifier: [C++ 7.1.5.1]
833/// 'const'
834/// 'volatile'
835/// [C99] 'restrict'
836///
837/// simple-type-specifier: [ C++ 7.1.5.2]
838/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
839/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
840/// 'char'
841/// 'wchar_t'
842/// 'bool'
843/// 'short'
844/// 'int'
845/// 'long'
846/// 'signed'
847/// 'unsigned'
848/// 'float'
849/// 'double'
850/// 'void'
851/// [C99] '_Bool'
852/// [C99] '_Complex'
853/// [C99] '_Imaginary' // Removed in TC2?
854/// [GNU] '_Decimal32'
855/// [GNU] '_Decimal64'
856/// [GNU] '_Decimal128'
857/// [GNU] typeof-specifier
858/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
859/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000860bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
861 const char *&PrevSpec,
862 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000863 SourceLocation Loc = Tok.getLocation();
864
865 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000866 case tok::identifier: // foo::bar
867 // Annotate typenames and C++ scope specifiers. If we get one, just
868 // recurse to handle whatever we get.
869 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000870 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000871 // Otherwise, not a type specifier.
872 return false;
873 case tok::coloncolon: // ::foo::bar
874 if (NextToken().is(tok::kw_new) || // ::new
875 NextToken().is(tok::kw_delete)) // ::delete
876 return false;
877
878 // Annotate typenames and C++ scope specifiers. If we get one, just
879 // recurse to handle whatever we get.
880 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000881 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000882 // Otherwise, not a type specifier.
883 return false;
884
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000885 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000886 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000887 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000888 Tok.getAnnotationValue());
889 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
890 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000891
892 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
893 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
894 // Objective-C interface. If we don't have Objective-C or a '<', this is
895 // just a normal reference to a typedef name.
896 if (!Tok.is(tok::less) || !getLang().ObjC1)
897 return true;
898
899 SourceLocation EndProtoLoc;
900 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
901 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
902 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
903
904 DS.SetRangeEnd(EndProtoLoc);
905 return true;
906 }
907
908 case tok::kw_short:
909 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
910 break;
911 case tok::kw_long:
912 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
913 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
914 else
915 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
916 break;
917 case tok::kw_signed:
918 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
919 break;
920 case tok::kw_unsigned:
921 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
922 break;
923 case tok::kw__Complex:
924 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
925 break;
926 case tok::kw__Imaginary:
927 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
928 break;
929 case tok::kw_void:
930 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
931 break;
932 case tok::kw_char:
933 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
934 break;
935 case tok::kw_int:
936 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
937 break;
938 case tok::kw_float:
939 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
940 break;
941 case tok::kw_double:
942 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
943 break;
944 case tok::kw_wchar_t:
945 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
946 break;
947 case tok::kw_bool:
948 case tok::kw__Bool:
949 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
950 break;
951 case tok::kw__Decimal32:
952 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
953 break;
954 case tok::kw__Decimal64:
955 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
956 break;
957 case tok::kw__Decimal128:
958 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
959 break;
960
961 // class-specifier:
962 case tok::kw_class:
963 case tok::kw_struct:
964 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000965 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000966 return true;
967
968 // enum-specifier:
969 case tok::kw_enum:
970 ParseEnumSpecifier(DS);
971 return true;
972
973 // cv-qualifier:
974 case tok::kw_const:
975 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
976 getLang())*2;
977 break;
978 case tok::kw_volatile:
979 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
980 getLang())*2;
981 break;
982 case tok::kw_restrict:
983 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
984 getLang())*2;
985 break;
986
987 // GNU typeof support.
988 case tok::kw_typeof:
989 ParseTypeofSpecifier(DS);
990 return true;
991
Steve Naroffedd04d52008-12-25 14:16:32 +0000992 case tok::kw___cdecl:
993 case tok::kw___stdcall:
994 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +0000995 if (!PP.getLangOptions().Microsoft) return false;
996 ConsumeToken();
997 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +0000998
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000999 default:
1000 // Not a type-specifier; do nothing.
1001 return false;
1002 }
1003
1004 // If the specifier combination wasn't legal, issue a diagnostic.
1005 if (isInvalid) {
1006 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001007 // Pick between error or extwarn.
1008 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1009 : diag::ext_duplicate_declspec;
1010 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001011 }
1012 DS.SetRangeEnd(Tok.getLocation());
1013 ConsumeToken(); // whatever we parsed above.
1014 return true;
1015}
Chris Lattner4b009652007-07-25 00:24:17 +00001016
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001017/// ParseStructDeclaration - Parse a struct declaration without the terminating
1018/// semicolon.
1019///
Chris Lattner4b009652007-07-25 00:24:17 +00001020/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001021/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001022/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001023/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001024/// struct-declarator-list:
1025/// struct-declarator
1026/// struct-declarator-list ',' struct-declarator
1027/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1028/// struct-declarator:
1029/// declarator
1030/// [GNU] declarator attributes[opt]
1031/// declarator[opt] ':' constant-expression
1032/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1033///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001034void Parser::
1035ParseStructDeclaration(DeclSpec &DS,
1036 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001037 if (Tok.is(tok::kw___extension__)) {
1038 // __extension__ silences extension warnings in the subexpression.
1039 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001040 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001041 return ParseStructDeclaration(DS, Fields);
1042 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001043
1044 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001045 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001046 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001047
Douglas Gregorb748fc52009-01-12 22:49:06 +00001048 // If there are no declarators, this is a free-standing declaration
1049 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001050 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001051 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001052 return;
1053 }
1054
1055 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001056 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001057 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001058 FieldDeclarator &DeclaratorInfo = Fields.back();
1059
Steve Naroffa9adf112007-08-20 22:28:22 +00001060 /// struct-declarator: declarator
1061 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001062 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001063 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001064
Chris Lattner34a01ad2007-10-09 17:33:22 +00001065 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001066 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001067 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001068 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001069 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001070 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001071 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001072 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001073
Steve Naroffa9adf112007-08-20 22:28:22 +00001074 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001075 if (Tok.is(tok::kw___attribute)) {
1076 SourceLocation Loc;
1077 AttributeList *AttrList = ParseAttributes(&Loc);
1078 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1079 }
1080
Steve Naroffa9adf112007-08-20 22:28:22 +00001081 // If we don't have a comma, it is either the end of the list (a ';')
1082 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001083 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001084 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001085
Steve Naroffa9adf112007-08-20 22:28:22 +00001086 // Consume the comma.
1087 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001088
Steve Naroffa9adf112007-08-20 22:28:22 +00001089 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001090 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001091
Steve Naroffa9adf112007-08-20 22:28:22 +00001092 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001093 if (Tok.is(tok::kw___attribute)) {
1094 SourceLocation Loc;
1095 AttributeList *AttrList = ParseAttributes(&Loc);
1096 Fields.back().D.AddAttributes(AttrList, Loc);
1097 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001098 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001099}
1100
1101/// ParseStructUnionBody
1102/// struct-contents:
1103/// struct-declaration-list
1104/// [EXT] empty
1105/// [GNU] "struct-declaration-list" without terminatoring ';'
1106/// struct-declaration-list:
1107/// struct-declaration
1108/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001109/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001110///
Chris Lattner4b009652007-07-25 00:24:17 +00001111void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1112 unsigned TagType, DeclTy *TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001113 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1114 PP.getSourceManager(),
1115 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001116
Chris Lattner4b009652007-07-25 00:24:17 +00001117 SourceLocation LBraceLoc = ConsumeBrace();
1118
Douglas Gregorcab994d2009-01-09 22:42:13 +00001119 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001120 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1121
Chris Lattner4b009652007-07-25 00:24:17 +00001122 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1123 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001124 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001125 Diag(Tok, diag::ext_empty_struct_union_enum)
1126 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001127
1128 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001129 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1130
Chris Lattner4b009652007-07-25 00:24:17 +00001131 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001132 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001133 // Each iteration of this loop reads one struct-declaration.
1134
1135 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001136 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001137 Diag(Tok, diag::ext_extra_struct_semi);
1138 ConsumeToken();
1139 continue;
1140 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001141
1142 // Parse all the comma separated declarators.
1143 DeclSpec DS;
1144 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001145 if (!Tok.is(tok::at)) {
1146 ParseStructDeclaration(DS, FieldDeclarators);
1147
1148 // Convert them all to fields.
1149 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1150 FieldDeclarator &FD = FieldDeclarators[i];
1151 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +00001152 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +00001153 DS.getSourceRange().getBegin(),
1154 FD.D, FD.BitfieldSize);
1155 FieldDecls.push_back(Field);
1156 }
1157 } else { // Handle @defs
1158 ConsumeToken();
1159 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1160 Diag(Tok, diag::err_unexpected_at);
1161 SkipUntil(tok::semi, true, true);
1162 continue;
1163 }
1164 ConsumeToken();
1165 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1166 if (!Tok.is(tok::identifier)) {
1167 Diag(Tok, diag::err_expected_ident);
1168 SkipUntil(tok::semi, true, true);
1169 continue;
1170 }
1171 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001172 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1173 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001174 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1175 ConsumeToken();
1176 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1177 }
Chris Lattner4b009652007-07-25 00:24:17 +00001178
Chris Lattner34a01ad2007-10-09 17:33:22 +00001179 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001180 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001181 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001182 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001183 break;
1184 } else {
1185 Diag(Tok, diag::err_expected_semi_decl_list);
1186 // Skip to end of block or statement
1187 SkipUntil(tok::r_brace, true, true);
1188 }
1189 }
1190
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001191 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001192
Chris Lattner4b009652007-07-25 00:24:17 +00001193 AttributeList *AttrList = 0;
1194 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001195 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001196 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001197
1198 Actions.ActOnFields(CurScope,
1199 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1200 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001201 AttrList);
1202 StructScope.Exit();
1203 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001204}
1205
1206
1207/// ParseEnumSpecifier
1208/// enum-specifier: [C99 6.7.2.2]
1209/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001210///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001211/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1212/// '}' attributes[opt]
1213/// 'enum' identifier
1214/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001215///
1216/// [C++] elaborated-type-specifier:
1217/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1218///
Chris Lattner4b009652007-07-25 00:24:17 +00001219void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001220 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001221 SourceLocation StartLoc = ConsumeToken();
1222
1223 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001224
1225 AttributeList *Attr = 0;
1226 // If attributes exist after tag, parse them.
1227 if (Tok.is(tok::kw___attribute))
1228 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001229
1230 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001231 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001232 if (Tok.isNot(tok::identifier)) {
1233 Diag(Tok, diag::err_expected_ident);
1234 if (Tok.isNot(tok::l_brace)) {
1235 // Has no name and is not a definition.
1236 // Skip the rest of this declarator, up until the comma or semicolon.
1237 SkipUntil(tok::comma, true);
1238 return;
1239 }
1240 }
1241 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001242
1243 // Must have either 'enum name' or 'enum {...}'.
1244 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1245 Diag(Tok, diag::err_expected_ident_lbrace);
1246
1247 // Skip the rest of this declarator, up until the comma or semicolon.
1248 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001249 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001250 }
1251
1252 // If an identifier is present, consume and remember it.
1253 IdentifierInfo *Name = 0;
1254 SourceLocation NameLoc;
1255 if (Tok.is(tok::identifier)) {
1256 Name = Tok.getIdentifierInfo();
1257 NameLoc = ConsumeToken();
1258 }
1259
1260 // There are three options here. If we have 'enum foo;', then this is a
1261 // forward declaration. If we have 'enum foo {...' then this is a
1262 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1263 //
1264 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1265 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1266 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1267 //
1268 Action::TagKind TK;
1269 if (Tok.is(tok::l_brace))
1270 TK = Action::TK_Definition;
1271 else if (Tok.is(tok::semi))
1272 TK = Action::TK_Declaration;
1273 else
1274 TK = Action::TK_Reference;
1275 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregord406b032009-02-06 22:42:48 +00001276 SS, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +00001277
Chris Lattner34a01ad2007-10-09 17:33:22 +00001278 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001279 ParseEnumBody(StartLoc, TagDecl);
1280
1281 // TODO: semantic analysis on the declspec for enums.
1282 const char *PrevSpec = 0;
1283 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001284 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001285}
1286
1287/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1288/// enumerator-list:
1289/// enumerator
1290/// enumerator-list ',' enumerator
1291/// enumerator:
1292/// enumeration-constant
1293/// enumeration-constant '=' constant-expression
1294/// enumeration-constant:
1295/// identifier
1296///
1297void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001298 // Enter the scope of the enum body and start the definition.
1299 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001300 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001301
Chris Lattner4b009652007-07-25 00:24:17 +00001302 SourceLocation LBraceLoc = ConsumeBrace();
1303
Chris Lattnerc9a92452007-08-27 17:24:30 +00001304 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001305 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001306 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001307
1308 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1309
1310 DeclTy *LastEnumConstDecl = 0;
1311
1312 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001313 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001314 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1315 SourceLocation IdentLoc = ConsumeToken();
1316
1317 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001318 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001319 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001320 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001321 AssignedVal = ParseConstantExpression();
1322 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001323 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001324 }
1325
1326 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001327 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001328 LastEnumConstDecl,
1329 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001330 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001331 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001332 EnumConstantDecls.push_back(EnumConstDecl);
1333 LastEnumConstDecl = EnumConstDecl;
1334
Chris Lattner34a01ad2007-10-09 17:33:22 +00001335 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001336 break;
1337 SourceLocation CommaLoc = ConsumeToken();
1338
Chris Lattner34a01ad2007-10-09 17:33:22 +00001339 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001340 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1341 }
1342
1343 // Eat the }.
1344 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1345
Steve Naroff0acc9c92007-09-15 18:49:24 +00001346 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001347 EnumConstantDecls.size());
1348
1349 DeclTy *AttrList = 0;
1350 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001351 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001352 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001353
1354 EnumScope.Exit();
1355 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001356}
1357
1358/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001359/// start of a type-qualifier-list.
1360bool Parser::isTypeQualifier() const {
1361 switch (Tok.getKind()) {
1362 default: return false;
1363 // type-qualifier
1364 case tok::kw_const:
1365 case tok::kw_volatile:
1366 case tok::kw_restrict:
1367 return true;
1368 }
1369}
1370
1371/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001372/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001373bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001374 switch (Tok.getKind()) {
1375 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001376
1377 case tok::identifier: // foo::bar
1378 // Annotate typenames and C++ scope specifiers. If we get one, just
1379 // recurse to handle whatever we get.
1380 if (TryAnnotateTypeOrScopeToken())
1381 return isTypeSpecifierQualifier();
1382 // Otherwise, not a type specifier.
1383 return false;
1384 case tok::coloncolon: // ::foo::bar
1385 if (NextToken().is(tok::kw_new) || // ::new
1386 NextToken().is(tok::kw_delete)) // ::delete
1387 return false;
1388
1389 // Annotate typenames and C++ scope specifiers. If we get one, just
1390 // recurse to handle whatever we get.
1391 if (TryAnnotateTypeOrScopeToken())
1392 return isTypeSpecifierQualifier();
1393 // Otherwise, not a type specifier.
1394 return false;
1395
Chris Lattner4b009652007-07-25 00:24:17 +00001396 // GNU attributes support.
1397 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001398 // GNU typeof support.
1399 case tok::kw_typeof:
1400
Chris Lattner4b009652007-07-25 00:24:17 +00001401 // type-specifiers
1402 case tok::kw_short:
1403 case tok::kw_long:
1404 case tok::kw_signed:
1405 case tok::kw_unsigned:
1406 case tok::kw__Complex:
1407 case tok::kw__Imaginary:
1408 case tok::kw_void:
1409 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001410 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001411 case tok::kw_int:
1412 case tok::kw_float:
1413 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001414 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001415 case tok::kw__Bool:
1416 case tok::kw__Decimal32:
1417 case tok::kw__Decimal64:
1418 case tok::kw__Decimal128:
1419
Chris Lattner2e78db32008-04-13 18:59:07 +00001420 // struct-or-union-specifier (C99) or class-specifier (C++)
1421 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001422 case tok::kw_struct:
1423 case tok::kw_union:
1424 // enum-specifier
1425 case tok::kw_enum:
1426
1427 // type-qualifier
1428 case tok::kw_const:
1429 case tok::kw_volatile:
1430 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001431
1432 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001433 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001434 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001435
1436 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1437 case tok::less:
1438 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001439
1440 case tok::kw___cdecl:
1441 case tok::kw___stdcall:
1442 case tok::kw___fastcall:
1443 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001444 }
1445}
1446
1447/// isDeclarationSpecifier() - Return true if the current token is part of a
1448/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001449bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001450 switch (Tok.getKind()) {
1451 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001452
1453 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001454 // Unfortunate hack to support "Class.factoryMethod" notation.
1455 if (getLang().ObjC1 && NextToken().is(tok::period))
1456 return false;
1457
Chris Lattnerb75fde62009-01-04 23:41:41 +00001458 // Annotate typenames and C++ scope specifiers. If we get one, just
1459 // recurse to handle whatever we get.
1460 if (TryAnnotateTypeOrScopeToken())
1461 return isDeclarationSpecifier();
1462 // Otherwise, not a declaration specifier.
1463 return false;
1464 case tok::coloncolon: // ::foo::bar
1465 if (NextToken().is(tok::kw_new) || // ::new
1466 NextToken().is(tok::kw_delete)) // ::delete
1467 return false;
1468
1469 // Annotate typenames and C++ scope specifiers. If we get one, just
1470 // recurse to handle whatever we get.
1471 if (TryAnnotateTypeOrScopeToken())
1472 return isDeclarationSpecifier();
1473 // Otherwise, not a declaration specifier.
1474 return false;
1475
Chris Lattner4b009652007-07-25 00:24:17 +00001476 // storage-class-specifier
1477 case tok::kw_typedef:
1478 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001479 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001480 case tok::kw_static:
1481 case tok::kw_auto:
1482 case tok::kw_register:
1483 case tok::kw___thread:
1484
1485 // type-specifiers
1486 case tok::kw_short:
1487 case tok::kw_long:
1488 case tok::kw_signed:
1489 case tok::kw_unsigned:
1490 case tok::kw__Complex:
1491 case tok::kw__Imaginary:
1492 case tok::kw_void:
1493 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001494 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001495 case tok::kw_int:
1496 case tok::kw_float:
1497 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001498 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001499 case tok::kw__Bool:
1500 case tok::kw__Decimal32:
1501 case tok::kw__Decimal64:
1502 case tok::kw__Decimal128:
1503
Chris Lattner2e78db32008-04-13 18:59:07 +00001504 // struct-or-union-specifier (C99) or class-specifier (C++)
1505 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001506 case tok::kw_struct:
1507 case tok::kw_union:
1508 // enum-specifier
1509 case tok::kw_enum:
1510
1511 // type-qualifier
1512 case tok::kw_const:
1513 case tok::kw_volatile:
1514 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001515
Chris Lattner4b009652007-07-25 00:24:17 +00001516 // function-specifier
1517 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001518 case tok::kw_virtual:
1519 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001520
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001521 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001522 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001523
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001524 // GNU typeof support.
1525 case tok::kw_typeof:
1526
1527 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001528 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001529 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001530
1531 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1532 case tok::less:
1533 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001534
Steve Naroffab1a3632009-01-06 19:34:12 +00001535 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001536 case tok::kw___cdecl:
1537 case tok::kw___stdcall:
1538 case tok::kw___fastcall:
1539 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001540 }
1541}
1542
1543
1544/// ParseTypeQualifierListOpt
1545/// type-qualifier-list: [C99 6.7.5]
1546/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001547/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001548/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001549/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001550///
Chris Lattner460696f2008-12-18 07:02:59 +00001551void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001552 while (1) {
1553 int isInvalid = false;
1554 const char *PrevSpec = 0;
1555 SourceLocation Loc = Tok.getLocation();
1556
1557 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001558 case tok::kw_const:
1559 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1560 getLang())*2;
1561 break;
1562 case tok::kw_volatile:
1563 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1564 getLang())*2;
1565 break;
1566 case tok::kw_restrict:
1567 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1568 getLang())*2;
1569 break;
Steve Naroffad620402008-12-25 14:41:26 +00001570 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001571 case tok::kw___cdecl:
1572 case tok::kw___stdcall:
1573 case tok::kw___fastcall:
1574 if (!PP.getLangOptions().Microsoft)
1575 goto DoneWithTypeQuals;
1576 // Just ignore it.
1577 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001578 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001579 if (AttributesAllowed) {
1580 DS.AddAttributes(ParseAttributes());
1581 continue; // do *not* consume the next token!
1582 }
1583 // otherwise, FALL THROUGH!
1584 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001585 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001586 // If this is not a type-qualifier token, we're done reading type
1587 // qualifiers. First verify that DeclSpec's are consistent.
1588 DS.Finish(Diags, PP.getSourceManager(), getLang());
1589 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001590 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001591
Chris Lattner4b009652007-07-25 00:24:17 +00001592 // If the specifier combination wasn't legal, issue a diagnostic.
1593 if (isInvalid) {
1594 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001595 // Pick between error or extwarn.
1596 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1597 : diag::ext_duplicate_declspec;
1598 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001599 }
1600 ConsumeToken();
1601 }
1602}
1603
1604
1605/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1606///
1607void Parser::ParseDeclarator(Declarator &D) {
1608 /// This implements the 'declarator' production in the C grammar, then checks
1609 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001610 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001611}
1612
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001613/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1614/// is parsed by the function passed to it. Pass null, and the direct-declarator
1615/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001616/// ptr-operator production.
1617///
Sebastian Redl75555032009-01-24 21:16:55 +00001618/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1619/// [C] pointer[opt] direct-declarator
1620/// [C++] direct-declarator
1621/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001622///
1623/// pointer: [C99 6.7.5]
1624/// '*' type-qualifier-list[opt]
1625/// '*' type-qualifier-list[opt] pointer
1626///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001627/// ptr-operator:
1628/// '*' cv-qualifier-seq[opt]
1629/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001630/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001631/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001632/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001633/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001634void Parser::ParseDeclaratorInternal(Declarator &D,
1635 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001636
Sebastian Redl75555032009-01-24 21:16:55 +00001637 // C++ member pointers start with a '::' or a nested-name.
1638 // Member pointers get special handling, since there's no place for the
1639 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001640 if (getLang().CPlusPlus &&
1641 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1642 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001643 CXXScopeSpec SS;
1644 if (ParseOptionalCXXScopeSpecifier(SS)) {
1645 if(Tok.isNot(tok::star)) {
1646 // The scope spec really belongs to the direct-declarator.
1647 D.getCXXScopeSpec() = SS;
1648 if (DirectDeclParser)
1649 (this->*DirectDeclParser)(D);
1650 return;
1651 }
1652
1653 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001654 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001655 DeclSpec DS;
1656 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001657 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001658
1659 // Recurse to parse whatever is left.
1660 ParseDeclaratorInternal(D, DirectDeclParser);
1661
1662 // Sema will have to catch (syntactically invalid) pointers into global
1663 // scope. It has to catch pointers into namespace scope anyway.
1664 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001665 Loc, DS.TakeAttributes()),
1666 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001667 return;
1668 }
1669 }
1670
1671 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001672 // Not a pointer, C++ reference, or block.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001673 if (Kind != tok::star &&
1674 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001675 // We parse rvalue refs in C++03, because otherwise the errors are scary.
1676 (Kind != tok::ampamp || !getLang().CPlusPlus) &&
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001677 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001678 if (DirectDeclParser)
1679 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001680 return;
1681 }
Sebastian Redl75555032009-01-24 21:16:55 +00001682
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001683 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1684 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001685 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001686 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001687
Steve Naroffdc22f212008-08-28 10:07:06 +00001688 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001689 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001690 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001691
Chris Lattner4b009652007-07-25 00:24:17 +00001692 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001693 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001694
Chris Lattner4b009652007-07-25 00:24:17 +00001695 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001696 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001697 if (Kind == tok::star)
1698 // Remember that we parsed a pointer type, and remember the type-quals.
1699 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001700 DS.TakeAttributes()),
1701 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001702 else
1703 // Remember that we parsed a Block type, and remember the type-quals.
1704 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001705 Loc),
1706 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001707 } else {
1708 // Is a reference
1709 DeclSpec DS;
1710
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001711 // Complain about rvalue references in C++03, but then go on and build
1712 // the declarator.
1713 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1714 Diag(Loc, diag::err_rvalue_reference);
1715
Chris Lattner4b009652007-07-25 00:24:17 +00001716 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1717 // cv-qualifiers are introduced through the use of a typedef or of a
1718 // template type argument, in which case the cv-qualifiers are ignored.
1719 //
1720 // [GNU] Retricted references are allowed.
1721 // [GNU] Attributes on references are allowed.
1722 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001723 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001724
1725 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1726 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1727 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001728 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001729 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1730 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001731 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001732 }
1733
1734 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001735 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001736
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001737 if (D.getNumTypeObjects() > 0) {
1738 // C++ [dcl.ref]p4: There shall be no references to references.
1739 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1740 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001741 if (const IdentifierInfo *II = D.getIdentifier())
1742 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1743 << II;
1744 else
1745 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1746 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001747
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001748 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001749 // can go ahead and build the (technically ill-formed)
1750 // declarator: reference collapsing will take care of it.
1751 }
1752 }
1753
Chris Lattner4b009652007-07-25 00:24:17 +00001754 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001755 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001756 DS.TakeAttributes(),
1757 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001758 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001759 }
1760}
1761
1762/// ParseDirectDeclarator
1763/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001764/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001765/// '(' declarator ')'
1766/// [GNU] '(' attributes declarator ')'
1767/// [C90] direct-declarator '[' constant-expression[opt] ']'
1768/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1769/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1770/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1771/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1772/// direct-declarator '(' parameter-type-list ')'
1773/// direct-declarator '(' identifier-list[opt] ')'
1774/// [GNU] direct-declarator '(' parameter-forward-declarations
1775/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001776/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1777/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001778/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001779///
1780/// declarator-id: [C++ 8]
1781/// id-expression
1782/// '::'[opt] nested-name-specifier[opt] type-name
1783///
1784/// id-expression: [C++ 5.1]
1785/// unqualified-id
1786/// qualified-id [TODO]
1787///
1788/// unqualified-id: [C++ 5.1]
1789/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001790/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001791/// conversion-function-id [TODO]
1792/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001793/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001794///
Chris Lattner4b009652007-07-25 00:24:17 +00001795void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001796 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001797
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001798 if (getLang().CPlusPlus) {
1799 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001800 // ParseDeclaratorInternal might already have parsed the scope.
1801 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1802 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001803 if (afterCXXScope) {
1804 // Change the declaration context for name lookup, until this function
1805 // is exited (and the declarator has been parsed).
1806 DeclScopeObj.EnterDeclaratorScope();
1807 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001808
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001809 if (Tok.is(tok::identifier)) {
1810 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001811
Douglas Gregor2fa10442008-12-18 19:37:40 +00001812 // If this identifier is the name of the current class, it's a
1813 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001814 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001815 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001816 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001817 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001818 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001819 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001820 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1821 ConsumeToken();
1822 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001823 } else if (Tok.is(tok::annot_template_id)) {
1824 TemplateIdAnnotation *TemplateId
1825 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1826
1827 // FIXME: Could this template-id name a constructor?
1828
1829 // FIXME: This is an egregious hack, where we silently ignore
1830 // the specialization (which should be a function template
1831 // specialization name) and use the name instead. This hack
1832 // will go away when we have support for function
1833 // specializations.
1834 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1835 TemplateId->Destroy();
1836 ConsumeToken();
1837 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001838 } else if (Tok.is(tok::kw_operator)) {
1839 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001840 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001841
Douglas Gregor853dd392008-12-26 15:00:45 +00001842 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001843 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1844 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001845 } else {
1846 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001847 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1848 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1849 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001850 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001851 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001852 }
1853 goto PastIdentifier;
1854 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001855 // This should be a C++ destructor.
1856 SourceLocation TildeLoc = ConsumeToken();
1857 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001858 // FIXME: Inaccurate.
1859 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001860 SourceLocation EndLoc;
1861 if (TypeTy *Type = ParseClassName(EndLoc)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001862 D.setDestructor(Type, TildeLoc, NameLoc);
1863 } else {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001864 D.SetIdentifier(0, TildeLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001865 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001866 } else {
1867 Diag(Tok, diag::err_expected_class_name);
1868 D.SetIdentifier(0, TildeLoc);
1869 }
1870 goto PastIdentifier;
1871 }
1872
1873 // If we reached this point, token is not identifier and not '~'.
1874
1875 if (afterCXXScope) {
1876 Diag(Tok, diag::err_expected_unqualified_id);
1877 D.SetIdentifier(0, Tok.getLocation());
1878 D.setInvalidType(true);
1879 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001880 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001881 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001882 }
1883
1884 // If we reached this point, we are either in C/ObjC or the token didn't
1885 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001886 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1887 assert(!getLang().CPlusPlus &&
1888 "There's a C++-specific check for tok::identifier above");
1889 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1890 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1891 ConsumeToken();
1892 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001893 // direct-declarator: '(' declarator ')'
1894 // direct-declarator: '(' attributes declarator ')'
1895 // Example: 'char (*X)' or 'int (*XX)(void)'
1896 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001897 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001898 // This could be something simple like "int" (in which case the declarator
1899 // portion is empty), if an abstract-declarator is allowed.
1900 D.SetIdentifier(0, Tok.getLocation());
1901 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00001902 if (D.getContext() == Declarator::MemberContext)
1903 Diag(Tok, diag::err_expected_member_name_or_semi)
1904 << D.getDeclSpec().getSourceRange();
1905 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001906 Diag(Tok, diag::err_expected_unqualified_id);
1907 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001908 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001909 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001910 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001911 }
1912
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001913 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001914 assert(D.isPastIdentifier() &&
1915 "Haven't past the location of the identifier yet?");
1916
1917 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001918 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001919 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1920 // In such a case, check if we actually have a function declarator; if it
1921 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001922 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1923 // When not in file scope, warn for ambiguous function declarators, just
1924 // in case the author intended it as a variable definition.
1925 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1926 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1927 break;
1928 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001929 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001930 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001931 ParseBracketDeclarator(D);
1932 } else {
1933 break;
1934 }
1935 }
1936}
1937
Chris Lattnera0d056d2008-04-06 05:45:57 +00001938/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1939/// only called before the identifier, so these are most likely just grouping
1940/// parens for precedence. If we find that these are actually function
1941/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1942///
1943/// direct-declarator:
1944/// '(' declarator ')'
1945/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001946/// direct-declarator '(' parameter-type-list ')'
1947/// direct-declarator '(' identifier-list[opt] ')'
1948/// [GNU] direct-declarator '(' parameter-forward-declarations
1949/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001950///
1951void Parser::ParseParenDeclarator(Declarator &D) {
1952 SourceLocation StartLoc = ConsumeParen();
1953 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1954
Chris Lattner1f185292008-10-20 02:05:46 +00001955 // Eat any attributes before we look at whether this is a grouping or function
1956 // declarator paren. If this is a grouping paren, the attribute applies to
1957 // the type being built up, for example:
1958 // int (__attribute__(()) *x)(long y)
1959 // If this ends up not being a grouping paren, the attribute applies to the
1960 // first argument, for example:
1961 // int (__attribute__(()) int x)
1962 // In either case, we need to eat any attributes to be able to determine what
1963 // sort of paren this is.
1964 //
1965 AttributeList *AttrList = 0;
1966 bool RequiresArg = false;
1967 if (Tok.is(tok::kw___attribute)) {
1968 AttrList = ParseAttributes();
1969
1970 // We require that the argument list (if this is a non-grouping paren) be
1971 // present even if the attribute list was empty.
1972 RequiresArg = true;
1973 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001974 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00001975 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1976 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00001977 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00001978
Chris Lattnera0d056d2008-04-06 05:45:57 +00001979 // If we haven't past the identifier yet (or where the identifier would be
1980 // stored, if this is an abstract declarator), then this is probably just
1981 // grouping parens. However, if this could be an abstract-declarator, then
1982 // this could also be the start of function arguments (consider 'void()').
1983 bool isGrouping;
1984
1985 if (!D.mayOmitIdentifier()) {
1986 // If this can't be an abstract-declarator, this *must* be a grouping
1987 // paren, because we haven't seen the identifier yet.
1988 isGrouping = true;
1989 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001990 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001991 isDeclarationSpecifier()) { // 'int(int)' is a function.
1992 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1993 // considered to be a type, not a K&R identifier-list.
1994 isGrouping = false;
1995 } else {
1996 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1997 isGrouping = true;
1998 }
1999
2000 // If this is a grouping paren, handle:
2001 // direct-declarator: '(' declarator ')'
2002 // direct-declarator: '(' attributes declarator ')'
2003 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002004 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002005 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002006 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002007 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002008
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002009 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002010 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002011 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002012
2013 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002014 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002015 return;
2016 }
2017
2018 // Okay, if this wasn't a grouping paren, it must be the start of a function
2019 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002020 // identifier (and remember where it would have been), then call into
2021 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002022 D.SetIdentifier(0, Tok.getLocation());
2023
Chris Lattner1f185292008-10-20 02:05:46 +00002024 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002025}
2026
2027/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2028/// declarator D up to a paren, which indicates that we are parsing function
2029/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002030///
Chris Lattner1f185292008-10-20 02:05:46 +00002031/// If AttrList is non-null, then the caller parsed those arguments immediately
2032/// after the open paren - they should be considered to be the first argument of
2033/// a parameter. If RequiresArg is true, then the first argument of the
2034/// function is required to be present and required to not be an identifier
2035/// list.
2036///
Chris Lattner4b009652007-07-25 00:24:17 +00002037/// This method also handles this portion of the grammar:
2038/// parameter-type-list: [C99 6.7.5]
2039/// parameter-list
2040/// parameter-list ',' '...'
2041///
2042/// parameter-list: [C99 6.7.5]
2043/// parameter-declaration
2044/// parameter-list ',' parameter-declaration
2045///
2046/// parameter-declaration: [C99 6.7.5]
2047/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002048/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002049/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002050/// declaration-specifiers abstract-declarator[opt]
2051/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002052/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002053/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2054///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002055/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002056/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002057///
Chris Lattner1f185292008-10-20 02:05:46 +00002058void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2059 AttributeList *AttrList,
2060 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002061 // lparen is already consumed!
2062 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002063
Chris Lattner1f185292008-10-20 02:05:46 +00002064 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002065 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002066 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002067 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002068 delete AttrList;
2069 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002070
Sebastian Redl0c986032009-02-09 18:23:29 +00002071 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002072
2073 // cv-qualifier-seq[opt].
2074 DeclSpec DS;
2075 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002076 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002077 if (!DS.getSourceRange().getEnd().isInvalid())
2078 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002079
2080 // Parse exception-specification[opt].
2081 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002082 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002083 }
2084
Chris Lattner9f7564b2008-04-06 06:57:35 +00002085 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002086 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002087 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002088 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002089 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002090 /*arglist*/ 0, 0,
2091 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002092 LParenLoc, D),
2093 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002094 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002095 }
2096
2097 // Alternatively, this parameter list may be an identifier list form for a
2098 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002099 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002100 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002101 // K&R identifier lists can't have typedefs as identifiers, per
2102 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002103 if (RequiresArg) {
2104 Diag(Tok, diag::err_argument_required_after_attribute);
2105 delete AttrList;
2106 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002107 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2108 // normal declarators, not for abstract-declarators.
2109 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002110 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002111 }
2112
2113 // Finally, a normal, non-empty parameter type list.
2114
2115 // Build up an array of information about the parsed arguments.
2116 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002117
2118 // Enter function-declaration scope, limiting any declarators to the
2119 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002120 ParseScope PrototypeScope(this,
2121 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002122
2123 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002124 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002125 while (1) {
2126 if (Tok.is(tok::ellipsis)) {
2127 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002128 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002129 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002130 }
2131
Chris Lattner9f7564b2008-04-06 06:57:35 +00002132 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002133
Chris Lattner9f7564b2008-04-06 06:57:35 +00002134 // Parse the declaration-specifiers.
2135 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002136
2137 // If the caller parsed attributes for the first argument, add them now.
2138 if (AttrList) {
2139 DS.AddAttributes(AttrList);
2140 AttrList = 0; // Only apply the attributes to the first parameter.
2141 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002142 ParseDeclarationSpecifiers(DS);
2143
Chris Lattner9f7564b2008-04-06 06:57:35 +00002144 // Parse the declarator. This is "PrototypeContext", because we must
2145 // accept either 'declarator' or 'abstract-declarator' here.
2146 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2147 ParseDeclarator(ParmDecl);
2148
2149 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002150 if (Tok.is(tok::kw___attribute)) {
2151 SourceLocation Loc;
2152 AttributeList *AttrList = ParseAttributes(&Loc);
2153 ParmDecl.AddAttributes(AttrList, Loc);
2154 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002155
Chris Lattner9f7564b2008-04-06 06:57:35 +00002156 // Remember this parsed parameter in ParamInfo.
2157 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2158
Douglas Gregor605de8d2008-12-16 21:30:33 +00002159 // DefArgToks is used when the parsing of default arguments needs
2160 // to be delayed.
2161 CachedTokens *DefArgToks = 0;
2162
Chris Lattner9f7564b2008-04-06 06:57:35 +00002163 // If no parameter was specified, verify that *something* was specified,
2164 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002165 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2166 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002167 // Completely missing, emit error.
2168 Diag(DSStart, diag::err_missing_param);
2169 } else {
2170 // Otherwise, we have something. Add it and let semantic analysis try
2171 // to grok it and add the result to the ParamInfo we are building.
2172
2173 // Inform the actions module about the parameter declarator, so it gets
2174 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00002175 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2176
2177 // Parse the default argument, if any. We parse the default
2178 // arguments in all dialects; the semantic analysis in
2179 // ActOnParamDefaultArgument will reject the default argument in
2180 // C.
2181 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002182 SourceLocation EqualLoc = Tok.getLocation();
2183
Chris Lattner3e254fb2008-04-08 04:40:51 +00002184 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002185 if (D.getContext() == Declarator::MemberContext) {
2186 // If we're inside a class definition, cache the tokens
2187 // corresponding to the default argument. We'll actually parse
2188 // them when we see the end of the class definition.
2189 // FIXME: Templates will require something similar.
2190 // FIXME: Can we use a smart pointer for Toks?
2191 DefArgToks = new CachedTokens;
2192
2193 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2194 tok::semi, false)) {
2195 delete DefArgToks;
2196 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002197 Actions.ActOnParamDefaultArgumentError(Param);
2198 } else
2199 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002200 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002201 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002202 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002203
2204 OwningExprResult DefArgResult(ParseAssignmentExpression());
2205 if (DefArgResult.isInvalid()) {
2206 Actions.ActOnParamDefaultArgumentError(Param);
2207 SkipUntil(tok::comma, tok::r_paren, true, true);
2208 } else {
2209 // Inform the actions module about the default argument
2210 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002211 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002212 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002213 }
2214 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002215
2216 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002217 ParmDecl.getIdentifierLoc(), Param,
2218 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002219 }
2220
2221 // If the next token is a comma, consume it and keep reading arguments.
2222 if (Tok.isNot(tok::comma)) break;
2223
2224 // Consume the comma.
2225 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002226 }
2227
Chris Lattner9f7564b2008-04-06 06:57:35 +00002228 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002229 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002230
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002231 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002232 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002233
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002234 DeclSpec DS;
2235 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002236 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002237 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002238 if (!DS.getSourceRange().getEnd().isInvalid())
2239 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002240
2241 // Parse exception-specification[opt].
2242 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002243 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002244 }
2245
Chris Lattner4b009652007-07-25 00:24:17 +00002246 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002247 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002248 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002249 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002250 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002251 LParenLoc, D),
2252 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002253}
2254
Chris Lattner35d9c912008-04-06 06:34:08 +00002255/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2256/// we found a K&R-style identifier list instead of a type argument list. The
2257/// current token is known to be the first identifier in the list.
2258///
2259/// identifier-list: [C99 6.7.5]
2260/// identifier
2261/// identifier-list ',' identifier
2262///
2263void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2264 Declarator &D) {
2265 // Build up an array of information about the parsed arguments.
2266 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2267 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2268
2269 // If there was no identifier specified for the declarator, either we are in
2270 // an abstract-declarator, or we are in a parameter declarator which was found
2271 // to be abstract. In abstract-declarators, identifier lists are not valid:
2272 // diagnose this.
2273 if (!D.getIdentifier())
2274 Diag(Tok, diag::ext_ident_list_in_param);
2275
2276 // Tok is known to be the first identifier in the list. Remember this
2277 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002278 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002279 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2280 Tok.getLocation(), 0));
2281
Chris Lattner113a56b2008-04-06 06:39:19 +00002282 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002283
2284 while (Tok.is(tok::comma)) {
2285 // Eat the comma.
2286 ConsumeToken();
2287
Chris Lattner113a56b2008-04-06 06:39:19 +00002288 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002289 if (Tok.isNot(tok::identifier)) {
2290 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002291 SkipUntil(tok::r_paren);
2292 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002293 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002294
Chris Lattner35d9c912008-04-06 06:34:08 +00002295 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002296
2297 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002298 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002299 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002300
2301 // Verify that the argument identifier has not already been mentioned.
2302 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002303 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002304 } else {
2305 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002306 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2307 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00002308 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002309
2310 // Eat the identifier.
2311 ConsumeToken();
2312 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002313
2314 // If we have the closing ')', eat it and we're done.
2315 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2316
Chris Lattner113a56b2008-04-06 06:39:19 +00002317 // Remember that we parsed a function type, and remember the attributes. This
2318 // function type is always a K&R style function type, which is not varargs and
2319 // has no prototype.
2320 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002321 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002322 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002323 /*TypeQuals*/0, LParenLoc, D),
2324 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002325}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002326
Chris Lattner4b009652007-07-25 00:24:17 +00002327/// [C90] direct-declarator '[' constant-expression[opt] ']'
2328/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2329/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2330/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2331/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2332void Parser::ParseBracketDeclarator(Declarator &D) {
2333 SourceLocation StartLoc = ConsumeBracket();
2334
Chris Lattner1525c3a2008-12-18 07:27:21 +00002335 // C array syntax has many features, but by-far the most common is [] and [4].
2336 // This code does a fast path to handle some of the most obvious cases.
2337 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002338 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002339 // Remember that we parsed the empty array type.
2340 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002341 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2342 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002343 return;
2344 } else if (Tok.getKind() == tok::numeric_constant &&
2345 GetLookAheadToken(1).is(tok::r_square)) {
2346 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002347 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002348 ConsumeToken();
2349
Sebastian Redl0c986032009-02-09 18:23:29 +00002350 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002351
2352 // If there was an error parsing the assignment-expression, recover.
2353 if (ExprRes.isInvalid())
2354 ExprRes.release(); // Deallocate expr, just use [].
2355
2356 // Remember that we parsed a array type, and remember its features.
2357 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002358 ExprRes.release(), StartLoc),
2359 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002360 return;
2361 }
2362
Chris Lattner4b009652007-07-25 00:24:17 +00002363 // If valid, this location is the position where we read the 'static' keyword.
2364 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002365 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002366 StaticLoc = ConsumeToken();
2367
2368 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002369 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002370 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002371 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002372
2373 // If we haven't already read 'static', check to see if there is one after the
2374 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002375 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002376 StaticLoc = ConsumeToken();
2377
2378 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2379 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002380 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002381
2382 // Handle the case where we have '[*]' as the array size. However, a leading
2383 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2384 // the the token after the star is a ']'. Since stars in arrays are
2385 // infrequent, use of lookahead is not costly here.
2386 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002387 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002388
Chris Lattner306d4df2008-12-18 06:50:14 +00002389 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002390 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002391 StaticLoc = SourceLocation(); // Drop the static.
2392 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002393 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002394 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002395 // Note, in C89, this production uses the constant-expr production instead
2396 // of assignment-expr. The only difference is that assignment-expr allows
2397 // things like '=' and '*='. Sema rejects these in C89 mode because they
2398 // are not i-c-e's, so we don't need to distinguish between the two here.
2399
Chris Lattner4b009652007-07-25 00:24:17 +00002400 // Parse the assignment-expression now.
2401 NumElements = ParseAssignmentExpression();
2402 }
2403
2404 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002405 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002406 // If the expression was invalid, skip it.
2407 SkipUntil(tok::r_square);
2408 return;
2409 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002410
2411 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2412
Chris Lattner1525c3a2008-12-18 07:27:21 +00002413 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002414 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2415 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002416 NumElements.release(), StartLoc),
2417 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002418}
2419
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002420/// [GNU] typeof-specifier:
2421/// typeof ( expressions )
2422/// typeof ( type-name )
2423/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002424///
2425void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002426 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002427 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002428 SourceLocation StartLoc = ConsumeToken();
2429
Chris Lattner34a01ad2007-10-09 17:33:22 +00002430 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002431 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002432 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002433 return;
2434 }
2435
Sebastian Redl14ca7412008-12-11 21:36:32 +00002436 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002437 if (Result.isInvalid()) {
2438 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002439 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002440 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002441
2442 const char *PrevSpec = 0;
2443 // Check for duplicate type specifiers.
2444 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002445 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002446 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002447
2448 // FIXME: Not accurate, the range gets one token more than it should.
2449 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002450 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002451 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002452
Steve Naroff7cbb1462007-07-31 12:34:36 +00002453 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2454
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002455 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002456 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002457
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002458 assert((Ty.isInvalid() || Ty.get()) &&
2459 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002460
Chris Lattner34a01ad2007-10-09 17:33:22 +00002461 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002462 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002463 return;
2464 }
2465 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002466
2467 if (Ty.isInvalid())
2468 DS.SetTypeSpecError();
2469 else {
2470 const char *PrevSpec = 0;
2471 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2472 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2473 Ty.get()))
2474 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2475 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002476 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002477 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002478
2479 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002480 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002481 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002482 return;
2483 }
2484 RParenLoc = ConsumeParen();
2485 const char *PrevSpec = 0;
2486 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2487 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002488 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002489 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002490 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002491 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002492}
2493
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002494