blob: 6e68b20fa5e34a947644fb7de7de60161b0efaa0 [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"
Sebastian Redla55e52c2008-11-25 22:21:31 +000018#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Douglas Gregor809070a2009-02-18 17:45:20 +000031Action::TypeResult Parser::ParseTypeName() {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Douglas Gregor809070a2009-02-18 17:45:20 +000040 if (DeclaratorInfo.getInvalidType())
41 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000044}
45
46/// ParseAttributes - Parse a non-empty attributes list.
47///
48/// [GNU] attributes:
49/// attribute
50/// attributes attribute
51///
52/// [GNU] attribute:
53/// '__attribute__' '(' '(' attribute-list ')' ')'
54///
55/// [GNU] attribute-list:
56/// attrib
57/// attribute_list ',' attrib
58///
59/// [GNU] attrib:
60/// empty
61/// attrib-name
62/// attrib-name '(' identifier ')'
63/// attrib-name '(' identifier ',' nonempty-expr-list ')'
64/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
65///
66/// [GNU] attrib-name:
67/// identifier
68/// typespec
69/// typequal
70/// storageclass
71///
72/// FIXME: The GCC grammar/code for this construct implies we need two
73/// token lookahead. Comment from gcc: "If they start with an identifier
74/// which is followed by a comma or close parenthesis, then the arguments
75/// start with that identifier; otherwise they are an expression list."
76///
77/// At the moment, I am not doing 2 token lookahead. I am also unaware of
78/// any attributes that don't work (based on my limited testing). Most
79/// attributes are very simple in practice. Until we find a bug, I don't see
80/// a pressing need to implement the 2 token lookahead.
81
Sebastian Redlab197ba2009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000084
85 AttributeList *CurrAttr = 0;
86
Chris Lattner04d66662007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000088 ConsumeToken();
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
90 "attribute")) {
91 SkipUntil(tok::r_paren, true); // skip until ) or ;
92 return CurrAttr;
93 }
94 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
96 return CurrAttr;
97 }
98 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000101
Chris Lattner04d66662007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
104 ConsumeToken();
105 continue;
106 }
107 // we have an identifier or declaration specifier (const, int, etc.)
108 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
109 SourceLocation AttrNameLoc = ConsumeToken();
110
111 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 ConsumeParen(); // ignore the left paren loc for now
114
Chris Lattner04d66662007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner04d66662007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 // __attribute__(( mode(byte) ))
121 ConsumeParen(); // ignore the right paren loc for now
122 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
123 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 }
Chris Lattner04d66662007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner04d66662007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 // __attribute__(( nonnull() ))
154 ConsumeParen(); // ignore the right paren loc for now
155 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
156 0, SourceLocation(), 0, 0, CurrAttr);
157 } else {
158 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 }
Chris Lattner04d66662007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 CurrAttr);
182 }
183 }
184 }
185 } else {
186 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
187 0, SourceLocation(), 0, 0, CurrAttr);
188 }
189 }
190 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000192 SourceLocation Loc = Tok.getLocation();;
193 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
194 SkipUntil(tok::r_paren, false);
195 }
196 if (EndLoc)
197 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 }
199 return CurrAttr;
200}
201
Steve Narofff59e17e2008-12-24 20:59:21 +0000202/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
203/// routine is called to skip/ignore tokens that comprise the MS declspec.
204void Parser::FuzzyParseMicrosoftDeclSpec() {
205 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
206 ConsumeToken();
207 if (Tok.is(tok::l_paren)) {
208 unsigned short savedParenCount = ParenCount;
209 do {
210 ConsumeAnyToken();
211 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
212 }
213 return;
214}
215
Reid Spencer5f016e22007-07-11 17:01:13 +0000216/// ParseDeclaration - Parse a full 'declaration', which consists of
217/// declaration-specifiers, some number of declarators, and a semicolon.
218/// 'Context' should be a Declarator::TheContext value.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000219///
220/// declaration: [C99 6.7]
221/// block-declaration ->
222/// simple-declaration
223/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000224/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000225/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000226/// [C++] using-directive
227/// [C++] using-declaration [TODO]
Chris Lattner8f08cb72007-08-25 06:57:03 +0000228/// others... [FIXME]
229///
Reid Spencer5f016e22007-07-11 17:01:13 +0000230Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner8f08cb72007-08-25 06:57:03 +0000231 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000232 case tok::kw_export:
233 case tok::kw_template:
Douglas Gregorcc636682009-02-17 23:15:12 +0000234 return ParseTemplateDeclarationOrSpecialization(Context);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000235 case tok::kw_namespace:
236 return ParseNamespace(Context);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000237 case tok::kw_using:
238 return ParseUsingDirectiveOrDeclaration(Context);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000239 default:
240 return ParseSimpleDeclaration(Context);
241 }
242}
243
244/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
245/// declaration-specifiers init-declarator-list[opt] ';'
246///[C90/C++]init-declarator-list ';' [TODO]
247/// [OMP] threadprivate-directive [TODO]
248Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 // Parse the common declaration-specifiers piece.
250 DeclSpec DS;
251 ParseDeclarationSpecifiers(DS);
252
253 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
254 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000255 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 ConsumeToken();
257 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
258 }
259
260 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
261 ParseDeclarator(DeclaratorInfo);
262
263 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
264}
265
Chris Lattner8f08cb72007-08-25 06:57:03 +0000266
Reid Spencer5f016e22007-07-11 17:01:13 +0000267/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
268/// parsing 'declaration-specifiers declarator'. This method is split out this
269/// way to handle the ambiguity between top-level function-definitions and
270/// declarations.
271///
Reid Spencer5f016e22007-07-11 17:01:13 +0000272/// init-declarator-list: [C99 6.7]
273/// init-declarator
274/// init-declarator-list ',' init-declarator
275/// init-declarator: [C99 6.7]
276/// declarator
277/// declarator '=' initializer
278/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
279/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000280/// [C++] declarator initializer[opt]
281///
282/// [C++] initializer:
283/// [C++] '=' initializer-clause
284/// [C++] '(' expression-list ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000285///
286Parser::DeclTy *Parser::
287ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
288
289 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
290 // that they can be chained properly if the actions want this.
291 Parser::DeclTy *LastDeclInGroup = 0;
292
293 // At this point, we know that it is not a function definition. Parse the
294 // rest of the init-declarator-list.
295 while (1) {
296 // If a simple-asm-expr is present, parse it.
Daniel Dunbara80f8742008-08-05 01:35:17 +0000297 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000298 SourceLocation Loc;
299 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000300 if (AsmLabel.isInvalid()) {
Daniel Dunbara80f8742008-08-05 01:35:17 +0000301 SkipUntil(tok::semi);
302 return 0;
303 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000304
Sebastian Redleffa8d12008-12-10 00:02:53 +0000305 D.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000306 D.SetRangeEnd(Loc);
Daniel Dunbara80f8742008-08-05 01:35:17 +0000307 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000308
309 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000310 if (Tok.is(tok::kw___attribute)) {
311 SourceLocation Loc;
312 AttributeList *AttrList = ParseAttributes(&Loc);
313 D.AddAttributes(AttrList, Loc);
314 }
Steve Naroffbb204692007-09-12 14:07:44 +0000315
316 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar914701e2008-08-05 16:28:08 +0000317 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000318
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // Parse declarator '=' initializer.
Chris Lattner04d66662007-10-09 17:33:22 +0000320 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000322 OwningExprResult Init(ParseInitializer());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000323 if (Init.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 SkipUntil(tok::semi);
325 return 0;
326 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000327 Actions.AddInitializerToDecl(LastDeclInGroup, move(Init));
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000328 } else if (Tok.is(tok::l_paren)) {
329 // Parse C++ direct initializer: '(' expression-list ')'
330 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000331 ExprVector Exprs(Actions);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000332 CommaLocsTy CommaLocs;
333
334 bool InvalidExpr = false;
335 if (ParseExpressionList(Exprs, CommaLocs)) {
336 SkipUntil(tok::r_paren);
337 InvalidExpr = true;
338 }
339 // Match the ')'.
340 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
341
342 if (!InvalidExpr) {
343 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
344 "Unexpected number of commas!");
345 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000346 Exprs.take(), Exprs.size(),
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000347 &CommaLocs[0], RParenLoc);
348 }
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000349 } else {
350 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 }
352
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 // If we don't have a comma, it is either the end of the list (a ';') or an
354 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000355 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 break;
357
358 // Consume the comma.
359 ConsumeToken();
360
361 // Parse the next declarator.
362 D.clear();
Chris Lattneraab740a2008-10-20 04:57:38 +0000363
364 // Accept attributes in an init-declarator. In the first declarator in a
365 // declaration, these would be part of the declspec. In subsequent
366 // declarators, they become part of the declarator itself, so that they
367 // don't apply to declarators after *this* one. Examples:
368 // short __attribute__((common)) var; -> declspec
369 // short var __attribute__((common)); -> declarator
370 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlab197ba2009-02-09 18:23:29 +0000371 if (Tok.is(tok::kw___attribute)) {
372 SourceLocation Loc;
373 AttributeList *AttrList = ParseAttributes(&Loc);
374 D.AddAttributes(AttrList, Loc);
375 }
Chris Lattneraab740a2008-10-20 04:57:38 +0000376
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 ParseDeclarator(D);
378 }
379
Chris Lattner04d66662007-10-09 17:33:22 +0000380 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 ConsumeToken();
Fariborz Jahanian41f2b322009-01-17 00:00:40 +0000382 // for(is key; in keys) is error.
383 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
384 Diag(Tok, diag::err_parse_error);
385 return 0;
386 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
388 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000389 // If this is an ObjC2 for-each loop, this is a successful declarator
390 // parse. The syntax for these looks like:
391 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000392 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000393 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
394 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 Diag(Tok, diag::err_parse_error);
396 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000397 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000398 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000399 ConsumeToken();
400 return 0;
401}
402
403/// ParseSpecifierQualifierList
404/// specifier-qualifier-list:
405/// type-specifier specifier-qualifier-list[opt]
406/// type-qualifier specifier-qualifier-list[opt]
407/// [GNU] attributes specifier-qualifier-list[opt]
408///
409void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
410 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
411 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 ParseDeclarationSpecifiers(DS);
413
414 // Validate declspec for type-name.
415 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000416 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 Diag(Tok, diag::err_typename_requires_specqual);
418
419 // Issue diagnostic and remove storage class if present.
420 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
421 if (DS.getStorageClassSpecLoc().isValid())
422 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
423 else
424 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
425 DS.ClearStorageClassSpecs();
426 }
427
428 // Issue diagnostic and remove function specfier if present.
429 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000430 if (DS.isInlineSpecified())
431 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
432 if (DS.isVirtualSpecified())
433 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
434 if (DS.isExplicitSpecified())
435 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 DS.ClearFunctionSpecs();
437 }
438}
439
440/// ParseDeclarationSpecifiers
441/// declaration-specifiers: [C99 6.7]
442/// storage-class-specifier declaration-specifiers[opt]
443/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000444/// [C99] function-specifier declaration-specifiers[opt]
445/// [GNU] attributes declaration-specifiers[opt]
446///
447/// storage-class-specifier: [C99 6.7.1]
448/// 'typedef'
449/// 'extern'
450/// 'static'
451/// 'auto'
452/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000453/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000454/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000455/// function-specifier: [C99 6.7.4]
456/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000457/// [C++] 'virtual'
458/// [C++] 'explicit'
Reid Spencer5f016e22007-07-11 17:01:13 +0000459///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000460void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Chris Lattner5e02c472009-01-05 00:07:25 +0000461 TemplateParameterLists *TemplateParams){
Chris Lattner81c018d2008-03-13 06:29:04 +0000462 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 while (1) {
464 int isInvalid = false;
465 const char *PrevSpec = 0;
466 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000469 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000470 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 // If this is not a declaration specifier token, we're done reading decl
472 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000473 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 return;
Chris Lattner5e02c472009-01-05 00:07:25 +0000475
476 case tok::coloncolon: // ::foo::bar
477 // Annotate C++ scope specifiers. If we get one, loop.
478 if (TryAnnotateCXXScopeToken())
479 continue;
480 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000481
482 case tok::annot_cxxscope: {
483 if (DS.hasTypeSpecifier())
484 goto DoneWithDeclSpec;
485
486 // We are looking for a qualified typename.
487 if (NextToken().isNot(tok::identifier))
488 goto DoneWithDeclSpec;
489
490 CXXScopeSpec SS;
491 SS.setScopeRep(Tok.getAnnotationValue());
492 SS.setRange(Tok.getAnnotationRange());
493
494 // If the next token is the name of the class type that the C++ scope
495 // denotes, followed by a '(', then this is a constructor declaration.
496 // We're done with the decl-specifiers.
497 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
498 CurScope, &SS) &&
499 GetLookAheadToken(2).is(tok::l_paren))
500 goto DoneWithDeclSpec;
501
Douglas Gregorb696ea32009-02-04 17:00:24 +0000502 Token Next = NextToken();
503 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
504 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000505
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000506 if (TypeRep == 0)
507 goto DoneWithDeclSpec;
508
509 ConsumeToken(); // The C++ scope.
510
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000511 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000512 TypeRep);
513 if (isInvalid)
514 break;
515
516 DS.SetRangeEnd(Tok.getLocation());
517 ConsumeToken(); // The typename.
518
519 continue;
520 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000521
522 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000523 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner80d0c892009-01-21 19:48:37 +0000524 Tok.getAnnotationValue());
525 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
526 ConsumeToken(); // The typename
527
528 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
529 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
530 // Objective-C interface. If we don't have Objective-C or a '<', this is
531 // just a normal reference to a typedef name.
532 if (!Tok.is(tok::less) || !getLang().ObjC1)
533 continue;
534
535 SourceLocation EndProtoLoc;
536 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
537 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
538 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
539
540 DS.SetRangeEnd(EndProtoLoc);
541 continue;
542 }
543
Chris Lattner3bd934a2008-07-26 01:18:38 +0000544 // typedef-name
545 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000546 // In C++, check to see if this is a scope specifier like foo::bar::, if
547 // so handle it as such. This is important for ctor parsing.
Chris Lattner837acd02009-01-21 19:19:26 +0000548 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
549 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000550
Chris Lattner3bd934a2008-07-26 01:18:38 +0000551 // This identifier can only be a typedef name if we haven't already seen
552 // a type-specifier. Without this check we misparse:
553 // typedef int X; struct Y { short X; }; as 'short int'.
554 if (DS.hasTypeSpecifier())
555 goto DoneWithDeclSpec;
556
557 // It has to be available as a typedef too!
Douglas Gregorb696ea32009-02-04 17:00:24 +0000558 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
559 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000560
561 if (TypeRep == 0 && getLang().CPlusPlus && NextToken().is(tok::less)) {
562 // If we have a template name, annotate the token and try again.
563 DeclTy *Template = 0;
564 if (TemplateNameKind TNK =
565 Actions.isTemplateName(*Tok.getIdentifierInfo(), CurScope,
566 Template)) {
567 AnnotateTemplateIdToken(Template, TNK, 0);
568 continue;
569 }
570 }
571
Chris Lattner3bd934a2008-07-26 01:18:38 +0000572 if (TypeRep == 0)
573 goto DoneWithDeclSpec;
574
Douglas Gregor55f6b142009-02-09 18:46:07 +0000575
576
Douglas Gregorb48fe382008-10-31 09:07:45 +0000577 // C++: If the identifier is actually the name of the class type
578 // being defined and the next token is a '(', then this is a
579 // constructor declaration. We're done with the decl-specifiers
580 // and will treat this token as an identifier.
581 if (getLang().CPlusPlus &&
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000582 CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000583 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
584 NextToken().getKind() == tok::l_paren)
585 goto DoneWithDeclSpec;
586
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000587 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner3bd934a2008-07-26 01:18:38 +0000588 TypeRep);
589 if (isInvalid)
590 break;
591
592 DS.SetRangeEnd(Tok.getLocation());
593 ConsumeToken(); // The identifier
594
595 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
596 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
597 // Objective-C interface. If we don't have Objective-C or a '<', this is
598 // just a normal reference to a typedef name.
599 if (!Tok.is(tok::less) || !getLang().ObjC1)
600 continue;
601
602 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000603 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000604 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000605 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000606
607 DS.SetRangeEnd(EndProtoLoc);
608
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000609 // Need to support trailing type qualifiers (e.g. "id<p> const").
610 // If a type specifier follows, it will be diagnosed elsewhere.
611 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000612 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 // GNU attributes support.
614 case tok::kw___attribute:
615 DS.AddAttributes(ParseAttributes());
616 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000617
618 // Microsoft declspec support.
619 case tok::kw___declspec:
620 if (!PP.getLangOptions().Microsoft)
621 goto DoneWithDeclSpec;
622 FuzzyParseMicrosoftDeclSpec();
623 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000624
Steve Naroff239f0732008-12-25 14:16:32 +0000625 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000626 case tok::kw___forceinline:
627 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000628 case tok::kw___cdecl:
629 case tok::kw___stdcall:
630 case tok::kw___fastcall:
631 if (!PP.getLangOptions().Microsoft)
632 goto DoneWithDeclSpec;
633 // Just ignore it.
634 break;
635
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 // storage-class-specifier
637 case tok::kw_typedef:
638 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
639 break;
640 case tok::kw_extern:
641 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000642 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
644 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000645 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000646 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
647 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000648 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 case tok::kw_static:
650 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000651 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
653 break;
654 case tok::kw_auto:
655 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
656 break;
657 case tok::kw_register:
658 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
659 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000660 case tok::kw_mutable:
661 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
662 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 case tok::kw___thread:
664 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
665 break;
666
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 continue;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000668
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 // function-specifier
670 case tok::kw_inline:
671 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
672 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000673 case tok::kw_virtual:
674 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
675 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000676 case tok::kw_explicit:
677 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
678 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000679
680 // type-specifier
681 case tok::kw_short:
682 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
683 break;
684 case tok::kw_long:
685 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
686 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
687 else
688 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
689 break;
690 case tok::kw_signed:
691 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
692 break;
693 case tok::kw_unsigned:
694 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
695 break;
696 case tok::kw__Complex:
697 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
698 break;
699 case tok::kw__Imaginary:
700 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
701 break;
702 case tok::kw_void:
703 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
704 break;
705 case tok::kw_char:
706 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
707 break;
708 case tok::kw_int:
709 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
710 break;
711 case tok::kw_float:
712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
713 break;
714 case tok::kw_double:
715 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
716 break;
717 case tok::kw_wchar_t:
718 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
719 break;
720 case tok::kw_bool:
721 case tok::kw__Bool:
722 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
723 break;
724 case tok::kw__Decimal32:
725 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
726 break;
727 case tok::kw__Decimal64:
728 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
729 break;
730 case tok::kw__Decimal128:
731 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
732 break;
733
734 // class-specifier:
735 case tok::kw_class:
736 case tok::kw_struct:
737 case tok::kw_union:
738 ParseClassSpecifier(DS, TemplateParams);
739 continue;
740
741 // enum-specifier:
742 case tok::kw_enum:
743 ParseEnumSpecifier(DS);
744 continue;
745
746 // cv-qualifier:
747 case tok::kw_const:
748 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
749 break;
750 case tok::kw_volatile:
751 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
752 getLang())*2;
753 break;
754 case tok::kw_restrict:
755 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
756 getLang())*2;
757 break;
758
759 // GNU typeof support.
760 case tok::kw_typeof:
761 ParseTypeofSpecifier(DS);
762 continue;
763
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000764 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000765 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000766 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
767 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000768 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000769 goto DoneWithDeclSpec;
770
771 {
772 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000773 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000774 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000775 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000776 DS.SetRangeEnd(EndProtoLoc);
777
Chris Lattner1ab3b962008-11-18 07:48:38 +0000778 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
779 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000780 // Need to support trailing type qualifiers (e.g. "id<p> const").
781 // If a type specifier follows, it will be diagnosed elsewhere.
782 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000783 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 }
785 // If the specifier combination wasn't legal, issue a diagnostic.
786 if (isInvalid) {
787 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000788 // Pick between error or extwarn.
789 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
790 : diag::ext_duplicate_declspec;
791 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000793 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000794 ConsumeToken();
795 }
796}
Douglas Gregoradcac882008-12-01 23:54:00 +0000797
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000798/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +0000799/// primarily follow the C++ grammar with additions for C99 and GNU,
800/// which together subsume the C grammar. Note that the C++
801/// type-specifier also includes the C type-qualifier (for const,
802/// volatile, and C99 restrict). Returns true if a type-specifier was
803/// found (and parsed), false otherwise.
804///
805/// type-specifier: [C++ 7.1.5]
806/// simple-type-specifier
807/// class-specifier
808/// enum-specifier
809/// elaborated-type-specifier [TODO]
810/// cv-qualifier
811///
812/// cv-qualifier: [C++ 7.1.5.1]
813/// 'const'
814/// 'volatile'
815/// [C99] 'restrict'
816///
817/// simple-type-specifier: [ C++ 7.1.5.2]
818/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
819/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
820/// 'char'
821/// 'wchar_t'
822/// 'bool'
823/// 'short'
824/// 'int'
825/// 'long'
826/// 'signed'
827/// 'unsigned'
828/// 'float'
829/// 'double'
830/// 'void'
831/// [C99] '_Bool'
832/// [C99] '_Complex'
833/// [C99] '_Imaginary' // Removed in TC2?
834/// [GNU] '_Decimal32'
835/// [GNU] '_Decimal64'
836/// [GNU] '_Decimal128'
837/// [GNU] typeof-specifier
838/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
839/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000840bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
841 const char *&PrevSpec,
842 TemplateParameterLists *TemplateParams){
Douglas Gregor12e083c2008-11-07 15:42:26 +0000843 SourceLocation Loc = Tok.getLocation();
844
845 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +0000846 case tok::identifier: // foo::bar
847 // Annotate typenames and C++ scope specifiers. If we get one, just
848 // recurse to handle whatever we get.
849 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000850 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000851 // Otherwise, not a type specifier.
852 return false;
853 case tok::coloncolon: // ::foo::bar
854 if (NextToken().is(tok::kw_new) || // ::new
855 NextToken().is(tok::kw_delete)) // ::delete
856 return false;
857
858 // Annotate typenames and C++ scope specifiers. If we get one, just
859 // recurse to handle whatever we get.
860 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000861 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000862 // Otherwise, not a type specifier.
863 return false;
864
Douglas Gregor12e083c2008-11-07 15:42:26 +0000865 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000866 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000867 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000868 Tok.getAnnotationValue());
869 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
870 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +0000871
872 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
873 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
874 // Objective-C interface. If we don't have Objective-C or a '<', this is
875 // just a normal reference to a typedef name.
876 if (!Tok.is(tok::less) || !getLang().ObjC1)
877 return true;
878
879 SourceLocation EndProtoLoc;
880 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
881 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
882 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
883
884 DS.SetRangeEnd(EndProtoLoc);
885 return true;
886 }
887
888 case tok::kw_short:
889 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
890 break;
891 case tok::kw_long:
892 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
893 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
894 else
895 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
896 break;
897 case tok::kw_signed:
898 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
899 break;
900 case tok::kw_unsigned:
901 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
902 break;
903 case tok::kw__Complex:
904 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
905 break;
906 case tok::kw__Imaginary:
907 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
908 break;
909 case tok::kw_void:
910 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
911 break;
912 case tok::kw_char:
913 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
914 break;
915 case tok::kw_int:
916 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
917 break;
918 case tok::kw_float:
919 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
920 break;
921 case tok::kw_double:
922 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
923 break;
924 case tok::kw_wchar_t:
925 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
926 break;
927 case tok::kw_bool:
928 case tok::kw__Bool:
929 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
930 break;
931 case tok::kw__Decimal32:
932 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
933 break;
934 case tok::kw__Decimal64:
935 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
936 break;
937 case tok::kw__Decimal128:
938 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
939 break;
940
941 // class-specifier:
942 case tok::kw_class:
943 case tok::kw_struct:
944 case tok::kw_union:
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000945 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor12e083c2008-11-07 15:42:26 +0000946 return true;
947
948 // enum-specifier:
949 case tok::kw_enum:
950 ParseEnumSpecifier(DS);
951 return true;
952
953 // cv-qualifier:
954 case tok::kw_const:
955 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
956 getLang())*2;
957 break;
958 case tok::kw_volatile:
959 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
960 getLang())*2;
961 break;
962 case tok::kw_restrict:
963 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
964 getLang())*2;
965 break;
966
967 // GNU typeof support.
968 case tok::kw_typeof:
969 ParseTypeofSpecifier(DS);
970 return true;
971
Steve Naroff239f0732008-12-25 14:16:32 +0000972 case tok::kw___cdecl:
973 case tok::kw___stdcall:
974 case tok::kw___fastcall:
Chris Lattner837acd02009-01-21 19:19:26 +0000975 if (!PP.getLangOptions().Microsoft) return false;
976 ConsumeToken();
977 return true;
Steve Naroff239f0732008-12-25 14:16:32 +0000978
Douglas Gregor12e083c2008-11-07 15:42:26 +0000979 default:
980 // Not a type-specifier; do nothing.
981 return false;
982 }
983
984 // If the specifier combination wasn't legal, issue a diagnostic.
985 if (isInvalid) {
986 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000987 // Pick between error or extwarn.
988 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
989 : diag::ext_duplicate_declspec;
990 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000991 }
992 DS.SetRangeEnd(Tok.getLocation());
993 ConsumeToken(); // whatever we parsed above.
994 return true;
995}
Reid Spencer5f016e22007-07-11 17:01:13 +0000996
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000997/// ParseStructDeclaration - Parse a struct declaration without the terminating
998/// semicolon.
999///
Reid Spencer5f016e22007-07-11 17:01:13 +00001000/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001001/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001002/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001003/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001004/// struct-declarator-list:
1005/// struct-declarator
1006/// struct-declarator-list ',' struct-declarator
1007/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1008/// struct-declarator:
1009/// declarator
1010/// [GNU] declarator attributes[opt]
1011/// declarator[opt] ':' constant-expression
1012/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1013///
Chris Lattnere1359422008-04-10 06:46:29 +00001014void Parser::
1015ParseStructDeclaration(DeclSpec &DS,
1016 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001017 if (Tok.is(tok::kw___extension__)) {
1018 // __extension__ silences extension warnings in the subexpression.
1019 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001020 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001021 return ParseStructDeclaration(DS, Fields);
1022 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001023
1024 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001025 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001026 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001027
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001028 // If there are no declarators, this is a free-standing declaration
1029 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001030 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001031 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001032 return;
1033 }
1034
1035 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001036 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001037 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001038 FieldDeclarator &DeclaratorInfo = Fields.back();
1039
Steve Naroff28a7ca82007-08-20 22:28:22 +00001040 /// struct-declarator: declarator
1041 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001042 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001043 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001044
Chris Lattner04d66662007-10-09 17:33:22 +00001045 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001046 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001047 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001048 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001049 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001050 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001051 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001052 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001053
Steve Naroff28a7ca82007-08-20 22:28:22 +00001054 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001055 if (Tok.is(tok::kw___attribute)) {
1056 SourceLocation Loc;
1057 AttributeList *AttrList = ParseAttributes(&Loc);
1058 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1059 }
1060
Steve Naroff28a7ca82007-08-20 22:28:22 +00001061 // If we don't have a comma, it is either the end of the list (a ';')
1062 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001063 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001064 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001065
Steve Naroff28a7ca82007-08-20 22:28:22 +00001066 // Consume the comma.
1067 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001068
Steve Naroff28a7ca82007-08-20 22:28:22 +00001069 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001070 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001071
Steve Naroff28a7ca82007-08-20 22:28:22 +00001072 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001073 if (Tok.is(tok::kw___attribute)) {
1074 SourceLocation Loc;
1075 AttributeList *AttrList = ParseAttributes(&Loc);
1076 Fields.back().D.AddAttributes(AttrList, Loc);
1077 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001078 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001079}
1080
1081/// ParseStructUnionBody
1082/// struct-contents:
1083/// struct-declaration-list
1084/// [EXT] empty
1085/// [GNU] "struct-declaration-list" without terminatoring ';'
1086/// struct-declaration-list:
1087/// struct-declaration
1088/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001089/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001090///
Reid Spencer5f016e22007-07-11 17:01:13 +00001091void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1092 unsigned TagType, DeclTy *TagDecl) {
1093 SourceLocation LBraceLoc = ConsumeBrace();
1094
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001095 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001096 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1097
Reid Spencer5f016e22007-07-11 17:01:13 +00001098 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1099 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001100 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001101 Diag(Tok, diag::ext_empty_struct_union_enum)
1102 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001103
1104 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001105 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1106
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001108 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001109 // Each iteration of this loop reads one struct-declaration.
1110
1111 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001112 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001113 Diag(Tok, diag::ext_extra_struct_semi);
1114 ConsumeToken();
1115 continue;
1116 }
Chris Lattnere1359422008-04-10 06:46:29 +00001117
1118 // Parse all the comma separated declarators.
1119 DeclSpec DS;
1120 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001121 if (!Tok.is(tok::at)) {
1122 ParseStructDeclaration(DS, FieldDeclarators);
1123
1124 // Convert them all to fields.
1125 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1126 FieldDeclarator &FD = FieldDeclarators[i];
1127 // Install the declarator into the current TagDecl.
Douglas Gregor44b43212008-12-11 16:49:14 +00001128 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001129 DS.getSourceRange().getBegin(),
1130 FD.D, FD.BitfieldSize);
1131 FieldDecls.push_back(Field);
1132 }
1133 } else { // Handle @defs
1134 ConsumeToken();
1135 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1136 Diag(Tok, diag::err_unexpected_at);
1137 SkipUntil(tok::semi, true, true);
1138 continue;
1139 }
1140 ConsumeToken();
1141 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1142 if (!Tok.is(tok::identifier)) {
1143 Diag(Tok, diag::err_expected_ident);
1144 SkipUntil(tok::semi, true, true);
1145 continue;
1146 }
1147 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001148 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1149 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001150 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1151 ConsumeToken();
1152 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1153 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001154
Chris Lattner04d66662007-10-09 17:33:22 +00001155 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001157 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001158 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 break;
1160 } else {
1161 Diag(Tok, diag::err_expected_semi_decl_list);
1162 // Skip to end of block or statement
1163 SkipUntil(tok::r_brace, true, true);
1164 }
1165 }
1166
Steve Naroff60fccee2007-10-29 21:38:07 +00001167 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001168
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 AttributeList *AttrList = 0;
1170 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001171 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001172 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001173
1174 Actions.ActOnFields(CurScope,
1175 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1176 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001177 AttrList);
1178 StructScope.Exit();
1179 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001180}
1181
1182
1183/// ParseEnumSpecifier
1184/// enum-specifier: [C99 6.7.2.2]
1185/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001186///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001187/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1188/// '}' attributes[opt]
1189/// 'enum' identifier
1190/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001191///
1192/// [C++] elaborated-type-specifier:
1193/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1194///
Reid Spencer5f016e22007-07-11 17:01:13 +00001195void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001196 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 SourceLocation StartLoc = ConsumeToken();
1198
1199 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001200
1201 AttributeList *Attr = 0;
1202 // If attributes exist after tag, parse them.
1203 if (Tok.is(tok::kw___attribute))
1204 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001205
1206 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001207 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001208 if (Tok.isNot(tok::identifier)) {
1209 Diag(Tok, diag::err_expected_ident);
1210 if (Tok.isNot(tok::l_brace)) {
1211 // Has no name and is not a definition.
1212 // Skip the rest of this declarator, up until the comma or semicolon.
1213 SkipUntil(tok::comma, true);
1214 return;
1215 }
1216 }
1217 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001218
1219 // Must have either 'enum name' or 'enum {...}'.
1220 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1221 Diag(Tok, diag::err_expected_ident_lbrace);
1222
1223 // Skip the rest of this declarator, up until the comma or semicolon.
1224 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001226 }
1227
1228 // If an identifier is present, consume and remember it.
1229 IdentifierInfo *Name = 0;
1230 SourceLocation NameLoc;
1231 if (Tok.is(tok::identifier)) {
1232 Name = Tok.getIdentifierInfo();
1233 NameLoc = ConsumeToken();
1234 }
1235
1236 // There are three options here. If we have 'enum foo;', then this is a
1237 // forward declaration. If we have 'enum foo {...' then this is a
1238 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1239 //
1240 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1241 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1242 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1243 //
1244 Action::TagKind TK;
1245 if (Tok.is(tok::l_brace))
1246 TK = Action::TK_Definition;
1247 else if (Tok.is(tok::semi))
1248 TK = Action::TK_Declaration;
1249 else
1250 TK = Action::TK_Reference;
1251 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregorddc29e12009-02-06 22:42:48 +00001252 SS, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001253
Chris Lattner04d66662007-10-09 17:33:22 +00001254 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 ParseEnumBody(StartLoc, TagDecl);
1256
1257 // TODO: semantic analysis on the declspec for enums.
1258 const char *PrevSpec = 0;
1259 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001260 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001261}
1262
1263/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1264/// enumerator-list:
1265/// enumerator
1266/// enumerator-list ',' enumerator
1267/// enumerator:
1268/// enumeration-constant
1269/// enumeration-constant '=' constant-expression
1270/// enumeration-constant:
1271/// identifier
1272///
1273void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001274 // Enter the scope of the enum body and start the definition.
1275 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001276 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001277
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 SourceLocation LBraceLoc = ConsumeBrace();
1279
Chris Lattner7946dd32007-08-27 17:24:30 +00001280 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001281 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001282 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001283
1284 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1285
1286 DeclTy *LastEnumConstDecl = 0;
1287
1288 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001289 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1291 SourceLocation IdentLoc = ConsumeToken();
1292
1293 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001294 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001295 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001297 AssignedVal = ParseConstantExpression();
1298 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 }
1301
1302 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +00001303 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 LastEnumConstDecl,
1305 IdentLoc, Ident,
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001306 EqualLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001307 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 EnumConstantDecls.push_back(EnumConstDecl);
1309 LastEnumConstDecl = EnumConstDecl;
1310
Chris Lattner04d66662007-10-09 17:33:22 +00001311 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 break;
1313 SourceLocation CommaLoc = ConsumeToken();
1314
Chris Lattner04d66662007-10-09 17:33:22 +00001315 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1317 }
1318
1319 // Eat the }.
1320 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1321
Steve Naroff08d92e42007-09-15 18:49:24 +00001322 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 EnumConstantDecls.size());
1324
1325 DeclTy *AttrList = 0;
1326 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001327 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001328 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001329
1330 EnumScope.Exit();
1331 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001332}
1333
1334/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001335/// start of a type-qualifier-list.
1336bool Parser::isTypeQualifier() const {
1337 switch (Tok.getKind()) {
1338 default: return false;
1339 // type-qualifier
1340 case tok::kw_const:
1341 case tok::kw_volatile:
1342 case tok::kw_restrict:
1343 return true;
1344 }
1345}
1346
1347/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001348/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001349bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 switch (Tok.getKind()) {
1351 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001352
1353 case tok::identifier: // foo::bar
1354 // Annotate typenames and C++ scope specifiers. If we get one, just
1355 // recurse to handle whatever we get.
1356 if (TryAnnotateTypeOrScopeToken())
1357 return isTypeSpecifierQualifier();
1358 // Otherwise, not a type specifier.
1359 return false;
1360 case tok::coloncolon: // ::foo::bar
1361 if (NextToken().is(tok::kw_new) || // ::new
1362 NextToken().is(tok::kw_delete)) // ::delete
1363 return false;
1364
1365 // Annotate typenames and C++ scope specifiers. If we get one, just
1366 // recurse to handle whatever we get.
1367 if (TryAnnotateTypeOrScopeToken())
1368 return isTypeSpecifierQualifier();
1369 // Otherwise, not a type specifier.
1370 return false;
1371
Reid Spencer5f016e22007-07-11 17:01:13 +00001372 // GNU attributes support.
1373 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001374 // GNU typeof support.
1375 case tok::kw_typeof:
1376
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 // type-specifiers
1378 case tok::kw_short:
1379 case tok::kw_long:
1380 case tok::kw_signed:
1381 case tok::kw_unsigned:
1382 case tok::kw__Complex:
1383 case tok::kw__Imaginary:
1384 case tok::kw_void:
1385 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001386 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001387 case tok::kw_int:
1388 case tok::kw_float:
1389 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001390 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 case tok::kw__Bool:
1392 case tok::kw__Decimal32:
1393 case tok::kw__Decimal64:
1394 case tok::kw__Decimal128:
1395
Chris Lattner99dc9142008-04-13 18:59:07 +00001396 // struct-or-union-specifier (C99) or class-specifier (C++)
1397 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001398 case tok::kw_struct:
1399 case tok::kw_union:
1400 // enum-specifier
1401 case tok::kw_enum:
1402
1403 // type-qualifier
1404 case tok::kw_const:
1405 case tok::kw_volatile:
1406 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001407
1408 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001409 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001410 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001411
1412 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1413 case tok::less:
1414 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001415
1416 case tok::kw___cdecl:
1417 case tok::kw___stdcall:
1418 case tok::kw___fastcall:
1419 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 }
1421}
1422
1423/// isDeclarationSpecifier() - Return true if the current token is part of a
1424/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001425bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001426 switch (Tok.getKind()) {
1427 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001428
1429 case tok::identifier: // foo::bar
1430 // Annotate typenames and C++ scope specifiers. If we get one, just
1431 // recurse to handle whatever we get.
1432 if (TryAnnotateTypeOrScopeToken())
1433 return isDeclarationSpecifier();
1434 // Otherwise, not a declaration specifier.
1435 return false;
1436 case tok::coloncolon: // ::foo::bar
1437 if (NextToken().is(tok::kw_new) || // ::new
1438 NextToken().is(tok::kw_delete)) // ::delete
1439 return false;
1440
1441 // Annotate typenames and C++ scope specifiers. If we get one, just
1442 // recurse to handle whatever we get.
1443 if (TryAnnotateTypeOrScopeToken())
1444 return isDeclarationSpecifier();
1445 // Otherwise, not a declaration specifier.
1446 return false;
1447
Reid Spencer5f016e22007-07-11 17:01:13 +00001448 // storage-class-specifier
1449 case tok::kw_typedef:
1450 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001451 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001452 case tok::kw_static:
1453 case tok::kw_auto:
1454 case tok::kw_register:
1455 case tok::kw___thread:
1456
1457 // type-specifiers
1458 case tok::kw_short:
1459 case tok::kw_long:
1460 case tok::kw_signed:
1461 case tok::kw_unsigned:
1462 case tok::kw__Complex:
1463 case tok::kw__Imaginary:
1464 case tok::kw_void:
1465 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001466 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001467 case tok::kw_int:
1468 case tok::kw_float:
1469 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001470 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001471 case tok::kw__Bool:
1472 case tok::kw__Decimal32:
1473 case tok::kw__Decimal64:
1474 case tok::kw__Decimal128:
1475
Chris Lattner99dc9142008-04-13 18:59:07 +00001476 // struct-or-union-specifier (C99) or class-specifier (C++)
1477 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001478 case tok::kw_struct:
1479 case tok::kw_union:
1480 // enum-specifier
1481 case tok::kw_enum:
1482
1483 // type-qualifier
1484 case tok::kw_const:
1485 case tok::kw_volatile:
1486 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001487
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 // function-specifier
1489 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001490 case tok::kw_virtual:
1491 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001492
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001493 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001494 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001495
Chris Lattner1ef08762007-08-09 17:01:07 +00001496 // GNU typeof support.
1497 case tok::kw_typeof:
1498
1499 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001500 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001501 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001502
1503 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1504 case tok::less:
1505 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001506
Steve Naroff47f52092009-01-06 19:34:12 +00001507 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001508 case tok::kw___cdecl:
1509 case tok::kw___stdcall:
1510 case tok::kw___fastcall:
1511 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001512 }
1513}
1514
1515
1516/// ParseTypeQualifierListOpt
1517/// type-qualifier-list: [C99 6.7.5]
1518/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001519/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001520/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001521/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001522///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001523void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 while (1) {
1525 int isInvalid = false;
1526 const char *PrevSpec = 0;
1527 SourceLocation Loc = Tok.getLocation();
1528
1529 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001530 case tok::kw_const:
1531 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1532 getLang())*2;
1533 break;
1534 case tok::kw_volatile:
1535 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1536 getLang())*2;
1537 break;
1538 case tok::kw_restrict:
1539 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1540 getLang())*2;
1541 break;
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001542 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001543 case tok::kw___cdecl:
1544 case tok::kw___stdcall:
1545 case tok::kw___fastcall:
1546 if (!PP.getLangOptions().Microsoft)
1547 goto DoneWithTypeQuals;
1548 // Just ignore it.
1549 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001550 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001551 if (AttributesAllowed) {
1552 DS.AddAttributes(ParseAttributes());
1553 continue; // do *not* consume the next token!
1554 }
1555 // otherwise, FALL THROUGH!
1556 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001557 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001558 // If this is not a type-qualifier token, we're done reading type
1559 // qualifiers. First verify that DeclSpec's are consistent.
1560 DS.Finish(Diags, PP.getSourceManager(), getLang());
1561 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001562 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001563
Reid Spencer5f016e22007-07-11 17:01:13 +00001564 // If the specifier combination wasn't legal, issue a diagnostic.
1565 if (isInvalid) {
1566 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001567 // Pick between error or extwarn.
1568 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1569 : diag::ext_duplicate_declspec;
1570 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001571 }
1572 ConsumeToken();
1573 }
1574}
1575
1576
1577/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1578///
1579void Parser::ParseDeclarator(Declarator &D) {
1580 /// This implements the 'declarator' production in the C grammar, then checks
1581 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001582 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001583}
1584
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001585/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1586/// is parsed by the function passed to it. Pass null, and the direct-declarator
1587/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001588/// ptr-operator production.
1589///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001590/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1591/// [C] pointer[opt] direct-declarator
1592/// [C++] direct-declarator
1593/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001594///
1595/// pointer: [C99 6.7.5]
1596/// '*' type-qualifier-list[opt]
1597/// '*' type-qualifier-list[opt] pointer
1598///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001599/// ptr-operator:
1600/// '*' cv-qualifier-seq[opt]
1601/// '&'
1602/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001603/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001604void Parser::ParseDeclaratorInternal(Declarator &D,
1605 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001606
Sebastian Redlf30208a2009-01-24 21:16:55 +00001607 // C++ member pointers start with a '::' or a nested-name.
1608 // Member pointers get special handling, since there's no place for the
1609 // scope spec in the generic path below.
1610 if ((Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1611 Tok.is(tok::annot_cxxscope)) && getLang().CPlusPlus) {
1612 CXXScopeSpec SS;
1613 if (ParseOptionalCXXScopeSpecifier(SS)) {
1614 if(Tok.isNot(tok::star)) {
1615 // The scope spec really belongs to the direct-declarator.
1616 D.getCXXScopeSpec() = SS;
1617 if (DirectDeclParser)
1618 (this->*DirectDeclParser)(D);
1619 return;
1620 }
1621
1622 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001623 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001624 DeclSpec DS;
1625 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001626 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001627
1628 // Recurse to parse whatever is left.
1629 ParseDeclaratorInternal(D, DirectDeclParser);
1630
1631 // Sema will have to catch (syntactically invalid) pointers into global
1632 // scope. It has to catch pointers into namespace scope anyway.
1633 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001634 Loc, DS.TakeAttributes()),
1635 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00001636 return;
1637 }
1638 }
1639
1640 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001641 // Not a pointer, C++ reference, or block.
1642 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001643 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001644 if (DirectDeclParser)
1645 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001646 return;
1647 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001648
Steve Naroff4ef1c992008-08-28 10:07:06 +00001649 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001650 SourceLocation Loc = ConsumeToken(); // Eat the *, ^ or &.
1651 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001652
Steve Naroff4ef1c992008-08-28 10:07:06 +00001653 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner76549142008-02-21 01:32:26 +00001654 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001655 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001656
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001658 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001659
Reid Spencer5f016e22007-07-11 17:01:13 +00001660 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001661 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001662 if (Kind == tok::star)
1663 // Remember that we parsed a pointer type, and remember the type-quals.
1664 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001665 DS.TakeAttributes()),
1666 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00001667 else
1668 // Remember that we parsed a Block type, and remember the type-quals.
1669 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001670 Loc),
1671 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 } else {
1673 // Is a reference
1674 DeclSpec DS;
1675
1676 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1677 // cv-qualifiers are introduced through the use of a typedef or of a
1678 // template type argument, in which case the cv-qualifiers are ignored.
1679 //
1680 // [GNU] Retricted references are allowed.
1681 // [GNU] Attributes on references are allowed.
1682 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001683 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001684
1685 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1686 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1687 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001688 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001689 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1690 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001691 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001692 }
1693
1694 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001695 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001696
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001697 if (D.getNumTypeObjects() > 0) {
1698 // C++ [dcl.ref]p4: There shall be no references to references.
1699 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1700 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001701 if (const IdentifierInfo *II = D.getIdentifier())
1702 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1703 << II;
1704 else
1705 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1706 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001707
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001708 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001709 // can go ahead and build the (technically ill-formed)
1710 // declarator: reference collapsing will take care of it.
1711 }
1712 }
1713
Reid Spencer5f016e22007-07-11 17:01:13 +00001714 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001715 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001716 DS.TakeAttributes()),
1717 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001718 }
1719}
1720
1721/// ParseDirectDeclarator
1722/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001723/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001724/// '(' declarator ')'
1725/// [GNU] '(' attributes declarator ')'
1726/// [C90] direct-declarator '[' constant-expression[opt] ']'
1727/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1728/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1729/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1730/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1731/// direct-declarator '(' parameter-type-list ')'
1732/// direct-declarator '(' identifier-list[opt] ')'
1733/// [GNU] direct-declarator '(' parameter-forward-declarations
1734/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001735/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1736/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001737/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001738///
1739/// declarator-id: [C++ 8]
1740/// id-expression
1741/// '::'[opt] nested-name-specifier[opt] type-name
1742///
1743/// id-expression: [C++ 5.1]
1744/// unqualified-id
1745/// qualified-id [TODO]
1746///
1747/// unqualified-id: [C++ 5.1]
1748/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001749/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001750/// conversion-function-id [TODO]
1751/// '~' class-name
1752/// template-id [TODO]
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001753///
Reid Spencer5f016e22007-07-11 17:01:13 +00001754void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001755 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001756
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001757 if (getLang().CPlusPlus) {
1758 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001759 // ParseDeclaratorInternal might already have parsed the scope.
1760 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1761 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001762 if (afterCXXScope) {
1763 // Change the declaration context for name lookup, until this function
1764 // is exited (and the declarator has been parsed).
1765 DeclScopeObj.EnterDeclaratorScope();
1766 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001767
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001768 if (Tok.is(tok::identifier)) {
1769 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001770
1771 // If this identifier is followed by a '<', we may have a template-id.
1772 DeclTy *Template;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001773 Action::TemplateNameKind TNK;
Douglas Gregoraaba5e32009-02-04 19:02:06 +00001774 if (getLang().CPlusPlus && NextToken().is(tok::less) &&
Douglas Gregor55f6b142009-02-09 18:46:07 +00001775 (TNK = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1776 CurScope, Template))) {
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001777 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001778 AnnotateTemplateIdToken(Template, TNK, 0);
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001779 // FIXME: Set the declarator to a template-id. How? I don't
1780 // know... for now, just use the identifier.
1781 D.SetIdentifier(II, Tok.getLocation());
1782 }
1783 // If this identifier is the name of the current class, it's a
1784 // constructor name.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001785 else if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroffb43a50f2009-01-28 19:39:02 +00001786 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +00001787 Tok.getLocation(), CurScope),
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001788 Tok.getLocation());
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001789 // This is a normal identifier.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001790 } else
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001791 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1792 ConsumeToken();
1793 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00001794 } else if (Tok.is(tok::kw_operator)) {
1795 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001796 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001797
Douglas Gregor70316a02008-12-26 15:00:45 +00001798 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00001799 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1800 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00001801 } else {
1802 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00001803 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1804 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1805 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00001806 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001807 }
Douglas Gregor70316a02008-12-26 15:00:45 +00001808 }
1809 goto PastIdentifier;
1810 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001811 // This should be a C++ destructor.
1812 SourceLocation TildeLoc = ConsumeToken();
1813 if (Tok.is(tok::identifier)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001814 // FIXME: Inaccurate.
1815 SourceLocation NameLoc = Tok.getLocation();
1816 if (TypeTy *Type = ParseClassName()) {
1817 D.setDestructor(Type, TildeLoc, NameLoc);
1818 } else {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001819 D.SetIdentifier(0, TildeLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001820 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001821 } else {
1822 Diag(Tok, diag::err_expected_class_name);
1823 D.SetIdentifier(0, TildeLoc);
1824 }
1825 goto PastIdentifier;
1826 }
1827
1828 // If we reached this point, token is not identifier and not '~'.
1829
1830 if (afterCXXScope) {
1831 Diag(Tok, diag::err_expected_unqualified_id);
1832 D.SetIdentifier(0, Tok.getLocation());
1833 D.setInvalidType(true);
1834 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001835 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001836 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001837 }
1838
1839 // If we reached this point, we are either in C/ObjC or the token didn't
1840 // satisfy any of the C++-specific checks.
1841
1842 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1843 assert(!getLang().CPlusPlus &&
1844 "There's a C++-specific check for tok::identifier above");
1845 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1846 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1847 ConsumeToken();
1848 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001849 // direct-declarator: '(' declarator ')'
1850 // direct-declarator: '(' attributes declarator ')'
1851 // Example: 'char (*X)' or 'int (*XX)(void)'
1852 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001853 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001854 // This could be something simple like "int" (in which case the declarator
1855 // portion is empty), if an abstract-declarator is allowed.
1856 D.SetIdentifier(0, Tok.getLocation());
1857 } else {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001858 if (getLang().CPlusPlus)
1859 Diag(Tok, diag::err_expected_unqualified_id);
1860 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00001861 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00001862 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00001863 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001864 }
1865
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001866 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00001867 assert(D.isPastIdentifier() &&
1868 "Haven't past the location of the identifier yet?");
1869
1870 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001871 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001872 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1873 // In such a case, check if we actually have a function declarator; if it
1874 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00001875 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1876 // When not in file scope, warn for ambiguous function declarators, just
1877 // in case the author intended it as a variable definition.
1878 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1879 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1880 break;
1881 }
Chris Lattneref4715c2008-04-06 05:45:57 +00001882 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001883 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001884 ParseBracketDeclarator(D);
1885 } else {
1886 break;
1887 }
1888 }
1889}
1890
Chris Lattneref4715c2008-04-06 05:45:57 +00001891/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1892/// only called before the identifier, so these are most likely just grouping
1893/// parens for precedence. If we find that these are actually function
1894/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1895///
1896/// direct-declarator:
1897/// '(' declarator ')'
1898/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00001899/// direct-declarator '(' parameter-type-list ')'
1900/// direct-declarator '(' identifier-list[opt] ')'
1901/// [GNU] direct-declarator '(' parameter-forward-declarations
1902/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00001903///
1904void Parser::ParseParenDeclarator(Declarator &D) {
1905 SourceLocation StartLoc = ConsumeParen();
1906 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1907
Chris Lattner7399ee02008-10-20 02:05:46 +00001908 // Eat any attributes before we look at whether this is a grouping or function
1909 // declarator paren. If this is a grouping paren, the attribute applies to
1910 // the type being built up, for example:
1911 // int (__attribute__(()) *x)(long y)
1912 // If this ends up not being a grouping paren, the attribute applies to the
1913 // first argument, for example:
1914 // int (__attribute__(()) int x)
1915 // In either case, we need to eat any attributes to be able to determine what
1916 // sort of paren this is.
1917 //
1918 AttributeList *AttrList = 0;
1919 bool RequiresArg = false;
1920 if (Tok.is(tok::kw___attribute)) {
1921 AttrList = ParseAttributes();
1922
1923 // We require that the argument list (if this is a non-grouping paren) be
1924 // present even if the attribute list was empty.
1925 RequiresArg = true;
1926 }
Steve Naroff239f0732008-12-25 14:16:32 +00001927 // Eat any Microsoft extensions.
Douglas Gregor5a2f5d32009-01-10 00:48:18 +00001928 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1929 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff239f0732008-12-25 14:16:32 +00001930 ConsumeToken();
Chris Lattner7399ee02008-10-20 02:05:46 +00001931
Chris Lattneref4715c2008-04-06 05:45:57 +00001932 // If we haven't past the identifier yet (or where the identifier would be
1933 // stored, if this is an abstract declarator), then this is probably just
1934 // grouping parens. However, if this could be an abstract-declarator, then
1935 // this could also be the start of function arguments (consider 'void()').
1936 bool isGrouping;
1937
1938 if (!D.mayOmitIdentifier()) {
1939 // If this can't be an abstract-declarator, this *must* be a grouping
1940 // paren, because we haven't seen the identifier yet.
1941 isGrouping = true;
1942 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001943 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00001944 isDeclarationSpecifier()) { // 'int(int)' is a function.
1945 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1946 // considered to be a type, not a K&R identifier-list.
1947 isGrouping = false;
1948 } else {
1949 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1950 isGrouping = true;
1951 }
1952
1953 // If this is a grouping paren, handle:
1954 // direct-declarator: '(' declarator ')'
1955 // direct-declarator: '(' attributes declarator ')'
1956 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001957 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001958 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00001959 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00001960 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001961
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001962 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00001963 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001964 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001965
1966 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001967 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00001968 return;
1969 }
1970
1971 // Okay, if this wasn't a grouping paren, it must be the start of a function
1972 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00001973 // identifier (and remember where it would have been), then call into
1974 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00001975 D.SetIdentifier(0, Tok.getLocation());
1976
Chris Lattner7399ee02008-10-20 02:05:46 +00001977 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00001978}
1979
1980/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1981/// declarator D up to a paren, which indicates that we are parsing function
1982/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00001983///
Chris Lattner7399ee02008-10-20 02:05:46 +00001984/// If AttrList is non-null, then the caller parsed those arguments immediately
1985/// after the open paren - they should be considered to be the first argument of
1986/// a parameter. If RequiresArg is true, then the first argument of the
1987/// function is required to be present and required to not be an identifier
1988/// list.
1989///
Reid Spencer5f016e22007-07-11 17:01:13 +00001990/// This method also handles this portion of the grammar:
1991/// parameter-type-list: [C99 6.7.5]
1992/// parameter-list
1993/// parameter-list ',' '...'
1994///
1995/// parameter-list: [C99 6.7.5]
1996/// parameter-declaration
1997/// parameter-list ',' parameter-declaration
1998///
1999/// parameter-declaration: [C99 6.7.5]
2000/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002001/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002002/// [GNU] declaration-specifiers declarator attributes
2003/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002004/// [C++] declaration-specifiers abstract-declarator[opt]
2005/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002006/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2007///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002008/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
2009/// and "exception-specification[opt]"(TODO).
2010///
Chris Lattner7399ee02008-10-20 02:05:46 +00002011void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2012 AttributeList *AttrList,
2013 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002014 // lparen is already consumed!
2015 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00002016
Chris Lattner7399ee02008-10-20 02:05:46 +00002017 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002018 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002019 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002020 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002021 delete AttrList;
2022 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002023
Sebastian Redlab197ba2009-02-09 18:23:29 +00002024 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002025
2026 // cv-qualifier-seq[opt].
2027 DeclSpec DS;
2028 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002029 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002030 if (!DS.getSourceRange().getEnd().isInvalid())
2031 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002032
2033 // Parse exception-specification[opt].
2034 if (Tok.is(tok::kw_throw))
Sebastian Redlab197ba2009-02-09 18:23:29 +00002035 ParseExceptionSpecification(Loc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002036 }
2037
Chris Lattnerf97409f2008-04-06 06:57:35 +00002038 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002039 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002040 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002041 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002042 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002043 /*arglist*/ 0, 0,
2044 DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002045 LParenLoc, D),
2046 Loc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002047 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00002048 }
2049
2050 // Alternatively, this parameter list may be an identifier list form for a
2051 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002052 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002053 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002054 // K&R identifier lists can't have typedefs as identifiers, per
2055 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002056 if (RequiresArg) {
2057 Diag(Tok, diag::err_argument_required_after_attribute);
2058 delete AttrList;
2059 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002060 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2061 // normal declarators, not for abstract-declarators.
2062 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002063 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002064 }
2065
2066 // Finally, a normal, non-empty parameter type list.
2067
2068 // Build up an array of information about the parsed arguments.
2069 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002070
2071 // Enter function-declaration scope, limiting any declarators to the
2072 // function prototype scope, including parameter declarators.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002073 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002074
2075 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002076 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002077 while (1) {
2078 if (Tok.is(tok::ellipsis)) {
2079 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002080 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002081 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002082 }
2083
Chris Lattnerf97409f2008-04-06 06:57:35 +00002084 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002085
Chris Lattnerf97409f2008-04-06 06:57:35 +00002086 // Parse the declaration-specifiers.
2087 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002088
2089 // If the caller parsed attributes for the first argument, add them now.
2090 if (AttrList) {
2091 DS.AddAttributes(AttrList);
2092 AttrList = 0; // Only apply the attributes to the first parameter.
2093 }
Douglas Gregorcc636682009-02-17 23:15:12 +00002094 ParseDeclarationSpecifiers(DS);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002095
2096 // Parse the declarator. This is "PrototypeContext", because we must
2097 // accept either 'declarator' or 'abstract-declarator' here.
2098 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2099 ParseDeclarator(ParmDecl);
2100
2101 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002102 if (Tok.is(tok::kw___attribute)) {
2103 SourceLocation Loc;
2104 AttributeList *AttrList = ParseAttributes(&Loc);
2105 ParmDecl.AddAttributes(AttrList, Loc);
2106 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002107
Chris Lattnerf97409f2008-04-06 06:57:35 +00002108 // Remember this parsed parameter in ParamInfo.
2109 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2110
Douglas Gregor72b505b2008-12-16 21:30:33 +00002111 // DefArgToks is used when the parsing of default arguments needs
2112 // to be delayed.
2113 CachedTokens *DefArgToks = 0;
2114
Chris Lattnerf97409f2008-04-06 06:57:35 +00002115 // If no parameter was specified, verify that *something* was specified,
2116 // otherwise we have a missing type and identifier.
2117 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
2118 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
2119 // Completely missing, emit error.
2120 Diag(DSStart, diag::err_missing_param);
2121 } else {
2122 // Otherwise, we have something. Add it and let semantic analysis try
2123 // to grok it and add the result to the ParamInfo we are building.
2124
2125 // Inform the actions module about the parameter declarator, so it gets
2126 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00002127 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2128
2129 // Parse the default argument, if any. We parse the default
2130 // arguments in all dialects; the semantic analysis in
2131 // ActOnParamDefaultArgument will reject the default argument in
2132 // C.
2133 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002134 SourceLocation EqualLoc = Tok.getLocation();
2135
Chris Lattner04421082008-04-08 04:40:51 +00002136 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002137 if (D.getContext() == Declarator::MemberContext) {
2138 // If we're inside a class definition, cache the tokens
2139 // corresponding to the default argument. We'll actually parse
2140 // them when we see the end of the class definition.
2141 // FIXME: Templates will require something similar.
2142 // FIXME: Can we use a smart pointer for Toks?
2143 DefArgToks = new CachedTokens;
2144
2145 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2146 tok::semi, false)) {
2147 delete DefArgToks;
2148 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002149 Actions.ActOnParamDefaultArgumentError(Param);
2150 } else
2151 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner04421082008-04-08 04:40:51 +00002152 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002153 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002154 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002155
2156 OwningExprResult DefArgResult(ParseAssignmentExpression());
2157 if (DefArgResult.isInvalid()) {
2158 Actions.ActOnParamDefaultArgumentError(Param);
2159 SkipUntil(tok::comma, tok::r_paren, true, true);
2160 } else {
2161 // Inform the actions module about the default argument
2162 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
2163 DefArgResult.release());
2164 }
Chris Lattner04421082008-04-08 04:40:51 +00002165 }
2166 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002167
2168 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002169 ParmDecl.getIdentifierLoc(), Param,
2170 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002171 }
2172
2173 // If the next token is a comma, consume it and keep reading arguments.
2174 if (Tok.isNot(tok::comma)) break;
2175
2176 // Consume the comma.
2177 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002178 }
2179
Chris Lattnerf97409f2008-04-06 06:57:35 +00002180 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002181 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002182
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002183 // If we have the closing ')', eat it.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002184 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002185
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002186 DeclSpec DS;
2187 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002188 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002189 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002190 if (!DS.getSourceRange().getEnd().isInvalid())
2191 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002192
2193 // Parse exception-specification[opt].
2194 if (Tok.is(tok::kw_throw))
Sebastian Redlab197ba2009-02-09 18:23:29 +00002195 ParseExceptionSpecification(Loc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002196 }
2197
Reid Spencer5f016e22007-07-11 17:01:13 +00002198 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002199 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002200 EllipsisLoc,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002201 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002202 DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002203 LParenLoc, D),
2204 Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002205}
2206
Chris Lattner66d28652008-04-06 06:34:08 +00002207/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2208/// we found a K&R-style identifier list instead of a type argument list. The
2209/// current token is known to be the first identifier in the list.
2210///
2211/// identifier-list: [C99 6.7.5]
2212/// identifier
2213/// identifier-list ',' identifier
2214///
2215void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2216 Declarator &D) {
2217 // Build up an array of information about the parsed arguments.
2218 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2219 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2220
2221 // If there was no identifier specified for the declarator, either we are in
2222 // an abstract-declarator, or we are in a parameter declarator which was found
2223 // to be abstract. In abstract-declarators, identifier lists are not valid:
2224 // diagnose this.
2225 if (!D.getIdentifier())
2226 Diag(Tok, diag::ext_ident_list_in_param);
2227
2228 // Tok is known to be the first identifier in the list. Remember this
2229 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002230 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002231 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2232 Tok.getLocation(), 0));
2233
Chris Lattner50c64772008-04-06 06:39:19 +00002234 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002235
2236 while (Tok.is(tok::comma)) {
2237 // Eat the comma.
2238 ConsumeToken();
2239
Chris Lattner50c64772008-04-06 06:39:19 +00002240 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002241 if (Tok.isNot(tok::identifier)) {
2242 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002243 SkipUntil(tok::r_paren);
2244 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002245 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002246
Chris Lattner66d28652008-04-06 06:34:08 +00002247 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002248
2249 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002250 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002251 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002252
2253 // Verify that the argument identifier has not already been mentioned.
2254 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002255 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002256 } else {
2257 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002258 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2259 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00002260 }
Chris Lattner66d28652008-04-06 06:34:08 +00002261
2262 // Eat the identifier.
2263 ConsumeToken();
2264 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002265
2266 // If we have the closing ')', eat it and we're done.
2267 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2268
Chris Lattner50c64772008-04-06 06:39:19 +00002269 // Remember that we parsed a function type, and remember the attributes. This
2270 // function type is always a K&R style function type, which is not varargs and
2271 // has no prototype.
2272 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002273 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002274 &ParamInfo[0], ParamInfo.size(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002275 /*TypeQuals*/0, LParenLoc, D),
2276 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002277}
Chris Lattneref4715c2008-04-06 05:45:57 +00002278
Reid Spencer5f016e22007-07-11 17:01:13 +00002279/// [C90] direct-declarator '[' constant-expression[opt] ']'
2280/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2281/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2282/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2283/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2284void Parser::ParseBracketDeclarator(Declarator &D) {
2285 SourceLocation StartLoc = ConsumeBracket();
2286
Chris Lattner378c7e42008-12-18 07:27:21 +00002287 // C array syntax has many features, but by-far the most common is [] and [4].
2288 // This code does a fast path to handle some of the most obvious cases.
2289 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002290 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002291 // Remember that we parsed the empty array type.
2292 OwningExprResult NumElements(Actions);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002293 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2294 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002295 return;
2296 } else if (Tok.getKind() == tok::numeric_constant &&
2297 GetLookAheadToken(1).is(tok::r_square)) {
2298 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002299 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002300 ConsumeToken();
2301
Sebastian Redlab197ba2009-02-09 18:23:29 +00002302 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002303
2304 // If there was an error parsing the assignment-expression, recover.
2305 if (ExprRes.isInvalid())
2306 ExprRes.release(); // Deallocate expr, just use [].
2307
2308 // Remember that we parsed a array type, and remember its features.
2309 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002310 ExprRes.release(), StartLoc),
2311 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002312 return;
2313 }
2314
Reid Spencer5f016e22007-07-11 17:01:13 +00002315 // If valid, this location is the position where we read the 'static' keyword.
2316 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002317 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002318 StaticLoc = ConsumeToken();
2319
2320 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002321 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002322 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002323 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002324
2325 // If we haven't already read 'static', check to see if there is one after the
2326 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002327 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002328 StaticLoc = ConsumeToken();
2329
2330 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2331 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002332 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002333
2334 // Handle the case where we have '[*]' as the array size. However, a leading
2335 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2336 // the the token after the star is a ']'. Since stars in arrays are
2337 // infrequent, use of lookahead is not costly here.
2338 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002339 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002340
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002341 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002342 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002343 StaticLoc = SourceLocation(); // Drop the static.
2344 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002345 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002346 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002347 // Note, in C89, this production uses the constant-expr production instead
2348 // of assignment-expr. The only difference is that assignment-expr allows
2349 // things like '=' and '*='. Sema rejects these in C89 mode because they
2350 // are not i-c-e's, so we don't need to distinguish between the two here.
2351
Reid Spencer5f016e22007-07-11 17:01:13 +00002352 // Parse the assignment-expression now.
2353 NumElements = ParseAssignmentExpression();
2354 }
2355
2356 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002357 if (NumElements.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002358 // If the expression was invalid, skip it.
2359 SkipUntil(tok::r_square);
2360 return;
2361 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002362
2363 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2364
Chris Lattner378c7e42008-12-18 07:27:21 +00002365 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002366 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2367 StaticLoc.isValid(), isStar,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002368 NumElements.release(), StartLoc),
2369 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002370}
2371
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002372/// [GNU] typeof-specifier:
2373/// typeof ( expressions )
2374/// typeof ( type-name )
2375/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002376///
2377void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002378 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002379 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002380 SourceLocation StartLoc = ConsumeToken();
2381
Chris Lattner04d66662007-10-09 17:33:22 +00002382 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002383 if (!getLang().CPlusPlus) {
Chris Lattner08631c52008-11-23 21:45:46 +00002384 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002385 return;
2386 }
2387
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002388 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor809070a2009-02-18 17:45:20 +00002389 if (Result.isInvalid()) {
2390 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002391 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002392 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002393
2394 const char *PrevSpec = 0;
2395 // Check for duplicate type specifiers.
2396 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002397 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002398 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002399
2400 // FIXME: Not accurate, the range gets one token more than it should.
2401 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002402 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002403 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002404
Steve Naroffd1861fd2007-07-31 12:34:36 +00002405 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2406
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00002407 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00002408 Action::TypeResult Ty = ParseTypeName();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002409
Douglas Gregor809070a2009-02-18 17:45:20 +00002410 assert((Ty.isInvalid() || Ty.get()) &&
2411 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002412
Chris Lattner04d66662007-10-09 17:33:22 +00002413 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002414 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002415 return;
2416 }
2417 RParenLoc = ConsumeParen();
Douglas Gregor809070a2009-02-18 17:45:20 +00002418
2419 if (Ty.isInvalid())
2420 DS.SetTypeSpecError();
2421 else {
2422 const char *PrevSpec = 0;
2423 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2424 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2425 Ty.get()))
2426 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2427 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00002428 } else { // we have an expression.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002429 OwningExprResult Result(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002430
2431 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002432 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor809070a2009-02-18 17:45:20 +00002433 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002434 return;
2435 }
2436 RParenLoc = ConsumeParen();
2437 const char *PrevSpec = 0;
2438 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2439 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002440 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002441 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002442 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00002443 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002444}
2445
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002446