blob: 5a8b2b85694193e1048bb77ccf3239db7cf21d7c [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 Lattner5261d0c2009-03-28 19:18:32 +0000231Parser::DeclPtrTy 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]
Chris Lattner5261d0c2009-03-28 19:18:32 +0000251Parser::DeclPtrTy 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///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000294Parser::DeclPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000295ParseInitDeclaratorListAfterFirstDeclarator(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.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000299 Parser::DeclPtrTy LastDeclInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000300
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);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000310 return DeclPtrTy();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000311 }
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);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000337 return DeclPtrTy();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000338 }
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.
Chris Lattner7f6c2872009-03-28 06:13:37 +0000396 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanianc1509b02009-01-17 00:00:40 +0000397 Diag(Tok, diag::err_parse_error);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000398 return DeclPtrTy();
Fariborz Jahanianc1509b02009-01-17 00:00:40 +0000399 }
Chris Lattner4b009652007-07-25 00:24:17 +0000400 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
401 }
Chris Lattner7f6c2872009-03-28 06:13:37 +0000402
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000403 // If this is an ObjC2 for-each loop, this is a successful declarator
404 // parse. The syntax for these looks like:
405 // 'for' '(' declaration 'in' expr ')' statement
Chris Lattner7f6c2872009-03-28 06:13:37 +0000406 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in())
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000407 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner7f6c2872009-03-28 06:13:37 +0000408
Chris Lattner4b009652007-07-25 00:24:17 +0000409 Diag(Tok, diag::err_parse_error);
410 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000411 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000412 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000413 ConsumeToken();
Chris Lattner5261d0c2009-03-28 19:18:32 +0000414 return DeclPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +0000415}
416
417/// ParseSpecifierQualifierList
418/// specifier-qualifier-list:
419/// type-specifier specifier-qualifier-list[opt]
420/// type-qualifier specifier-qualifier-list[opt]
421/// [GNU] attributes specifier-qualifier-list[opt]
422///
423void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
424 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
425 /// parse declaration-specifiers and complain about extra stuff.
426 ParseDeclarationSpecifiers(DS);
427
428 // Validate declspec for type-name.
429 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000430 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000431 Diag(Tok, diag::err_typename_requires_specqual);
432
433 // Issue diagnostic and remove storage class if present.
434 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
435 if (DS.getStorageClassSpecLoc().isValid())
436 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
437 else
438 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
439 DS.ClearStorageClassSpecs();
440 }
441
442 // Issue diagnostic and remove function specfier if present.
443 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000444 if (DS.isInlineSpecified())
445 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
446 if (DS.isVirtualSpecified())
447 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
448 if (DS.isExplicitSpecified())
449 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000450 DS.ClearFunctionSpecs();
451 }
452}
453
454/// ParseDeclarationSpecifiers
455/// declaration-specifiers: [C99 6.7]
456/// storage-class-specifier declaration-specifiers[opt]
457/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000458/// [C99] function-specifier declaration-specifiers[opt]
459/// [GNU] attributes declaration-specifiers[opt]
460///
461/// storage-class-specifier: [C99 6.7.1]
462/// 'typedef'
463/// 'extern'
464/// 'static'
465/// 'auto'
466/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000467/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000468/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000469/// function-specifier: [C99 6.7.4]
470/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000471/// [C++] 'virtual'
472/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000473///
Douglas Gregor52473432008-12-24 02:52:09 +0000474void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000475 TemplateParameterLists *TemplateParams,
476 AccessSpecifier AS){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000477 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000478 while (1) {
479 int isInvalid = false;
480 const char *PrevSpec = 0;
481 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000482
Chris Lattner4b009652007-07-25 00:24:17 +0000483 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000484 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000485 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000486 // If this is not a declaration specifier token, we're done reading decl
487 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000488 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000489 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000490
491 case tok::coloncolon: // ::foo::bar
492 // Annotate C++ scope specifiers. If we get one, loop.
493 if (TryAnnotateCXXScopeToken())
494 continue;
495 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000496
497 case tok::annot_cxxscope: {
498 if (DS.hasTypeSpecifier())
499 goto DoneWithDeclSpec;
500
501 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000502 Token Next = NextToken();
503 if (Next.is(tok::annot_template_id) &&
504 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
505 ->Kind == TNK_Class_template) {
506 // We have a qualified template-id, e.g., N::A<int>
507 CXXScopeSpec SS;
508 ParseOptionalCXXScopeSpecifier(SS);
509 assert(Tok.is(tok::annot_template_id) &&
510 "ParseOptionalCXXScopeSpecifier not working");
511 AnnotateTemplateIdTokenAsType(&SS);
512 continue;
513 }
514
515 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000516 goto DoneWithDeclSpec;
517
518 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000519 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000520 SS.setRange(Tok.getAnnotationRange());
521
522 // If the next token is the name of the class type that the C++ scope
523 // denotes, followed by a '(', then this is a constructor declaration.
524 // We're done with the decl-specifiers.
525 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
526 CurScope, &SS) &&
527 GetLookAheadToken(2).is(tok::l_paren))
528 goto DoneWithDeclSpec;
529
Douglas Gregor1075a162009-02-04 17:00:24 +0000530 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
531 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000532
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000533 if (TypeRep == 0)
534 goto DoneWithDeclSpec;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000535
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000536 ConsumeToken(); // The C++ scope.
537
Douglas Gregora60c62e2009-02-09 15:09:02 +0000538 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000539 TypeRep);
540 if (isInvalid)
541 break;
542
543 DS.SetRangeEnd(Tok.getLocation());
544 ConsumeToken(); // The typename.
545
546 continue;
547 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000548
549 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000550 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerc297b722009-01-21 19:48:37 +0000551 Tok.getAnnotationValue());
552 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
553 ConsumeToken(); // The typename
554
555 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
556 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
557 // Objective-C interface. If we don't have Objective-C or a '<', this is
558 // just a normal reference to a typedef name.
559 if (!Tok.is(tok::less) || !getLang().ObjC1)
560 continue;
561
562 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000563 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000564 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
565 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
566
567 DS.SetRangeEnd(EndProtoLoc);
568 continue;
569 }
570
Chris Lattnerfda18db2008-07-26 01:18:38 +0000571 // typedef-name
572 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000573 // In C++, check to see if this is a scope specifier like foo::bar::, if
574 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000575 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
576 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000577
Chris Lattnerfda18db2008-07-26 01:18:38 +0000578 // This identifier can only be a typedef name if we haven't already seen
579 // a type-specifier. Without this check we misparse:
580 // typedef int X; struct Y { short X; }; as 'short int'.
581 if (DS.hasTypeSpecifier())
582 goto DoneWithDeclSpec;
583
584 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000585 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
586 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000587
Chris Lattnerfda18db2008-07-26 01:18:38 +0000588 if (TypeRep == 0)
589 goto DoneWithDeclSpec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000590
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000591 // C++: If the identifier is actually the name of the class type
592 // being defined and the next token is a '(', then this is a
593 // constructor declaration. We're done with the decl-specifiers
594 // and will treat this token as an identifier.
595 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000596 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000597 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
598 NextToken().getKind() == tok::l_paren)
599 goto DoneWithDeclSpec;
600
Douglas Gregora60c62e2009-02-09 15:09:02 +0000601 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000602 TypeRep);
603 if (isInvalid)
604 break;
605
606 DS.SetRangeEnd(Tok.getLocation());
607 ConsumeToken(); // The identifier
608
609 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
610 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
611 // Objective-C interface. If we don't have Objective-C or a '<', this is
612 // just a normal reference to a typedef name.
613 if (!Tok.is(tok::less) || !getLang().ObjC1)
614 continue;
615
616 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000617 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000618 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000619 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000620
621 DS.SetRangeEnd(EndProtoLoc);
622
Steve Narofff7683302008-09-22 10:28:57 +0000623 // Need to support trailing type qualifiers (e.g. "id<p> const").
624 // If a type specifier follows, it will be diagnosed elsewhere.
625 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000626 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000627
628 // type-name
629 case tok::annot_template_id: {
630 TemplateIdAnnotation *TemplateId
631 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
632 if (TemplateId->Kind != TNK_Class_template) {
633 // This template-id does not refer to a type name, so we're
634 // done with the type-specifiers.
635 goto DoneWithDeclSpec;
636 }
637
638 // Turn the template-id annotation token into a type annotation
639 // token, then try again to parse it as a type-specifier.
640 if (AnnotateTemplateIdTokenAsType())
641 DS.SetTypeSpecError();
642
643 continue;
644 }
645
Chris Lattner4b009652007-07-25 00:24:17 +0000646 // GNU attributes support.
647 case tok::kw___attribute:
648 DS.AddAttributes(ParseAttributes());
649 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000650
651 // Microsoft declspec support.
652 case tok::kw___declspec:
653 if (!PP.getLangOptions().Microsoft)
654 goto DoneWithDeclSpec;
655 FuzzyParseMicrosoftDeclSpec();
656 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000657
Steve Naroffedd04d52008-12-25 14:16:32 +0000658 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000659 case tok::kw___forceinline:
660 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000661 case tok::kw___cdecl:
662 case tok::kw___stdcall:
663 case tok::kw___fastcall:
664 if (!PP.getLangOptions().Microsoft)
665 goto DoneWithDeclSpec;
666 // Just ignore it.
667 break;
668
Chris Lattner4b009652007-07-25 00:24:17 +0000669 // storage-class-specifier
670 case tok::kw_typedef:
671 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
672 break;
673 case tok::kw_extern:
674 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000675 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000676 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
677 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000678 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000679 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
680 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000681 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000682 case tok::kw_static:
683 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000684 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000685 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
686 break;
687 case tok::kw_auto:
688 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
689 break;
690 case tok::kw_register:
691 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
692 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000693 case tok::kw_mutable:
694 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
695 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000696 case tok::kw___thread:
697 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
698 break;
699
Chris Lattner4b009652007-07-25 00:24:17 +0000700 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000701
Chris Lattner4b009652007-07-25 00:24:17 +0000702 // function-specifier
703 case tok::kw_inline:
704 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
705 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000706 case tok::kw_virtual:
707 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
708 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000709 case tok::kw_explicit:
710 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
711 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000712
713 // type-specifier
714 case tok::kw_short:
715 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
716 break;
717 case tok::kw_long:
718 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
719 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
720 else
721 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
722 break;
723 case tok::kw_signed:
724 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
725 break;
726 case tok::kw_unsigned:
727 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
728 break;
729 case tok::kw__Complex:
730 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
731 break;
732 case tok::kw__Imaginary:
733 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
734 break;
735 case tok::kw_void:
736 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
737 break;
738 case tok::kw_char:
739 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
740 break;
741 case tok::kw_int:
742 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
743 break;
744 case tok::kw_float:
745 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
746 break;
747 case tok::kw_double:
748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
749 break;
750 case tok::kw_wchar_t:
751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
752 break;
753 case tok::kw_bool:
754 case tok::kw__Bool:
755 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
756 break;
757 case tok::kw__Decimal32:
758 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
759 break;
760 case tok::kw__Decimal64:
761 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
762 break;
763 case tok::kw__Decimal128:
764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
765 break;
766
767 // class-specifier:
768 case tok::kw_class:
769 case tok::kw_struct:
770 case tok::kw_union:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000771 ParseClassSpecifier(DS, TemplateParams, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000772 continue;
773
774 // enum-specifier:
775 case tok::kw_enum:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000776 ParseEnumSpecifier(DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000777 continue;
778
779 // cv-qualifier:
780 case tok::kw_const:
781 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
782 break;
783 case tok::kw_volatile:
784 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
785 getLang())*2;
786 break;
787 case tok::kw_restrict:
788 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
789 getLang())*2;
790 break;
791
Douglas Gregord3022602009-03-27 23:10:48 +0000792 // C++ typename-specifier:
793 case tok::kw_typename:
794 if (TryAnnotateTypeOrScopeToken())
795 continue;
796 break;
797
Chris Lattnerc297b722009-01-21 19:48:37 +0000798 // GNU typeof support.
799 case tok::kw_typeof:
800 ParseTypeofSpecifier(DS);
801 continue;
802
Steve Naroff5f0466b2008-06-05 00:02:44 +0000803 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000804 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000805 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
806 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000807 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000808 goto DoneWithDeclSpec;
809
810 {
811 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000812 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000813 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000814 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000815 DS.SetRangeEnd(EndProtoLoc);
816
Chris Lattnerf006a222008-11-18 07:48:38 +0000817 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
818 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000819 // Need to support trailing type qualifiers (e.g. "id<p> const").
820 // If a type specifier follows, it will be diagnosed elsewhere.
821 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000822 }
Chris Lattner4b009652007-07-25 00:24:17 +0000823 }
824 // If the specifier combination wasn't legal, issue a diagnostic.
825 if (isInvalid) {
826 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000827 // Pick between error or extwarn.
828 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
829 : diag::ext_duplicate_declspec;
830 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000831 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000832 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000833 ConsumeToken();
834 }
835}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000836
Chris Lattnerd706dc82009-01-06 06:59:53 +0000837/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000838/// primarily follow the C++ grammar with additions for C99 and GNU,
839/// which together subsume the C grammar. Note that the C++
840/// type-specifier also includes the C type-qualifier (for const,
841/// volatile, and C99 restrict). Returns true if a type-specifier was
842/// found (and parsed), false otherwise.
843///
844/// type-specifier: [C++ 7.1.5]
845/// simple-type-specifier
846/// class-specifier
847/// enum-specifier
848/// elaborated-type-specifier [TODO]
849/// cv-qualifier
850///
851/// cv-qualifier: [C++ 7.1.5.1]
852/// 'const'
853/// 'volatile'
854/// [C99] 'restrict'
855///
856/// simple-type-specifier: [ C++ 7.1.5.2]
857/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
858/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
859/// 'char'
860/// 'wchar_t'
861/// 'bool'
862/// 'short'
863/// 'int'
864/// 'long'
865/// 'signed'
866/// 'unsigned'
867/// 'float'
868/// 'double'
869/// 'void'
870/// [C99] '_Bool'
871/// [C99] '_Complex'
872/// [C99] '_Imaginary' // Removed in TC2?
873/// [GNU] '_Decimal32'
874/// [GNU] '_Decimal64'
875/// [GNU] '_Decimal128'
876/// [GNU] typeof-specifier
877/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
878/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000879bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
880 const char *&PrevSpec,
881 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000882 SourceLocation Loc = Tok.getLocation();
883
884 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000885 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +0000886 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +0000887 // Annotate typenames and C++ scope specifiers. If we get one, just
888 // recurse to handle whatever we get.
889 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000890 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000891 // Otherwise, not a type specifier.
892 return false;
893 case tok::coloncolon: // ::foo::bar
894 if (NextToken().is(tok::kw_new) || // ::new
895 NextToken().is(tok::kw_delete)) // ::delete
896 return false;
897
898 // Annotate typenames and C++ scope specifiers. If we get one, just
899 // recurse to handle whatever we get.
900 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000901 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000902 // Otherwise, not a type specifier.
903 return false;
904
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000905 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000906 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000907 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000908 Tok.getAnnotationValue());
909 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
910 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000911
912 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
913 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
914 // Objective-C interface. If we don't have Objective-C or a '<', this is
915 // just a normal reference to a typedef name.
916 if (!Tok.is(tok::less) || !getLang().ObjC1)
917 return true;
918
919 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000920 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000921 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
922 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
923
924 DS.SetRangeEnd(EndProtoLoc);
925 return true;
926 }
927
928 case tok::kw_short:
929 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
930 break;
931 case tok::kw_long:
932 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
933 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
934 else
935 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
936 break;
937 case tok::kw_signed:
938 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
939 break;
940 case tok::kw_unsigned:
941 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
942 break;
943 case tok::kw__Complex:
944 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
945 break;
946 case tok::kw__Imaginary:
947 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
948 break;
949 case tok::kw_void:
950 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
951 break;
952 case tok::kw_char:
953 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
954 break;
955 case tok::kw_int:
956 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
957 break;
958 case tok::kw_float:
959 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
960 break;
961 case tok::kw_double:
962 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
963 break;
964 case tok::kw_wchar_t:
965 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
966 break;
967 case tok::kw_bool:
968 case tok::kw__Bool:
969 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
970 break;
971 case tok::kw__Decimal32:
972 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
973 break;
974 case tok::kw__Decimal64:
975 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
976 break;
977 case tok::kw__Decimal128:
978 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
979 break;
980
981 // class-specifier:
982 case tok::kw_class:
983 case tok::kw_struct:
984 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000985 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000986 return true;
987
988 // enum-specifier:
989 case tok::kw_enum:
990 ParseEnumSpecifier(DS);
991 return true;
992
993 // cv-qualifier:
994 case tok::kw_const:
995 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
996 getLang())*2;
997 break;
998 case tok::kw_volatile:
999 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1000 getLang())*2;
1001 break;
1002 case tok::kw_restrict:
1003 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1004 getLang())*2;
1005 break;
1006
1007 // GNU typeof support.
1008 case tok::kw_typeof:
1009 ParseTypeofSpecifier(DS);
1010 return true;
1011
Steve Naroffedd04d52008-12-25 14:16:32 +00001012 case tok::kw___cdecl:
1013 case tok::kw___stdcall:
1014 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001015 if (!PP.getLangOptions().Microsoft) return false;
1016 ConsumeToken();
1017 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001018
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001019 default:
1020 // Not a type-specifier; do nothing.
1021 return false;
1022 }
1023
1024 // If the specifier combination wasn't legal, issue a diagnostic.
1025 if (isInvalid) {
1026 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001027 // Pick between error or extwarn.
1028 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1029 : diag::ext_duplicate_declspec;
1030 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001031 }
1032 DS.SetRangeEnd(Tok.getLocation());
1033 ConsumeToken(); // whatever we parsed above.
1034 return true;
1035}
Chris Lattner4b009652007-07-25 00:24:17 +00001036
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001037/// ParseStructDeclaration - Parse a struct declaration without the terminating
1038/// semicolon.
1039///
Chris Lattner4b009652007-07-25 00:24:17 +00001040/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001041/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001042/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001043/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001044/// struct-declarator-list:
1045/// struct-declarator
1046/// struct-declarator-list ',' struct-declarator
1047/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1048/// struct-declarator:
1049/// declarator
1050/// [GNU] declarator attributes[opt]
1051/// declarator[opt] ':' constant-expression
1052/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1053///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001054void Parser::
1055ParseStructDeclaration(DeclSpec &DS,
1056 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001057 if (Tok.is(tok::kw___extension__)) {
1058 // __extension__ silences extension warnings in the subexpression.
1059 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001060 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001061 return ParseStructDeclaration(DS, Fields);
1062 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001063
1064 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001065 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001066 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001067
Douglas Gregorb748fc52009-01-12 22:49:06 +00001068 // If there are no declarators, this is a free-standing declaration
1069 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001070 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001071 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001072 return;
1073 }
1074
1075 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001076 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001077 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001078 FieldDeclarator &DeclaratorInfo = Fields.back();
1079
Steve Naroffa9adf112007-08-20 22:28:22 +00001080 /// struct-declarator: declarator
1081 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001082 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001083 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001084
Chris Lattner34a01ad2007-10-09 17:33:22 +00001085 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001086 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001087 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001088 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001089 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001090 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001091 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001092 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001093
Steve Naroffa9adf112007-08-20 22:28:22 +00001094 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001095 if (Tok.is(tok::kw___attribute)) {
1096 SourceLocation Loc;
1097 AttributeList *AttrList = ParseAttributes(&Loc);
1098 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1099 }
1100
Steve Naroffa9adf112007-08-20 22:28:22 +00001101 // If we don't have a comma, it is either the end of the list (a ';')
1102 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001103 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001104 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001105
Steve Naroffa9adf112007-08-20 22:28:22 +00001106 // Consume the comma.
1107 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001108
Steve Naroffa9adf112007-08-20 22:28:22 +00001109 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001110 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001111
Steve Naroffa9adf112007-08-20 22:28:22 +00001112 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001113 if (Tok.is(tok::kw___attribute)) {
1114 SourceLocation Loc;
1115 AttributeList *AttrList = ParseAttributes(&Loc);
1116 Fields.back().D.AddAttributes(AttrList, Loc);
1117 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001118 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001119}
1120
1121/// ParseStructUnionBody
1122/// struct-contents:
1123/// struct-declaration-list
1124/// [EXT] empty
1125/// [GNU] "struct-declaration-list" without terminatoring ';'
1126/// struct-declaration-list:
1127/// struct-declaration
1128/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001129/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001130///
Chris Lattner4b009652007-07-25 00:24:17 +00001131void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001132 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001133 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1134 PP.getSourceManager(),
1135 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001136
Chris Lattner4b009652007-07-25 00:24:17 +00001137 SourceLocation LBraceLoc = ConsumeBrace();
1138
Douglas Gregorcab994d2009-01-09 22:42:13 +00001139 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001140 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1141
Chris Lattner4b009652007-07-25 00:24:17 +00001142 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1143 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001144 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001145 Diag(Tok, diag::ext_empty_struct_union_enum)
1146 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001147
Chris Lattner5261d0c2009-03-28 19:18:32 +00001148 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001149 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1150
Chris Lattner4b009652007-07-25 00:24:17 +00001151 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001152 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001153 // Each iteration of this loop reads one struct-declaration.
1154
1155 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001156 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001157 Diag(Tok, diag::ext_extra_struct_semi);
1158 ConsumeToken();
1159 continue;
1160 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001161
1162 // Parse all the comma separated declarators.
1163 DeclSpec DS;
1164 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001165 if (!Tok.is(tok::at)) {
1166 ParseStructDeclaration(DS, FieldDeclarators);
1167
1168 // Convert them all to fields.
1169 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1170 FieldDeclarator &FD = FieldDeclarators[i];
1171 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001172 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1173 DS.getSourceRange().getBegin(),
1174 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001175 FieldDecls.push_back(Field);
1176 }
1177 } else { // Handle @defs
1178 ConsumeToken();
1179 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1180 Diag(Tok, diag::err_unexpected_at);
1181 SkipUntil(tok::semi, true, true);
1182 continue;
1183 }
1184 ConsumeToken();
1185 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1186 if (!Tok.is(tok::identifier)) {
1187 Diag(Tok, diag::err_expected_ident);
1188 SkipUntil(tok::semi, true, true);
1189 continue;
1190 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001191 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001192 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1193 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001194 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1195 ConsumeToken();
1196 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1197 }
Chris Lattner4b009652007-07-25 00:24:17 +00001198
Chris Lattner34a01ad2007-10-09 17:33:22 +00001199 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001200 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001201 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001202 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001203 break;
1204 } else {
1205 Diag(Tok, diag::err_expected_semi_decl_list);
1206 // Skip to end of block or statement
1207 SkipUntil(tok::r_brace, true, true);
1208 }
1209 }
1210
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001211 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001212
Chris Lattner4b009652007-07-25 00:24:17 +00001213 AttributeList *AttrList = 0;
1214 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001215 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001216 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001217
1218 Actions.ActOnFields(CurScope,
1219 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1220 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001221 AttrList);
1222 StructScope.Exit();
1223 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001224}
1225
1226
1227/// ParseEnumSpecifier
1228/// enum-specifier: [C99 6.7.2.2]
1229/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001230///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001231/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1232/// '}' attributes[opt]
1233/// 'enum' identifier
1234/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001235///
1236/// [C++] elaborated-type-specifier:
1237/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1238///
Douglas Gregor0c793bb2009-03-25 22:00:53 +00001239void Parser::ParseEnumSpecifier(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001240 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001241 SourceLocation StartLoc = ConsumeToken();
1242
1243 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001244
1245 AttributeList *Attr = 0;
1246 // If attributes exist after tag, parse them.
1247 if (Tok.is(tok::kw___attribute))
1248 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001249
1250 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001251 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001252 if (Tok.isNot(tok::identifier)) {
1253 Diag(Tok, diag::err_expected_ident);
1254 if (Tok.isNot(tok::l_brace)) {
1255 // Has no name and is not a definition.
1256 // Skip the rest of this declarator, up until the comma or semicolon.
1257 SkipUntil(tok::comma, true);
1258 return;
1259 }
1260 }
1261 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001262
1263 // Must have either 'enum name' or 'enum {...}'.
1264 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1265 Diag(Tok, diag::err_expected_ident_lbrace);
1266
1267 // Skip the rest of this declarator, up until the comma or semicolon.
1268 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001269 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001270 }
1271
1272 // If an identifier is present, consume and remember it.
1273 IdentifierInfo *Name = 0;
1274 SourceLocation NameLoc;
1275 if (Tok.is(tok::identifier)) {
1276 Name = Tok.getIdentifierInfo();
1277 NameLoc = ConsumeToken();
1278 }
1279
1280 // There are three options here. If we have 'enum foo;', then this is a
1281 // forward declaration. If we have 'enum foo {...' then this is a
1282 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1283 //
1284 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1285 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1286 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1287 //
1288 Action::TagKind TK;
1289 if (Tok.is(tok::l_brace))
1290 TK = Action::TK_Definition;
1291 else if (Tok.is(tok::semi))
1292 TK = Action::TK_Declaration;
1293 else
1294 TK = Action::TK_Reference;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001295 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1296 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001297
Chris Lattner34a01ad2007-10-09 17:33:22 +00001298 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001299 ParseEnumBody(StartLoc, TagDecl);
1300
1301 // TODO: semantic analysis on the declspec for enums.
1302 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001303 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1304 TagDecl.getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +00001305 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001306}
1307
1308/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1309/// enumerator-list:
1310/// enumerator
1311/// enumerator-list ',' enumerator
1312/// enumerator:
1313/// enumeration-constant
1314/// enumeration-constant '=' constant-expression
1315/// enumeration-constant:
1316/// identifier
1317///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001318void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001319 // Enter the scope of the enum body and start the definition.
1320 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001321 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001322
Chris Lattner4b009652007-07-25 00:24:17 +00001323 SourceLocation LBraceLoc = ConsumeBrace();
1324
Chris Lattnerc9a92452007-08-27 17:24:30 +00001325 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001326 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001327 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001328
Chris Lattner5261d0c2009-03-28 19:18:32 +00001329 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001330
Chris Lattner5261d0c2009-03-28 19:18:32 +00001331 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001332
1333 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001334 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001335 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1336 SourceLocation IdentLoc = ConsumeToken();
1337
1338 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001339 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001340 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001341 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001342 AssignedVal = ParseConstantExpression();
1343 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001344 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001345 }
1346
1347 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001348 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1349 LastEnumConstDecl,
1350 IdentLoc, Ident,
1351 EqualLoc,
1352 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001353 EnumConstantDecls.push_back(EnumConstDecl);
1354 LastEnumConstDecl = EnumConstDecl;
1355
Chris Lattner34a01ad2007-10-09 17:33:22 +00001356 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001357 break;
1358 SourceLocation CommaLoc = ConsumeToken();
1359
Chris Lattner34a01ad2007-10-09 17:33:22 +00001360 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001361 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1362 }
1363
1364 // Eat the }.
1365 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1366
Steve Naroff0acc9c92007-09-15 18:49:24 +00001367 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001368 EnumConstantDecls.size());
1369
Chris Lattner5261d0c2009-03-28 19:18:32 +00001370 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001371 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001372 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001373 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001374
1375 EnumScope.Exit();
1376 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001377}
1378
1379/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001380/// start of a type-qualifier-list.
1381bool Parser::isTypeQualifier() const {
1382 switch (Tok.getKind()) {
1383 default: return false;
1384 // type-qualifier
1385 case tok::kw_const:
1386 case tok::kw_volatile:
1387 case tok::kw_restrict:
1388 return true;
1389 }
1390}
1391
1392/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001393/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001394bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001395 switch (Tok.getKind()) {
1396 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001397
1398 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001399 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001400 // Annotate typenames and C++ scope specifiers. If we get one, just
1401 // recurse to handle whatever we get.
1402 if (TryAnnotateTypeOrScopeToken())
1403 return isTypeSpecifierQualifier();
1404 // Otherwise, not a type specifier.
1405 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001406
Chris Lattnerb75fde62009-01-04 23:41:41 +00001407 case tok::coloncolon: // ::foo::bar
1408 if (NextToken().is(tok::kw_new) || // ::new
1409 NextToken().is(tok::kw_delete)) // ::delete
1410 return false;
1411
1412 // Annotate typenames and C++ scope specifiers. If we get one, just
1413 // recurse to handle whatever we get.
1414 if (TryAnnotateTypeOrScopeToken())
1415 return isTypeSpecifierQualifier();
1416 // Otherwise, not a type specifier.
1417 return false;
1418
Chris Lattner4b009652007-07-25 00:24:17 +00001419 // GNU attributes support.
1420 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001421 // GNU typeof support.
1422 case tok::kw_typeof:
1423
Chris Lattner4b009652007-07-25 00:24:17 +00001424 // type-specifiers
1425 case tok::kw_short:
1426 case tok::kw_long:
1427 case tok::kw_signed:
1428 case tok::kw_unsigned:
1429 case tok::kw__Complex:
1430 case tok::kw__Imaginary:
1431 case tok::kw_void:
1432 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001433 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001434 case tok::kw_int:
1435 case tok::kw_float:
1436 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001437 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001438 case tok::kw__Bool:
1439 case tok::kw__Decimal32:
1440 case tok::kw__Decimal64:
1441 case tok::kw__Decimal128:
1442
Chris Lattner2e78db32008-04-13 18:59:07 +00001443 // struct-or-union-specifier (C99) or class-specifier (C++)
1444 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001445 case tok::kw_struct:
1446 case tok::kw_union:
1447 // enum-specifier
1448 case tok::kw_enum:
1449
1450 // type-qualifier
1451 case tok::kw_const:
1452 case tok::kw_volatile:
1453 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001454
1455 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001456 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001457 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001458
1459 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1460 case tok::less:
1461 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001462
1463 case tok::kw___cdecl:
1464 case tok::kw___stdcall:
1465 case tok::kw___fastcall:
1466 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001467 }
1468}
1469
1470/// isDeclarationSpecifier() - Return true if the current token is part of a
1471/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001472bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001473 switch (Tok.getKind()) {
1474 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001475
1476 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001477 // Unfortunate hack to support "Class.factoryMethod" notation.
1478 if (getLang().ObjC1 && NextToken().is(tok::period))
1479 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001480 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001481
Douglas Gregord3022602009-03-27 23:10:48 +00001482 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001483 // Annotate typenames and C++ scope specifiers. If we get one, just
1484 // recurse to handle whatever we get.
1485 if (TryAnnotateTypeOrScopeToken())
1486 return isDeclarationSpecifier();
1487 // Otherwise, not a declaration specifier.
1488 return false;
1489 case tok::coloncolon: // ::foo::bar
1490 if (NextToken().is(tok::kw_new) || // ::new
1491 NextToken().is(tok::kw_delete)) // ::delete
1492 return false;
1493
1494 // Annotate typenames and C++ scope specifiers. If we get one, just
1495 // recurse to handle whatever we get.
1496 if (TryAnnotateTypeOrScopeToken())
1497 return isDeclarationSpecifier();
1498 // Otherwise, not a declaration specifier.
1499 return false;
1500
Chris Lattner4b009652007-07-25 00:24:17 +00001501 // storage-class-specifier
1502 case tok::kw_typedef:
1503 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001504 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001505 case tok::kw_static:
1506 case tok::kw_auto:
1507 case tok::kw_register:
1508 case tok::kw___thread:
1509
1510 // type-specifiers
1511 case tok::kw_short:
1512 case tok::kw_long:
1513 case tok::kw_signed:
1514 case tok::kw_unsigned:
1515 case tok::kw__Complex:
1516 case tok::kw__Imaginary:
1517 case tok::kw_void:
1518 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001519 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001520 case tok::kw_int:
1521 case tok::kw_float:
1522 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001523 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001524 case tok::kw__Bool:
1525 case tok::kw__Decimal32:
1526 case tok::kw__Decimal64:
1527 case tok::kw__Decimal128:
1528
Chris Lattner2e78db32008-04-13 18:59:07 +00001529 // struct-or-union-specifier (C99) or class-specifier (C++)
1530 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001531 case tok::kw_struct:
1532 case tok::kw_union:
1533 // enum-specifier
1534 case tok::kw_enum:
1535
1536 // type-qualifier
1537 case tok::kw_const:
1538 case tok::kw_volatile:
1539 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001540
Chris Lattner4b009652007-07-25 00:24:17 +00001541 // function-specifier
1542 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001543 case tok::kw_virtual:
1544 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001545
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001546 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001547 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001548
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001549 // GNU typeof support.
1550 case tok::kw_typeof:
1551
1552 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001553 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001554 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001555
1556 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1557 case tok::less:
1558 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001559
Steve Naroffab1a3632009-01-06 19:34:12 +00001560 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001561 case tok::kw___cdecl:
1562 case tok::kw___stdcall:
1563 case tok::kw___fastcall:
1564 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001565 }
1566}
1567
1568
1569/// ParseTypeQualifierListOpt
1570/// type-qualifier-list: [C99 6.7.5]
1571/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001572/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001573/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001574/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001575///
Chris Lattner460696f2008-12-18 07:02:59 +00001576void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001577 while (1) {
1578 int isInvalid = false;
1579 const char *PrevSpec = 0;
1580 SourceLocation Loc = Tok.getLocation();
1581
1582 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001583 case tok::kw_const:
1584 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1585 getLang())*2;
1586 break;
1587 case tok::kw_volatile:
1588 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1589 getLang())*2;
1590 break;
1591 case tok::kw_restrict:
1592 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1593 getLang())*2;
1594 break;
Steve Naroffad620402008-12-25 14:41:26 +00001595 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001596 case tok::kw___cdecl:
1597 case tok::kw___stdcall:
1598 case tok::kw___fastcall:
1599 if (!PP.getLangOptions().Microsoft)
1600 goto DoneWithTypeQuals;
1601 // Just ignore it.
1602 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001603 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001604 if (AttributesAllowed) {
1605 DS.AddAttributes(ParseAttributes());
1606 continue; // do *not* consume the next token!
1607 }
1608 // otherwise, FALL THROUGH!
1609 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001610 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001611 // If this is not a type-qualifier token, we're done reading type
1612 // qualifiers. First verify that DeclSpec's are consistent.
1613 DS.Finish(Diags, PP.getSourceManager(), getLang());
1614 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001615 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001616
Chris Lattner4b009652007-07-25 00:24:17 +00001617 // If the specifier combination wasn't legal, issue a diagnostic.
1618 if (isInvalid) {
1619 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001620 // Pick between error or extwarn.
1621 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1622 : diag::ext_duplicate_declspec;
1623 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001624 }
1625 ConsumeToken();
1626 }
1627}
1628
1629
1630/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1631///
1632void Parser::ParseDeclarator(Declarator &D) {
1633 /// This implements the 'declarator' production in the C grammar, then checks
1634 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001635 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001636}
1637
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001638/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1639/// is parsed by the function passed to it. Pass null, and the direct-declarator
1640/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001641/// ptr-operator production.
1642///
Sebastian Redl75555032009-01-24 21:16:55 +00001643/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1644/// [C] pointer[opt] direct-declarator
1645/// [C++] direct-declarator
1646/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001647///
1648/// pointer: [C99 6.7.5]
1649/// '*' type-qualifier-list[opt]
1650/// '*' type-qualifier-list[opt] pointer
1651///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001652/// ptr-operator:
1653/// '*' cv-qualifier-seq[opt]
1654/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001655/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001656/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001657/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001658/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001659void Parser::ParseDeclaratorInternal(Declarator &D,
1660 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001661
Sebastian Redl75555032009-01-24 21:16:55 +00001662 // C++ member pointers start with a '::' or a nested-name.
1663 // Member pointers get special handling, since there's no place for the
1664 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001665 if (getLang().CPlusPlus &&
1666 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1667 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001668 CXXScopeSpec SS;
1669 if (ParseOptionalCXXScopeSpecifier(SS)) {
1670 if(Tok.isNot(tok::star)) {
1671 // The scope spec really belongs to the direct-declarator.
1672 D.getCXXScopeSpec() = SS;
1673 if (DirectDeclParser)
1674 (this->*DirectDeclParser)(D);
1675 return;
1676 }
1677
1678 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001679 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001680 DeclSpec DS;
1681 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001682 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001683
1684 // Recurse to parse whatever is left.
1685 ParseDeclaratorInternal(D, DirectDeclParser);
1686
1687 // Sema will have to catch (syntactically invalid) pointers into global
1688 // scope. It has to catch pointers into namespace scope anyway.
1689 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001690 Loc, DS.TakeAttributes()),
1691 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001692 return;
1693 }
1694 }
1695
1696 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001697 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001698 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001699 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001700 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001701 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001702 if (DirectDeclParser)
1703 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001704 return;
1705 }
Sebastian Redl75555032009-01-24 21:16:55 +00001706
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001707 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1708 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001709 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001710 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001711
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001712 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001713 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001714 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001715
Chris Lattner4b009652007-07-25 00:24:17 +00001716 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001717 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001718
Chris Lattner4b009652007-07-25 00:24:17 +00001719 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001720 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001721 if (Kind == tok::star)
1722 // Remember that we parsed a pointer type, and remember the type-quals.
1723 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001724 DS.TakeAttributes()),
1725 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001726 else
1727 // Remember that we parsed a Block type, and remember the type-quals.
1728 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001729 Loc),
1730 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001731 } else {
1732 // Is a reference
1733 DeclSpec DS;
1734
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001735 // Complain about rvalue references in C++03, but then go on and build
1736 // the declarator.
1737 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1738 Diag(Loc, diag::err_rvalue_reference);
1739
Chris Lattner4b009652007-07-25 00:24:17 +00001740 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1741 // cv-qualifiers are introduced through the use of a typedef or of a
1742 // template type argument, in which case the cv-qualifiers are ignored.
1743 //
1744 // [GNU] Retricted references are allowed.
1745 // [GNU] Attributes on references are allowed.
1746 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001747 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001748
1749 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1750 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1751 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001752 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001753 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1754 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001755 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001756 }
1757
1758 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001759 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001760
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001761 if (D.getNumTypeObjects() > 0) {
1762 // C++ [dcl.ref]p4: There shall be no references to references.
1763 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1764 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001765 if (const IdentifierInfo *II = D.getIdentifier())
1766 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1767 << II;
1768 else
1769 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1770 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001771
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001772 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001773 // can go ahead and build the (technically ill-formed)
1774 // declarator: reference collapsing will take care of it.
1775 }
1776 }
1777
Chris Lattner4b009652007-07-25 00:24:17 +00001778 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001779 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001780 DS.TakeAttributes(),
1781 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001782 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001783 }
1784}
1785
1786/// ParseDirectDeclarator
1787/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001788/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001789/// '(' declarator ')'
1790/// [GNU] '(' attributes declarator ')'
1791/// [C90] direct-declarator '[' constant-expression[opt] ']'
1792/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1793/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1794/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1795/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1796/// direct-declarator '(' parameter-type-list ')'
1797/// direct-declarator '(' identifier-list[opt] ')'
1798/// [GNU] direct-declarator '(' parameter-forward-declarations
1799/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001800/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1801/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001802/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001803///
1804/// declarator-id: [C++ 8]
1805/// id-expression
1806/// '::'[opt] nested-name-specifier[opt] type-name
1807///
1808/// id-expression: [C++ 5.1]
1809/// unqualified-id
1810/// qualified-id [TODO]
1811///
1812/// unqualified-id: [C++ 5.1]
1813/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001814/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001815/// conversion-function-id [TODO]
1816/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001817/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001818///
Chris Lattner4b009652007-07-25 00:24:17 +00001819void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001820 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001821
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001822 if (getLang().CPlusPlus) {
1823 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001824 // ParseDeclaratorInternal might already have parsed the scope.
1825 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1826 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001827 if (afterCXXScope) {
1828 // Change the declaration context for name lookup, until this function
1829 // is exited (and the declarator has been parsed).
1830 DeclScopeObj.EnterDeclaratorScope();
1831 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001832
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001833 if (Tok.is(tok::identifier)) {
1834 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001835
Douglas Gregor2fa10442008-12-18 19:37:40 +00001836 // If this identifier is the name of the current class, it's a
1837 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001838 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001839 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001840 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001841 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001842 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001843 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001844 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1845 ConsumeToken();
1846 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001847 } else if (Tok.is(tok::annot_template_id)) {
1848 TemplateIdAnnotation *TemplateId
1849 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1850
1851 // FIXME: Could this template-id name a constructor?
1852
1853 // FIXME: This is an egregious hack, where we silently ignore
1854 // the specialization (which should be a function template
1855 // specialization name) and use the name instead. This hack
1856 // will go away when we have support for function
1857 // specializations.
1858 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1859 TemplateId->Destroy();
1860 ConsumeToken();
1861 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001862 } else if (Tok.is(tok::kw_operator)) {
1863 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001864 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001865
Douglas Gregor853dd392008-12-26 15:00:45 +00001866 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001867 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1868 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001869 } else {
1870 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001871 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1872 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1873 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001874 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001875 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001876 }
1877 goto PastIdentifier;
1878 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001879 // This should be a C++ destructor.
1880 SourceLocation TildeLoc = ConsumeToken();
1881 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001882 // FIXME: Inaccurate.
1883 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001884 SourceLocation EndLoc;
1885 if (TypeTy *Type = ParseClassName(EndLoc)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001886 D.setDestructor(Type, TildeLoc, NameLoc);
1887 } else {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001888 D.SetIdentifier(0, TildeLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001889 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001890 } else {
1891 Diag(Tok, diag::err_expected_class_name);
1892 D.SetIdentifier(0, TildeLoc);
1893 }
1894 goto PastIdentifier;
1895 }
1896
1897 // If we reached this point, token is not identifier and not '~'.
1898
1899 if (afterCXXScope) {
1900 Diag(Tok, diag::err_expected_unqualified_id);
1901 D.SetIdentifier(0, Tok.getLocation());
1902 D.setInvalidType(true);
1903 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001904 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001905 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001906 }
1907
1908 // If we reached this point, we are either in C/ObjC or the token didn't
1909 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001910 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1911 assert(!getLang().CPlusPlus &&
1912 "There's a C++-specific check for tok::identifier above");
1913 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1914 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1915 ConsumeToken();
1916 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001917 // direct-declarator: '(' declarator ')'
1918 // direct-declarator: '(' attributes declarator ')'
1919 // Example: 'char (*X)' or 'int (*XX)(void)'
1920 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001921 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001922 // This could be something simple like "int" (in which case the declarator
1923 // portion is empty), if an abstract-declarator is allowed.
1924 D.SetIdentifier(0, Tok.getLocation());
1925 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00001926 if (D.getContext() == Declarator::MemberContext)
1927 Diag(Tok, diag::err_expected_member_name_or_semi)
1928 << D.getDeclSpec().getSourceRange();
1929 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001930 Diag(Tok, diag::err_expected_unqualified_id);
1931 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001932 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001933 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001934 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001935 }
1936
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001937 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001938 assert(D.isPastIdentifier() &&
1939 "Haven't past the location of the identifier yet?");
1940
1941 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001942 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001943 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1944 // In such a case, check if we actually have a function declarator; if it
1945 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001946 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1947 // When not in file scope, warn for ambiguous function declarators, just
1948 // in case the author intended it as a variable definition.
1949 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1950 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1951 break;
1952 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001953 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001954 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001955 ParseBracketDeclarator(D);
1956 } else {
1957 break;
1958 }
1959 }
1960}
1961
Chris Lattnera0d056d2008-04-06 05:45:57 +00001962/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1963/// only called before the identifier, so these are most likely just grouping
1964/// parens for precedence. If we find that these are actually function
1965/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1966///
1967/// direct-declarator:
1968/// '(' declarator ')'
1969/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001970/// direct-declarator '(' parameter-type-list ')'
1971/// direct-declarator '(' identifier-list[opt] ')'
1972/// [GNU] direct-declarator '(' parameter-forward-declarations
1973/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001974///
1975void Parser::ParseParenDeclarator(Declarator &D) {
1976 SourceLocation StartLoc = ConsumeParen();
1977 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1978
Chris Lattner1f185292008-10-20 02:05:46 +00001979 // Eat any attributes before we look at whether this is a grouping or function
1980 // declarator paren. If this is a grouping paren, the attribute applies to
1981 // the type being built up, for example:
1982 // int (__attribute__(()) *x)(long y)
1983 // If this ends up not being a grouping paren, the attribute applies to the
1984 // first argument, for example:
1985 // int (__attribute__(()) int x)
1986 // In either case, we need to eat any attributes to be able to determine what
1987 // sort of paren this is.
1988 //
1989 AttributeList *AttrList = 0;
1990 bool RequiresArg = false;
1991 if (Tok.is(tok::kw___attribute)) {
1992 AttrList = ParseAttributes();
1993
1994 // We require that the argument list (if this is a non-grouping paren) be
1995 // present even if the attribute list was empty.
1996 RequiresArg = true;
1997 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001998 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00001999 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2000 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00002001 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002002
Chris Lattnera0d056d2008-04-06 05:45:57 +00002003 // If we haven't past the identifier yet (or where the identifier would be
2004 // stored, if this is an abstract declarator), then this is probably just
2005 // grouping parens. However, if this could be an abstract-declarator, then
2006 // this could also be the start of function arguments (consider 'void()').
2007 bool isGrouping;
2008
2009 if (!D.mayOmitIdentifier()) {
2010 // If this can't be an abstract-declarator, this *must* be a grouping
2011 // paren, because we haven't seen the identifier yet.
2012 isGrouping = true;
2013 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002014 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002015 isDeclarationSpecifier()) { // 'int(int)' is a function.
2016 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2017 // considered to be a type, not a K&R identifier-list.
2018 isGrouping = false;
2019 } else {
2020 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2021 isGrouping = true;
2022 }
2023
2024 // If this is a grouping paren, handle:
2025 // direct-declarator: '(' declarator ')'
2026 // direct-declarator: '(' attributes declarator ')'
2027 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002028 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002029 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002030 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002031 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002032
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002033 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002034 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002035 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002036
2037 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002038 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002039 return;
2040 }
2041
2042 // Okay, if this wasn't a grouping paren, it must be the start of a function
2043 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002044 // identifier (and remember where it would have been), then call into
2045 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002046 D.SetIdentifier(0, Tok.getLocation());
2047
Chris Lattner1f185292008-10-20 02:05:46 +00002048 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002049}
2050
2051/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2052/// declarator D up to a paren, which indicates that we are parsing function
2053/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002054///
Chris Lattner1f185292008-10-20 02:05:46 +00002055/// If AttrList is non-null, then the caller parsed those arguments immediately
2056/// after the open paren - they should be considered to be the first argument of
2057/// a parameter. If RequiresArg is true, then the first argument of the
2058/// function is required to be present and required to not be an identifier
2059/// list.
2060///
Chris Lattner4b009652007-07-25 00:24:17 +00002061/// This method also handles this portion of the grammar:
2062/// parameter-type-list: [C99 6.7.5]
2063/// parameter-list
2064/// parameter-list ',' '...'
2065///
2066/// parameter-list: [C99 6.7.5]
2067/// parameter-declaration
2068/// parameter-list ',' parameter-declaration
2069///
2070/// parameter-declaration: [C99 6.7.5]
2071/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002072/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002073/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002074/// declaration-specifiers abstract-declarator[opt]
2075/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002076/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002077/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2078///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002079/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002080/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002081///
Chris Lattner1f185292008-10-20 02:05:46 +00002082void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2083 AttributeList *AttrList,
2084 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002085 // lparen is already consumed!
2086 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002087
Chris Lattner1f185292008-10-20 02:05:46 +00002088 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002089 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002090 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002091 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002092 delete AttrList;
2093 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002094
Sebastian Redl0c986032009-02-09 18:23:29 +00002095 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002096
2097 // cv-qualifier-seq[opt].
2098 DeclSpec DS;
2099 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002100 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002101 if (!DS.getSourceRange().getEnd().isInvalid())
2102 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002103
2104 // Parse exception-specification[opt].
2105 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002106 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002107 }
2108
Chris Lattner9f7564b2008-04-06 06:57:35 +00002109 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002110 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002111 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002112 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002113 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002114 /*arglist*/ 0, 0,
2115 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002116 LParenLoc, D),
2117 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002118 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002119 }
2120
2121 // Alternatively, this parameter list may be an identifier list form for a
2122 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002123 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002124 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002125 // K&R identifier lists can't have typedefs as identifiers, per
2126 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002127 if (RequiresArg) {
2128 Diag(Tok, diag::err_argument_required_after_attribute);
2129 delete AttrList;
2130 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002131 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2132 // normal declarators, not for abstract-declarators.
2133 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002134 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002135 }
2136
2137 // Finally, a normal, non-empty parameter type list.
2138
2139 // Build up an array of information about the parsed arguments.
2140 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002141
2142 // Enter function-declaration scope, limiting any declarators to the
2143 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002144 ParseScope PrototypeScope(this,
2145 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002146
2147 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002148 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002149 while (1) {
2150 if (Tok.is(tok::ellipsis)) {
2151 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002152 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002153 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002154 }
2155
Chris Lattner9f7564b2008-04-06 06:57:35 +00002156 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002157
Chris Lattner9f7564b2008-04-06 06:57:35 +00002158 // Parse the declaration-specifiers.
2159 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002160
2161 // If the caller parsed attributes for the first argument, add them now.
2162 if (AttrList) {
2163 DS.AddAttributes(AttrList);
2164 AttrList = 0; // Only apply the attributes to the first parameter.
2165 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002166 ParseDeclarationSpecifiers(DS);
2167
Chris Lattner9f7564b2008-04-06 06:57:35 +00002168 // Parse the declarator. This is "PrototypeContext", because we must
2169 // accept either 'declarator' or 'abstract-declarator' here.
2170 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2171 ParseDeclarator(ParmDecl);
2172
2173 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002174 if (Tok.is(tok::kw___attribute)) {
2175 SourceLocation Loc;
2176 AttributeList *AttrList = ParseAttributes(&Loc);
2177 ParmDecl.AddAttributes(AttrList, Loc);
2178 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002179
Chris Lattner9f7564b2008-04-06 06:57:35 +00002180 // Remember this parsed parameter in ParamInfo.
2181 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2182
Douglas Gregor605de8d2008-12-16 21:30:33 +00002183 // DefArgToks is used when the parsing of default arguments needs
2184 // to be delayed.
2185 CachedTokens *DefArgToks = 0;
2186
Chris Lattner9f7564b2008-04-06 06:57:35 +00002187 // If no parameter was specified, verify that *something* was specified,
2188 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002189 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2190 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002191 // Completely missing, emit error.
2192 Diag(DSStart, diag::err_missing_param);
2193 } else {
2194 // Otherwise, we have something. Add it and let semantic analysis try
2195 // to grok it and add the result to the ParamInfo we are building.
2196
2197 // Inform the actions module about the parameter declarator, so it gets
2198 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002199 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002200
2201 // Parse the default argument, if any. We parse the default
2202 // arguments in all dialects; the semantic analysis in
2203 // ActOnParamDefaultArgument will reject the default argument in
2204 // C.
2205 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002206 SourceLocation EqualLoc = Tok.getLocation();
2207
Chris Lattner3e254fb2008-04-08 04:40:51 +00002208 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002209 if (D.getContext() == Declarator::MemberContext) {
2210 // If we're inside a class definition, cache the tokens
2211 // corresponding to the default argument. We'll actually parse
2212 // them when we see the end of the class definition.
2213 // FIXME: Templates will require something similar.
2214 // FIXME: Can we use a smart pointer for Toks?
2215 DefArgToks = new CachedTokens;
2216
2217 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2218 tok::semi, false)) {
2219 delete DefArgToks;
2220 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002221 Actions.ActOnParamDefaultArgumentError(Param);
2222 } else
2223 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002224 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002225 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002226 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002227
2228 OwningExprResult DefArgResult(ParseAssignmentExpression());
2229 if (DefArgResult.isInvalid()) {
2230 Actions.ActOnParamDefaultArgumentError(Param);
2231 SkipUntil(tok::comma, tok::r_paren, true, true);
2232 } else {
2233 // Inform the actions module about the default argument
2234 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002235 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002236 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002237 }
2238 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002239
2240 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002241 ParmDecl.getIdentifierLoc(), Param,
2242 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002243 }
2244
2245 // If the next token is a comma, consume it and keep reading arguments.
2246 if (Tok.isNot(tok::comma)) break;
2247
2248 // Consume the comma.
2249 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002250 }
2251
Chris Lattner9f7564b2008-04-06 06:57:35 +00002252 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002253 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002254
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002255 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002256 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002257
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002258 DeclSpec DS;
2259 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002260 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002261 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002262 if (!DS.getSourceRange().getEnd().isInvalid())
2263 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002264
2265 // Parse exception-specification[opt].
2266 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002267 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002268 }
2269
Chris Lattner4b009652007-07-25 00:24:17 +00002270 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002271 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002272 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002273 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002274 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002275 LParenLoc, D),
2276 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002277}
2278
Chris Lattner35d9c912008-04-06 06:34:08 +00002279/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2280/// we found a K&R-style identifier list instead of a type argument list. The
2281/// current token is known to be the first identifier in the list.
2282///
2283/// identifier-list: [C99 6.7.5]
2284/// identifier
2285/// identifier-list ',' identifier
2286///
2287void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2288 Declarator &D) {
2289 // Build up an array of information about the parsed arguments.
2290 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2291 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2292
2293 // If there was no identifier specified for the declarator, either we are in
2294 // an abstract-declarator, or we are in a parameter declarator which was found
2295 // to be abstract. In abstract-declarators, identifier lists are not valid:
2296 // diagnose this.
2297 if (!D.getIdentifier())
2298 Diag(Tok, diag::ext_ident_list_in_param);
2299
2300 // Tok is known to be the first identifier in the list. Remember this
2301 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002302 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002303 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002304 Tok.getLocation(),
2305 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002306
Chris Lattner113a56b2008-04-06 06:39:19 +00002307 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002308
2309 while (Tok.is(tok::comma)) {
2310 // Eat the comma.
2311 ConsumeToken();
2312
Chris Lattner113a56b2008-04-06 06:39:19 +00002313 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002314 if (Tok.isNot(tok::identifier)) {
2315 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002316 SkipUntil(tok::r_paren);
2317 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002318 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002319
Chris Lattner35d9c912008-04-06 06:34:08 +00002320 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002321
2322 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002323 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002324 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002325
2326 // Verify that the argument identifier has not already been mentioned.
2327 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002328 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002329 } else {
2330 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002331 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002332 Tok.getLocation(),
2333 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002334 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002335
2336 // Eat the identifier.
2337 ConsumeToken();
2338 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002339
2340 // If we have the closing ')', eat it and we're done.
2341 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2342
Chris Lattner113a56b2008-04-06 06:39:19 +00002343 // Remember that we parsed a function type, and remember the attributes. This
2344 // function type is always a K&R style function type, which is not varargs and
2345 // has no prototype.
2346 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002347 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002348 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002349 /*TypeQuals*/0, LParenLoc, D),
2350 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002351}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002352
Chris Lattner4b009652007-07-25 00:24:17 +00002353/// [C90] direct-declarator '[' constant-expression[opt] ']'
2354/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2355/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2356/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2357/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2358void Parser::ParseBracketDeclarator(Declarator &D) {
2359 SourceLocation StartLoc = ConsumeBracket();
2360
Chris Lattner1525c3a2008-12-18 07:27:21 +00002361 // C array syntax has many features, but by-far the most common is [] and [4].
2362 // This code does a fast path to handle some of the most obvious cases.
2363 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002364 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002365 // Remember that we parsed the empty array type.
2366 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002367 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2368 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002369 return;
2370 } else if (Tok.getKind() == tok::numeric_constant &&
2371 GetLookAheadToken(1).is(tok::r_square)) {
2372 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002373 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002374 ConsumeToken();
2375
Sebastian Redl0c986032009-02-09 18:23:29 +00002376 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002377
2378 // If there was an error parsing the assignment-expression, recover.
2379 if (ExprRes.isInvalid())
2380 ExprRes.release(); // Deallocate expr, just use [].
2381
2382 // Remember that we parsed a array type, and remember its features.
2383 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002384 ExprRes.release(), StartLoc),
2385 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002386 return;
2387 }
2388
Chris Lattner4b009652007-07-25 00:24:17 +00002389 // If valid, this location is the position where we read the 'static' keyword.
2390 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002391 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002392 StaticLoc = ConsumeToken();
2393
2394 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002395 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002396 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002397 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002398
2399 // If we haven't already read 'static', check to see if there is one after the
2400 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002401 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002402 StaticLoc = ConsumeToken();
2403
2404 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2405 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002406 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002407
2408 // Handle the case where we have '[*]' as the array size. However, a leading
2409 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2410 // the the token after the star is a ']'. Since stars in arrays are
2411 // infrequent, use of lookahead is not costly here.
2412 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002413 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002414
Chris Lattner306d4df2008-12-18 06:50:14 +00002415 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002416 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002417 StaticLoc = SourceLocation(); // Drop the static.
2418 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002419 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002420 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002421 // Note, in C89, this production uses the constant-expr production instead
2422 // of assignment-expr. The only difference is that assignment-expr allows
2423 // things like '=' and '*='. Sema rejects these in C89 mode because they
2424 // are not i-c-e's, so we don't need to distinguish between the two here.
2425
Chris Lattner4b009652007-07-25 00:24:17 +00002426 // Parse the assignment-expression now.
2427 NumElements = ParseAssignmentExpression();
2428 }
2429
2430 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002431 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002432 // If the expression was invalid, skip it.
2433 SkipUntil(tok::r_square);
2434 return;
2435 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002436
2437 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2438
Chris Lattner1525c3a2008-12-18 07:27:21 +00002439 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002440 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2441 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002442 NumElements.release(), StartLoc),
2443 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002444}
2445
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002446/// [GNU] typeof-specifier:
2447/// typeof ( expressions )
2448/// typeof ( type-name )
2449/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002450///
2451void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002452 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002453 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002454 SourceLocation StartLoc = ConsumeToken();
2455
Chris Lattner34a01ad2007-10-09 17:33:22 +00002456 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002457 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002458 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002459 return;
2460 }
2461
Sebastian Redl14ca7412008-12-11 21:36:32 +00002462 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002463 if (Result.isInvalid()) {
2464 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002465 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002466 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002467
2468 const char *PrevSpec = 0;
2469 // Check for duplicate type specifiers.
2470 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002471 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002472 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002473
2474 // FIXME: Not accurate, the range gets one token more than it should.
2475 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002476 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002477 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002478
Steve Naroff7cbb1462007-07-31 12:34:36 +00002479 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2480
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002481 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002482 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002483
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002484 assert((Ty.isInvalid() || Ty.get()) &&
2485 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002486
Chris Lattner34a01ad2007-10-09 17:33:22 +00002487 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002488 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002489 return;
2490 }
2491 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002492
2493 if (Ty.isInvalid())
2494 DS.SetTypeSpecError();
2495 else {
2496 const char *PrevSpec = 0;
2497 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2498 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2499 Ty.get()))
2500 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2501 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002502 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002503 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002504
2505 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002506 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002507 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002508 return;
2509 }
2510 RParenLoc = ConsumeParen();
2511 const char *PrevSpec = 0;
2512 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2513 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002514 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002515 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002516 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002517 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002518}
2519
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002520