blob: c11383c3eca9d2c938cec0a136afa6162a45defd [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"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "llvm/ADT/SmallSet.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
25/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Sebastian Redlaaacda92009-05-29 18:02:33 +000030Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Chris Lattner4b009652007-07-25 00:24:17 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
33 ParseSpecifierQualifierList(DS);
Sebastian Redlaaacda92009-05-29 18:02:33 +000034
Chris Lattner4b009652007-07-25 00:24:17 +000035 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
Sebastian Redlaaacda92009-05-29 18:02:33 +000038 if (Range)
39 *Range = DeclaratorInfo.getSourceRange();
40
Chris Lattner34c61332009-04-25 08:06:05 +000041 if (DeclaratorInfo.isInvalidType())
Douglas Gregor6c0f4062009-02-18 17:45:20 +000042 return true;
43
44 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattner4b009652007-07-25 00:24:17 +000045}
46
47/// ParseAttributes - Parse a non-empty attributes list.
48///
49/// [GNU] attributes:
50/// attribute
51/// attributes attribute
52///
53/// [GNU] attribute:
54/// '__attribute__' '(' '(' attribute-list ')' ')'
55///
56/// [GNU] attribute-list:
57/// attrib
58/// attribute_list ',' attrib
59///
60/// [GNU] attrib:
61/// empty
62/// attrib-name
63/// attrib-name '(' identifier ')'
64/// attrib-name '(' identifier ',' nonempty-expr-list ')'
65/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
66///
67/// [GNU] attrib-name:
68/// identifier
69/// typespec
70/// typequal
71/// storageclass
72///
73/// FIXME: The GCC grammar/code for this construct implies we need two
74/// token lookahead. Comment from gcc: "If they start with an identifier
75/// which is followed by a comma or close parenthesis, then the arguments
76/// start with that identifier; otherwise they are an expression list."
77///
78/// At the moment, I am not doing 2 token lookahead. I am also unaware of
79/// any attributes that don't work (based on my limited testing). Most
80/// attributes are very simple in practice. Until we find a bug, I don't see
81/// a pressing need to implement the 2 token lookahead.
82
Sebastian Redl0c986032009-02-09 18:23:29 +000083AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000084 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000085
86 AttributeList *CurrAttr = 0;
87
Chris Lattner34a01ad2007-10-09 17:33:22 +000088 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000089 ConsumeToken();
90 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
91 "attribute")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
96 SkipUntil(tok::r_paren, true); // skip until ) or ;
97 return CurrAttr;
98 }
99 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +0000100 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
101 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000102
Chris Lattner34a01ad2007-10-09 17:33:22 +0000103 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000104 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
105 ConsumeToken();
106 continue;
107 }
108 // we have an identifier or declaration specifier (const, int, etc.)
109 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
110 SourceLocation AttrNameLoc = ConsumeToken();
111
112 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000113 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000114 ConsumeParen(); // ignore the left paren loc for now
115
Chris Lattner34a01ad2007-10-09 17:33:22 +0000116 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000117 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
118 SourceLocation ParmLoc = ConsumeToken();
119
Chris Lattner34a01ad2007-10-09 17:33:22 +0000120 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000121 // __attribute__(( mode(byte) ))
122 ConsumeParen(); // ignore the right paren loc for now
123 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
124 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000125 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000126 ConsumeToken();
127 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000128 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000129 bool ArgExprsOk = true;
130
131 // now parse the non-empty comma separated list of expressions
132 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000133 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000134 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000135 ArgExprsOk = false;
136 SkipUntil(tok::r_paren);
137 break;
138 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000139 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000140 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000141 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000142 break;
143 ConsumeToken(); // Eat the comma, move to the next argument
144 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000145 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000146 ConsumeParen(); // ignore the right paren loc for now
147 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000148 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000149 }
150 }
151 } else { // not an identifier
Nate Begeman60702162009-06-26 06:32:41 +0000152 switch (Tok.getKind()) {
153 case tok::r_paren:
Chris Lattner4b009652007-07-25 00:24:17 +0000154 // parse a possibly empty comma separated list of expressions
Chris Lattner4b009652007-07-25 00:24:17 +0000155 // __attribute__(( nonnull() ))
156 ConsumeParen(); // ignore the right paren loc for now
157 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
158 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman60702162009-06-26 06:32:41 +0000159 break;
160 case tok::kw_char:
161 case tok::kw_wchar_t:
162 case tok::kw_bool:
163 case tok::kw_short:
164 case tok::kw_int:
165 case tok::kw_long:
166 case tok::kw_signed:
167 case tok::kw_unsigned:
168 case tok::kw_float:
169 case tok::kw_double:
170 case tok::kw_void:
171 case tok::kw_typeof:
172 // If it's a builtin type name, eat it and expect a rparen
173 // __attribute__(( vec_type_hint(char) ))
174 ConsumeToken();
175 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
176 0, SourceLocation(), 0, 0, CurrAttr);
177 if (Tok.is(tok::r_paren))
178 ConsumeParen();
179 break;
180 default:
Chris Lattner4b009652007-07-25 00:24:17 +0000181 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000182 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000183 bool ArgExprsOk = true;
184
185 // now parse the list of expressions
186 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000187 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000188 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000189 ArgExprsOk = false;
190 SkipUntil(tok::r_paren);
191 break;
192 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000193 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000194 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000195 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000196 break;
197 ConsumeToken(); // Eat the comma, move to the next argument
198 }
199 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000200 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000201 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000202 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
203 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000204 CurrAttr);
205 }
Nate Begeman60702162009-06-26 06:32:41 +0000206 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000207 }
208 }
209 } else {
210 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
211 0, SourceLocation(), 0, 0, CurrAttr);
212 }
213 }
214 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Chris Lattner4b009652007-07-25 00:24:17 +0000215 SkipUntil(tok::r_paren, false);
Sebastian Redl0c986032009-02-09 18:23:29 +0000216 SourceLocation Loc = Tok.getLocation();;
217 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
218 SkipUntil(tok::r_paren, false);
219 }
220 if (EndLoc)
221 *EndLoc = Loc;
Chris Lattner4b009652007-07-25 00:24:17 +0000222 }
223 return CurrAttr;
224}
225
Eli Friedmancd231842009-06-08 07:21:15 +0000226/// ParseMicrosoftDeclSpec - Parse an __declspec construct
227///
228/// [MS] decl-specifier:
229/// __declspec ( extended-decl-modifier-seq )
230///
231/// [MS] extended-decl-modifier-seq:
232/// extended-decl-modifier[opt]
233/// extended-decl-modifier extended-decl-modifier-seq
234
Eli Friedman891d82f2009-06-08 23:27:34 +0000235AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000236 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmancd231842009-06-08 07:21:15 +0000237
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000238 ConsumeToken();
Eli Friedmancd231842009-06-08 07:21:15 +0000239 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
240 "declspec")) {
241 SkipUntil(tok::r_paren, true); // skip until ) or ;
242 return CurrAttr;
243 }
Eli Friedman891d82f2009-06-08 23:27:34 +0000244 while (Tok.getIdentifierInfo()) {
Eli Friedmancd231842009-06-08 07:21:15 +0000245 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
246 SourceLocation AttrNameLoc = ConsumeToken();
247 if (Tok.is(tok::l_paren)) {
248 ConsumeParen();
249 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
250 // correctly.
251 OwningExprResult ArgExpr(ParseAssignmentExpression());
252 if (!ArgExpr.isInvalid()) {
253 ExprTy* ExprList = ArgExpr.take();
254 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
255 SourceLocation(), &ExprList, 1,
256 CurrAttr, true);
257 }
258 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
259 SkipUntil(tok::r_paren, false);
260 } else {
261 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, SourceLocation(),
262 0, 0, CurrAttr, true);
263 }
264 }
265 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
266 SkipUntil(tok::r_paren, false);
Eli Friedman891d82f2009-06-08 23:27:34 +0000267 return CurrAttr;
268}
269
270AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
271 // Treat these like attributes
272 // FIXME: Allow Sema to distinguish between these and real attributes!
273 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
274 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
275 Tok.is(tok::kw___w64)) {
276 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
277 SourceLocation AttrNameLoc = ConsumeToken();
278 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
279 // FIXME: Support these properly!
280 continue;
281 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
282 SourceLocation(), 0, 0, CurrAttr, true);
283 }
284 return CurrAttr;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000285}
286
Chris Lattner4b009652007-07-25 00:24:17 +0000287/// ParseDeclaration - Parse a full 'declaration', which consists of
288/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000289/// 'Context' should be a Declarator::TheContext value. This returns the
290/// location of the semicolon in DeclEnd.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000291///
292/// declaration: [C99 6.7]
293/// block-declaration ->
294/// simple-declaration
295/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000296/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000297/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000298/// [C++] using-directive
Douglas Gregorcad27f62009-06-22 23:06:13 +0000299/// [C++] using-declaration
Sebastian Redla8cecf62009-03-24 22:27:57 +0000300/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000301/// others... [FIXME]
302///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000303Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
304 SourceLocation &DeclEnd) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000305 DeclPtrTy SingleDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000306 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000307 case tok::kw_template:
Douglas Gregore3298aa2009-05-12 21:31:51 +0000308 case tok::kw_export:
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000309 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000310 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000311 case tok::kw_namespace:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000312 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000313 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000314 case tok::kw_using:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000315 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000316 break;
Anders Carlssonab041982009-03-11 16:27:10 +0000317 case tok::kw_static_assert:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000318 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000319 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000320 default:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000321 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000322 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000323
324 // This routine returns a DeclGroup, if the thing we parsed only contains a
325 // single decl, convert it now.
326 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000327}
328
329/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
330/// declaration-specifiers init-declarator-list[opt] ';'
331///[C90/C++]init-declarator-list ';' [TODO]
332/// [OMP] threadprivate-directive [TODO]
Chris Lattnerf8016042009-03-29 17:27:48 +0000333///
334/// If RequireSemi is false, this does not check for a ';' at the end of the
335/// declaration.
336Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000337 SourceLocation &DeclEnd,
Chris Lattnerf8016042009-03-29 17:27:48 +0000338 bool RequireSemi) {
Chris Lattner4b009652007-07-25 00:24:17 +0000339 // Parse the common declaration-specifiers piece.
340 DeclSpec DS;
341 ParseDeclarationSpecifiers(DS);
342
343 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
344 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000345 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000346 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000347 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
348 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000349 }
350
351 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
352 ParseDeclarator(DeclaratorInfo);
353
Chris Lattner2c41d482009-03-29 17:18:04 +0000354 DeclGroupPtrTy DG =
355 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnerf8016042009-03-29 17:27:48 +0000356
Chris Lattner9802a0a2009-04-02 04:16:50 +0000357 DeclEnd = Tok.getLocation();
358
Chris Lattnerf8016042009-03-29 17:27:48 +0000359 // If the client wants to check what comes after the declaration, just return
360 // immediately without checking anything!
361 if (!RequireSemi) return DG;
Chris Lattner2c41d482009-03-29 17:18:04 +0000362
363 if (Tok.is(tok::semi)) {
364 ConsumeToken();
Chris Lattner2c41d482009-03-29 17:18:04 +0000365 return DG;
366 }
367
Chris Lattner2c41d482009-03-29 17:18:04 +0000368 Diag(Tok, diag::err_expected_semi_declation);
369 // Skip to end of block or statement
370 SkipUntil(tok::r_brace, true, true);
371 if (Tok.is(tok::semi))
372 ConsumeToken();
373 return DG;
Chris Lattner4b009652007-07-25 00:24:17 +0000374}
375
Douglas Gregore3298aa2009-05-12 21:31:51 +0000376/// \brief Parse 'declaration' after parsing 'declaration-specifiers
377/// declarator'. This method parses the remainder of the declaration
378/// (including any attributes or initializer, among other things) and
379/// finalizes the declaration.
Chris Lattner4b009652007-07-25 00:24:17 +0000380///
Chris Lattner4b009652007-07-25 00:24:17 +0000381/// init-declarator: [C99 6.7]
382/// declarator
383/// declarator '=' initializer
384/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
385/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000386/// [C++] declarator initializer[opt]
387///
388/// [C++] initializer:
389/// [C++] '=' initializer-clause
390/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000391/// [C++0x] '=' 'default' [TODO]
392/// [C++0x] '=' 'delete'
393///
394/// According to the standard grammar, =default and =delete are function
395/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000396///
Douglas Gregor2ae1d772009-06-23 23:11:28 +0000397Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
398 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregore3298aa2009-05-12 21:31:51 +0000399 // If a simple-asm-expr is present, parse it.
400 if (Tok.is(tok::kw_asm)) {
401 SourceLocation Loc;
402 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
403 if (AsmLabel.isInvalid()) {
404 SkipUntil(tok::semi, true, true);
405 return DeclPtrTy();
406 }
407
408 D.setAsmLabel(AsmLabel.release());
409 D.SetRangeEnd(Loc);
410 }
411
412 // If attributes are present, parse them.
413 if (Tok.is(tok::kw___attribute)) {
414 SourceLocation Loc;
415 AttributeList *AttrList = ParseAttributes(&Loc);
416 D.AddAttributes(AttrList, Loc);
417 }
418
419 // Inform the current actions module that we just parsed this declarator.
Douglas Gregor2ae1d772009-06-23 23:11:28 +0000420 DeclPtrTy ThisDecl = TemplateInfo.TemplateParams?
421 Actions.ActOnTemplateDeclarator(CurScope,
422 Action::MultiTemplateParamsArg(Actions,
423 TemplateInfo.TemplateParams->data(),
424 TemplateInfo.TemplateParams->size()),
425 D)
426 : Actions.ActOnDeclarator(CurScope, D);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000427
428 // Parse declarator '=' initializer.
429 if (Tok.is(tok::equal)) {
430 ConsumeToken();
431 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
432 SourceLocation DelLoc = ConsumeToken();
433 Actions.SetDeclDeleted(ThisDecl, DelLoc);
434 } else {
Argiris Kirtzidis68370592009-06-17 22:50:06 +0000435 if (getLang().CPlusPlus)
436 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
437
Douglas Gregore3298aa2009-05-12 21:31:51 +0000438 OwningExprResult Init(ParseInitializer());
Argiris Kirtzidis68370592009-06-17 22:50:06 +0000439
440 if (getLang().CPlusPlus)
441 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
442
Douglas Gregore3298aa2009-05-12 21:31:51 +0000443 if (Init.isInvalid()) {
444 SkipUntil(tok::semi, true, true);
445 return DeclPtrTy();
446 }
Anders Carlssonf9f05b82009-05-30 21:37:25 +0000447 Actions.AddInitializerToDecl(ThisDecl, Actions.FullExpr(Init));
Douglas Gregore3298aa2009-05-12 21:31:51 +0000448 }
449 } else if (Tok.is(tok::l_paren)) {
450 // Parse C++ direct initializer: '(' expression-list ')'
451 SourceLocation LParenLoc = ConsumeParen();
452 ExprVector Exprs(Actions);
453 CommaLocsTy CommaLocs;
454
455 if (ParseExpressionList(Exprs, CommaLocs)) {
456 SkipUntil(tok::r_paren);
457 } else {
458 // Match the ')'.
459 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
460
461 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
462 "Unexpected number of commas!");
463 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
464 move_arg(Exprs),
Jay Foad9e6bef42009-05-21 09:52:38 +0000465 CommaLocs.data(), RParenLoc);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000466 }
467 } else {
468 Actions.ActOnUninitializedDecl(ThisDecl);
469 }
470
471 return ThisDecl;
472}
473
474/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
475/// parsing 'declaration-specifiers declarator'. This method is split out this
476/// way to handle the ambiguity between top-level function-definitions and
477/// declarations.
478///
479/// init-declarator-list: [C99 6.7]
480/// init-declarator
481/// init-declarator-list ',' init-declarator
482///
483/// According to the standard grammar, =default and =delete are function
484/// definitions, but that definitely doesn't fit with the parser here.
485///
Chris Lattnera17991f2009-03-29 16:50:03 +0000486Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000487ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000488 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
489 // that we parse together here.
490 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000491
492 // At this point, we know that it is not a function definition. Parse the
493 // rest of the init-declarator-list.
494 while (1) {
Douglas Gregore3298aa2009-05-12 21:31:51 +0000495 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
496 if (ThisDecl.get())
497 DeclsInGroup.push_back(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000498
Chris Lattner4b009652007-07-25 00:24:17 +0000499 // If we don't have a comma, it is either the end of the list (a ';') or an
500 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000501 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000502 break;
503
504 // Consume the comma.
505 ConsumeToken();
506
507 // Parse the next declarator.
508 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000509
510 // Accept attributes in an init-declarator. In the first declarator in a
511 // declaration, these would be part of the declspec. In subsequent
512 // declarators, they become part of the declarator itself, so that they
513 // don't apply to declarators after *this* one. Examples:
514 // short __attribute__((common)) var; -> declspec
515 // short var __attribute__((common)); -> declarator
516 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000517 if (Tok.is(tok::kw___attribute)) {
518 SourceLocation Loc;
519 AttributeList *AttrList = ParseAttributes(&Loc);
520 D.AddAttributes(AttrList, Loc);
521 }
Chris Lattner926cf542008-10-20 04:57:38 +0000522
Chris Lattner4b009652007-07-25 00:24:17 +0000523 ParseDeclarator(D);
524 }
525
Eli Friedman4d57af22009-05-29 01:49:24 +0000526 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
527 DeclsInGroup.data(),
Chris Lattner2c41d482009-03-29 17:18:04 +0000528 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000529}
530
531/// ParseSpecifierQualifierList
532/// specifier-qualifier-list:
533/// type-specifier specifier-qualifier-list[opt]
534/// type-qualifier specifier-qualifier-list[opt]
535/// [GNU] attributes specifier-qualifier-list[opt]
536///
537void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
538 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
539 /// parse declaration-specifiers and complain about extra stuff.
540 ParseDeclarationSpecifiers(DS);
541
542 // Validate declspec for type-name.
543 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera52aec42009-04-14 21:16:09 +0000544 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
545 !DS.getAttributes())
Chris Lattner4b009652007-07-25 00:24:17 +0000546 Diag(Tok, diag::err_typename_requires_specqual);
547
548 // Issue diagnostic and remove storage class if present.
549 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
550 if (DS.getStorageClassSpecLoc().isValid())
551 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
552 else
553 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
554 DS.ClearStorageClassSpecs();
555 }
556
557 // Issue diagnostic and remove function specfier if present.
558 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000559 if (DS.isInlineSpecified())
560 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
561 if (DS.isVirtualSpecified())
562 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
563 if (DS.isExplicitSpecified())
564 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000565 DS.ClearFunctionSpecs();
566 }
567}
568
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000569/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
570/// specified token is valid after the identifier in a declarator which
571/// immediately follows the declspec. For example, these things are valid:
572///
573/// int x [ 4]; // direct-declarator
574/// int x ( int y); // direct-declarator
575/// int(int x ) // direct-declarator
576/// int x ; // simple-declaration
577/// int x = 17; // init-declarator-list
578/// int x , y; // init-declarator-list
579/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera52aec42009-04-14 21:16:09 +0000580/// int x : 4; // struct-declarator
Chris Lattnerca6cc362009-04-12 22:29:43 +0000581/// int x { 5}; // C++'0x unified initializers
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000582///
583/// This is not, because 'x' does not immediately follow the declspec (though
584/// ')' happens to be valid anyway).
585/// int (x)
586///
587static bool isValidAfterIdentifierInDeclarator(const Token &T) {
588 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
589 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera52aec42009-04-14 21:16:09 +0000590 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000591}
592
Chris Lattner82353c62009-04-14 21:34:55 +0000593
594/// ParseImplicitInt - This method is called when we have an non-typename
595/// identifier in a declspec (which normally terminates the decl spec) when
596/// the declspec has no type specifier. In this case, the declspec is either
597/// malformed or is "implicit int" (in K&R and C89).
598///
599/// This method handles diagnosing this prettily and returns false if the
600/// declspec is done being processed. If it recovers and thinks there may be
601/// other pieces of declspec after it, it returns true.
602///
Chris Lattner52cd7622009-04-14 22:17:06 +0000603bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000604 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner82353c62009-04-14 21:34:55 +0000605 AccessSpecifier AS) {
Chris Lattner52cd7622009-04-14 22:17:06 +0000606 assert(Tok.is(tok::identifier) && "should have identifier");
607
Chris Lattner82353c62009-04-14 21:34:55 +0000608 SourceLocation Loc = Tok.getLocation();
609 // If we see an identifier that is not a type name, we normally would
610 // parse it as the identifer being declared. However, when a typename
611 // is typo'd or the definition is not included, this will incorrectly
612 // parse the typename as the identifier name and fall over misparsing
613 // later parts of the diagnostic.
614 //
615 // As such, we try to do some look-ahead in cases where this would
616 // otherwise be an "implicit-int" case to see if this is invalid. For
617 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
618 // an identifier with implicit int, we'd get a parse error because the
619 // next token is obviously invalid for a type. Parse these as a case
620 // with an invalid type specifier.
621 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
622
623 // Since we know that this either implicit int (which is rare) or an
624 // error, we'd do lookahead to try to do better recovery.
625 if (isValidAfterIdentifierInDeclarator(NextToken())) {
626 // If this token is valid for implicit int, e.g. "static x = 4", then
627 // we just avoid eating the identifier, so it will be parsed as the
628 // identifier in the declarator.
629 return false;
630 }
631
632 // Otherwise, if we don't consume this token, we are going to emit an
633 // error anyway. Try to recover from various common problems. Check
634 // to see if this was a reference to a tag name without a tag specified.
635 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattner52cd7622009-04-14 22:17:06 +0000636 //
637 // C++ doesn't need this, and isTagName doesn't take SS.
638 if (SS == 0) {
639 const char *TagName = 0;
640 tok::TokenKind TagKind = tok::unknown;
Chris Lattner82353c62009-04-14 21:34:55 +0000641
Chris Lattner82353c62009-04-14 21:34:55 +0000642 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
643 default: break;
644 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
645 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
646 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
647 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
648 }
Chris Lattner82353c62009-04-14 21:34:55 +0000649
Chris Lattner52cd7622009-04-14 22:17:06 +0000650 if (TagName) {
651 Diag(Loc, diag::err_use_of_tag_name_without_tag)
652 << Tok.getIdentifierInfo() << TagName
653 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
654
655 // Parse this as a tag as if the missing tag were present.
656 if (TagKind == tok::kw_enum)
657 ParseEnumSpecifier(Loc, DS, AS);
658 else
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000659 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattner52cd7622009-04-14 22:17:06 +0000660 return true;
661 }
Chris Lattner82353c62009-04-14 21:34:55 +0000662 }
663
664 // Since this is almost certainly an invalid type name, emit a
665 // diagnostic that says it, eat the token, and mark the declspec as
666 // invalid.
Chris Lattner52cd7622009-04-14 22:17:06 +0000667 SourceRange R;
668 if (SS) R = SS->getRange();
669
670 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattner82353c62009-04-14 21:34:55 +0000671 const char *PrevSpec;
672 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
673 DS.SetRangeEnd(Tok.getLocation());
674 ConsumeToken();
675
676 // TODO: Could inject an invalid typedef decl in an enclosing scope to
677 // avoid rippling error messages on subsequent uses of the same type,
678 // could be useful if #include was forgotten.
679 return false;
680}
681
Chris Lattner4b009652007-07-25 00:24:17 +0000682/// ParseDeclarationSpecifiers
683/// declaration-specifiers: [C99 6.7]
684/// storage-class-specifier declaration-specifiers[opt]
685/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000686/// [C99] function-specifier declaration-specifiers[opt]
687/// [GNU] attributes declaration-specifiers[opt]
688///
689/// storage-class-specifier: [C99 6.7.1]
690/// 'typedef'
691/// 'extern'
692/// 'static'
693/// 'auto'
694/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000695/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000696/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000697/// function-specifier: [C99 6.7.4]
698/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000699/// [C++] 'virtual'
700/// [C++] 'explicit'
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000701/// 'friend': [C++ dcl.friend]
702
Chris Lattner4b009652007-07-25 00:24:17 +0000703///
Douglas Gregor52473432008-12-24 02:52:09 +0000704void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000705 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000706 AccessSpecifier AS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000707 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000708 while (1) {
709 int isInvalid = false;
710 const char *PrevSpec = 0;
711 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000712
Chris Lattner4b009652007-07-25 00:24:17 +0000713 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000714 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000715 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000716 // If this is not a declaration specifier token, we're done reading decl
717 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000718 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000719 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000720
721 case tok::coloncolon: // ::foo::bar
722 // Annotate C++ scope specifiers. If we get one, loop.
723 if (TryAnnotateCXXScopeToken())
724 continue;
725 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000726
727 case tok::annot_cxxscope: {
728 if (DS.hasTypeSpecifier())
729 goto DoneWithDeclSpec;
730
731 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000732 Token Next = NextToken();
733 if (Next.is(tok::annot_template_id) &&
734 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000735 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000736 // We have a qualified template-id, e.g., N::A<int>
737 CXXScopeSpec SS;
738 ParseOptionalCXXScopeSpecifier(SS);
739 assert(Tok.is(tok::annot_template_id) &&
740 "ParseOptionalCXXScopeSpecifier not working");
741 AnnotateTemplateIdTokenAsType(&SS);
742 continue;
743 }
744
745 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000746 goto DoneWithDeclSpec;
747
748 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000749 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000750 SS.setRange(Tok.getAnnotationRange());
751
752 // If the next token is the name of the class type that the C++ scope
753 // denotes, followed by a '(', then this is a constructor declaration.
754 // We're done with the decl-specifiers.
Chris Lattner52cd7622009-04-14 22:17:06 +0000755 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000756 CurScope, &SS) &&
757 GetLookAheadToken(2).is(tok::l_paren))
758 goto DoneWithDeclSpec;
759
Douglas Gregor1075a162009-02-04 17:00:24 +0000760 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
761 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000762
Chris Lattner52cd7622009-04-14 22:17:06 +0000763 // If the referenced identifier is not a type, then this declspec is
764 // erroneous: We already checked about that it has no type specifier, and
765 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
766 // typename.
767 if (TypeRep == 0) {
768 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000769 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000770 goto DoneWithDeclSpec;
Chris Lattner52cd7622009-04-14 22:17:06 +0000771 }
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000772
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000773 ConsumeToken(); // The C++ scope.
774
Douglas Gregora60c62e2009-02-09 15:09:02 +0000775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000776 TypeRep);
777 if (isInvalid)
778 break;
779
780 DS.SetRangeEnd(Tok.getLocation());
781 ConsumeToken(); // The typename.
782
783 continue;
784 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000785
786 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000787 if (Tok.getAnnotationValue())
788 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
789 Tok.getAnnotationValue());
790 else
791 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000792 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
793 ConsumeToken(); // The typename
794
795 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
796 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
797 // Objective-C interface. If we don't have Objective-C or a '<', this is
798 // just a normal reference to a typedef name.
799 if (!Tok.is(tok::less) || !getLang().ObjC1)
800 continue;
801
802 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000803 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000804 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
805 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
806
807 DS.SetRangeEnd(EndProtoLoc);
808 continue;
809 }
810
Chris Lattnerfda18db2008-07-26 01:18:38 +0000811 // typedef-name
812 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000813 // In C++, check to see if this is a scope specifier like foo::bar::, if
814 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000815 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
816 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000817
Chris Lattnerfda18db2008-07-26 01:18:38 +0000818 // This identifier can only be a typedef name if we haven't already seen
819 // a type-specifier. Without this check we misparse:
820 // typedef int X; struct Y { short X; }; as 'short int'.
821 if (DS.hasTypeSpecifier())
822 goto DoneWithDeclSpec;
823
824 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000825 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
826 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000827
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000828 // If this is not a typedef name, don't parse it as part of the declspec,
829 // it must be an implicit int or an error.
830 if (TypeRep == 0) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000831 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000832 goto DoneWithDeclSpec;
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000833 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000834
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000835 // C++: If the identifier is actually the name of the class type
836 // being defined and the next token is a '(', then this is a
837 // constructor declaration. We're done with the decl-specifiers
838 // and will treat this token as an identifier.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000839 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000840 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
841 NextToken().getKind() == tok::l_paren)
842 goto DoneWithDeclSpec;
843
Douglas Gregora60c62e2009-02-09 15:09:02 +0000844 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000845 TypeRep);
846 if (isInvalid)
847 break;
848
849 DS.SetRangeEnd(Tok.getLocation());
850 ConsumeToken(); // The identifier
851
852 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
853 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
854 // Objective-C interface. If we don't have Objective-C or a '<', this is
855 // just a normal reference to a typedef name.
856 if (!Tok.is(tok::less) || !getLang().ObjC1)
857 continue;
858
859 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000860 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000861 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000862 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000863
864 DS.SetRangeEnd(EndProtoLoc);
865
Steve Narofff7683302008-09-22 10:28:57 +0000866 // Need to support trailing type qualifiers (e.g. "id<p> const").
867 // If a type specifier follows, it will be diagnosed elsewhere.
868 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000869 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000870
871 // type-name
872 case tok::annot_template_id: {
873 TemplateIdAnnotation *TemplateId
874 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000875 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000876 // This template-id does not refer to a type name, so we're
877 // done with the type-specifiers.
878 goto DoneWithDeclSpec;
879 }
880
881 // Turn the template-id annotation token into a type annotation
882 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000883 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000884 continue;
885 }
886
Chris Lattner4b009652007-07-25 00:24:17 +0000887 // GNU attributes support.
888 case tok::kw___attribute:
889 DS.AddAttributes(ParseAttributes());
890 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000891
892 // Microsoft declspec support.
893 case tok::kw___declspec:
Eli Friedmancd231842009-06-08 07:21:15 +0000894 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000895 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000896
Steve Naroffedd04d52008-12-25 14:16:32 +0000897 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000898 case tok::kw___forceinline:
Eli Friedman891d82f2009-06-08 23:27:34 +0000899 // FIXME: Add handling here!
900 break;
901
902 case tok::kw___ptr64:
Steve Naroffad620402008-12-25 14:41:26 +0000903 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000904 case tok::kw___cdecl:
905 case tok::kw___stdcall:
906 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +0000907 DS.AddAttributes(ParseMicrosoftTypeAttributes());
908 continue;
909
Chris Lattner4b009652007-07-25 00:24:17 +0000910 // storage-class-specifier
911 case tok::kw_typedef:
912 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
913 break;
914 case tok::kw_extern:
915 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000916 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000917 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
918 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000919 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000920 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
921 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000922 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000923 case tok::kw_static:
924 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000925 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000926 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
927 break;
928 case tok::kw_auto:
929 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
930 break;
931 case tok::kw_register:
932 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
933 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000934 case tok::kw_mutable:
935 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
936 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000937 case tok::kw___thread:
938 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
939 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000940
Chris Lattner4b009652007-07-25 00:24:17 +0000941 // function-specifier
942 case tok::kw_inline:
943 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
944 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000945 case tok::kw_virtual:
946 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
947 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000948 case tok::kw_explicit:
949 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
950 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000951
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000952 // friend
953 case tok::kw_friend:
954 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
955 break;
956
Chris Lattnerc297b722009-01-21 19:48:37 +0000957 // type-specifier
958 case tok::kw_short:
959 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
960 break;
961 case tok::kw_long:
962 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
963 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
964 else
965 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
966 break;
967 case tok::kw_signed:
968 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
969 break;
970 case tok::kw_unsigned:
971 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
972 break;
973 case tok::kw__Complex:
974 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
975 break;
976 case tok::kw__Imaginary:
977 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
978 break;
979 case tok::kw_void:
980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
981 break;
982 case tok::kw_char:
983 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
984 break;
985 case tok::kw_int:
986 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
987 break;
988 case tok::kw_float:
989 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
990 break;
991 case tok::kw_double:
992 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
993 break;
994 case tok::kw_wchar_t:
995 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
996 break;
997 case tok::kw_bool:
998 case tok::kw__Bool:
999 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1000 break;
1001 case tok::kw__Decimal32:
1002 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1003 break;
1004 case tok::kw__Decimal64:
1005 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1006 break;
1007 case tok::kw__Decimal128:
1008 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1009 break;
1010
1011 // class-specifier:
1012 case tok::kw_class:
1013 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001014 case tok::kw_union: {
1015 tok::TokenKind Kind = Tok.getKind();
1016 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001017 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +00001018 continue;
Chris Lattner197b4342009-04-12 21:49:30 +00001019 }
Chris Lattnerc297b722009-01-21 19:48:37 +00001020
1021 // enum-specifier:
1022 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001023 ConsumeToken();
1024 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +00001025 continue;
1026
1027 // cv-qualifier:
1028 case tok::kw_const:
1029 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
1030 break;
1031 case tok::kw_volatile:
1032 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1033 getLang())*2;
1034 break;
1035 case tok::kw_restrict:
1036 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1037 getLang())*2;
1038 break;
1039
Douglas Gregord3022602009-03-27 23:10:48 +00001040 // C++ typename-specifier:
1041 case tok::kw_typename:
1042 if (TryAnnotateTypeOrScopeToken())
1043 continue;
1044 break;
1045
Chris Lattnerc297b722009-01-21 19:48:37 +00001046 // GNU typeof support.
1047 case tok::kw_typeof:
1048 ParseTypeofSpecifier(DS);
1049 continue;
1050
Anders Carlssoneed418b2009-06-24 17:47:40 +00001051 case tok::kw_decltype:
1052 ParseDecltypeSpecifier(DS);
1053 continue;
1054
Steve Naroff5f0466b2008-06-05 00:02:44 +00001055 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +00001056 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +00001057 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1058 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +00001059 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +00001060 goto DoneWithDeclSpec;
1061
1062 {
1063 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001064 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +00001065 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +00001066 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +00001067 DS.SetRangeEnd(EndProtoLoc);
1068
Chris Lattnerf006a222008-11-18 07:48:38 +00001069 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +00001070 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +00001071 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +00001072 // Need to support trailing type qualifiers (e.g. "id<p> const").
1073 // If a type specifier follows, it will be diagnosed elsewhere.
1074 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +00001075 }
Chris Lattner4b009652007-07-25 00:24:17 +00001076 }
1077 // If the specifier combination wasn't legal, issue a diagnostic.
1078 if (isInvalid) {
1079 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001080 // Pick between error or extwarn.
1081 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1082 : diag::ext_duplicate_declspec;
1083 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001084 }
Chris Lattnera4ff4272008-03-13 06:29:04 +00001085 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001086 ConsumeToken();
1087 }
1088}
Douglas Gregorb3bec712008-12-01 23:54:00 +00001089
Chris Lattnerd706dc82009-01-06 06:59:53 +00001090/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001091/// primarily follow the C++ grammar with additions for C99 and GNU,
1092/// which together subsume the C grammar. Note that the C++
1093/// type-specifier also includes the C type-qualifier (for const,
1094/// volatile, and C99 restrict). Returns true if a type-specifier was
1095/// found (and parsed), false otherwise.
1096///
1097/// type-specifier: [C++ 7.1.5]
1098/// simple-type-specifier
1099/// class-specifier
1100/// enum-specifier
1101/// elaborated-type-specifier [TODO]
1102/// cv-qualifier
1103///
1104/// cv-qualifier: [C++ 7.1.5.1]
1105/// 'const'
1106/// 'volatile'
1107/// [C99] 'restrict'
1108///
1109/// simple-type-specifier: [ C++ 7.1.5.2]
1110/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1111/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1112/// 'char'
1113/// 'wchar_t'
1114/// 'bool'
1115/// 'short'
1116/// 'int'
1117/// 'long'
1118/// 'signed'
1119/// 'unsigned'
1120/// 'float'
1121/// 'double'
1122/// 'void'
1123/// [C99] '_Bool'
1124/// [C99] '_Complex'
1125/// [C99] '_Imaginary' // Removed in TC2?
1126/// [GNU] '_Decimal32'
1127/// [GNU] '_Decimal64'
1128/// [GNU] '_Decimal128'
1129/// [GNU] typeof-specifier
1130/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1131/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlssoneed418b2009-06-24 17:47:40 +00001132/// [C++0x] 'decltype' ( expression )
Chris Lattnerd706dc82009-01-06 06:59:53 +00001133bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1134 const char *&PrevSpec,
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001135 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001136 SourceLocation Loc = Tok.getLocation();
1137
1138 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +00001139 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001140 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +00001141 // Annotate typenames and C++ scope specifiers. If we get one, just
1142 // recurse to handle whatever we get.
1143 if (TryAnnotateTypeOrScopeToken())
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001144 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001145 // Otherwise, not a type specifier.
1146 return false;
1147 case tok::coloncolon: // ::foo::bar
1148 if (NextToken().is(tok::kw_new) || // ::new
1149 NextToken().is(tok::kw_delete)) // ::delete
1150 return false;
1151
1152 // Annotate typenames and C++ scope specifiers. If we get one, just
1153 // recurse to handle whatever we get.
1154 if (TryAnnotateTypeOrScopeToken())
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001155 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001156 // Otherwise, not a type specifier.
1157 return false;
1158
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001159 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +00001160 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +00001161 if (Tok.getAnnotationValue())
1162 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1163 Tok.getAnnotationValue());
1164 else
1165 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001166 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1167 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001168
1169 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1170 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1171 // Objective-C interface. If we don't have Objective-C or a '<', this is
1172 // just a normal reference to a typedef name.
1173 if (!Tok.is(tok::less) || !getLang().ObjC1)
1174 return true;
1175
1176 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001177 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001178 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1179 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1180
1181 DS.SetRangeEnd(EndProtoLoc);
1182 return true;
1183 }
1184
1185 case tok::kw_short:
1186 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1187 break;
1188 case tok::kw_long:
1189 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1190 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1191 else
1192 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1193 break;
1194 case tok::kw_signed:
1195 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1196 break;
1197 case tok::kw_unsigned:
1198 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1199 break;
1200 case tok::kw__Complex:
1201 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1202 break;
1203 case tok::kw__Imaginary:
1204 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1205 break;
1206 case tok::kw_void:
1207 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1208 break;
1209 case tok::kw_char:
1210 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1211 break;
1212 case tok::kw_int:
1213 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1214 break;
1215 case tok::kw_float:
1216 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1217 break;
1218 case tok::kw_double:
1219 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1220 break;
1221 case tok::kw_wchar_t:
1222 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1223 break;
1224 case tok::kw_bool:
1225 case tok::kw__Bool:
1226 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1227 break;
1228 case tok::kw__Decimal32:
1229 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1230 break;
1231 case tok::kw__Decimal64:
1232 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1233 break;
1234 case tok::kw__Decimal128:
1235 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1236 break;
1237
1238 // class-specifier:
1239 case tok::kw_class:
1240 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001241 case tok::kw_union: {
1242 tok::TokenKind Kind = Tok.getKind();
1243 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001244 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001245 return true;
Chris Lattner197b4342009-04-12 21:49:30 +00001246 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001247
1248 // enum-specifier:
1249 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001250 ConsumeToken();
1251 ParseEnumSpecifier(Loc, DS);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001252 return true;
1253
1254 // cv-qualifier:
1255 case tok::kw_const:
1256 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1257 getLang())*2;
1258 break;
1259 case tok::kw_volatile:
1260 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1261 getLang())*2;
1262 break;
1263 case tok::kw_restrict:
1264 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1265 getLang())*2;
1266 break;
1267
1268 // GNU typeof support.
1269 case tok::kw_typeof:
1270 ParseTypeofSpecifier(DS);
1271 return true;
1272
Anders Carlssoneed418b2009-06-24 17:47:40 +00001273 // C++0x decltype support.
1274 case tok::kw_decltype:
1275 ParseDecltypeSpecifier(DS);
1276 return true;
1277
Eli Friedman891d82f2009-06-08 23:27:34 +00001278 case tok::kw___ptr64:
1279 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001280 case tok::kw___cdecl:
1281 case tok::kw___stdcall:
1282 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001283 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner5bb837e2009-01-21 19:19:26 +00001284 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001285
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001286 default:
1287 // Not a type-specifier; do nothing.
1288 return false;
1289 }
1290
1291 // If the specifier combination wasn't legal, issue a diagnostic.
1292 if (isInvalid) {
1293 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001294 // Pick between error or extwarn.
1295 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1296 : diag::ext_duplicate_declspec;
1297 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001298 }
1299 DS.SetRangeEnd(Tok.getLocation());
1300 ConsumeToken(); // whatever we parsed above.
1301 return true;
1302}
Chris Lattner4b009652007-07-25 00:24:17 +00001303
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001304/// ParseStructDeclaration - Parse a struct declaration without the terminating
1305/// semicolon.
1306///
Chris Lattner4b009652007-07-25 00:24:17 +00001307/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001308/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001309/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001310/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001311/// struct-declarator-list:
1312/// struct-declarator
1313/// struct-declarator-list ',' struct-declarator
1314/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1315/// struct-declarator:
1316/// declarator
1317/// [GNU] declarator attributes[opt]
1318/// declarator[opt] ':' constant-expression
1319/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1320///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001321void Parser::
1322ParseStructDeclaration(DeclSpec &DS,
1323 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001324 if (Tok.is(tok::kw___extension__)) {
1325 // __extension__ silences extension warnings in the subexpression.
1326 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001327 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001328 return ParseStructDeclaration(DS, Fields);
1329 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001330
1331 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001332 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001333 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001334
Douglas Gregorb748fc52009-01-12 22:49:06 +00001335 // If there are no declarators, this is a free-standing declaration
1336 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001337 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001338 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001339 return;
1340 }
1341
1342 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001343 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001344 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001345 FieldDeclarator &DeclaratorInfo = Fields.back();
1346
Steve Naroffa9adf112007-08-20 22:28:22 +00001347 /// struct-declarator: declarator
1348 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001349 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001350 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001351
Chris Lattner34a01ad2007-10-09 17:33:22 +00001352 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001353 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001354 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001355 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001356 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001357 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001358 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001359 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001360
Steve Naroffa9adf112007-08-20 22:28:22 +00001361 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001362 if (Tok.is(tok::kw___attribute)) {
1363 SourceLocation Loc;
1364 AttributeList *AttrList = ParseAttributes(&Loc);
1365 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1366 }
1367
Steve Naroffa9adf112007-08-20 22:28:22 +00001368 // If we don't have a comma, it is either the end of the list (a ';')
1369 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001370 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001371 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001372
Steve Naroffa9adf112007-08-20 22:28:22 +00001373 // Consume the comma.
1374 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001375
Steve Naroffa9adf112007-08-20 22:28:22 +00001376 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001377 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001378
Steve Naroffa9adf112007-08-20 22:28:22 +00001379 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001380 if (Tok.is(tok::kw___attribute)) {
1381 SourceLocation Loc;
1382 AttributeList *AttrList = ParseAttributes(&Loc);
1383 Fields.back().D.AddAttributes(AttrList, Loc);
1384 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001385 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001386}
1387
1388/// ParseStructUnionBody
1389/// struct-contents:
1390/// struct-declaration-list
1391/// [EXT] empty
1392/// [GNU] "struct-declaration-list" without terminatoring ';'
1393/// struct-declaration-list:
1394/// struct-declaration
1395/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001396/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001397///
Chris Lattner4b009652007-07-25 00:24:17 +00001398void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001399 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001400 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1401 PP.getSourceManager(),
1402 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001403
Chris Lattner4b009652007-07-25 00:24:17 +00001404 SourceLocation LBraceLoc = ConsumeBrace();
1405
Douglas Gregorcab994d2009-01-09 22:42:13 +00001406 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001407 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1408
Chris Lattner4b009652007-07-25 00:24:17 +00001409 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1410 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001411 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001412 Diag(Tok, diag::ext_empty_struct_union_enum)
1413 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001414
Chris Lattner5261d0c2009-03-28 19:18:32 +00001415 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001416 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1417
Chris Lattner4b009652007-07-25 00:24:17 +00001418 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001419 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001420 // Each iteration of this loop reads one struct-declaration.
1421
1422 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001423 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001424 Diag(Tok, diag::ext_extra_struct_semi)
1425 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001426 ConsumeToken();
1427 continue;
1428 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001429
1430 // Parse all the comma separated declarators.
1431 DeclSpec DS;
1432 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001433 if (!Tok.is(tok::at)) {
1434 ParseStructDeclaration(DS, FieldDeclarators);
1435
1436 // Convert them all to fields.
1437 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1438 FieldDeclarator &FD = FieldDeclarators[i];
1439 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001440 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1441 DS.getSourceRange().getBegin(),
1442 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001443 FieldDecls.push_back(Field);
1444 }
1445 } else { // Handle @defs
1446 ConsumeToken();
1447 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1448 Diag(Tok, diag::err_unexpected_at);
1449 SkipUntil(tok::semi, true, true);
1450 continue;
1451 }
1452 ConsumeToken();
1453 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1454 if (!Tok.is(tok::identifier)) {
1455 Diag(Tok, diag::err_expected_ident);
1456 SkipUntil(tok::semi, true, true);
1457 continue;
1458 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001459 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001460 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1461 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001462 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1463 ConsumeToken();
1464 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1465 }
Chris Lattner4b009652007-07-25 00:24:17 +00001466
Chris Lattner34a01ad2007-10-09 17:33:22 +00001467 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001468 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001469 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001470 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001471 break;
1472 } else {
1473 Diag(Tok, diag::err_expected_semi_decl_list);
1474 // Skip to end of block or statement
1475 SkipUntil(tok::r_brace, true, true);
1476 }
1477 }
1478
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001479 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001480
Chris Lattner4b009652007-07-25 00:24:17 +00001481 AttributeList *AttrList = 0;
1482 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001483 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001484 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001485
1486 Actions.ActOnFields(CurScope,
Jay Foad9e6bef42009-05-21 09:52:38 +00001487 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +00001488 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001489 AttrList);
1490 StructScope.Exit();
1491 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001492}
1493
1494
1495/// ParseEnumSpecifier
1496/// enum-specifier: [C99 6.7.2.2]
1497/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001498///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001499/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1500/// '}' attributes[opt]
1501/// 'enum' identifier
1502/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001503///
1504/// [C++] elaborated-type-specifier:
1505/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1506///
Chris Lattner197b4342009-04-12 21:49:30 +00001507void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1508 AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +00001509 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001510
1511 AttributeList *Attr = 0;
1512 // If attributes exist after tag, parse them.
1513 if (Tok.is(tok::kw___attribute))
1514 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001515
1516 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001517 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001518 if (Tok.isNot(tok::identifier)) {
1519 Diag(Tok, diag::err_expected_ident);
1520 if (Tok.isNot(tok::l_brace)) {
1521 // Has no name and is not a definition.
1522 // Skip the rest of this declarator, up until the comma or semicolon.
1523 SkipUntil(tok::comma, true);
1524 return;
1525 }
1526 }
1527 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001528
1529 // Must have either 'enum name' or 'enum {...}'.
1530 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1531 Diag(Tok, diag::err_expected_ident_lbrace);
1532
1533 // Skip the rest of this declarator, up until the comma or semicolon.
1534 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001535 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001536 }
1537
1538 // If an identifier is present, consume and remember it.
1539 IdentifierInfo *Name = 0;
1540 SourceLocation NameLoc;
1541 if (Tok.is(tok::identifier)) {
1542 Name = Tok.getIdentifierInfo();
1543 NameLoc = ConsumeToken();
1544 }
1545
1546 // There are three options here. If we have 'enum foo;', then this is a
1547 // forward declaration. If we have 'enum foo {...' then this is a
1548 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1549 //
1550 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1551 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1552 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1553 //
1554 Action::TagKind TK;
1555 if (Tok.is(tok::l_brace))
1556 TK = Action::TK_Definition;
1557 else if (Tok.is(tok::semi))
1558 TK = Action::TK_Declaration;
1559 else
1560 TK = Action::TK_Reference;
Douglas Gregor71f06032009-05-28 23:31:59 +00001561 bool Owned = false;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001562 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
Douglas Gregor71f06032009-05-28 23:31:59 +00001563 StartLoc, SS, Name, NameLoc, Attr, AS,
1564 Owned);
Chris Lattner4b009652007-07-25 00:24:17 +00001565
Chris Lattner34a01ad2007-10-09 17:33:22 +00001566 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001567 ParseEnumBody(StartLoc, TagDecl);
1568
1569 // TODO: semantic analysis on the declspec for enums.
1570 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001571 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
Douglas Gregor71f06032009-05-28 23:31:59 +00001572 TagDecl.getAs<void>(), Owned))
Chris Lattnerf006a222008-11-18 07:48:38 +00001573 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001574}
1575
1576/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1577/// enumerator-list:
1578/// enumerator
1579/// enumerator-list ',' enumerator
1580/// enumerator:
1581/// enumeration-constant
1582/// enumeration-constant '=' constant-expression
1583/// enumeration-constant:
1584/// identifier
1585///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001586void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001587 // Enter the scope of the enum body and start the definition.
1588 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001589 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001590
Chris Lattner4b009652007-07-25 00:24:17 +00001591 SourceLocation LBraceLoc = ConsumeBrace();
1592
Chris Lattnerc9a92452007-08-27 17:24:30 +00001593 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001594 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001595 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001596
Chris Lattner5261d0c2009-03-28 19:18:32 +00001597 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001598
Chris Lattner5261d0c2009-03-28 19:18:32 +00001599 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001600
1601 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001602 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001603 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1604 SourceLocation IdentLoc = ConsumeToken();
1605
1606 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001607 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001608 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001609 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001610 AssignedVal = ParseConstantExpression();
1611 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001612 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001613 }
1614
1615 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001616 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1617 LastEnumConstDecl,
1618 IdentLoc, Ident,
1619 EqualLoc,
1620 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001621 EnumConstantDecls.push_back(EnumConstDecl);
1622 LastEnumConstDecl = EnumConstDecl;
1623
Chris Lattner34a01ad2007-10-09 17:33:22 +00001624 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001625 break;
1626 SourceLocation CommaLoc = ConsumeToken();
1627
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001628 if (Tok.isNot(tok::identifier) &&
1629 !(getLang().C99 || getLang().CPlusPlus0x))
1630 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1631 << getLang().CPlusPlus
1632 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001633 }
1634
1635 // Eat the }.
Mike Stump155750e2009-05-16 07:06:02 +00001636 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001637
Mike Stump155750e2009-05-16 07:06:02 +00001638 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
Jay Foad9e6bef42009-05-21 09:52:38 +00001639 EnumConstantDecls.data(), EnumConstantDecls.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001640
Chris Lattner5261d0c2009-03-28 19:18:32 +00001641 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001642 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001643 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001644 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001645
1646 EnumScope.Exit();
1647 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001648}
1649
1650/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001651/// start of a type-qualifier-list.
1652bool Parser::isTypeQualifier() const {
1653 switch (Tok.getKind()) {
1654 default: return false;
1655 // type-qualifier
1656 case tok::kw_const:
1657 case tok::kw_volatile:
1658 case tok::kw_restrict:
1659 return true;
1660 }
1661}
1662
1663/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001664/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001665bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001666 switch (Tok.getKind()) {
1667 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001668
1669 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001670 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001671 // Annotate typenames and C++ scope specifiers. If we get one, just
1672 // recurse to handle whatever we get.
1673 if (TryAnnotateTypeOrScopeToken())
1674 return isTypeSpecifierQualifier();
1675 // Otherwise, not a type specifier.
1676 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001677
Chris Lattnerb75fde62009-01-04 23:41:41 +00001678 case tok::coloncolon: // ::foo::bar
1679 if (NextToken().is(tok::kw_new) || // ::new
1680 NextToken().is(tok::kw_delete)) // ::delete
1681 return false;
1682
1683 // Annotate typenames and C++ scope specifiers. If we get one, just
1684 // recurse to handle whatever we get.
1685 if (TryAnnotateTypeOrScopeToken())
1686 return isTypeSpecifierQualifier();
1687 // Otherwise, not a type specifier.
1688 return false;
1689
Chris Lattner4b009652007-07-25 00:24:17 +00001690 // GNU attributes support.
1691 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001692 // GNU typeof support.
1693 case tok::kw_typeof:
1694
Chris Lattner4b009652007-07-25 00:24:17 +00001695 // type-specifiers
1696 case tok::kw_short:
1697 case tok::kw_long:
1698 case tok::kw_signed:
1699 case tok::kw_unsigned:
1700 case tok::kw__Complex:
1701 case tok::kw__Imaginary:
1702 case tok::kw_void:
1703 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001704 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001705 case tok::kw_int:
1706 case tok::kw_float:
1707 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001708 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001709 case tok::kw__Bool:
1710 case tok::kw__Decimal32:
1711 case tok::kw__Decimal64:
1712 case tok::kw__Decimal128:
1713
Chris Lattner2e78db32008-04-13 18:59:07 +00001714 // struct-or-union-specifier (C99) or class-specifier (C++)
1715 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001716 case tok::kw_struct:
1717 case tok::kw_union:
1718 // enum-specifier
1719 case tok::kw_enum:
1720
1721 // type-qualifier
1722 case tok::kw_const:
1723 case tok::kw_volatile:
1724 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001725
1726 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001727 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001728 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001729
1730 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1731 case tok::less:
1732 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001733
1734 case tok::kw___cdecl:
1735 case tok::kw___stdcall:
1736 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001737 case tok::kw___w64:
1738 case tok::kw___ptr64:
1739 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001740 }
1741}
1742
1743/// isDeclarationSpecifier() - Return true if the current token is part of a
1744/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001745bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001746 switch (Tok.getKind()) {
1747 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001748
1749 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001750 // Unfortunate hack to support "Class.factoryMethod" notation.
1751 if (getLang().ObjC1 && NextToken().is(tok::period))
1752 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001753 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001754
Douglas Gregord3022602009-03-27 23:10:48 +00001755 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001756 // Annotate typenames and C++ scope specifiers. If we get one, just
1757 // recurse to handle whatever we get.
1758 if (TryAnnotateTypeOrScopeToken())
1759 return isDeclarationSpecifier();
1760 // Otherwise, not a declaration specifier.
1761 return false;
1762 case tok::coloncolon: // ::foo::bar
1763 if (NextToken().is(tok::kw_new) || // ::new
1764 NextToken().is(tok::kw_delete)) // ::delete
1765 return false;
1766
1767 // Annotate typenames and C++ scope specifiers. If we get one, just
1768 // recurse to handle whatever we get.
1769 if (TryAnnotateTypeOrScopeToken())
1770 return isDeclarationSpecifier();
1771 // Otherwise, not a declaration specifier.
1772 return false;
1773
Chris Lattner4b009652007-07-25 00:24:17 +00001774 // storage-class-specifier
1775 case tok::kw_typedef:
1776 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001777 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001778 case tok::kw_static:
1779 case tok::kw_auto:
1780 case tok::kw_register:
1781 case tok::kw___thread:
1782
1783 // type-specifiers
1784 case tok::kw_short:
1785 case tok::kw_long:
1786 case tok::kw_signed:
1787 case tok::kw_unsigned:
1788 case tok::kw__Complex:
1789 case tok::kw__Imaginary:
1790 case tok::kw_void:
1791 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001792 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001793 case tok::kw_int:
1794 case tok::kw_float:
1795 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001796 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001797 case tok::kw__Bool:
1798 case tok::kw__Decimal32:
1799 case tok::kw__Decimal64:
1800 case tok::kw__Decimal128:
1801
Chris Lattner2e78db32008-04-13 18:59:07 +00001802 // struct-or-union-specifier (C99) or class-specifier (C++)
1803 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001804 case tok::kw_struct:
1805 case tok::kw_union:
1806 // enum-specifier
1807 case tok::kw_enum:
1808
1809 // type-qualifier
1810 case tok::kw_const:
1811 case tok::kw_volatile:
1812 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001813
Chris Lattner4b009652007-07-25 00:24:17 +00001814 // function-specifier
1815 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001816 case tok::kw_virtual:
1817 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001818
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001819 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001820 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001821
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001822 // GNU typeof support.
1823 case tok::kw_typeof:
1824
1825 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001826 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001827 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001828
1829 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1830 case tok::less:
1831 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001832
Steve Naroffab1a3632009-01-06 19:34:12 +00001833 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001834 case tok::kw___cdecl:
1835 case tok::kw___stdcall:
1836 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001837 case tok::kw___w64:
1838 case tok::kw___ptr64:
1839 case tok::kw___forceinline:
1840 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001841 }
1842}
1843
1844
1845/// ParseTypeQualifierListOpt
1846/// type-qualifier-list: [C99 6.7.5]
1847/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001848/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001849/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001850/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001851///
Chris Lattner460696f2008-12-18 07:02:59 +00001852void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001853 while (1) {
1854 int isInvalid = false;
1855 const char *PrevSpec = 0;
1856 SourceLocation Loc = Tok.getLocation();
1857
1858 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001859 case tok::kw_const:
1860 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1861 getLang())*2;
1862 break;
1863 case tok::kw_volatile:
1864 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1865 getLang())*2;
1866 break;
1867 case tok::kw_restrict:
1868 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1869 getLang())*2;
1870 break;
Eli Friedman891d82f2009-06-08 23:27:34 +00001871 case tok::kw___w64:
Steve Naroffad620402008-12-25 14:41:26 +00001872 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001873 case tok::kw___cdecl:
1874 case tok::kw___stdcall:
1875 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001876 if (AttributesAllowed) {
1877 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1878 continue;
1879 }
1880 goto DoneWithTypeQuals;
Chris Lattner4b009652007-07-25 00:24:17 +00001881 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001882 if (AttributesAllowed) {
1883 DS.AddAttributes(ParseAttributes());
1884 continue; // do *not* consume the next token!
1885 }
1886 // otherwise, FALL THROUGH!
1887 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001888 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001889 // If this is not a type-qualifier token, we're done reading type
1890 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001891 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001892 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001893 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001894
Chris Lattner4b009652007-07-25 00:24:17 +00001895 // If the specifier combination wasn't legal, issue a diagnostic.
1896 if (isInvalid) {
1897 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001898 // Pick between error or extwarn.
1899 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1900 : diag::ext_duplicate_declspec;
1901 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001902 }
1903 ConsumeToken();
1904 }
1905}
1906
1907
1908/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1909///
1910void Parser::ParseDeclarator(Declarator &D) {
1911 /// This implements the 'declarator' production in the C grammar, then checks
1912 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001913 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001914}
1915
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001916/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1917/// is parsed by the function passed to it. Pass null, and the direct-declarator
1918/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001919/// ptr-operator production.
1920///
Sebastian Redl75555032009-01-24 21:16:55 +00001921/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1922/// [C] pointer[opt] direct-declarator
1923/// [C++] direct-declarator
1924/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001925///
1926/// pointer: [C99 6.7.5]
1927/// '*' type-qualifier-list[opt]
1928/// '*' type-qualifier-list[opt] pointer
1929///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001930/// ptr-operator:
1931/// '*' cv-qualifier-seq[opt]
1932/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001933/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001934/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001935/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001936/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001937void Parser::ParseDeclaratorInternal(Declarator &D,
1938 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001939
Sebastian Redl75555032009-01-24 21:16:55 +00001940 // C++ member pointers start with a '::' or a nested-name.
1941 // Member pointers get special handling, since there's no place for the
1942 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001943 if (getLang().CPlusPlus &&
1944 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1945 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001946 CXXScopeSpec SS;
1947 if (ParseOptionalCXXScopeSpecifier(SS)) {
1948 if(Tok.isNot(tok::star)) {
1949 // The scope spec really belongs to the direct-declarator.
1950 D.getCXXScopeSpec() = SS;
1951 if (DirectDeclParser)
1952 (this->*DirectDeclParser)(D);
1953 return;
1954 }
1955
1956 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001957 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001958 DeclSpec DS;
1959 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001960 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001961
1962 // Recurse to parse whatever is left.
1963 ParseDeclaratorInternal(D, DirectDeclParser);
1964
1965 // Sema will have to catch (syntactically invalid) pointers into global
1966 // scope. It has to catch pointers into namespace scope anyway.
1967 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001968 Loc, DS.TakeAttributes()),
1969 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001970 return;
1971 }
1972 }
1973
1974 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001975 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001976 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001977 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001978 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001979 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001980 if (DirectDeclParser)
1981 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001982 return;
1983 }
Sebastian Redl75555032009-01-24 21:16:55 +00001984
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001985 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1986 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001987 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001988 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001989
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001990 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001991 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001992 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001993
Chris Lattner4b009652007-07-25 00:24:17 +00001994 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001995 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001996
Chris Lattner4b009652007-07-25 00:24:17 +00001997 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001998 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001999 if (Kind == tok::star)
2000 // Remember that we parsed a pointer type, and remember the type-quals.
2001 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00002002 DS.TakeAttributes()),
2003 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00002004 else
2005 // Remember that we parsed a Block type, and remember the type-quals.
2006 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump7ff82e72009-04-21 00:51:43 +00002007 Loc, DS.TakeAttributes()),
Sebastian Redl0c986032009-02-09 18:23:29 +00002008 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00002009 } else {
2010 // Is a reference
2011 DeclSpec DS;
2012
Sebastian Redl4e67adb2009-03-23 00:00:23 +00002013 // Complain about rvalue references in C++03, but then go on and build
2014 // the declarator.
2015 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2016 Diag(Loc, diag::err_rvalue_reference);
2017
Chris Lattner4b009652007-07-25 00:24:17 +00002018 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2019 // cv-qualifiers are introduced through the use of a typedef or of a
2020 // template type argument, in which case the cv-qualifiers are ignored.
2021 //
2022 // [GNU] Retricted references are allowed.
2023 // [GNU] Attributes on references are allowed.
2024 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00002025 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00002026
2027 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2028 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2029 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00002030 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00002031 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2032 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00002033 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00002034 }
2035
2036 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002037 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00002038
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002039 if (D.getNumTypeObjects() > 0) {
2040 // C++ [dcl.ref]p4: There shall be no references to references.
2041 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2042 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002043 if (const IdentifierInfo *II = D.getIdentifier())
2044 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2045 << II;
2046 else
2047 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2048 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002049
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002050 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002051 // can go ahead and build the (technically ill-formed)
2052 // declarator: reference collapsing will take care of it.
2053 }
2054 }
2055
Chris Lattner4b009652007-07-25 00:24:17 +00002056 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00002057 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00002058 DS.TakeAttributes(),
2059 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00002060 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00002061 }
2062}
2063
2064/// ParseDirectDeclarator
2065/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002066/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00002067/// '(' declarator ')'
2068/// [GNU] '(' attributes declarator ')'
2069/// [C90] direct-declarator '[' constant-expression[opt] ']'
2070/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2071/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2072/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2073/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2074/// direct-declarator '(' parameter-type-list ')'
2075/// direct-declarator '(' identifier-list[opt] ')'
2076/// [GNU] direct-declarator '(' parameter-forward-declarations
2077/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002078/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2079/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00002080/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002081///
2082/// declarator-id: [C++ 8]
2083/// id-expression
2084/// '::'[opt] nested-name-specifier[opt] type-name
2085///
2086/// id-expression: [C++ 5.1]
2087/// unqualified-id
2088/// qualified-id [TODO]
2089///
2090/// unqualified-id: [C++ 5.1]
2091/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002092/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002093/// conversion-function-id [TODO]
2094/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00002095/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00002096///
Chris Lattner4b009652007-07-25 00:24:17 +00002097void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002098 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002099
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002100 if (getLang().CPlusPlus) {
2101 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00002102 // ParseDeclaratorInternal might already have parsed the scope.
2103 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2104 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002105 if (afterCXXScope) {
2106 // Change the declaration context for name lookup, until this function
2107 // is exited (and the declarator has been parsed).
2108 DeclScopeObj.EnterDeclaratorScope();
2109 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002110
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002111 if (Tok.is(tok::identifier)) {
2112 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssone19759d2009-04-30 22:41:11 +00002113
2114 // If this identifier is the name of the current class, it's a
2115 // constructor name.
2116 if (!D.getDeclSpec().hasTypeSpecifier() &&
2117 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
2118 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
2119 Tok.getLocation(), CurScope),
2120 Tok.getLocation());
2121 // This is a normal identifier.
2122 } else
2123 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002124 ConsumeToken();
2125 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00002126 } else if (Tok.is(tok::annot_template_id)) {
2127 TemplateIdAnnotation *TemplateId
2128 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2129
2130 // FIXME: Could this template-id name a constructor?
2131
2132 // FIXME: This is an egregious hack, where we silently ignore
2133 // the specialization (which should be a function template
2134 // specialization name) and use the name instead. This hack
2135 // will go away when we have support for function
2136 // specializations.
2137 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2138 TemplateId->Destroy();
2139 ConsumeToken();
2140 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00002141 } else if (Tok.is(tok::kw_operator)) {
2142 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00002143 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00002144
Douglas Gregor853dd392008-12-26 15:00:45 +00002145 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00002146 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2147 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00002148 } else {
2149 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00002150 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2151 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2152 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00002153 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00002154 }
Douglas Gregor853dd392008-12-26 15:00:45 +00002155 }
2156 goto PastIdentifier;
2157 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002158 // This should be a C++ destructor.
2159 SourceLocation TildeLoc = ConsumeToken();
2160 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002161 // FIXME: Inaccurate.
2162 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00002163 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00002164 TypeResult Type = ParseClassName(EndLoc);
2165 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002166 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002167 else
2168 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002169 } else {
2170 Diag(Tok, diag::err_expected_class_name);
2171 D.SetIdentifier(0, TildeLoc);
2172 }
2173 goto PastIdentifier;
2174 }
2175
2176 // If we reached this point, token is not identifier and not '~'.
2177
2178 if (afterCXXScope) {
2179 Diag(Tok, diag::err_expected_unqualified_id);
2180 D.SetIdentifier(0, Tok.getLocation());
2181 D.setInvalidType(true);
2182 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002183 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00002184 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002185 }
2186
2187 // If we reached this point, we are either in C/ObjC or the token didn't
2188 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002189 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2190 assert(!getLang().CPlusPlus &&
2191 "There's a C++-specific check for tok::identifier above");
2192 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2193 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2194 ConsumeToken();
2195 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002196 // direct-declarator: '(' declarator ')'
2197 // direct-declarator: '(' attributes declarator ')'
2198 // Example: 'char (*X)' or 'int (*XX)(void)'
2199 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002200 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002201 // This could be something simple like "int" (in which case the declarator
2202 // portion is empty), if an abstract-declarator is allowed.
2203 D.SetIdentifier(0, Tok.getLocation());
2204 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00002205 if (D.getContext() == Declarator::MemberContext)
2206 Diag(Tok, diag::err_expected_member_name_or_semi)
2207 << D.getDeclSpec().getSourceRange();
2208 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002209 Diag(Tok, diag::err_expected_unqualified_id);
2210 else
Chris Lattnerf006a222008-11-18 07:48:38 +00002211 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00002212 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00002213 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002214 }
2215
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002216 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00002217 assert(D.isPastIdentifier() &&
2218 "Haven't past the location of the identifier yet?");
2219
2220 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002221 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002222 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2223 // In such a case, check if we actually have a function declarator; if it
2224 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00002225 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2226 // When not in file scope, warn for ambiguous function declarators, just
2227 // in case the author intended it as a variable definition.
2228 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2229 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2230 break;
2231 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00002232 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00002233 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002234 ParseBracketDeclarator(D);
2235 } else {
2236 break;
2237 }
2238 }
2239}
2240
Chris Lattnera0d056d2008-04-06 05:45:57 +00002241/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2242/// only called before the identifier, so these are most likely just grouping
2243/// parens for precedence. If we find that these are actually function
2244/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2245///
2246/// direct-declarator:
2247/// '(' declarator ')'
2248/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00002249/// direct-declarator '(' parameter-type-list ')'
2250/// direct-declarator '(' identifier-list[opt] ')'
2251/// [GNU] direct-declarator '(' parameter-forward-declarations
2252/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00002253///
2254void Parser::ParseParenDeclarator(Declarator &D) {
2255 SourceLocation StartLoc = ConsumeParen();
2256 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2257
Chris Lattner1f185292008-10-20 02:05:46 +00002258 // Eat any attributes before we look at whether this is a grouping or function
2259 // declarator paren. If this is a grouping paren, the attribute applies to
2260 // the type being built up, for example:
2261 // int (__attribute__(()) *x)(long y)
2262 // If this ends up not being a grouping paren, the attribute applies to the
2263 // first argument, for example:
2264 // int (__attribute__(()) int x)
2265 // In either case, we need to eat any attributes to be able to determine what
2266 // sort of paren this is.
2267 //
2268 AttributeList *AttrList = 0;
2269 bool RequiresArg = false;
2270 if (Tok.is(tok::kw___attribute)) {
2271 AttrList = ParseAttributes();
2272
2273 // We require that the argument list (if this is a non-grouping paren) be
2274 // present even if the attribute list was empty.
2275 RequiresArg = true;
2276 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002277 // Eat any Microsoft extensions.
Eli Friedman891d82f2009-06-08 23:27:34 +00002278 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2279 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2280 Tok.is(tok::kw___ptr64)) {
2281 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2282 }
Chris Lattner1f185292008-10-20 02:05:46 +00002283
Chris Lattnera0d056d2008-04-06 05:45:57 +00002284 // If we haven't past the identifier yet (or where the identifier would be
2285 // stored, if this is an abstract declarator), then this is probably just
2286 // grouping parens. However, if this could be an abstract-declarator, then
2287 // this could also be the start of function arguments (consider 'void()').
2288 bool isGrouping;
2289
2290 if (!D.mayOmitIdentifier()) {
2291 // If this can't be an abstract-declarator, this *must* be a grouping
2292 // paren, because we haven't seen the identifier yet.
2293 isGrouping = true;
2294 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002295 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002296 isDeclarationSpecifier()) { // 'int(int)' is a function.
2297 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2298 // considered to be a type, not a K&R identifier-list.
2299 isGrouping = false;
2300 } else {
2301 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2302 isGrouping = true;
2303 }
2304
2305 // If this is a grouping paren, handle:
2306 // direct-declarator: '(' declarator ')'
2307 // direct-declarator: '(' attributes declarator ')'
2308 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002309 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002310 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002311 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002312 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002313
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002314 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002315 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002316 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002317
2318 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002319 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002320 return;
2321 }
2322
2323 // Okay, if this wasn't a grouping paren, it must be the start of a function
2324 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002325 // identifier (and remember where it would have been), then call into
2326 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002327 D.SetIdentifier(0, Tok.getLocation());
2328
Chris Lattner1f185292008-10-20 02:05:46 +00002329 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002330}
2331
2332/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2333/// declarator D up to a paren, which indicates that we are parsing function
2334/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002335///
Chris Lattner1f185292008-10-20 02:05:46 +00002336/// If AttrList is non-null, then the caller parsed those arguments immediately
2337/// after the open paren - they should be considered to be the first argument of
2338/// a parameter. If RequiresArg is true, then the first argument of the
2339/// function is required to be present and required to not be an identifier
2340/// list.
2341///
Chris Lattner4b009652007-07-25 00:24:17 +00002342/// This method also handles this portion of the grammar:
2343/// parameter-type-list: [C99 6.7.5]
2344/// parameter-list
2345/// parameter-list ',' '...'
2346///
2347/// parameter-list: [C99 6.7.5]
2348/// parameter-declaration
2349/// parameter-list ',' parameter-declaration
2350///
2351/// parameter-declaration: [C99 6.7.5]
2352/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002353/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002354/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002355/// declaration-specifiers abstract-declarator[opt]
2356/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002357/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002358/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2359///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002360/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002361/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002362///
Chris Lattner1f185292008-10-20 02:05:46 +00002363void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2364 AttributeList *AttrList,
2365 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002366 // lparen is already consumed!
2367 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002368
Chris Lattner1f185292008-10-20 02:05:46 +00002369 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002370 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002371 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002372 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002373 delete AttrList;
2374 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002375
Sebastian Redl0c986032009-02-09 18:23:29 +00002376 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002377
2378 // cv-qualifier-seq[opt].
2379 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002380 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002381 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002382 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002383 llvm::SmallVector<TypeTy*, 2> Exceptions;
2384 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002385 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002386 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002387 if (!DS.getSourceRange().getEnd().isInvalid())
2388 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002389
2390 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002391 if (Tok.is(tok::kw_throw)) {
2392 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002393 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002394 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2395 hasAnyExceptionSpec);
2396 assert(Exceptions.size() == ExceptionRanges.size() &&
2397 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002398 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002399 }
2400
Chris Lattner9f7564b2008-04-06 06:57:35 +00002401 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002402 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002403 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002404 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002405 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002406 /*arglist*/ 0, 0,
2407 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002408 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002409 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002410 Exceptions.data(),
2411 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002412 Exceptions.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002413 LParenLoc, D),
2414 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002415 return;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002416 }
2417
Chris Lattner1f185292008-10-20 02:05:46 +00002418 // Alternatively, this parameter list may be an identifier list form for a
2419 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002420 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002421 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002422 // K&R identifier lists can't have typedefs as identifiers, per
2423 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002424 if (RequiresArg) {
2425 Diag(Tok, diag::err_argument_required_after_attribute);
2426 delete AttrList;
2427 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002428 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2429 // normal declarators, not for abstract-declarators.
2430 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002431 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002432 }
2433
2434 // Finally, a normal, non-empty parameter type list.
2435
2436 // Build up an array of information about the parsed arguments.
2437 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002438
2439 // Enter function-declaration scope, limiting any declarators to the
2440 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002441 ParseScope PrototypeScope(this,
2442 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002443
2444 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002445 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002446 while (1) {
2447 if (Tok.is(tok::ellipsis)) {
2448 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002449 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002450 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002451 }
2452
Chris Lattner9f7564b2008-04-06 06:57:35 +00002453 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002454
Chris Lattner9f7564b2008-04-06 06:57:35 +00002455 // Parse the declaration-specifiers.
2456 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002457
2458 // If the caller parsed attributes for the first argument, add them now.
2459 if (AttrList) {
2460 DS.AddAttributes(AttrList);
2461 AttrList = 0; // Only apply the attributes to the first parameter.
2462 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002463 ParseDeclarationSpecifiers(DS);
2464
Chris Lattner9f7564b2008-04-06 06:57:35 +00002465 // Parse the declarator. This is "PrototypeContext", because we must
2466 // accept either 'declarator' or 'abstract-declarator' here.
2467 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2468 ParseDeclarator(ParmDecl);
2469
2470 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002471 if (Tok.is(tok::kw___attribute)) {
2472 SourceLocation Loc;
2473 AttributeList *AttrList = ParseAttributes(&Loc);
2474 ParmDecl.AddAttributes(AttrList, Loc);
2475 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002476
Chris Lattner9f7564b2008-04-06 06:57:35 +00002477 // Remember this parsed parameter in ParamInfo.
2478 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2479
Douglas Gregor605de8d2008-12-16 21:30:33 +00002480 // DefArgToks is used when the parsing of default arguments needs
2481 // to be delayed.
2482 CachedTokens *DefArgToks = 0;
2483
Chris Lattner9f7564b2008-04-06 06:57:35 +00002484 // If no parameter was specified, verify that *something* was specified,
2485 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002486 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2487 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002488 // Completely missing, emit error.
2489 Diag(DSStart, diag::err_missing_param);
2490 } else {
2491 // Otherwise, we have something. Add it and let semantic analysis try
2492 // to grok it and add the result to the ParamInfo we are building.
2493
2494 // Inform the actions module about the parameter declarator, so it gets
2495 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002496 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002497
2498 // Parse the default argument, if any. We parse the default
2499 // arguments in all dialects; the semantic analysis in
2500 // ActOnParamDefaultArgument will reject the default argument in
2501 // C.
2502 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002503 SourceLocation EqualLoc = Tok.getLocation();
2504
Chris Lattner3e254fb2008-04-08 04:40:51 +00002505 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002506 if (D.getContext() == Declarator::MemberContext) {
2507 // If we're inside a class definition, cache the tokens
2508 // corresponding to the default argument. We'll actually parse
2509 // them when we see the end of the class definition.
2510 // FIXME: Templates will require something similar.
2511 // FIXME: Can we use a smart pointer for Toks?
2512 DefArgToks = new CachedTokens;
2513
2514 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2515 tok::semi, false)) {
2516 delete DefArgToks;
2517 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002518 Actions.ActOnParamDefaultArgumentError(Param);
2519 } else
Anders Carlssona116e6e2009-06-12 16:51:40 +00002520 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
2521 (*DefArgToks)[1].getLocation());
Chris Lattner3e254fb2008-04-08 04:40:51 +00002522 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002523 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002524 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002525
2526 OwningExprResult DefArgResult(ParseAssignmentExpression());
2527 if (DefArgResult.isInvalid()) {
2528 Actions.ActOnParamDefaultArgumentError(Param);
2529 SkipUntil(tok::comma, tok::r_paren, true, true);
2530 } else {
2531 // Inform the actions module about the default argument
2532 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002533 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002534 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002535 }
2536 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002537
2538 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002539 ParmDecl.getIdentifierLoc(), Param,
2540 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002541 }
2542
2543 // If the next token is a comma, consume it and keep reading arguments.
2544 if (Tok.isNot(tok::comma)) break;
2545
2546 // Consume the comma.
2547 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002548 }
2549
Chris Lattner9f7564b2008-04-06 06:57:35 +00002550 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002551 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002552
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002553 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002554 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002555
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002556 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002557 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002558 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002559 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002560 llvm::SmallVector<TypeTy*, 2> Exceptions;
2561 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002562 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002563 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002564 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002565 if (!DS.getSourceRange().getEnd().isInvalid())
2566 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002567
2568 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002569 if (Tok.is(tok::kw_throw)) {
2570 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002571 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002572 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2573 hasAnyExceptionSpec);
2574 assert(Exceptions.size() == ExceptionRanges.size() &&
2575 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002576 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002577 }
2578
Chris Lattner4b009652007-07-25 00:24:17 +00002579 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002580 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002581 EllipsisLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00002582 ParamInfo.data(), ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002583 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002584 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002585 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002586 Exceptions.data(),
2587 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002588 Exceptions.size(), LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002589 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002590}
2591
Chris Lattner35d9c912008-04-06 06:34:08 +00002592/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2593/// we found a K&R-style identifier list instead of a type argument list. The
2594/// current token is known to be the first identifier in the list.
2595///
2596/// identifier-list: [C99 6.7.5]
2597/// identifier
2598/// identifier-list ',' identifier
2599///
2600void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2601 Declarator &D) {
2602 // Build up an array of information about the parsed arguments.
2603 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2604 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2605
2606 // If there was no identifier specified for the declarator, either we are in
2607 // an abstract-declarator, or we are in a parameter declarator which was found
2608 // to be abstract. In abstract-declarators, identifier lists are not valid:
2609 // diagnose this.
2610 if (!D.getIdentifier())
2611 Diag(Tok, diag::ext_ident_list_in_param);
2612
2613 // Tok is known to be the first identifier in the list. Remember this
2614 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002615 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002616 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002617 Tok.getLocation(),
2618 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002619
Chris Lattner113a56b2008-04-06 06:39:19 +00002620 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002621
2622 while (Tok.is(tok::comma)) {
2623 // Eat the comma.
2624 ConsumeToken();
2625
Chris Lattner113a56b2008-04-06 06:39:19 +00002626 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002627 if (Tok.isNot(tok::identifier)) {
2628 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002629 SkipUntil(tok::r_paren);
2630 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002631 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002632
Chris Lattner35d9c912008-04-06 06:34:08 +00002633 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002634
2635 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002636 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002637 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002638
2639 // Verify that the argument identifier has not already been mentioned.
2640 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002641 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002642 } else {
2643 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002644 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002645 Tok.getLocation(),
2646 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002647 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002648
2649 // Eat the identifier.
2650 ConsumeToken();
2651 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002652
2653 // If we have the closing ')', eat it and we're done.
2654 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2655
Chris Lattner113a56b2008-04-06 06:39:19 +00002656 // Remember that we parsed a function type, and remember the attributes. This
2657 // function type is always a K&R style function type, which is not varargs and
2658 // has no prototype.
2659 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002660 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002661 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002662 /*TypeQuals*/0,
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002663 /*exception*/false,
2664 SourceLocation(), false, 0, 0, 0,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002665 LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002666 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002667}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002668
Chris Lattner4b009652007-07-25 00:24:17 +00002669/// [C90] direct-declarator '[' constant-expression[opt] ']'
2670/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2671/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2672/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2673/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2674void Parser::ParseBracketDeclarator(Declarator &D) {
2675 SourceLocation StartLoc = ConsumeBracket();
2676
Chris Lattner1525c3a2008-12-18 07:27:21 +00002677 // C array syntax has many features, but by-far the most common is [] and [4].
2678 // This code does a fast path to handle some of the most obvious cases.
2679 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002680 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002681 // Remember that we parsed the empty array type.
2682 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002683 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2684 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002685 return;
2686 } else if (Tok.getKind() == tok::numeric_constant &&
2687 GetLookAheadToken(1).is(tok::r_square)) {
2688 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002689 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002690 ConsumeToken();
2691
Sebastian Redl0c986032009-02-09 18:23:29 +00002692 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002693
2694 // If there was an error parsing the assignment-expression, recover.
2695 if (ExprRes.isInvalid())
2696 ExprRes.release(); // Deallocate expr, just use [].
2697
2698 // Remember that we parsed a array type, and remember its features.
2699 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002700 ExprRes.release(), StartLoc),
2701 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002702 return;
2703 }
2704
Chris Lattner4b009652007-07-25 00:24:17 +00002705 // If valid, this location is the position where we read the 'static' keyword.
2706 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002707 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002708 StaticLoc = ConsumeToken();
2709
2710 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002711 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002712 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002713 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002714
2715 // If we haven't already read 'static', check to see if there is one after the
2716 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002717 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002718 StaticLoc = ConsumeToken();
2719
2720 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2721 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002722 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002723
2724 // Handle the case where we have '[*]' as the array size. However, a leading
2725 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2726 // the the token after the star is a ']'. Since stars in arrays are
2727 // infrequent, use of lookahead is not costly here.
2728 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002729 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002730
Chris Lattner306d4df2008-12-18 06:50:14 +00002731 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002732 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002733 StaticLoc = SourceLocation(); // Drop the static.
2734 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002735 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002736 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002737 // Note, in C89, this production uses the constant-expr production instead
2738 // of assignment-expr. The only difference is that assignment-expr allows
2739 // things like '=' and '*='. Sema rejects these in C89 mode because they
2740 // are not i-c-e's, so we don't need to distinguish between the two here.
2741
Douglas Gregor98189262009-06-19 23:52:42 +00002742 // Parse the constant-expression or assignment-expression now (depending
2743 // on dialect).
2744 if (getLang().CPlusPlus)
2745 NumElements = ParseConstantExpression();
2746 else
2747 NumElements = ParseAssignmentExpression();
Chris Lattner4b009652007-07-25 00:24:17 +00002748 }
2749
2750 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002751 if (NumElements.isInvalid()) {
Chris Lattnerf3ce8572009-04-24 22:30:50 +00002752 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002753 // If the expression was invalid, skip it.
2754 SkipUntil(tok::r_square);
2755 return;
2756 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002757
2758 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2759
Chris Lattner1525c3a2008-12-18 07:27:21 +00002760 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002761 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2762 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002763 NumElements.release(), StartLoc),
2764 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002765}
2766
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002767/// [GNU] typeof-specifier:
2768/// typeof ( expressions )
2769/// typeof ( type-name )
2770/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002771///
2772void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002773 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002774 Token OpTok = Tok;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002775 SourceLocation StartLoc = ConsumeToken();
2776
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002777 bool isCastExpr;
2778 TypeTy *CastTy;
2779 SourceRange CastRange;
2780 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2781 isCastExpr,
2782 CastTy,
2783 CastRange);
2784
2785 if (CastRange.getEnd().isInvalid())
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002786 // FIXME: Not accurate, the range gets one token more than it should.
2787 DS.SetRangeEnd(Tok.getLocation());
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002788 else
2789 DS.SetRangeEnd(CastRange.getEnd());
2790
2791 if (isCastExpr) {
2792 if (!CastTy) {
2793 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002794 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002795 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002796
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002797 const char *PrevSpec = 0;
2798 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2799 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2800 CastTy))
2801 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2802 return;
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002803 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002804
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002805 // If we get here, the operand to the typeof was an expresion.
2806 if (Operand.isInvalid()) {
2807 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002808 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002809 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002810
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002811 const char *PrevSpec = 0;
2812 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2813 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
2814 Operand.release()))
2815 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002816}