blob: 4a3532c4103ba421fa380e9b9540c630e691b69a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl4c5d3202008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Sebastian Redlef65f062009-05-29 18:02:33 +000030Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
33 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000034
Reid Spencer5f016e22007-07-11 17:01:13 +000035 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000038 if (Range)
39 *Range = DeclaratorInfo.getSourceRange();
40
Chris Lattnereaaebc72009-04-25 08:06:05 +000041 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000042 return true;
43
44 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redlab197ba2009-02-09 18:23:29 +000083AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000084 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000085
86 AttributeList *CurrAttr = 0;
87
Chris Lattner04d66662007-10-09 17:33:22 +000088 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000100 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
101 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000102
Chris Lattner04d66662007-10-09 17:33:22 +0000103 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000113 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 ConsumeParen(); // ignore the left paren loc for now
115
Chris Lattner04d66662007-10-09 17:33:22 +0000116 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
118 SourceLocation ParmLoc = ConsumeToken();
119
Chris Lattner04d66662007-10-09 17:33:22 +0000120 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000125 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 ConsumeToken();
127 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000128 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 bool ArgExprsOk = true;
130
131 // now parse the non-empty comma separated list of expressions
132 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000133 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000134 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 ArgExprsOk = false;
136 SkipUntil(tok::r_paren);
137 break;
138 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000139 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 }
Chris Lattner04d66662007-10-09 17:33:22 +0000141 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 break;
143 ConsumeToken(); // Eat the comma, move to the next argument
144 }
Chris Lattner04d66662007-10-09 17:33:22 +0000145 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 ConsumeParen(); // ignore the right paren loc for now
147 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000148 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 }
150 }
151 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000152 switch (Tok.getKind()) {
153 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +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 Begeman6f3d8382009-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:
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000182 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 bool ArgExprsOk = true;
184
185 // now parse the list of expressions
186 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000187 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000188 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 ArgExprsOk = false;
190 SkipUntil(tok::r_paren);
191 break;
192 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000193 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 }
Chris Lattner04d66662007-10-09 17:33:22 +0000195 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 break;
197 ConsumeToken(); // Eat the comma, move to the next argument
198 }
199 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000200 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000202 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
203 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 CurrAttr);
205 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000206 break;
Reid Spencer5f016e22007-07-11 17:01:13 +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))
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 SkipUntil(tok::r_paren, false);
Sebastian Redlab197ba2009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 }
223 return CurrAttr;
224}
225
Eli Friedmana23b4852009-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 Friedman290eeb02009-06-08 23:27:34 +0000235AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000236 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000237
Steve Narofff59e17e2008-12-24 20:59:21 +0000238 ConsumeToken();
Eli Friedmana23b4852009-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 Friedman290eeb02009-06-08 23:27:34 +0000244 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-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 Friedman290eeb02009-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 Narofff59e17e2008-12-24 20:59:21 +0000285}
286
Reid Spencer5f016e22007-07-11 17:01:13 +0000287/// ParseDeclaration - Parse a full 'declaration', which consists of
288/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000289/// 'Context' should be a Declarator::TheContext value. This returns the
290/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000291///
292/// declaration: [C99 6.7]
293/// block-declaration ->
294/// simple-declaration
295/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000296/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000297/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000298/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000299/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000300/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000301/// others... [FIXME]
302///
Chris Lattner97144fc2009-04-02 04:16:50 +0000303Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
304 SourceLocation &DeclEnd) {
Chris Lattner682bf922009-03-29 16:50:03 +0000305 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000306 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000307 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000308 case tok::kw_export:
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000309 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000310 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000311 case tok::kw_namespace:
Chris Lattner97144fc2009-04-02 04:16:50 +0000312 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000313 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000314 case tok::kw_using:
Chris Lattner97144fc2009-04-02 04:16:50 +0000315 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000316 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000317 case tok::kw_static_assert:
Chris Lattner97144fc2009-04-02 04:16:50 +0000318 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000319 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000320 default:
Chris Lattner97144fc2009-04-02 04:16:50 +0000321 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000322 }
Chris Lattner682bf922009-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 Lattner8f08cb72007-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 Lattnercd147752009-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 Lattner97144fc2009-04-02 04:16:50 +0000337 SourceLocation &DeclEnd,
Chris Lattnercd147752009-03-29 17:27:48 +0000338 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000345 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000347 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
348 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 }
350
351 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
352 ParseDeclarator(DeclaratorInfo);
353
Chris Lattner23c4b182009-03-29 17:18:04 +0000354 DeclGroupPtrTy DG =
355 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnercd147752009-03-29 17:27:48 +0000356
Chris Lattner97144fc2009-04-02 04:16:50 +0000357 DeclEnd = Tok.getLocation();
358
Chris Lattnercd147752009-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 Lattner23c4b182009-03-29 17:18:04 +0000362
363 if (Tok.is(tok::semi)) {
364 ConsumeToken();
Chris Lattner23c4b182009-03-29 17:18:04 +0000365 return DG;
366 }
367
Chris Lattner23c4b182009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000374}
375
Douglas Gregor1426e532009-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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000380///
Reid Spencer5f016e22007-07-11 17:01:13 +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
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000386/// [C++] declarator initializer[opt]
387///
388/// [C++] initializer:
389/// [C++] '=' initializer-clause
390/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000396///
Douglas Gregore542c862009-06-23 23:11:28 +0000397Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
398 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-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 Gregore542c862009-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 Gregor1426e532009-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 {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000435 if (getLang().CPlusPlus)
436 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
437
Douglas Gregor1426e532009-05-12 21:31:51 +0000438 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000439
440 if (getLang().CPlusPlus)
441 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
442
Douglas Gregor1426e532009-05-12 21:31:51 +0000443 if (Init.isInvalid()) {
444 SkipUntil(tok::semi, true, true);
445 return DeclPtrTy();
446 }
Anders Carlssonf5dcd382009-05-30 21:37:25 +0000447 Actions.AddInitializerToDecl(ThisDecl, Actions.FullExpr(Init));
Douglas Gregor1426e532009-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 Foadbeaaccd2009-05-21 09:52:38 +0000465 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-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 Lattner682bf922009-03-29 16:50:03 +0000486Parser::DeclGroupPtrTy Parser::
Reid Spencer5f016e22007-07-11 17:01:13 +0000487ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner682bf922009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregor1426e532009-05-12 21:31:51 +0000495 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
496 if (ThisDecl.get())
497 DeclsInGroup.push_back(ThisDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000498
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 // If we don't have a comma, it is either the end of the list (a ';') or an
500 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000501 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 break;
503
504 // Consume the comma.
505 ConsumeToken();
506
507 // Parse the next declarator.
508 D.clear();
Chris Lattneraab740a2008-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 Redlab197ba2009-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 Lattneraab740a2008-10-20 04:57:38 +0000522
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 ParseDeclarator(D);
524 }
525
Eli Friedmanc1dc6532009-05-29 01:49:24 +0000526 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
527 DeclsInGroup.data(),
Chris Lattner23c4b182009-03-29 17:18:04 +0000528 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 ParseDeclarationSpecifiers(DS);
541
542 // Validate declspec for type-name.
543 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000544 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
545 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregorb48fe382008-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);
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 DS.ClearFunctionSpecs();
566 }
567}
568
Chris Lattnerc199ab32009-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 Lattnerb6645dd2009-04-14 21:16:09 +0000580/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000581/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-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 Lattnerb6645dd2009-04-14 21:16:09 +0000590 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000591}
592
Chris Lattnere40c2952009-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 Lattnerf4382f52009-04-14 22:17:06 +0000603bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000604 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000605 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000606 assert(Tok.is(tok::identifier) && "should have identifier");
607
Chris Lattnere40c2952009-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 Lattnerf4382f52009-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 Lattnere40c2952009-04-14 21:34:55 +0000641
Chris Lattnere40c2952009-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 Lattnere40c2952009-04-14 21:34:55 +0000649
Chris Lattnerf4382f52009-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 Gregor4d9a16f2009-05-12 23:25:50 +0000659 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000660 return true;
661 }
Chris Lattnere40c2952009-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 Lattnerf4382f52009-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 Lattnere40c2952009-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000682/// ParseDeclarationSpecifiers
683/// declaration-specifiers: [C99 6.7]
684/// storage-class-specifier declaration-specifiers[opt]
685/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl669d5d72008-11-14 23:42:31 +0000695/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000696/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000697/// function-specifier: [C99 6.7.4]
698/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000699/// [C++] 'virtual'
700/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000701/// 'friend': [C++ dcl.friend]
702
Reid Spencer5f016e22007-07-11 17:01:13 +0000703///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000704void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000705 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerc199ab32009-04-12 20:42:31 +0000706 AccessSpecifier AS) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000707 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 while (1) {
709 int isInvalid = false;
710 const char *PrevSpec = 0;
711 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000712
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000714 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000715 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 // If this is not a declaration specifier token, we're done reading decl
717 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000718 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 return;
Chris Lattner5e02c472009-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;
Argyrios Kyrtzidiseb83ecd2008-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 Gregor9135c722009-03-25 15:40:00 +0000732 Token Next = NextToken();
733 if (Next.is(tok::annot_template_id) &&
734 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000735 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-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))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000746 goto DoneWithDeclSpec;
747
748 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000749 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-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 Lattnerf4382f52009-04-14 22:17:06 +0000755 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000756 CurScope, &SS) &&
757 GetLookAheadToken(2).is(tok::l_paren))
758 goto DoneWithDeclSpec;
759
Douglas Gregorb696ea32009-02-04 17:00:24 +0000760 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
761 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000762
Chris Lattnerf4382f52009-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 Gregor4d9a16f2009-05-12 23:25:50 +0000769 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000770 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000771 }
Douglas Gregore4e5b052009-03-19 00:18:19 +0000772
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000773 ConsumeToken(); // The C++ scope.
774
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-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 Lattner80d0c892009-01-21 19:48:37 +0000785
786 case tok::annot_typename: {
Douglas Gregor31a19b62009-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 Lattner80d0c892009-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 Lattnerb28317a2009-03-28 19:18:32 +0000803 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner80d0c892009-01-21 19:48:37 +0000804 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +0000805 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattner80d0c892009-01-21 19:48:37 +0000806
807 DS.SetRangeEnd(EndProtoLoc);
808 continue;
809 }
810
Chris Lattner3bd934a2008-07-26 01:18:38 +0000811 // typedef-name
812 case tok::identifier: {
Chris Lattner5e02c472009-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 Lattner837acd02009-01-21 19:19:26 +0000815 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
816 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000817
Chris Lattner3bd934a2008-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 Gregorb696ea32009-02-04 17:00:24 +0000825 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
826 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000827
Chris Lattnerc199ab32009-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 Gregor4d9a16f2009-05-12 23:25:50 +0000831 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000832 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000833 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000834
Douglas Gregorb48fe382008-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 Lattnerc199ab32009-04-12 20:42:31 +0000839 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000840 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
841 NextToken().getKind() == tok::l_paren)
842 goto DoneWithDeclSpec;
843
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000844 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner3bd934a2008-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 Lattnerb28317a2009-03-28 19:18:32 +0000860 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000861 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +0000862 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000863
864 DS.SetRangeEnd(EndProtoLoc);
865
Steve Naroff4f9b9f12008-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 Lattner3bd934a2008-07-26 01:18:38 +0000869 }
Douglas Gregor39a8de12009-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 Gregorc45c2322009-03-31 00:43:58 +0000875 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-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 Gregor31a19b62009-04-01 21:51:26 +0000883 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000884 continue;
885 }
886
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 // GNU attributes support.
888 case tok::kw___attribute:
889 DS.AddAttributes(ParseAttributes());
890 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000891
892 // Microsoft declspec support.
893 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +0000894 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +0000895 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000896
Steve Naroff239f0732008-12-25 14:16:32 +0000897 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000898 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +0000899 // FIXME: Add handling here!
900 break;
901
902 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000903 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000904 case tok::kw___cdecl:
905 case tok::kw___stdcall:
906 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000907 DS.AddAttributes(ParseMicrosoftTypeAttributes());
908 continue;
909
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner1ab3b962008-11-18 07:48:38 +0000916 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
918 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000919 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000920 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
921 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000922 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 case tok::kw_static:
924 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000925 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000926 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
927 break;
928 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +0000929 if (getLang().CPlusPlus0x)
930 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec);
931 else
932 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 break;
934 case tok::kw_register:
935 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
936 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000937 case tok::kw_mutable:
938 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
939 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 case tok::kw___thread:
941 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
942 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000943
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 // function-specifier
945 case tok::kw_inline:
946 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
947 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000948 case tok::kw_virtual:
949 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
950 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000951 case tok::kw_explicit:
952 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
953 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000954
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000955 // friend
956 case tok::kw_friend:
957 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
958 break;
959
Chris Lattner80d0c892009-01-21 19:48:37 +0000960 // type-specifier
961 case tok::kw_short:
962 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
963 break;
964 case tok::kw_long:
965 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
966 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
967 else
968 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
969 break;
970 case tok::kw_signed:
971 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
972 break;
973 case tok::kw_unsigned:
974 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
975 break;
976 case tok::kw__Complex:
977 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
978 break;
979 case tok::kw__Imaginary:
980 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
981 break;
982 case tok::kw_void:
983 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
984 break;
985 case tok::kw_char:
986 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
987 break;
988 case tok::kw_int:
989 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
990 break;
991 case tok::kw_float:
992 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
993 break;
994 case tok::kw_double:
995 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
996 break;
997 case tok::kw_wchar_t:
998 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
999 break;
1000 case tok::kw_bool:
1001 case tok::kw__Bool:
1002 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1003 break;
1004 case tok::kw__Decimal32:
1005 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1006 break;
1007 case tok::kw__Decimal64:
1008 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1009 break;
1010 case tok::kw__Decimal128:
1011 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1012 break;
1013
1014 // class-specifier:
1015 case tok::kw_class:
1016 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001017 case tok::kw_union: {
1018 tok::TokenKind Kind = Tok.getKind();
1019 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001020 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001021 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001022 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001023
1024 // enum-specifier:
1025 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001026 ConsumeToken();
1027 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001028 continue;
1029
1030 // cv-qualifier:
1031 case tok::kw_const:
1032 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
1033 break;
1034 case tok::kw_volatile:
1035 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1036 getLang())*2;
1037 break;
1038 case tok::kw_restrict:
1039 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1040 getLang())*2;
1041 break;
1042
Douglas Gregord57959a2009-03-27 23:10:48 +00001043 // C++ typename-specifier:
1044 case tok::kw_typename:
1045 if (TryAnnotateTypeOrScopeToken())
1046 continue;
1047 break;
1048
Chris Lattner80d0c892009-01-21 19:48:37 +00001049 // GNU typeof support.
1050 case tok::kw_typeof:
1051 ParseTypeofSpecifier(DS);
1052 continue;
1053
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001054 case tok::kw_decltype:
1055 ParseDecltypeSpecifier(DS);
1056 continue;
1057
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001058 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001059 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001060 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1061 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001062 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001063 goto DoneWithDeclSpec;
1064
1065 {
1066 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001067 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +00001068 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +00001069 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +00001070 DS.SetRangeEnd(EndProtoLoc);
1071
Chris Lattner1ab3b962008-11-18 07:48:38 +00001072 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001073 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001074 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001075 // Need to support trailing type qualifiers (e.g. "id<p> const").
1076 // If a type specifier follows, it will be diagnosed elsewhere.
1077 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001078 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 }
1080 // If the specifier combination wasn't legal, issue a diagnostic.
1081 if (isInvalid) {
1082 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001083 // Pick between error or extwarn.
1084 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1085 : diag::ext_duplicate_declspec;
1086 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001088 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 ConsumeToken();
1090 }
1091}
Douglas Gregoradcac882008-12-01 23:54:00 +00001092
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001093/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001094/// primarily follow the C++ grammar with additions for C99 and GNU,
1095/// which together subsume the C grammar. Note that the C++
1096/// type-specifier also includes the C type-qualifier (for const,
1097/// volatile, and C99 restrict). Returns true if a type-specifier was
1098/// found (and parsed), false otherwise.
1099///
1100/// type-specifier: [C++ 7.1.5]
1101/// simple-type-specifier
1102/// class-specifier
1103/// enum-specifier
1104/// elaborated-type-specifier [TODO]
1105/// cv-qualifier
1106///
1107/// cv-qualifier: [C++ 7.1.5.1]
1108/// 'const'
1109/// 'volatile'
1110/// [C99] 'restrict'
1111///
1112/// simple-type-specifier: [ C++ 7.1.5.2]
1113/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1114/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1115/// 'char'
1116/// 'wchar_t'
1117/// 'bool'
1118/// 'short'
1119/// 'int'
1120/// 'long'
1121/// 'signed'
1122/// 'unsigned'
1123/// 'float'
1124/// 'double'
1125/// 'void'
1126/// [C99] '_Bool'
1127/// [C99] '_Complex'
1128/// [C99] '_Imaginary' // Removed in TC2?
1129/// [GNU] '_Decimal32'
1130/// [GNU] '_Decimal64'
1131/// [GNU] '_Decimal128'
1132/// [GNU] typeof-specifier
1133/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1134/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001135/// [C++0x] 'decltype' ( expression )
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001136bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1137 const char *&PrevSpec,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001138 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001139 SourceLocation Loc = Tok.getLocation();
1140
1141 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001142 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001143 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001144 // Annotate typenames and C++ scope specifiers. If we get one, just
1145 // recurse to handle whatever we get.
1146 if (TryAnnotateTypeOrScopeToken())
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001147 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001148 // Otherwise, not a type specifier.
1149 return false;
1150 case tok::coloncolon: // ::foo::bar
1151 if (NextToken().is(tok::kw_new) || // ::new
1152 NextToken().is(tok::kw_delete)) // ::delete
1153 return false;
1154
1155 // Annotate typenames and C++ scope specifiers. If we get one, just
1156 // recurse to handle whatever we get.
1157 if (TryAnnotateTypeOrScopeToken())
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001158 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001159 // Otherwise, not a type specifier.
1160 return false;
1161
Douglas Gregor12e083c2008-11-07 15:42:26 +00001162 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001163 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001164 if (Tok.getAnnotationValue())
1165 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1166 Tok.getAnnotationValue());
1167 else
1168 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001169 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1170 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +00001171
1172 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1173 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1174 // Objective-C interface. If we don't have Objective-C or a '<', this is
1175 // just a normal reference to a typedef name.
1176 if (!Tok.is(tok::less) || !getLang().ObjC1)
1177 return true;
1178
1179 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001180 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001181 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +00001182 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001183
1184 DS.SetRangeEnd(EndProtoLoc);
1185 return true;
1186 }
1187
1188 case tok::kw_short:
1189 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1190 break;
1191 case tok::kw_long:
1192 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1193 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1194 else
1195 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1196 break;
1197 case tok::kw_signed:
1198 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1199 break;
1200 case tok::kw_unsigned:
1201 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1202 break;
1203 case tok::kw__Complex:
1204 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1205 break;
1206 case tok::kw__Imaginary:
1207 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1208 break;
1209 case tok::kw_void:
1210 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1211 break;
1212 case tok::kw_char:
1213 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1214 break;
1215 case tok::kw_int:
1216 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1217 break;
1218 case tok::kw_float:
1219 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1220 break;
1221 case tok::kw_double:
1222 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1223 break;
1224 case tok::kw_wchar_t:
1225 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1226 break;
1227 case tok::kw_bool:
1228 case tok::kw__Bool:
1229 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1230 break;
1231 case tok::kw__Decimal32:
1232 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1233 break;
1234 case tok::kw__Decimal64:
1235 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1236 break;
1237 case tok::kw__Decimal128:
1238 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1239 break;
1240
1241 // class-specifier:
1242 case tok::kw_class:
1243 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001244 case tok::kw_union: {
1245 tok::TokenKind Kind = Tok.getKind();
1246 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001247 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001248 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001249 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001250
1251 // enum-specifier:
1252 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001253 ConsumeToken();
1254 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001255 return true;
1256
1257 // cv-qualifier:
1258 case tok::kw_const:
1259 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1260 getLang())*2;
1261 break;
1262 case tok::kw_volatile:
1263 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1264 getLang())*2;
1265 break;
1266 case tok::kw_restrict:
1267 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1268 getLang())*2;
1269 break;
1270
1271 // GNU typeof support.
1272 case tok::kw_typeof:
1273 ParseTypeofSpecifier(DS);
1274 return true;
1275
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001276 // C++0x decltype support.
1277 case tok::kw_decltype:
1278 ParseDecltypeSpecifier(DS);
1279 return true;
1280
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001281 // C++0x auto support.
1282 case tok::kw_auto:
1283 if (!getLang().CPlusPlus0x)
1284 return false;
1285
1286 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec);
1287 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001288 case tok::kw___ptr64:
1289 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001290 case tok::kw___cdecl:
1291 case tok::kw___stdcall:
1292 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001293 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001294 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001295
Douglas Gregor12e083c2008-11-07 15:42:26 +00001296 default:
1297 // Not a type-specifier; do nothing.
1298 return false;
1299 }
1300
1301 // If the specifier combination wasn't legal, issue a diagnostic.
1302 if (isInvalid) {
1303 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001304 // Pick between error or extwarn.
1305 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1306 : diag::ext_duplicate_declspec;
1307 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001308 }
1309 DS.SetRangeEnd(Tok.getLocation());
1310 ConsumeToken(); // whatever we parsed above.
1311 return true;
1312}
Reid Spencer5f016e22007-07-11 17:01:13 +00001313
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001314/// ParseStructDeclaration - Parse a struct declaration without the terminating
1315/// semicolon.
1316///
Reid Spencer5f016e22007-07-11 17:01:13 +00001317/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001318/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001319/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001320/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001321/// struct-declarator-list:
1322/// struct-declarator
1323/// struct-declarator-list ',' struct-declarator
1324/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1325/// struct-declarator:
1326/// declarator
1327/// [GNU] declarator attributes[opt]
1328/// declarator[opt] ':' constant-expression
1329/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1330///
Chris Lattnere1359422008-04-10 06:46:29 +00001331void Parser::
1332ParseStructDeclaration(DeclSpec &DS,
1333 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001334 if (Tok.is(tok::kw___extension__)) {
1335 // __extension__ silences extension warnings in the subexpression.
1336 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001337 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001338 return ParseStructDeclaration(DS, Fields);
1339 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001340
1341 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001342 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001343 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001344
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001345 // If there are no declarators, this is a free-standing declaration
1346 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001347 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001348 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001349 return;
1350 }
1351
1352 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001353 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001354 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001355 FieldDeclarator &DeclaratorInfo = Fields.back();
1356
Steve Naroff28a7ca82007-08-20 22:28:22 +00001357 /// struct-declarator: declarator
1358 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001359 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001360 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001361
Chris Lattner04d66662007-10-09 17:33:22 +00001362 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001363 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001364 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001365 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001366 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001367 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001368 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001369 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001370
Steve Naroff28a7ca82007-08-20 22:28:22 +00001371 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001372 if (Tok.is(tok::kw___attribute)) {
1373 SourceLocation Loc;
1374 AttributeList *AttrList = ParseAttributes(&Loc);
1375 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1376 }
1377
Steve Naroff28a7ca82007-08-20 22:28:22 +00001378 // If we don't have a comma, it is either the end of the list (a ';')
1379 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001380 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001381 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001382
Steve Naroff28a7ca82007-08-20 22:28:22 +00001383 // Consume the comma.
1384 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001385
Steve Naroff28a7ca82007-08-20 22:28:22 +00001386 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001387 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001388
Steve Naroff28a7ca82007-08-20 22:28:22 +00001389 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001390 if (Tok.is(tok::kw___attribute)) {
1391 SourceLocation Loc;
1392 AttributeList *AttrList = ParseAttributes(&Loc);
1393 Fields.back().D.AddAttributes(AttrList, Loc);
1394 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001395 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001396}
1397
1398/// ParseStructUnionBody
1399/// struct-contents:
1400/// struct-declaration-list
1401/// [EXT] empty
1402/// [GNU] "struct-declaration-list" without terminatoring ';'
1403/// struct-declaration-list:
1404/// struct-declaration
1405/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001406/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001407///
Reid Spencer5f016e22007-07-11 17:01:13 +00001408void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001409 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001410 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1411 PP.getSourceManager(),
1412 "parsing struct/union body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001413
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 SourceLocation LBraceLoc = ConsumeBrace();
1415
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001416 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001417 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1418
Reid Spencer5f016e22007-07-11 17:01:13 +00001419 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1420 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001421 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001422 Diag(Tok, diag::ext_empty_struct_union_enum)
1423 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001424
Chris Lattnerb28317a2009-03-28 19:18:32 +00001425 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001426 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1427
Reid Spencer5f016e22007-07-11 17:01:13 +00001428 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001429 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 // Each iteration of this loop reads one struct-declaration.
1431
1432 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001433 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001434 Diag(Tok, diag::ext_extra_struct_semi)
1435 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 ConsumeToken();
1437 continue;
1438 }
Chris Lattnere1359422008-04-10 06:46:29 +00001439
1440 // Parse all the comma separated declarators.
1441 DeclSpec DS;
1442 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001443 if (!Tok.is(tok::at)) {
1444 ParseStructDeclaration(DS, FieldDeclarators);
1445
1446 // Convert them all to fields.
1447 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1448 FieldDeclarator &FD = FieldDeclarators[i];
1449 // Install the declarator into the current TagDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001450 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1451 DS.getSourceRange().getBegin(),
1452 FD.D, FD.BitfieldSize);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001453 FieldDecls.push_back(Field);
1454 }
1455 } else { // Handle @defs
1456 ConsumeToken();
1457 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1458 Diag(Tok, diag::err_unexpected_at);
1459 SkipUntil(tok::semi, true, true);
1460 continue;
1461 }
1462 ConsumeToken();
1463 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1464 if (!Tok.is(tok::identifier)) {
1465 Diag(Tok, diag::err_expected_ident);
1466 SkipUntil(tok::semi, true, true);
1467 continue;
1468 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001469 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001470 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1471 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001472 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1473 ConsumeToken();
1474 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1475 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001476
Chris Lattner04d66662007-10-09 17:33:22 +00001477 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001478 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001479 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001480 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001481 break;
1482 } else {
1483 Diag(Tok, diag::err_expected_semi_decl_list);
1484 // Skip to end of block or statement
1485 SkipUntil(tok::r_brace, true, true);
1486 }
1487 }
1488
Steve Naroff60fccee2007-10-29 21:38:07 +00001489 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001490
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 AttributeList *AttrList = 0;
1492 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001493 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001494 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001495
1496 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001497 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001498 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001499 AttrList);
1500 StructScope.Exit();
1501 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001502}
1503
1504
1505/// ParseEnumSpecifier
1506/// enum-specifier: [C99 6.7.2.2]
1507/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001508///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001509/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1510/// '}' attributes[opt]
1511/// 'enum' identifier
1512/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001513///
1514/// [C++] elaborated-type-specifier:
1515/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1516///
Chris Lattner4c97d762009-04-12 21:49:30 +00001517void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1518 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001520
1521 AttributeList *Attr = 0;
1522 // If attributes exist after tag, parse them.
1523 if (Tok.is(tok::kw___attribute))
1524 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001525
1526 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001527 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001528 if (Tok.isNot(tok::identifier)) {
1529 Diag(Tok, diag::err_expected_ident);
1530 if (Tok.isNot(tok::l_brace)) {
1531 // Has no name and is not a definition.
1532 // Skip the rest of this declarator, up until the comma or semicolon.
1533 SkipUntil(tok::comma, true);
1534 return;
1535 }
1536 }
1537 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001538
1539 // Must have either 'enum name' or 'enum {...}'.
1540 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1541 Diag(Tok, diag::err_expected_ident_lbrace);
1542
1543 // Skip the rest of this declarator, up until the comma or semicolon.
1544 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001545 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001546 }
1547
1548 // If an identifier is present, consume and remember it.
1549 IdentifierInfo *Name = 0;
1550 SourceLocation NameLoc;
1551 if (Tok.is(tok::identifier)) {
1552 Name = Tok.getIdentifierInfo();
1553 NameLoc = ConsumeToken();
1554 }
1555
1556 // There are three options here. If we have 'enum foo;', then this is a
1557 // forward declaration. If we have 'enum foo {...' then this is a
1558 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1559 //
1560 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1561 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1562 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1563 //
1564 Action::TagKind TK;
1565 if (Tok.is(tok::l_brace))
1566 TK = Action::TK_Definition;
1567 else if (Tok.is(tok::semi))
1568 TK = Action::TK_Declaration;
1569 else
1570 TK = Action::TK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001571 bool Owned = false;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001572 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001573 StartLoc, SS, Name, NameLoc, Attr, AS,
1574 Owned);
Reid Spencer5f016e22007-07-11 17:01:13 +00001575
Chris Lattner04d66662007-10-09 17:33:22 +00001576 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001577 ParseEnumBody(StartLoc, TagDecl);
1578
1579 // TODO: semantic analysis on the declspec for enums.
1580 const char *PrevSpec = 0;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001581 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
Douglas Gregor402abb52009-05-28 23:31:59 +00001582 TagDecl.getAs<void>(), Owned))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001583 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001584}
1585
1586/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1587/// enumerator-list:
1588/// enumerator
1589/// enumerator-list ',' enumerator
1590/// enumerator:
1591/// enumeration-constant
1592/// enumeration-constant '=' constant-expression
1593/// enumeration-constant:
1594/// identifier
1595///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001596void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001597 // Enter the scope of the enum body and start the definition.
1598 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001599 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001600
Reid Spencer5f016e22007-07-11 17:01:13 +00001601 SourceLocation LBraceLoc = ConsumeBrace();
1602
Chris Lattner7946dd32007-08-27 17:24:30 +00001603 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001604 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001605 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001606
Chris Lattnerb28317a2009-03-28 19:18:32 +00001607 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001608
Chris Lattnerb28317a2009-03-28 19:18:32 +00001609 DeclPtrTy LastEnumConstDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001610
1611 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001612 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001613 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1614 SourceLocation IdentLoc = ConsumeToken();
1615
1616 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001617 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001618 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001619 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001620 AssignedVal = ParseConstantExpression();
1621 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001623 }
1624
1625 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001626 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1627 LastEnumConstDecl,
1628 IdentLoc, Ident,
1629 EqualLoc,
1630 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001631 EnumConstantDecls.push_back(EnumConstDecl);
1632 LastEnumConstDecl = EnumConstDecl;
1633
Chris Lattner04d66662007-10-09 17:33:22 +00001634 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001635 break;
1636 SourceLocation CommaLoc = ConsumeToken();
1637
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001638 if (Tok.isNot(tok::identifier) &&
1639 !(getLang().C99 || getLang().CPlusPlus0x))
1640 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1641 << getLang().CPlusPlus
1642 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001643 }
1644
1645 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001646 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001647
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001648 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001649 EnumConstantDecls.data(), EnumConstantDecls.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001650
Chris Lattnerb28317a2009-03-28 19:18:32 +00001651 Action::AttrTy *AttrList = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001652 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001653 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001654 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001655
1656 EnumScope.Exit();
1657 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001658}
1659
1660/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001661/// start of a type-qualifier-list.
1662bool Parser::isTypeQualifier() const {
1663 switch (Tok.getKind()) {
1664 default: return false;
1665 // type-qualifier
1666 case tok::kw_const:
1667 case tok::kw_volatile:
1668 case tok::kw_restrict:
1669 return true;
1670 }
1671}
1672
1673/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001674/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001675bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001676 switch (Tok.getKind()) {
1677 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001678
1679 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001680 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001681 // Annotate typenames and C++ scope specifiers. If we get one, just
1682 // recurse to handle whatever we get.
1683 if (TryAnnotateTypeOrScopeToken())
1684 return isTypeSpecifierQualifier();
1685 // Otherwise, not a type specifier.
1686 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001687
Chris Lattner166a8fc2009-01-04 23:41:41 +00001688 case tok::coloncolon: // ::foo::bar
1689 if (NextToken().is(tok::kw_new) || // ::new
1690 NextToken().is(tok::kw_delete)) // ::delete
1691 return false;
1692
1693 // Annotate typenames and C++ scope specifiers. If we get one, just
1694 // recurse to handle whatever we get.
1695 if (TryAnnotateTypeOrScopeToken())
1696 return isTypeSpecifierQualifier();
1697 // Otherwise, not a type specifier.
1698 return false;
1699
Reid Spencer5f016e22007-07-11 17:01:13 +00001700 // GNU attributes support.
1701 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001702 // GNU typeof support.
1703 case tok::kw_typeof:
1704
Reid Spencer5f016e22007-07-11 17:01:13 +00001705 // type-specifiers
1706 case tok::kw_short:
1707 case tok::kw_long:
1708 case tok::kw_signed:
1709 case tok::kw_unsigned:
1710 case tok::kw__Complex:
1711 case tok::kw__Imaginary:
1712 case tok::kw_void:
1713 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001714 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001715 case tok::kw_int:
1716 case tok::kw_float:
1717 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001718 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001719 case tok::kw__Bool:
1720 case tok::kw__Decimal32:
1721 case tok::kw__Decimal64:
1722 case tok::kw__Decimal128:
1723
Chris Lattner99dc9142008-04-13 18:59:07 +00001724 // struct-or-union-specifier (C99) or class-specifier (C++)
1725 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001726 case tok::kw_struct:
1727 case tok::kw_union:
1728 // enum-specifier
1729 case tok::kw_enum:
1730
1731 // type-qualifier
1732 case tok::kw_const:
1733 case tok::kw_volatile:
1734 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001735
1736 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001737 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001738 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001739
1740 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1741 case tok::less:
1742 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001743
1744 case tok::kw___cdecl:
1745 case tok::kw___stdcall:
1746 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001747 case tok::kw___w64:
1748 case tok::kw___ptr64:
1749 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001750 }
1751}
1752
1753/// isDeclarationSpecifier() - Return true if the current token is part of a
1754/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001755bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001756 switch (Tok.getKind()) {
1757 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001758
1759 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001760 // Unfortunate hack to support "Class.factoryMethod" notation.
1761 if (getLang().ObjC1 && NextToken().is(tok::period))
1762 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001763 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001764
Douglas Gregord57959a2009-03-27 23:10:48 +00001765 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001766 // Annotate typenames and C++ scope specifiers. If we get one, just
1767 // recurse to handle whatever we get.
1768 if (TryAnnotateTypeOrScopeToken())
1769 return isDeclarationSpecifier();
1770 // Otherwise, not a declaration specifier.
1771 return false;
1772 case tok::coloncolon: // ::foo::bar
1773 if (NextToken().is(tok::kw_new) || // ::new
1774 NextToken().is(tok::kw_delete)) // ::delete
1775 return false;
1776
1777 // Annotate typenames and C++ scope specifiers. If we get one, just
1778 // recurse to handle whatever we get.
1779 if (TryAnnotateTypeOrScopeToken())
1780 return isDeclarationSpecifier();
1781 // Otherwise, not a declaration specifier.
1782 return false;
1783
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 // storage-class-specifier
1785 case tok::kw_typedef:
1786 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001787 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001788 case tok::kw_static:
1789 case tok::kw_auto:
1790 case tok::kw_register:
1791 case tok::kw___thread:
1792
1793 // type-specifiers
1794 case tok::kw_short:
1795 case tok::kw_long:
1796 case tok::kw_signed:
1797 case tok::kw_unsigned:
1798 case tok::kw__Complex:
1799 case tok::kw__Imaginary:
1800 case tok::kw_void:
1801 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001802 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 case tok::kw_int:
1804 case tok::kw_float:
1805 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001806 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001807 case tok::kw__Bool:
1808 case tok::kw__Decimal32:
1809 case tok::kw__Decimal64:
1810 case tok::kw__Decimal128:
1811
Chris Lattner99dc9142008-04-13 18:59:07 +00001812 // struct-or-union-specifier (C99) or class-specifier (C++)
1813 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001814 case tok::kw_struct:
1815 case tok::kw_union:
1816 // enum-specifier
1817 case tok::kw_enum:
1818
1819 // type-qualifier
1820 case tok::kw_const:
1821 case tok::kw_volatile:
1822 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001823
Reid Spencer5f016e22007-07-11 17:01:13 +00001824 // function-specifier
1825 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001826 case tok::kw_virtual:
1827 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001828
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001829 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001830 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001831
Chris Lattner1ef08762007-08-09 17:01:07 +00001832 // GNU typeof support.
1833 case tok::kw_typeof:
1834
1835 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001836 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001837 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001838
1839 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1840 case tok::less:
1841 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001842
Steve Naroff47f52092009-01-06 19:34:12 +00001843 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001844 case tok::kw___cdecl:
1845 case tok::kw___stdcall:
1846 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001847 case tok::kw___w64:
1848 case tok::kw___ptr64:
1849 case tok::kw___forceinline:
1850 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001851 }
1852}
1853
1854
1855/// ParseTypeQualifierListOpt
1856/// type-qualifier-list: [C99 6.7.5]
1857/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001858/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001859/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001860/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001861///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001862void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001863 while (1) {
1864 int isInvalid = false;
1865 const char *PrevSpec = 0;
1866 SourceLocation Loc = Tok.getLocation();
1867
1868 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001869 case tok::kw_const:
1870 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1871 getLang())*2;
1872 break;
1873 case tok::kw_volatile:
1874 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1875 getLang())*2;
1876 break;
1877 case tok::kw_restrict:
1878 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1879 getLang())*2;
1880 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001881 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001882 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001883 case tok::kw___cdecl:
1884 case tok::kw___stdcall:
1885 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001886 if (AttributesAllowed) {
1887 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1888 continue;
1889 }
1890 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00001891 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001892 if (AttributesAllowed) {
1893 DS.AddAttributes(ParseAttributes());
1894 continue; // do *not* consume the next token!
1895 }
1896 // otherwise, FALL THROUGH!
1897 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001898 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001899 // If this is not a type-qualifier token, we're done reading type
1900 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001901 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001902 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001903 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001904
Reid Spencer5f016e22007-07-11 17:01:13 +00001905 // If the specifier combination wasn't legal, issue a diagnostic.
1906 if (isInvalid) {
1907 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001908 // Pick between error or extwarn.
1909 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1910 : diag::ext_duplicate_declspec;
1911 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001912 }
1913 ConsumeToken();
1914 }
1915}
1916
1917
1918/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1919///
1920void Parser::ParseDeclarator(Declarator &D) {
1921 /// This implements the 'declarator' production in the C grammar, then checks
1922 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001923 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001924}
1925
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001926/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1927/// is parsed by the function passed to it. Pass null, and the direct-declarator
1928/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001929/// ptr-operator production.
1930///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001931/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1932/// [C] pointer[opt] direct-declarator
1933/// [C++] direct-declarator
1934/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001935///
1936/// pointer: [C99 6.7.5]
1937/// '*' type-qualifier-list[opt]
1938/// '*' type-qualifier-list[opt] pointer
1939///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001940/// ptr-operator:
1941/// '*' cv-qualifier-seq[opt]
1942/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00001943/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001944/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00001945/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001946/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001947void Parser::ParseDeclaratorInternal(Declarator &D,
1948 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001949
Sebastian Redlf30208a2009-01-24 21:16:55 +00001950 // C++ member pointers start with a '::' or a nested-name.
1951 // Member pointers get special handling, since there's no place for the
1952 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00001953 if (getLang().CPlusPlus &&
1954 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1955 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001956 CXXScopeSpec SS;
1957 if (ParseOptionalCXXScopeSpecifier(SS)) {
1958 if(Tok.isNot(tok::star)) {
1959 // The scope spec really belongs to the direct-declarator.
1960 D.getCXXScopeSpec() = SS;
1961 if (DirectDeclParser)
1962 (this->*DirectDeclParser)(D);
1963 return;
1964 }
1965
1966 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001967 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001968 DeclSpec DS;
1969 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001970 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001971
1972 // Recurse to parse whatever is left.
1973 ParseDeclaratorInternal(D, DirectDeclParser);
1974
1975 // Sema will have to catch (syntactically invalid) pointers into global
1976 // scope. It has to catch pointers into namespace scope anyway.
1977 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001978 Loc, DS.TakeAttributes()),
1979 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00001980 return;
1981 }
1982 }
1983
1984 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001985 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00001986 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00001987 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00001988 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00001989 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001990 if (DirectDeclParser)
1991 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001992 return;
1993 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001994
Sebastian Redl05532f22009-03-15 22:02:01 +00001995 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1996 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00001997 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001998 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001999
Chris Lattner9af55002009-03-27 04:18:06 +00002000 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002001 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002002 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002003
Reid Spencer5f016e22007-07-11 17:01:13 +00002004 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002005 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002006
Reid Spencer5f016e22007-07-11 17:01:13 +00002007 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002008 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002009 if (Kind == tok::star)
2010 // Remember that we parsed a pointer type, and remember the type-quals.
2011 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002012 DS.TakeAttributes()),
2013 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002014 else
2015 // Remember that we parsed a Block type, and remember the type-quals.
2016 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002017 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002018 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002019 } else {
2020 // Is a reference
2021 DeclSpec DS;
2022
Sebastian Redl743de1f2009-03-23 00:00:23 +00002023 // Complain about rvalue references in C++03, but then go on and build
2024 // the declarator.
2025 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2026 Diag(Loc, diag::err_rvalue_reference);
2027
Reid Spencer5f016e22007-07-11 17:01:13 +00002028 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2029 // cv-qualifiers are introduced through the use of a typedef or of a
2030 // template type argument, in which case the cv-qualifiers are ignored.
2031 //
2032 // [GNU] Retricted references are allowed.
2033 // [GNU] Attributes on references are allowed.
2034 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002035 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002036
2037 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2038 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2039 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002040 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002041 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2042 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002043 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002044 }
2045
2046 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002047 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002048
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002049 if (D.getNumTypeObjects() > 0) {
2050 // C++ [dcl.ref]p4: There shall be no references to references.
2051 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2052 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002053 if (const IdentifierInfo *II = D.getIdentifier())
2054 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2055 << II;
2056 else
2057 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2058 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002059
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002060 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002061 // can go ahead and build the (technically ill-formed)
2062 // declarator: reference collapsing will take care of it.
2063 }
2064 }
2065
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002067 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002068 DS.TakeAttributes(),
2069 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002070 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002071 }
2072}
2073
2074/// ParseDirectDeclarator
2075/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002076/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002077/// '(' declarator ')'
2078/// [GNU] '(' attributes declarator ')'
2079/// [C90] direct-declarator '[' constant-expression[opt] ']'
2080/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2081/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2082/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2083/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2084/// direct-declarator '(' parameter-type-list ')'
2085/// direct-declarator '(' identifier-list[opt] ')'
2086/// [GNU] direct-declarator '(' parameter-forward-declarations
2087/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002088/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2089/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002090/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002091///
2092/// declarator-id: [C++ 8]
2093/// id-expression
2094/// '::'[opt] nested-name-specifier[opt] type-name
2095///
2096/// id-expression: [C++ 5.1]
2097/// unqualified-id
2098/// qualified-id [TODO]
2099///
2100/// unqualified-id: [C++ 5.1]
2101/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002102/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002103/// conversion-function-id [TODO]
2104/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002105/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002106///
Reid Spencer5f016e22007-07-11 17:01:13 +00002107void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002108 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002109
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002110 if (getLang().CPlusPlus) {
2111 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002112 // ParseDeclaratorInternal might already have parsed the scope.
2113 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2114 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002115 if (afterCXXScope) {
2116 // Change the declaration context for name lookup, until this function
2117 // is exited (and the declarator has been parsed).
2118 DeclScopeObj.EnterDeclaratorScope();
2119 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002120
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002121 if (Tok.is(tok::identifier)) {
2122 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlsson4649cac2009-04-30 22:41:11 +00002123
2124 // If this identifier is the name of the current class, it's a
2125 // constructor name.
2126 if (!D.getDeclSpec().hasTypeSpecifier() &&
2127 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
2128 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
2129 Tok.getLocation(), CurScope),
2130 Tok.getLocation());
2131 // This is a normal identifier.
2132 } else
2133 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002134 ConsumeToken();
2135 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00002136 } else if (Tok.is(tok::annot_template_id)) {
2137 TemplateIdAnnotation *TemplateId
2138 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2139
2140 // FIXME: Could this template-id name a constructor?
2141
2142 // FIXME: This is an egregious hack, where we silently ignore
2143 // the specialization (which should be a function template
2144 // specialization name) and use the name instead. This hack
2145 // will go away when we have support for function
2146 // specializations.
2147 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2148 TemplateId->Destroy();
2149 ConsumeToken();
2150 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00002151 } else if (Tok.is(tok::kw_operator)) {
2152 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002153 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002154
Douglas Gregor70316a02008-12-26 15:00:45 +00002155 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00002156 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2157 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00002158 } else {
2159 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00002160 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2161 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2162 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00002163 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002164 }
Douglas Gregor70316a02008-12-26 15:00:45 +00002165 }
2166 goto PastIdentifier;
2167 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002168 // This should be a C++ destructor.
2169 SourceLocation TildeLoc = ConsumeToken();
2170 if (Tok.is(tok::identifier)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002171 // FIXME: Inaccurate.
2172 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00002173 SourceLocation EndLoc;
Douglas Gregor31a19b62009-04-01 21:51:26 +00002174 TypeResult Type = ParseClassName(EndLoc);
2175 if (Type.isInvalid())
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002176 D.SetIdentifier(0, TildeLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002177 else
2178 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002179 } else {
2180 Diag(Tok, diag::err_expected_class_name);
2181 D.SetIdentifier(0, TildeLoc);
2182 }
2183 goto PastIdentifier;
2184 }
2185
2186 // If we reached this point, token is not identifier and not '~'.
2187
2188 if (afterCXXScope) {
2189 Diag(Tok, diag::err_expected_unqualified_id);
2190 D.SetIdentifier(0, Tok.getLocation());
2191 D.setInvalidType(true);
2192 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002193 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002194 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002195 }
2196
2197 // If we reached this point, we are either in C/ObjC or the token didn't
2198 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002199 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2200 assert(!getLang().CPlusPlus &&
2201 "There's a C++-specific check for tok::identifier above");
2202 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2203 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2204 ConsumeToken();
2205 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002206 // direct-declarator: '(' declarator ')'
2207 // direct-declarator: '(' attributes declarator ')'
2208 // Example: 'char (*X)' or 'int (*XX)(void)'
2209 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002210 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002211 // This could be something simple like "int" (in which case the declarator
2212 // portion is empty), if an abstract-declarator is allowed.
2213 D.SetIdentifier(0, Tok.getLocation());
2214 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002215 if (D.getContext() == Declarator::MemberContext)
2216 Diag(Tok, diag::err_expected_member_name_or_semi)
2217 << D.getDeclSpec().getSourceRange();
2218 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002219 Diag(Tok, diag::err_expected_unqualified_id);
2220 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002221 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002222 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002223 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002224 }
2225
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002226 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002227 assert(D.isPastIdentifier() &&
2228 "Haven't past the location of the identifier yet?");
2229
2230 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002231 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002232 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2233 // In such a case, check if we actually have a function declarator; if it
2234 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002235 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2236 // When not in file scope, warn for ambiguous function declarators, just
2237 // in case the author intended it as a variable definition.
2238 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2239 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2240 break;
2241 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002242 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002243 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002244 ParseBracketDeclarator(D);
2245 } else {
2246 break;
2247 }
2248 }
2249}
2250
Chris Lattneref4715c2008-04-06 05:45:57 +00002251/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2252/// only called before the identifier, so these are most likely just grouping
2253/// parens for precedence. If we find that these are actually function
2254/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2255///
2256/// direct-declarator:
2257/// '(' declarator ')'
2258/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002259/// direct-declarator '(' parameter-type-list ')'
2260/// direct-declarator '(' identifier-list[opt] ')'
2261/// [GNU] direct-declarator '(' parameter-forward-declarations
2262/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002263///
2264void Parser::ParseParenDeclarator(Declarator &D) {
2265 SourceLocation StartLoc = ConsumeParen();
2266 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2267
Chris Lattner7399ee02008-10-20 02:05:46 +00002268 // Eat any attributes before we look at whether this is a grouping or function
2269 // declarator paren. If this is a grouping paren, the attribute applies to
2270 // the type being built up, for example:
2271 // int (__attribute__(()) *x)(long y)
2272 // If this ends up not being a grouping paren, the attribute applies to the
2273 // first argument, for example:
2274 // int (__attribute__(()) int x)
2275 // In either case, we need to eat any attributes to be able to determine what
2276 // sort of paren this is.
2277 //
2278 AttributeList *AttrList = 0;
2279 bool RequiresArg = false;
2280 if (Tok.is(tok::kw___attribute)) {
2281 AttrList = ParseAttributes();
2282
2283 // We require that the argument list (if this is a non-grouping paren) be
2284 // present even if the attribute list was empty.
2285 RequiresArg = true;
2286 }
Steve Naroff239f0732008-12-25 14:16:32 +00002287 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002288 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2289 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2290 Tok.is(tok::kw___ptr64)) {
2291 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2292 }
Chris Lattner7399ee02008-10-20 02:05:46 +00002293
Chris Lattneref4715c2008-04-06 05:45:57 +00002294 // If we haven't past the identifier yet (or where the identifier would be
2295 // stored, if this is an abstract declarator), then this is probably just
2296 // grouping parens. However, if this could be an abstract-declarator, then
2297 // this could also be the start of function arguments (consider 'void()').
2298 bool isGrouping;
2299
2300 if (!D.mayOmitIdentifier()) {
2301 // If this can't be an abstract-declarator, this *must* be a grouping
2302 // paren, because we haven't seen the identifier yet.
2303 isGrouping = true;
2304 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002305 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002306 isDeclarationSpecifier()) { // 'int(int)' is a function.
2307 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2308 // considered to be a type, not a K&R identifier-list.
2309 isGrouping = false;
2310 } else {
2311 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2312 isGrouping = true;
2313 }
2314
2315 // If this is a grouping paren, handle:
2316 // direct-declarator: '(' declarator ')'
2317 // direct-declarator: '(' attributes declarator ')'
2318 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002319 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002320 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002321 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002322 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002323
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002324 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002325 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002326 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002327
2328 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002329 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002330 return;
2331 }
2332
2333 // Okay, if this wasn't a grouping paren, it must be the start of a function
2334 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002335 // identifier (and remember where it would have been), then call into
2336 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002337 D.SetIdentifier(0, Tok.getLocation());
2338
Chris Lattner7399ee02008-10-20 02:05:46 +00002339 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002340}
2341
2342/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2343/// declarator D up to a paren, which indicates that we are parsing function
2344/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002345///
Chris Lattner7399ee02008-10-20 02:05:46 +00002346/// If AttrList is non-null, then the caller parsed those arguments immediately
2347/// after the open paren - they should be considered to be the first argument of
2348/// a parameter. If RequiresArg is true, then the first argument of the
2349/// function is required to be present and required to not be an identifier
2350/// list.
2351///
Reid Spencer5f016e22007-07-11 17:01:13 +00002352/// This method also handles this portion of the grammar:
2353/// parameter-type-list: [C99 6.7.5]
2354/// parameter-list
2355/// parameter-list ',' '...'
2356///
2357/// parameter-list: [C99 6.7.5]
2358/// parameter-declaration
2359/// parameter-list ',' parameter-declaration
2360///
2361/// parameter-declaration: [C99 6.7.5]
2362/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002363/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002364/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002365/// declaration-specifiers abstract-declarator[opt]
2366/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002367/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002368/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2369///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002370/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002371/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002372///
Chris Lattner7399ee02008-10-20 02:05:46 +00002373void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2374 AttributeList *AttrList,
2375 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002376 // lparen is already consumed!
2377 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00002378
Chris Lattner7399ee02008-10-20 02:05:46 +00002379 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002380 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002381 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002382 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002383 delete AttrList;
2384 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002385
Sebastian Redlab197ba2009-02-09 18:23:29 +00002386 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002387
2388 // cv-qualifier-seq[opt].
2389 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002390 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002391 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002392 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002393 llvm::SmallVector<TypeTy*, 2> Exceptions;
2394 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002395 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002396 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002397 if (!DS.getSourceRange().getEnd().isInvalid())
2398 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002399
2400 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002401 if (Tok.is(tok::kw_throw)) {
2402 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002403 ThrowLoc = Tok.getLocation();
Sebastian Redlef65f062009-05-29 18:02:33 +00002404 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2405 hasAnyExceptionSpec);
2406 assert(Exceptions.size() == ExceptionRanges.size() &&
2407 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002408 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002409 }
2410
Chris Lattnerf97409f2008-04-06 06:57:35 +00002411 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002412 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002413 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002414 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002415 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002416 /*arglist*/ 0, 0,
2417 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002418 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002419 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002420 Exceptions.data(),
2421 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002422 Exceptions.size(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002423 LParenLoc, D),
2424 Loc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002425 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002426 }
2427
Chris Lattner7399ee02008-10-20 02:05:46 +00002428 // Alternatively, this parameter list may be an identifier list form for a
2429 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002430 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002431 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002432 // K&R identifier lists can't have typedefs as identifiers, per
2433 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002434 if (RequiresArg) {
2435 Diag(Tok, diag::err_argument_required_after_attribute);
2436 delete AttrList;
2437 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002438 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2439 // normal declarators, not for abstract-declarators.
2440 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002441 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002442 }
2443
2444 // Finally, a normal, non-empty parameter type list.
2445
2446 // Build up an array of information about the parsed arguments.
2447 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002448
2449 // Enter function-declaration scope, limiting any declarators to the
2450 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002451 ParseScope PrototypeScope(this,
2452 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002453
2454 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002455 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002456 while (1) {
2457 if (Tok.is(tok::ellipsis)) {
2458 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002459 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002460 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002461 }
2462
Chris Lattnerf97409f2008-04-06 06:57:35 +00002463 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002464
Chris Lattnerf97409f2008-04-06 06:57:35 +00002465 // Parse the declaration-specifiers.
2466 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002467
2468 // If the caller parsed attributes for the first argument, add them now.
2469 if (AttrList) {
2470 DS.AddAttributes(AttrList);
2471 AttrList = 0; // Only apply the attributes to the first parameter.
2472 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002473 ParseDeclarationSpecifiers(DS);
2474
Chris Lattnerf97409f2008-04-06 06:57:35 +00002475 // Parse the declarator. This is "PrototypeContext", because we must
2476 // accept either 'declarator' or 'abstract-declarator' here.
2477 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2478 ParseDeclarator(ParmDecl);
2479
2480 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002481 if (Tok.is(tok::kw___attribute)) {
2482 SourceLocation Loc;
2483 AttributeList *AttrList = ParseAttributes(&Loc);
2484 ParmDecl.AddAttributes(AttrList, Loc);
2485 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002486
Chris Lattnerf97409f2008-04-06 06:57:35 +00002487 // Remember this parsed parameter in ParamInfo.
2488 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2489
Douglas Gregor72b505b2008-12-16 21:30:33 +00002490 // DefArgToks is used when the parsing of default arguments needs
2491 // to be delayed.
2492 CachedTokens *DefArgToks = 0;
2493
Chris Lattnerf97409f2008-04-06 06:57:35 +00002494 // If no parameter was specified, verify that *something* was specified,
2495 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002496 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2497 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002498 // Completely missing, emit error.
2499 Diag(DSStart, diag::err_missing_param);
2500 } else {
2501 // Otherwise, we have something. Add it and let semantic analysis try
2502 // to grok it and add the result to the ParamInfo we are building.
2503
2504 // Inform the actions module about the parameter declarator, so it gets
2505 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002506 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002507
2508 // Parse the default argument, if any. We parse the default
2509 // arguments in all dialects; the semantic analysis in
2510 // ActOnParamDefaultArgument will reject the default argument in
2511 // C.
2512 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002513 SourceLocation EqualLoc = Tok.getLocation();
2514
Chris Lattner04421082008-04-08 04:40:51 +00002515 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002516 if (D.getContext() == Declarator::MemberContext) {
2517 // If we're inside a class definition, cache the tokens
2518 // corresponding to the default argument. We'll actually parse
2519 // them when we see the end of the class definition.
2520 // FIXME: Templates will require something similar.
2521 // FIXME: Can we use a smart pointer for Toks?
2522 DefArgToks = new CachedTokens;
2523
2524 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2525 tok::semi, false)) {
2526 delete DefArgToks;
2527 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002528 Actions.ActOnParamDefaultArgumentError(Param);
2529 } else
Anders Carlsson5e300d12009-06-12 16:51:40 +00002530 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
2531 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002532 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002533 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002534 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002535
2536 OwningExprResult DefArgResult(ParseAssignmentExpression());
2537 if (DefArgResult.isInvalid()) {
2538 Actions.ActOnParamDefaultArgumentError(Param);
2539 SkipUntil(tok::comma, tok::r_paren, true, true);
2540 } else {
2541 // Inform the actions module about the default argument
2542 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002543 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002544 }
Chris Lattner04421082008-04-08 04:40:51 +00002545 }
2546 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002547
2548 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002549 ParmDecl.getIdentifierLoc(), Param,
2550 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002551 }
2552
2553 // If the next token is a comma, consume it and keep reading arguments.
2554 if (Tok.isNot(tok::comma)) break;
2555
2556 // Consume the comma.
2557 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002558 }
2559
Chris Lattnerf97409f2008-04-06 06:57:35 +00002560 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002561 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002562
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002563 // If we have the closing ')', eat it.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002564 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002565
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002566 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002567 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002568 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002569 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002570 llvm::SmallVector<TypeTy*, 2> Exceptions;
2571 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002572 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002573 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002574 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002575 if (!DS.getSourceRange().getEnd().isInvalid())
2576 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002577
2578 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002579 if (Tok.is(tok::kw_throw)) {
2580 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002581 ThrowLoc = Tok.getLocation();
Sebastian Redlef65f062009-05-29 18:02:33 +00002582 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2583 hasAnyExceptionSpec);
2584 assert(Exceptions.size() == ExceptionRanges.size() &&
2585 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002586 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002587 }
2588
Reid Spencer5f016e22007-07-11 17:01:13 +00002589 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002590 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002591 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002592 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002593 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002594 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002595 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002596 Exceptions.data(),
2597 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002598 Exceptions.size(), LParenLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002599 Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002600}
2601
Chris Lattner66d28652008-04-06 06:34:08 +00002602/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2603/// we found a K&R-style identifier list instead of a type argument list. The
2604/// current token is known to be the first identifier in the list.
2605///
2606/// identifier-list: [C99 6.7.5]
2607/// identifier
2608/// identifier-list ',' identifier
2609///
2610void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2611 Declarator &D) {
2612 // Build up an array of information about the parsed arguments.
2613 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2614 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2615
2616 // If there was no identifier specified for the declarator, either we are in
2617 // an abstract-declarator, or we are in a parameter declarator which was found
2618 // to be abstract. In abstract-declarators, identifier lists are not valid:
2619 // diagnose this.
2620 if (!D.getIdentifier())
2621 Diag(Tok, diag::ext_ident_list_in_param);
2622
2623 // Tok is known to be the first identifier in the list. Remember this
2624 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002625 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002626 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002627 Tok.getLocation(),
2628 DeclPtrTy()));
Chris Lattner66d28652008-04-06 06:34:08 +00002629
Chris Lattner50c64772008-04-06 06:39:19 +00002630 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002631
2632 while (Tok.is(tok::comma)) {
2633 // Eat the comma.
2634 ConsumeToken();
2635
Chris Lattner50c64772008-04-06 06:39:19 +00002636 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002637 if (Tok.isNot(tok::identifier)) {
2638 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002639 SkipUntil(tok::r_paren);
2640 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002641 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002642
Chris Lattner66d28652008-04-06 06:34:08 +00002643 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002644
2645 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002646 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002647 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002648
2649 // Verify that the argument identifier has not already been mentioned.
2650 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002651 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002652 } else {
2653 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002654 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002655 Tok.getLocation(),
2656 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002657 }
Chris Lattner66d28652008-04-06 06:34:08 +00002658
2659 // Eat the identifier.
2660 ConsumeToken();
2661 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002662
2663 // If we have the closing ')', eat it and we're done.
2664 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2665
Chris Lattner50c64772008-04-06 06:39:19 +00002666 // Remember that we parsed a function type, and remember the attributes. This
2667 // function type is always a K&R style function type, which is not varargs and
2668 // has no prototype.
2669 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002670 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002671 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002672 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002673 /*exception*/false,
2674 SourceLocation(), false, 0, 0, 0,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002675 LParenLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002676 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002677}
Chris Lattneref4715c2008-04-06 05:45:57 +00002678
Reid Spencer5f016e22007-07-11 17:01:13 +00002679/// [C90] direct-declarator '[' constant-expression[opt] ']'
2680/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2681/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2682/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2683/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2684void Parser::ParseBracketDeclarator(Declarator &D) {
2685 SourceLocation StartLoc = ConsumeBracket();
2686
Chris Lattner378c7e42008-12-18 07:27:21 +00002687 // C array syntax has many features, but by-far the most common is [] and [4].
2688 // This code does a fast path to handle some of the most obvious cases.
2689 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002690 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002691 // Remember that we parsed the empty array type.
2692 OwningExprResult NumElements(Actions);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002693 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2694 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002695 return;
2696 } else if (Tok.getKind() == tok::numeric_constant &&
2697 GetLookAheadToken(1).is(tok::r_square)) {
2698 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002699 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002700 ConsumeToken();
2701
Sebastian Redlab197ba2009-02-09 18:23:29 +00002702 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002703
2704 // If there was an error parsing the assignment-expression, recover.
2705 if (ExprRes.isInvalid())
2706 ExprRes.release(); // Deallocate expr, just use [].
2707
2708 // Remember that we parsed a array type, and remember its features.
2709 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002710 ExprRes.release(), StartLoc),
2711 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002712 return;
2713 }
2714
Reid Spencer5f016e22007-07-11 17:01:13 +00002715 // If valid, this location is the position where we read the 'static' keyword.
2716 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002717 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002718 StaticLoc = ConsumeToken();
2719
2720 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002721 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002722 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002723 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002724
2725 // If we haven't already read 'static', check to see if there is one after the
2726 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002727 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002728 StaticLoc = ConsumeToken();
2729
2730 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2731 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002732 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002733
2734 // Handle the case where we have '[*]' as the array size. However, a leading
2735 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2736 // the the token after the star is a ']'. Since stars in arrays are
2737 // infrequent, use of lookahead is not costly here.
2738 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002739 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002740
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002741 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002742 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002743 StaticLoc = SourceLocation(); // Drop the static.
2744 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002745 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002746 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002747 // Note, in C89, this production uses the constant-expr production instead
2748 // of assignment-expr. The only difference is that assignment-expr allows
2749 // things like '=' and '*='. Sema rejects these in C89 mode because they
2750 // are not i-c-e's, so we don't need to distinguish between the two here.
2751
Douglas Gregore0762c92009-06-19 23:52:42 +00002752 // Parse the constant-expression or assignment-expression now (depending
2753 // on dialect).
2754 if (getLang().CPlusPlus)
2755 NumElements = ParseConstantExpression();
2756 else
2757 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002758 }
2759
2760 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002761 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002762 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002763 // If the expression was invalid, skip it.
2764 SkipUntil(tok::r_square);
2765 return;
2766 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002767
2768 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2769
Chris Lattner378c7e42008-12-18 07:27:21 +00002770 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002771 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2772 StaticLoc.isValid(), isStar,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002773 NumElements.release(), StartLoc),
2774 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002775}
2776
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002777/// [GNU] typeof-specifier:
2778/// typeof ( expressions )
2779/// typeof ( type-name )
2780/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002781///
2782void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002783 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002784 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002785 SourceLocation StartLoc = ConsumeToken();
2786
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002787 bool isCastExpr;
2788 TypeTy *CastTy;
2789 SourceRange CastRange;
2790 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2791 isCastExpr,
2792 CastTy,
2793 CastRange);
2794
2795 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002796 // FIXME: Not accurate, the range gets one token more than it should.
2797 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002798 else
2799 DS.SetRangeEnd(CastRange.getEnd());
2800
2801 if (isCastExpr) {
2802 if (!CastTy) {
2803 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002804 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002805 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002806
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002807 const char *PrevSpec = 0;
2808 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2809 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2810 CastTy))
2811 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2812 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002813 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002814
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002815 // If we get here, the operand to the typeof was an expresion.
2816 if (Operand.isInvalid()) {
2817 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002818 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002819 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002820
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002821 const char *PrevSpec = 0;
2822 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2823 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
2824 Operand.release()))
2825 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002826}