blob: 6d2aad6905f1a16e4c8021255c6b9600342d20c0 [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]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000228// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000229/// others... [FIXME]
230///
Reid Spencer5f016e22007-07-11 17:01:13 +0000231Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner8f08cb72007-08-25 06:57:03 +0000232 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000233 case tok::kw_export:
234 case tok::kw_template:
Douglas Gregorcc636682009-02-17 23:15:12 +0000235 return ParseTemplateDeclarationOrSpecialization(Context);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000236 case tok::kw_namespace:
237 return ParseNamespace(Context);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000238 case tok::kw_using:
239 return ParseUsingDirectiveOrDeclaration(Context);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000240 case tok::kw_static_assert:
241 return ParseStaticAssertDeclaration();
Chris Lattner8f08cb72007-08-25 06:57:03 +0000242 default:
243 return ParseSimpleDeclaration(Context);
244 }
245}
246
247/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
248/// declaration-specifiers init-declarator-list[opt] ';'
249///[C90/C++]init-declarator-list ';' [TODO]
250/// [OMP] threadprivate-directive [TODO]
251Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 // Parse the common declaration-specifiers piece.
253 DeclSpec DS;
254 ParseDeclarationSpecifiers(DS);
255
256 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
257 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000258 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 ConsumeToken();
260 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
261 }
262
263 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
264 ParseDeclarator(DeclaratorInfo);
265
266 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
267}
268
Chris Lattner8f08cb72007-08-25 06:57:03 +0000269
Reid Spencer5f016e22007-07-11 17:01:13 +0000270/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
271/// parsing 'declaration-specifiers declarator'. This method is split out this
272/// way to handle the ambiguity between top-level function-definitions and
273/// declarations.
274///
Reid Spencer5f016e22007-07-11 17:01:13 +0000275/// init-declarator-list: [C99 6.7]
276/// init-declarator
277/// init-declarator-list ',' init-declarator
278/// init-declarator: [C99 6.7]
279/// declarator
280/// declarator '=' initializer
281/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
282/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000283/// [C++] declarator initializer[opt]
284///
285/// [C++] initializer:
286/// [C++] '=' initializer-clause
287/// [C++] '(' expression-list ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000288///
289Parser::DeclTy *Parser::
290ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
291
292 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
293 // that they can be chained properly if the actions want this.
294 Parser::DeclTy *LastDeclInGroup = 0;
295
296 // At this point, we know that it is not a function definition. Parse the
297 // rest of the init-declarator-list.
298 while (1) {
299 // If a simple-asm-expr is present, parse it.
Daniel Dunbara80f8742008-08-05 01:35:17 +0000300 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000301 SourceLocation Loc;
302 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000303 if (AsmLabel.isInvalid()) {
Daniel Dunbara80f8742008-08-05 01:35:17 +0000304 SkipUntil(tok::semi);
305 return 0;
306 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000307
Sebastian Redleffa8d12008-12-10 00:02:53 +0000308 D.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000309 D.SetRangeEnd(Loc);
Daniel Dunbara80f8742008-08-05 01:35:17 +0000310 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000311
312 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000313 if (Tok.is(tok::kw___attribute)) {
314 SourceLocation Loc;
315 AttributeList *AttrList = ParseAttributes(&Loc);
316 D.AddAttributes(AttrList, Loc);
317 }
Steve Naroffbb204692007-09-12 14:07:44 +0000318
319 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar914701e2008-08-05 16:28:08 +0000320 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000321
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 // Parse declarator '=' initializer.
Chris Lattner04d66662007-10-09 17:33:22 +0000323 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000325 OwningExprResult Init(ParseInitializer());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000326 if (Init.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 SkipUntil(tok::semi);
328 return 0;
329 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000330 Actions.AddInitializerToDecl(LastDeclInGroup, move(Init));
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000331 } else if (Tok.is(tok::l_paren)) {
332 // Parse C++ direct initializer: '(' expression-list ')'
333 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000334 ExprVector Exprs(Actions);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000335 CommaLocsTy CommaLocs;
336
337 bool InvalidExpr = false;
338 if (ParseExpressionList(Exprs, CommaLocs)) {
339 SkipUntil(tok::r_paren);
340 InvalidExpr = true;
341 }
342 // Match the ')'.
343 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
344
345 if (!InvalidExpr) {
346 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
347 "Unexpected number of commas!");
348 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000349 move_arg(Exprs),
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000350 &CommaLocs[0], RParenLoc);
351 }
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000352 } else {
353 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 }
355
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // If we don't have a comma, it is either the end of the list (a ';') or an
357 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000358 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 break;
360
361 // Consume the comma.
362 ConsumeToken();
363
364 // Parse the next declarator.
365 D.clear();
Chris Lattneraab740a2008-10-20 04:57:38 +0000366
367 // Accept attributes in an init-declarator. In the first declarator in a
368 // declaration, these would be part of the declspec. In subsequent
369 // declarators, they become part of the declarator itself, so that they
370 // don't apply to declarators after *this* one. Examples:
371 // short __attribute__((common)) var; -> declspec
372 // short var __attribute__((common)); -> declarator
373 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlab197ba2009-02-09 18:23:29 +0000374 if (Tok.is(tok::kw___attribute)) {
375 SourceLocation Loc;
376 AttributeList *AttrList = ParseAttributes(&Loc);
377 D.AddAttributes(AttrList, Loc);
378 }
Chris Lattneraab740a2008-10-20 04:57:38 +0000379
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 ParseDeclarator(D);
381 }
382
Chris Lattner04d66662007-10-09 17:33:22 +0000383 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 ConsumeToken();
Fariborz Jahanian41f2b322009-01-17 00:00:40 +0000385 // for(is key; in keys) is error.
386 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
387 Diag(Tok, diag::err_parse_error);
388 return 0;
389 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
391 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000392 // If this is an ObjC2 for-each loop, this is a successful declarator
393 // parse. The syntax for these looks like:
394 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000395 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000396 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
397 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 Diag(Tok, diag::err_parse_error);
399 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000400 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000401 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 ConsumeToken();
403 return 0;
404}
405
406/// ParseSpecifierQualifierList
407/// specifier-qualifier-list:
408/// type-specifier specifier-qualifier-list[opt]
409/// type-qualifier specifier-qualifier-list[opt]
410/// [GNU] attributes specifier-qualifier-list[opt]
411///
412void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
413 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
414 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 ParseDeclarationSpecifiers(DS);
416
417 // Validate declspec for type-name.
418 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000419 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 Diag(Tok, diag::err_typename_requires_specqual);
421
422 // Issue diagnostic and remove storage class if present.
423 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
424 if (DS.getStorageClassSpecLoc().isValid())
425 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
426 else
427 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
428 DS.ClearStorageClassSpecs();
429 }
430
431 // Issue diagnostic and remove function specfier if present.
432 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000433 if (DS.isInlineSpecified())
434 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
435 if (DS.isVirtualSpecified())
436 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
437 if (DS.isExplicitSpecified())
438 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 DS.ClearFunctionSpecs();
440 }
441}
442
443/// ParseDeclarationSpecifiers
444/// declaration-specifiers: [C99 6.7]
445/// storage-class-specifier declaration-specifiers[opt]
446/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000447/// [C99] function-specifier declaration-specifiers[opt]
448/// [GNU] attributes declaration-specifiers[opt]
449///
450/// storage-class-specifier: [C99 6.7.1]
451/// 'typedef'
452/// 'extern'
453/// 'static'
454/// 'auto'
455/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000456/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000457/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000458/// function-specifier: [C99 6.7.4]
459/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000460/// [C++] 'virtual'
461/// [C++] 'explicit'
Reid Spencer5f016e22007-07-11 17:01:13 +0000462///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000463void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Chris Lattner5e02c472009-01-05 00:07:25 +0000464 TemplateParameterLists *TemplateParams){
Chris Lattner81c018d2008-03-13 06:29:04 +0000465 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 while (1) {
467 int isInvalid = false;
468 const char *PrevSpec = 0;
469 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000470
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000472 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000473 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 // If this is not a declaration specifier token, we're done reading decl
475 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000476 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 return;
Chris Lattner5e02c472009-01-05 00:07:25 +0000478
479 case tok::coloncolon: // ::foo::bar
480 // Annotate C++ scope specifiers. If we get one, loop.
481 if (TryAnnotateCXXScopeToken())
482 continue;
483 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000484
485 case tok::annot_cxxscope: {
486 if (DS.hasTypeSpecifier())
487 goto DoneWithDeclSpec;
488
489 // We are looking for a qualified typename.
490 if (NextToken().isNot(tok::identifier))
491 goto DoneWithDeclSpec;
492
493 CXXScopeSpec SS;
Douglas Gregore4e5b052009-03-19 00:18:19 +0000494 SS.setFromAnnotationData(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000495 SS.setRange(Tok.getAnnotationRange());
496
497 // If the next token is the name of the class type that the C++ scope
498 // denotes, followed by a '(', then this is a constructor declaration.
499 // We're done with the decl-specifiers.
500 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
501 CurScope, &SS) &&
502 GetLookAheadToken(2).is(tok::l_paren))
503 goto DoneWithDeclSpec;
504
Douglas Gregorb696ea32009-02-04 17:00:24 +0000505 Token Next = NextToken();
506 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
507 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000508
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000509 if (TypeRep == 0)
510 goto DoneWithDeclSpec;
Douglas Gregore4e5b052009-03-19 00:18:19 +0000511
512 CXXScopeSpec::freeAnnotationData(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000513 ConsumeToken(); // The C++ scope.
514
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000515 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000516 TypeRep);
517 if (isInvalid)
518 break;
519
520 DS.SetRangeEnd(Tok.getLocation());
521 ConsumeToken(); // The typename.
522
523 continue;
524 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000525
526 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000527 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner80d0c892009-01-21 19:48:37 +0000528 Tok.getAnnotationValue());
529 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
530 ConsumeToken(); // The typename
531
532 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
533 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
534 // Objective-C interface. If we don't have Objective-C or a '<', this is
535 // just a normal reference to a typedef name.
536 if (!Tok.is(tok::less) || !getLang().ObjC1)
537 continue;
538
539 SourceLocation EndProtoLoc;
540 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
541 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
542 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
543
544 DS.SetRangeEnd(EndProtoLoc);
545 continue;
546 }
547
Chris Lattner3bd934a2008-07-26 01:18:38 +0000548 // typedef-name
549 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000550 // In C++, check to see if this is a scope specifier like foo::bar::, if
551 // so handle it as such. This is important for ctor parsing.
Chris Lattner837acd02009-01-21 19:19:26 +0000552 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
553 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000554
Chris Lattner3bd934a2008-07-26 01:18:38 +0000555 // This identifier can only be a typedef name if we haven't already seen
556 // a type-specifier. Without this check we misparse:
557 // typedef int X; struct Y { short X; }; as 'short int'.
558 if (DS.hasTypeSpecifier())
559 goto DoneWithDeclSpec;
560
561 // It has to be available as a typedef too!
Douglas Gregorb696ea32009-02-04 17:00:24 +0000562 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
563 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000564
Chris Lattner3bd934a2008-07-26 01:18:38 +0000565 if (TypeRep == 0)
566 goto DoneWithDeclSpec;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000567
Douglas Gregorb48fe382008-10-31 09:07:45 +0000568 // C++: If the identifier is actually the name of the class type
569 // being defined and the next token is a '(', then this is a
570 // constructor declaration. We're done with the decl-specifiers
571 // and will treat this token as an identifier.
572 if (getLang().CPlusPlus &&
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000573 CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000574 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
575 NextToken().getKind() == tok::l_paren)
576 goto DoneWithDeclSpec;
577
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000578 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner3bd934a2008-07-26 01:18:38 +0000579 TypeRep);
580 if (isInvalid)
581 break;
582
583 DS.SetRangeEnd(Tok.getLocation());
584 ConsumeToken(); // The identifier
585
586 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
587 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
588 // Objective-C interface. If we don't have Objective-C or a '<', this is
589 // just a normal reference to a typedef name.
590 if (!Tok.is(tok::less) || !getLang().ObjC1)
591 continue;
592
593 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000594 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000595 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000596 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000597
598 DS.SetRangeEnd(EndProtoLoc);
599
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000600 // Need to support trailing type qualifiers (e.g. "id<p> const").
601 // If a type specifier follows, it will be diagnosed elsewhere.
602 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000603 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000604
605 // type-name
606 case tok::annot_template_id: {
607 TemplateIdAnnotation *TemplateId
608 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
609 if (TemplateId->Kind != TNK_Class_template) {
610 // This template-id does not refer to a type name, so we're
611 // done with the type-specifiers.
612 goto DoneWithDeclSpec;
613 }
614
615 // Turn the template-id annotation token into a type annotation
616 // token, then try again to parse it as a type-specifier.
617 if (AnnotateTemplateIdTokenAsType())
618 DS.SetTypeSpecError();
619
620 continue;
621 }
622
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 // GNU attributes support.
624 case tok::kw___attribute:
625 DS.AddAttributes(ParseAttributes());
626 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000627
628 // Microsoft declspec support.
629 case tok::kw___declspec:
630 if (!PP.getLangOptions().Microsoft)
631 goto DoneWithDeclSpec;
632 FuzzyParseMicrosoftDeclSpec();
633 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000634
Steve Naroff239f0732008-12-25 14:16:32 +0000635 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000636 case tok::kw___forceinline:
637 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000638 case tok::kw___cdecl:
639 case tok::kw___stdcall:
640 case tok::kw___fastcall:
641 if (!PP.getLangOptions().Microsoft)
642 goto DoneWithDeclSpec;
643 // Just ignore it.
644 break;
645
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 // storage-class-specifier
647 case tok::kw_typedef:
648 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
649 break;
650 case tok::kw_extern:
651 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000652 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
654 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000655 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000656 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
657 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000658 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 case tok::kw_static:
660 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000661 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
663 break;
664 case tok::kw_auto:
665 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
666 break;
667 case tok::kw_register:
668 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
669 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000670 case tok::kw_mutable:
671 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
672 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 case tok::kw___thread:
674 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
675 break;
676
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 continue;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000678
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 // function-specifier
680 case tok::kw_inline:
681 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
682 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000683 case tok::kw_virtual:
684 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
685 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000686 case tok::kw_explicit:
687 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
688 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000689
690 // type-specifier
691 case tok::kw_short:
692 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
693 break;
694 case tok::kw_long:
695 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
696 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
697 else
698 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
699 break;
700 case tok::kw_signed:
701 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
702 break;
703 case tok::kw_unsigned:
704 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
705 break;
706 case tok::kw__Complex:
707 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
708 break;
709 case tok::kw__Imaginary:
710 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
711 break;
712 case tok::kw_void:
713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
714 break;
715 case tok::kw_char:
716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
717 break;
718 case tok::kw_int:
719 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
720 break;
721 case tok::kw_float:
722 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
723 break;
724 case tok::kw_double:
725 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
726 break;
727 case tok::kw_wchar_t:
728 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
729 break;
730 case tok::kw_bool:
731 case tok::kw__Bool:
732 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
733 break;
734 case tok::kw__Decimal32:
735 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
736 break;
737 case tok::kw__Decimal64:
738 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
739 break;
740 case tok::kw__Decimal128:
741 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
742 break;
743
744 // class-specifier:
745 case tok::kw_class:
746 case tok::kw_struct:
747 case tok::kw_union:
748 ParseClassSpecifier(DS, TemplateParams);
749 continue;
750
751 // enum-specifier:
752 case tok::kw_enum:
753 ParseEnumSpecifier(DS);
754 continue;
755
756 // cv-qualifier:
757 case tok::kw_const:
758 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
759 break;
760 case tok::kw_volatile:
761 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
762 getLang())*2;
763 break;
764 case tok::kw_restrict:
765 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
766 getLang())*2;
767 break;
768
769 // GNU typeof support.
770 case tok::kw_typeof:
771 ParseTypeofSpecifier(DS);
772 continue;
773
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000774 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000775 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000776 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
777 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000778 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000779 goto DoneWithDeclSpec;
780
781 {
782 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000783 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000784 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000785 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000786 DS.SetRangeEnd(EndProtoLoc);
787
Chris Lattner1ab3b962008-11-18 07:48:38 +0000788 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
789 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000790 // Need to support trailing type qualifiers (e.g. "id<p> const").
791 // If a type specifier follows, it will be diagnosed elsewhere.
792 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000793 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000794 }
795 // If the specifier combination wasn't legal, issue a diagnostic.
796 if (isInvalid) {
797 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000798 // Pick between error or extwarn.
799 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
800 : diag::ext_duplicate_declspec;
801 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000803 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 ConsumeToken();
805 }
806}
Douglas Gregoradcac882008-12-01 23:54:00 +0000807
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000808/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +0000809/// primarily follow the C++ grammar with additions for C99 and GNU,
810/// which together subsume the C grammar. Note that the C++
811/// type-specifier also includes the C type-qualifier (for const,
812/// volatile, and C99 restrict). Returns true if a type-specifier was
813/// found (and parsed), false otherwise.
814///
815/// type-specifier: [C++ 7.1.5]
816/// simple-type-specifier
817/// class-specifier
818/// enum-specifier
819/// elaborated-type-specifier [TODO]
820/// cv-qualifier
821///
822/// cv-qualifier: [C++ 7.1.5.1]
823/// 'const'
824/// 'volatile'
825/// [C99] 'restrict'
826///
827/// simple-type-specifier: [ C++ 7.1.5.2]
828/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
829/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
830/// 'char'
831/// 'wchar_t'
832/// 'bool'
833/// 'short'
834/// 'int'
835/// 'long'
836/// 'signed'
837/// 'unsigned'
838/// 'float'
839/// 'double'
840/// 'void'
841/// [C99] '_Bool'
842/// [C99] '_Complex'
843/// [C99] '_Imaginary' // Removed in TC2?
844/// [GNU] '_Decimal32'
845/// [GNU] '_Decimal64'
846/// [GNU] '_Decimal128'
847/// [GNU] typeof-specifier
848/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
849/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000850bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
851 const char *&PrevSpec,
852 TemplateParameterLists *TemplateParams){
Douglas Gregor12e083c2008-11-07 15:42:26 +0000853 SourceLocation Loc = Tok.getLocation();
854
855 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +0000856 case tok::identifier: // foo::bar
857 // Annotate typenames and C++ scope specifiers. If we get one, just
858 // recurse to handle whatever we get.
859 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000860 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000861 // Otherwise, not a type specifier.
862 return false;
863 case tok::coloncolon: // ::foo::bar
864 if (NextToken().is(tok::kw_new) || // ::new
865 NextToken().is(tok::kw_delete)) // ::delete
866 return false;
867
868 // Annotate typenames and C++ scope specifiers. If we get one, just
869 // recurse to handle whatever we get.
870 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000871 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000872 // Otherwise, not a type specifier.
873 return false;
874
Douglas Gregor12e083c2008-11-07 15:42:26 +0000875 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000876 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000877 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000878 Tok.getAnnotationValue());
879 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
880 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +0000881
882 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
883 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
884 // Objective-C interface. If we don't have Objective-C or a '<', this is
885 // just a normal reference to a typedef name.
886 if (!Tok.is(tok::less) || !getLang().ObjC1)
887 return true;
888
889 SourceLocation EndProtoLoc;
890 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
891 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
892 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
893
894 DS.SetRangeEnd(EndProtoLoc);
895 return true;
896 }
897
898 case tok::kw_short:
899 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
900 break;
901 case tok::kw_long:
902 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
903 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
904 else
905 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
906 break;
907 case tok::kw_signed:
908 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
909 break;
910 case tok::kw_unsigned:
911 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
912 break;
913 case tok::kw__Complex:
914 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
915 break;
916 case tok::kw__Imaginary:
917 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
918 break;
919 case tok::kw_void:
920 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
921 break;
922 case tok::kw_char:
923 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
924 break;
925 case tok::kw_int:
926 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
927 break;
928 case tok::kw_float:
929 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
930 break;
931 case tok::kw_double:
932 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
933 break;
934 case tok::kw_wchar_t:
935 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
936 break;
937 case tok::kw_bool:
938 case tok::kw__Bool:
939 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
940 break;
941 case tok::kw__Decimal32:
942 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
943 break;
944 case tok::kw__Decimal64:
945 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
946 break;
947 case tok::kw__Decimal128:
948 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
949 break;
950
951 // class-specifier:
952 case tok::kw_class:
953 case tok::kw_struct:
954 case tok::kw_union:
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000955 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor12e083c2008-11-07 15:42:26 +0000956 return true;
957
958 // enum-specifier:
959 case tok::kw_enum:
960 ParseEnumSpecifier(DS);
961 return true;
962
963 // cv-qualifier:
964 case tok::kw_const:
965 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
966 getLang())*2;
967 break;
968 case tok::kw_volatile:
969 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
970 getLang())*2;
971 break;
972 case tok::kw_restrict:
973 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
974 getLang())*2;
975 break;
976
977 // GNU typeof support.
978 case tok::kw_typeof:
979 ParseTypeofSpecifier(DS);
980 return true;
981
Steve Naroff239f0732008-12-25 14:16:32 +0000982 case tok::kw___cdecl:
983 case tok::kw___stdcall:
984 case tok::kw___fastcall:
Chris Lattner837acd02009-01-21 19:19:26 +0000985 if (!PP.getLangOptions().Microsoft) return false;
986 ConsumeToken();
987 return true;
Steve Naroff239f0732008-12-25 14:16:32 +0000988
Douglas Gregor12e083c2008-11-07 15:42:26 +0000989 default:
990 // Not a type-specifier; do nothing.
991 return false;
992 }
993
994 // If the specifier combination wasn't legal, issue a diagnostic.
995 if (isInvalid) {
996 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000997 // Pick between error or extwarn.
998 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
999 : diag::ext_duplicate_declspec;
1000 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001001 }
1002 DS.SetRangeEnd(Tok.getLocation());
1003 ConsumeToken(); // whatever we parsed above.
1004 return true;
1005}
Reid Spencer5f016e22007-07-11 17:01:13 +00001006
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001007/// ParseStructDeclaration - Parse a struct declaration without the terminating
1008/// semicolon.
1009///
Reid Spencer5f016e22007-07-11 17:01:13 +00001010/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001011/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001012/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001013/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001014/// struct-declarator-list:
1015/// struct-declarator
1016/// struct-declarator-list ',' struct-declarator
1017/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1018/// struct-declarator:
1019/// declarator
1020/// [GNU] declarator attributes[opt]
1021/// declarator[opt] ':' constant-expression
1022/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1023///
Chris Lattnere1359422008-04-10 06:46:29 +00001024void Parser::
1025ParseStructDeclaration(DeclSpec &DS,
1026 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001027 if (Tok.is(tok::kw___extension__)) {
1028 // __extension__ silences extension warnings in the subexpression.
1029 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001030 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001031 return ParseStructDeclaration(DS, Fields);
1032 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001033
1034 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001035 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001036 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001037
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001038 // If there are no declarators, this is a free-standing declaration
1039 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001040 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001041 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001042 return;
1043 }
1044
1045 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001046 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001047 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001048 FieldDeclarator &DeclaratorInfo = Fields.back();
1049
Steve Naroff28a7ca82007-08-20 22:28:22 +00001050 /// struct-declarator: declarator
1051 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001052 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001053 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001054
Chris Lattner04d66662007-10-09 17:33:22 +00001055 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001056 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001057 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001058 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001059 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001060 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001061 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001062 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001063
Steve Naroff28a7ca82007-08-20 22:28:22 +00001064 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001065 if (Tok.is(tok::kw___attribute)) {
1066 SourceLocation Loc;
1067 AttributeList *AttrList = ParseAttributes(&Loc);
1068 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1069 }
1070
Steve Naroff28a7ca82007-08-20 22:28:22 +00001071 // If we don't have a comma, it is either the end of the list (a ';')
1072 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001073 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001074 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001075
Steve Naroff28a7ca82007-08-20 22:28:22 +00001076 // Consume the comma.
1077 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001078
Steve Naroff28a7ca82007-08-20 22:28:22 +00001079 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001080 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001081
Steve Naroff28a7ca82007-08-20 22:28:22 +00001082 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001083 if (Tok.is(tok::kw___attribute)) {
1084 SourceLocation Loc;
1085 AttributeList *AttrList = ParseAttributes(&Loc);
1086 Fields.back().D.AddAttributes(AttrList, Loc);
1087 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001088 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001089}
1090
1091/// ParseStructUnionBody
1092/// struct-contents:
1093/// struct-declaration-list
1094/// [EXT] empty
1095/// [GNU] "struct-declaration-list" without terminatoring ';'
1096/// struct-declaration-list:
1097/// struct-declaration
1098/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001099/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001100///
Reid Spencer5f016e22007-07-11 17:01:13 +00001101void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1102 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001103 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1104 PP.getSourceManager(),
1105 "parsing struct/union body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001106
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 SourceLocation LBraceLoc = ConsumeBrace();
1108
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001109 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001110 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1111
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1113 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001114 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001115 Diag(Tok, diag::ext_empty_struct_union_enum)
1116 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001117
1118 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001119 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1120
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001122 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 // Each iteration of this loop reads one struct-declaration.
1124
1125 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001126 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 Diag(Tok, diag::ext_extra_struct_semi);
1128 ConsumeToken();
1129 continue;
1130 }
Chris Lattnere1359422008-04-10 06:46:29 +00001131
1132 // Parse all the comma separated declarators.
1133 DeclSpec DS;
1134 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001135 if (!Tok.is(tok::at)) {
1136 ParseStructDeclaration(DS, FieldDeclarators);
1137
1138 // Convert them all to fields.
1139 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1140 FieldDeclarator &FD = FieldDeclarators[i];
1141 // Install the declarator into the current TagDecl.
Douglas Gregor44b43212008-12-11 16:49:14 +00001142 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001143 DS.getSourceRange().getBegin(),
1144 FD.D, FD.BitfieldSize);
1145 FieldDecls.push_back(Field);
1146 }
1147 } else { // Handle @defs
1148 ConsumeToken();
1149 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1150 Diag(Tok, diag::err_unexpected_at);
1151 SkipUntil(tok::semi, true, true);
1152 continue;
1153 }
1154 ConsumeToken();
1155 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1156 if (!Tok.is(tok::identifier)) {
1157 Diag(Tok, diag::err_expected_ident);
1158 SkipUntil(tok::semi, true, true);
1159 continue;
1160 }
1161 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001162 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1163 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001164 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1165 ConsumeToken();
1166 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1167 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001168
Chris Lattner04d66662007-10-09 17:33:22 +00001169 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001171 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001172 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 break;
1174 } else {
1175 Diag(Tok, diag::err_expected_semi_decl_list);
1176 // Skip to end of block or statement
1177 SkipUntil(tok::r_brace, true, true);
1178 }
1179 }
1180
Steve Naroff60fccee2007-10-29 21:38:07 +00001181 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001182
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 AttributeList *AttrList = 0;
1184 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001185 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001186 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001187
1188 Actions.ActOnFields(CurScope,
1189 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1190 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001191 AttrList);
1192 StructScope.Exit();
1193 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001194}
1195
1196
1197/// ParseEnumSpecifier
1198/// enum-specifier: [C99 6.7.2.2]
1199/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001200///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001201/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1202/// '}' attributes[opt]
1203/// 'enum' identifier
1204/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001205///
1206/// [C++] elaborated-type-specifier:
1207/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1208///
Reid Spencer5f016e22007-07-11 17:01:13 +00001209void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001210 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 SourceLocation StartLoc = ConsumeToken();
1212
1213 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001214
1215 AttributeList *Attr = 0;
1216 // If attributes exist after tag, parse them.
1217 if (Tok.is(tok::kw___attribute))
1218 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001219
1220 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001221 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001222 if (Tok.isNot(tok::identifier)) {
1223 Diag(Tok, diag::err_expected_ident);
1224 if (Tok.isNot(tok::l_brace)) {
1225 // Has no name and is not a definition.
1226 // Skip the rest of this declarator, up until the comma or semicolon.
1227 SkipUntil(tok::comma, true);
1228 return;
1229 }
1230 }
1231 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001232
1233 // Must have either 'enum name' or 'enum {...}'.
1234 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1235 Diag(Tok, diag::err_expected_ident_lbrace);
1236
1237 // Skip the rest of this declarator, up until the comma or semicolon.
1238 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001240 }
1241
1242 // If an identifier is present, consume and remember it.
1243 IdentifierInfo *Name = 0;
1244 SourceLocation NameLoc;
1245 if (Tok.is(tok::identifier)) {
1246 Name = Tok.getIdentifierInfo();
1247 NameLoc = ConsumeToken();
1248 }
1249
1250 // There are three options here. If we have 'enum foo;', then this is a
1251 // forward declaration. If we have 'enum foo {...' then this is a
1252 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1253 //
1254 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1255 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1256 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1257 //
1258 Action::TagKind TK;
1259 if (Tok.is(tok::l_brace))
1260 TK = Action::TK_Definition;
1261 else if (Tok.is(tok::semi))
1262 TK = Action::TK_Declaration;
1263 else
1264 TK = Action::TK_Reference;
1265 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregorddc29e12009-02-06 22:42:48 +00001266 SS, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001267
Chris Lattner04d66662007-10-09 17:33:22 +00001268 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 ParseEnumBody(StartLoc, TagDecl);
1270
1271 // TODO: semantic analysis on the declspec for enums.
1272 const char *PrevSpec = 0;
1273 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001274 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001275}
1276
1277/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1278/// enumerator-list:
1279/// enumerator
1280/// enumerator-list ',' enumerator
1281/// enumerator:
1282/// enumeration-constant
1283/// enumeration-constant '=' constant-expression
1284/// enumeration-constant:
1285/// identifier
1286///
1287void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001288 // Enter the scope of the enum body and start the definition.
1289 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001290 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001291
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 SourceLocation LBraceLoc = ConsumeBrace();
1293
Chris Lattner7946dd32007-08-27 17:24:30 +00001294 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001295 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001296 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001297
1298 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1299
1300 DeclTy *LastEnumConstDecl = 0;
1301
1302 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001303 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1305 SourceLocation IdentLoc = ConsumeToken();
1306
1307 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001308 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001309 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001311 AssignedVal = ParseConstantExpression();
1312 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 }
1315
1316 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +00001317 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 LastEnumConstDecl,
1319 IdentLoc, Ident,
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001320 EqualLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001321 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 EnumConstantDecls.push_back(EnumConstDecl);
1323 LastEnumConstDecl = EnumConstDecl;
1324
Chris Lattner04d66662007-10-09 17:33:22 +00001325 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 break;
1327 SourceLocation CommaLoc = ConsumeToken();
1328
Chris Lattner04d66662007-10-09 17:33:22 +00001329 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001330 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1331 }
1332
1333 // Eat the }.
1334 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1335
Steve Naroff08d92e42007-09-15 18:49:24 +00001336 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 EnumConstantDecls.size());
1338
1339 DeclTy *AttrList = 0;
1340 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001341 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001343
1344 EnumScope.Exit();
1345 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001346}
1347
1348/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001349/// start of a type-qualifier-list.
1350bool Parser::isTypeQualifier() const {
1351 switch (Tok.getKind()) {
1352 default: return false;
1353 // type-qualifier
1354 case tok::kw_const:
1355 case tok::kw_volatile:
1356 case tok::kw_restrict:
1357 return true;
1358 }
1359}
1360
1361/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001362/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001363bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001364 switch (Tok.getKind()) {
1365 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001366
1367 case tok::identifier: // foo::bar
1368 // Annotate typenames and C++ scope specifiers. If we get one, just
1369 // recurse to handle whatever we get.
1370 if (TryAnnotateTypeOrScopeToken())
1371 return isTypeSpecifierQualifier();
1372 // Otherwise, not a type specifier.
1373 return false;
1374 case tok::coloncolon: // ::foo::bar
1375 if (NextToken().is(tok::kw_new) || // ::new
1376 NextToken().is(tok::kw_delete)) // ::delete
1377 return false;
1378
1379 // Annotate typenames and C++ scope specifiers. If we get one, just
1380 // recurse to handle whatever we get.
1381 if (TryAnnotateTypeOrScopeToken())
1382 return isTypeSpecifierQualifier();
1383 // Otherwise, not a type specifier.
1384 return false;
1385
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 // GNU attributes support.
1387 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001388 // GNU typeof support.
1389 case tok::kw_typeof:
1390
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 // type-specifiers
1392 case tok::kw_short:
1393 case tok::kw_long:
1394 case tok::kw_signed:
1395 case tok::kw_unsigned:
1396 case tok::kw__Complex:
1397 case tok::kw__Imaginary:
1398 case tok::kw_void:
1399 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001400 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 case tok::kw_int:
1402 case tok::kw_float:
1403 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001404 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001405 case tok::kw__Bool:
1406 case tok::kw__Decimal32:
1407 case tok::kw__Decimal64:
1408 case tok::kw__Decimal128:
1409
Chris Lattner99dc9142008-04-13 18:59:07 +00001410 // struct-or-union-specifier (C99) or class-specifier (C++)
1411 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001412 case tok::kw_struct:
1413 case tok::kw_union:
1414 // enum-specifier
1415 case tok::kw_enum:
1416
1417 // type-qualifier
1418 case tok::kw_const:
1419 case tok::kw_volatile:
1420 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001421
1422 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001423 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001424 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001425
1426 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1427 case tok::less:
1428 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001429
1430 case tok::kw___cdecl:
1431 case tok::kw___stdcall:
1432 case tok::kw___fastcall:
1433 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001434 }
1435}
1436
1437/// isDeclarationSpecifier() - Return true if the current token is part of a
1438/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001439bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001440 switch (Tok.getKind()) {
1441 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001442
1443 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001444 // Unfortunate hack to support "Class.factoryMethod" notation.
1445 if (getLang().ObjC1 && NextToken().is(tok::period))
1446 return false;
1447
Chris Lattner166a8fc2009-01-04 23:41:41 +00001448 // Annotate typenames and C++ scope specifiers. If we get one, just
1449 // recurse to handle whatever we get.
1450 if (TryAnnotateTypeOrScopeToken())
1451 return isDeclarationSpecifier();
1452 // Otherwise, not a declaration specifier.
1453 return false;
1454 case tok::coloncolon: // ::foo::bar
1455 if (NextToken().is(tok::kw_new) || // ::new
1456 NextToken().is(tok::kw_delete)) // ::delete
1457 return false;
1458
1459 // Annotate typenames and C++ scope specifiers. If we get one, just
1460 // recurse to handle whatever we get.
1461 if (TryAnnotateTypeOrScopeToken())
1462 return isDeclarationSpecifier();
1463 // Otherwise, not a declaration specifier.
1464 return false;
1465
Reid Spencer5f016e22007-07-11 17:01:13 +00001466 // storage-class-specifier
1467 case tok::kw_typedef:
1468 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001469 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001470 case tok::kw_static:
1471 case tok::kw_auto:
1472 case tok::kw_register:
1473 case tok::kw___thread:
1474
1475 // type-specifiers
1476 case tok::kw_short:
1477 case tok::kw_long:
1478 case tok::kw_signed:
1479 case tok::kw_unsigned:
1480 case tok::kw__Complex:
1481 case tok::kw__Imaginary:
1482 case tok::kw_void:
1483 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001484 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001485 case tok::kw_int:
1486 case tok::kw_float:
1487 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001488 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001489 case tok::kw__Bool:
1490 case tok::kw__Decimal32:
1491 case tok::kw__Decimal64:
1492 case tok::kw__Decimal128:
1493
Chris Lattner99dc9142008-04-13 18:59:07 +00001494 // struct-or-union-specifier (C99) or class-specifier (C++)
1495 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001496 case tok::kw_struct:
1497 case tok::kw_union:
1498 // enum-specifier
1499 case tok::kw_enum:
1500
1501 // type-qualifier
1502 case tok::kw_const:
1503 case tok::kw_volatile:
1504 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001505
Reid Spencer5f016e22007-07-11 17:01:13 +00001506 // function-specifier
1507 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001508 case tok::kw_virtual:
1509 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001510
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001511 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001512 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001513
Chris Lattner1ef08762007-08-09 17:01:07 +00001514 // GNU typeof support.
1515 case tok::kw_typeof:
1516
1517 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001518 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001520
1521 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1522 case tok::less:
1523 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001524
Steve Naroff47f52092009-01-06 19:34:12 +00001525 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001526 case tok::kw___cdecl:
1527 case tok::kw___stdcall:
1528 case tok::kw___fastcall:
1529 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001530 }
1531}
1532
1533
1534/// ParseTypeQualifierListOpt
1535/// type-qualifier-list: [C99 6.7.5]
1536/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001537/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001538/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001539/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001540///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001541void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001542 while (1) {
1543 int isInvalid = false;
1544 const char *PrevSpec = 0;
1545 SourceLocation Loc = Tok.getLocation();
1546
1547 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001548 case tok::kw_const:
1549 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1550 getLang())*2;
1551 break;
1552 case tok::kw_volatile:
1553 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1554 getLang())*2;
1555 break;
1556 case tok::kw_restrict:
1557 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1558 getLang())*2;
1559 break;
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001560 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001561 case tok::kw___cdecl:
1562 case tok::kw___stdcall:
1563 case tok::kw___fastcall:
1564 if (!PP.getLangOptions().Microsoft)
1565 goto DoneWithTypeQuals;
1566 // Just ignore it.
1567 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001568 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001569 if (AttributesAllowed) {
1570 DS.AddAttributes(ParseAttributes());
1571 continue; // do *not* consume the next token!
1572 }
1573 // otherwise, FALL THROUGH!
1574 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001575 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001576 // If this is not a type-qualifier token, we're done reading type
1577 // qualifiers. First verify that DeclSpec's are consistent.
1578 DS.Finish(Diags, PP.getSourceManager(), getLang());
1579 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001580 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001581
Reid Spencer5f016e22007-07-11 17:01:13 +00001582 // If the specifier combination wasn't legal, issue a diagnostic.
1583 if (isInvalid) {
1584 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001585 // Pick between error or extwarn.
1586 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1587 : diag::ext_duplicate_declspec;
1588 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001589 }
1590 ConsumeToken();
1591 }
1592}
1593
1594
1595/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1596///
1597void Parser::ParseDeclarator(Declarator &D) {
1598 /// This implements the 'declarator' production in the C grammar, then checks
1599 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001600 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001601}
1602
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001603/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1604/// is parsed by the function passed to it. Pass null, and the direct-declarator
1605/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001606/// ptr-operator production.
1607///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001608/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1609/// [C] pointer[opt] direct-declarator
1610/// [C++] direct-declarator
1611/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001612///
1613/// pointer: [C99 6.7.5]
1614/// '*' type-qualifier-list[opt]
1615/// '*' type-qualifier-list[opt] pointer
1616///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001617/// ptr-operator:
1618/// '*' cv-qualifier-seq[opt]
1619/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00001620/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001621/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00001622/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001623/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001624void Parser::ParseDeclaratorInternal(Declarator &D,
1625 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001626
Sebastian Redlf30208a2009-01-24 21:16:55 +00001627 // C++ member pointers start with a '::' or a nested-name.
1628 // Member pointers get special handling, since there's no place for the
1629 // scope spec in the generic path below.
1630 if ((Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1631 Tok.is(tok::annot_cxxscope)) && getLang().CPlusPlus) {
1632 CXXScopeSpec SS;
1633 if (ParseOptionalCXXScopeSpecifier(SS)) {
1634 if(Tok.isNot(tok::star)) {
1635 // The scope spec really belongs to the direct-declarator.
1636 D.getCXXScopeSpec() = SS;
1637 if (DirectDeclParser)
1638 (this->*DirectDeclParser)(D);
1639 return;
1640 }
1641
1642 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001643 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001644 DeclSpec DS;
1645 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001646 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001647
1648 // Recurse to parse whatever is left.
1649 ParseDeclaratorInternal(D, DirectDeclParser);
1650
1651 // Sema will have to catch (syntactically invalid) pointers into global
1652 // scope. It has to catch pointers into namespace scope anyway.
1653 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001654 Loc, DS.TakeAttributes()),
1655 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00001656 return;
1657 }
1658 }
1659
1660 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001661 // Not a pointer, C++ reference, or block.
1662 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00001663 // We parse rvalue refs in C++03, because otherwise the errors are scary.
1664 (Kind != tok::ampamp || !getLang().CPlusPlus) &&
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001665 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001666 if (DirectDeclParser)
1667 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001668 return;
1669 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001670
Sebastian Redl05532f22009-03-15 22:02:01 +00001671 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1672 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00001673 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001674 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001675
Steve Naroff4ef1c992008-08-28 10:07:06 +00001676 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner76549142008-02-21 01:32:26 +00001677 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001678 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001679
Reid Spencer5f016e22007-07-11 17:01:13 +00001680 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001681 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001682
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001684 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001685 if (Kind == tok::star)
1686 // Remember that we parsed a pointer type, and remember the type-quals.
1687 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001688 DS.TakeAttributes()),
1689 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00001690 else
1691 // Remember that we parsed a Block type, and remember the type-quals.
1692 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001693 Loc),
1694 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001695 } else {
1696 // Is a reference
1697 DeclSpec DS;
1698
Sebastian Redl743de1f2009-03-23 00:00:23 +00001699 // Complain about rvalue references in C++03, but then go on and build
1700 // the declarator.
1701 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1702 Diag(Loc, diag::err_rvalue_reference);
1703
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1705 // cv-qualifiers are introduced through the use of a typedef or of a
1706 // template type argument, in which case the cv-qualifiers are ignored.
1707 //
1708 // [GNU] Retricted references are allowed.
1709 // [GNU] Attributes on references are allowed.
1710 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001711 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001712
1713 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1714 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1715 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001716 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001717 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1718 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001719 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001720 }
1721
1722 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001723 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001724
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001725 if (D.getNumTypeObjects() > 0) {
1726 // C++ [dcl.ref]p4: There shall be no references to references.
1727 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1728 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001729 if (const IdentifierInfo *II = D.getIdentifier())
1730 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1731 << II;
1732 else
1733 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1734 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001735
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001736 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001737 // can go ahead and build the (technically ill-formed)
1738 // declarator: reference collapsing will take care of it.
1739 }
1740 }
1741
Reid Spencer5f016e22007-07-11 17:01:13 +00001742 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001743 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00001744 DS.TakeAttributes(),
1745 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001746 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001747 }
1748}
1749
1750/// ParseDirectDeclarator
1751/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001752/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001753/// '(' declarator ')'
1754/// [GNU] '(' attributes declarator ')'
1755/// [C90] direct-declarator '[' constant-expression[opt] ']'
1756/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1757/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1758/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1759/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1760/// direct-declarator '(' parameter-type-list ')'
1761/// direct-declarator '(' identifier-list[opt] ')'
1762/// [GNU] direct-declarator '(' parameter-forward-declarations
1763/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001764/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1765/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001766/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001767///
1768/// declarator-id: [C++ 8]
1769/// id-expression
1770/// '::'[opt] nested-name-specifier[opt] type-name
1771///
1772/// id-expression: [C++ 5.1]
1773/// unqualified-id
1774/// qualified-id [TODO]
1775///
1776/// unqualified-id: [C++ 5.1]
1777/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001778/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001779/// conversion-function-id [TODO]
1780/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00001781/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001782///
Reid Spencer5f016e22007-07-11 17:01:13 +00001783void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001784 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001785
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001786 if (getLang().CPlusPlus) {
1787 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001788 // ParseDeclaratorInternal might already have parsed the scope.
1789 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1790 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001791 if (afterCXXScope) {
1792 // Change the declaration context for name lookup, until this function
1793 // is exited (and the declarator has been parsed).
1794 DeclScopeObj.EnterDeclaratorScope();
1795 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001796
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001797 if (Tok.is(tok::identifier)) {
1798 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001799
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001800 // If this identifier is the name of the current class, it's a
1801 // constructor name.
Douglas Gregor39a8de12009-02-25 19:37:18 +00001802 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroffb43a50f2009-01-28 19:39:02 +00001803 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +00001804 Tok.getLocation(), CurScope),
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001805 Tok.getLocation());
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001806 // This is a normal identifier.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001807 } else
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001808 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1809 ConsumeToken();
1810 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001811 } else if (Tok.is(tok::annot_template_id)) {
1812 TemplateIdAnnotation *TemplateId
1813 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1814
1815 // FIXME: Could this template-id name a constructor?
1816
1817 // FIXME: This is an egregious hack, where we silently ignore
1818 // the specialization (which should be a function template
1819 // specialization name) and use the name instead. This hack
1820 // will go away when we have support for function
1821 // specializations.
1822 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1823 TemplateId->Destroy();
1824 ConsumeToken();
1825 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00001826 } else if (Tok.is(tok::kw_operator)) {
1827 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001828 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001829
Douglas Gregor70316a02008-12-26 15:00:45 +00001830 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00001831 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1832 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00001833 } else {
1834 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00001835 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1836 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1837 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00001838 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001839 }
Douglas Gregor70316a02008-12-26 15:00:45 +00001840 }
1841 goto PastIdentifier;
1842 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001843 // This should be a C++ destructor.
1844 SourceLocation TildeLoc = ConsumeToken();
1845 if (Tok.is(tok::identifier)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001846 // FIXME: Inaccurate.
1847 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00001848 SourceLocation EndLoc;
1849 if (TypeTy *Type = ParseClassName(EndLoc)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001850 D.setDestructor(Type, TildeLoc, NameLoc);
1851 } else {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001852 D.SetIdentifier(0, TildeLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001853 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001854 } else {
1855 Diag(Tok, diag::err_expected_class_name);
1856 D.SetIdentifier(0, TildeLoc);
1857 }
1858 goto PastIdentifier;
1859 }
1860
1861 // If we reached this point, token is not identifier and not '~'.
1862
1863 if (afterCXXScope) {
1864 Diag(Tok, diag::err_expected_unqualified_id);
1865 D.SetIdentifier(0, Tok.getLocation());
1866 D.setInvalidType(true);
1867 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001868 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001869 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001870 }
1871
1872 // If we reached this point, we are either in C/ObjC or the token didn't
1873 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001874 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1875 assert(!getLang().CPlusPlus &&
1876 "There's a C++-specific check for tok::identifier above");
1877 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1878 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1879 ConsumeToken();
1880 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001881 // direct-declarator: '(' declarator ')'
1882 // direct-declarator: '(' attributes declarator ')'
1883 // Example: 'char (*X)' or 'int (*XX)(void)'
1884 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001885 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001886 // This could be something simple like "int" (in which case the declarator
1887 // portion is empty), if an abstract-declarator is allowed.
1888 D.SetIdentifier(0, Tok.getLocation());
1889 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00001890 if (D.getContext() == Declarator::MemberContext)
1891 Diag(Tok, diag::err_expected_member_name_or_semi)
1892 << D.getDeclSpec().getSourceRange();
1893 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001894 Diag(Tok, diag::err_expected_unqualified_id);
1895 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00001896 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00001898 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001899 }
1900
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001901 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00001902 assert(D.isPastIdentifier() &&
1903 "Haven't past the location of the identifier yet?");
1904
1905 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001906 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001907 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1908 // In such a case, check if we actually have a function declarator; if it
1909 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00001910 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1911 // When not in file scope, warn for ambiguous function declarators, just
1912 // in case the author intended it as a variable definition.
1913 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1914 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1915 break;
1916 }
Chris Lattneref4715c2008-04-06 05:45:57 +00001917 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001918 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 ParseBracketDeclarator(D);
1920 } else {
1921 break;
1922 }
1923 }
1924}
1925
Chris Lattneref4715c2008-04-06 05:45:57 +00001926/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1927/// only called before the identifier, so these are most likely just grouping
1928/// parens for precedence. If we find that these are actually function
1929/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1930///
1931/// direct-declarator:
1932/// '(' declarator ')'
1933/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00001934/// direct-declarator '(' parameter-type-list ')'
1935/// direct-declarator '(' identifier-list[opt] ')'
1936/// [GNU] direct-declarator '(' parameter-forward-declarations
1937/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00001938///
1939void Parser::ParseParenDeclarator(Declarator &D) {
1940 SourceLocation StartLoc = ConsumeParen();
1941 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1942
Chris Lattner7399ee02008-10-20 02:05:46 +00001943 // Eat any attributes before we look at whether this is a grouping or function
1944 // declarator paren. If this is a grouping paren, the attribute applies to
1945 // the type being built up, for example:
1946 // int (__attribute__(()) *x)(long y)
1947 // If this ends up not being a grouping paren, the attribute applies to the
1948 // first argument, for example:
1949 // int (__attribute__(()) int x)
1950 // In either case, we need to eat any attributes to be able to determine what
1951 // sort of paren this is.
1952 //
1953 AttributeList *AttrList = 0;
1954 bool RequiresArg = false;
1955 if (Tok.is(tok::kw___attribute)) {
1956 AttrList = ParseAttributes();
1957
1958 // We require that the argument list (if this is a non-grouping paren) be
1959 // present even if the attribute list was empty.
1960 RequiresArg = true;
1961 }
Steve Naroff239f0732008-12-25 14:16:32 +00001962 // Eat any Microsoft extensions.
Douglas Gregor5a2f5d32009-01-10 00:48:18 +00001963 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1964 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff239f0732008-12-25 14:16:32 +00001965 ConsumeToken();
Chris Lattner7399ee02008-10-20 02:05:46 +00001966
Chris Lattneref4715c2008-04-06 05:45:57 +00001967 // If we haven't past the identifier yet (or where the identifier would be
1968 // stored, if this is an abstract declarator), then this is probably just
1969 // grouping parens. However, if this could be an abstract-declarator, then
1970 // this could also be the start of function arguments (consider 'void()').
1971 bool isGrouping;
1972
1973 if (!D.mayOmitIdentifier()) {
1974 // If this can't be an abstract-declarator, this *must* be a grouping
1975 // paren, because we haven't seen the identifier yet.
1976 isGrouping = true;
1977 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001978 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00001979 isDeclarationSpecifier()) { // 'int(int)' is a function.
1980 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1981 // considered to be a type, not a K&R identifier-list.
1982 isGrouping = false;
1983 } else {
1984 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1985 isGrouping = true;
1986 }
1987
1988 // If this is a grouping paren, handle:
1989 // direct-declarator: '(' declarator ')'
1990 // direct-declarator: '(' attributes declarator ')'
1991 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001992 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001993 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00001994 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00001995 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001996
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001997 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00001998 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001999 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002000
2001 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002002 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002003 return;
2004 }
2005
2006 // Okay, if this wasn't a grouping paren, it must be the start of a function
2007 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002008 // identifier (and remember where it would have been), then call into
2009 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002010 D.SetIdentifier(0, Tok.getLocation());
2011
Chris Lattner7399ee02008-10-20 02:05:46 +00002012 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002013}
2014
2015/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2016/// declarator D up to a paren, which indicates that we are parsing function
2017/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002018///
Chris Lattner7399ee02008-10-20 02:05:46 +00002019/// If AttrList is non-null, then the caller parsed those arguments immediately
2020/// after the open paren - they should be considered to be the first argument of
2021/// a parameter. If RequiresArg is true, then the first argument of the
2022/// function is required to be present and required to not be an identifier
2023/// list.
2024///
Reid Spencer5f016e22007-07-11 17:01:13 +00002025/// This method also handles this portion of the grammar:
2026/// parameter-type-list: [C99 6.7.5]
2027/// parameter-list
2028/// parameter-list ',' '...'
2029///
2030/// parameter-list: [C99 6.7.5]
2031/// parameter-declaration
2032/// parameter-list ',' parameter-declaration
2033///
2034/// parameter-declaration: [C99 6.7.5]
2035/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002036/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002037/// [GNU] declaration-specifiers declarator attributes
2038/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002039/// [C++] declaration-specifiers abstract-declarator[opt]
2040/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002041/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2042///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002043/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
2044/// and "exception-specification[opt]"(TODO).
2045///
Chris Lattner7399ee02008-10-20 02:05:46 +00002046void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2047 AttributeList *AttrList,
2048 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002049 // lparen is already consumed!
2050 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00002051
Chris Lattner7399ee02008-10-20 02:05:46 +00002052 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002053 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002054 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002055 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002056 delete AttrList;
2057 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002058
Sebastian Redlab197ba2009-02-09 18:23:29 +00002059 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002060
2061 // cv-qualifier-seq[opt].
2062 DeclSpec DS;
2063 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002064 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002065 if (!DS.getSourceRange().getEnd().isInvalid())
2066 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002067
2068 // Parse exception-specification[opt].
2069 if (Tok.is(tok::kw_throw))
Sebastian Redlab197ba2009-02-09 18:23:29 +00002070 ParseExceptionSpecification(Loc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002071 }
2072
Chris Lattnerf97409f2008-04-06 06:57:35 +00002073 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002074 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002075 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002076 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002077 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002078 /*arglist*/ 0, 0,
2079 DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002080 LParenLoc, D),
2081 Loc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002082 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00002083 }
2084
2085 // Alternatively, this parameter list may be an identifier list form for a
2086 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002087 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002088 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002089 // K&R identifier lists can't have typedefs as identifiers, per
2090 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002091 if (RequiresArg) {
2092 Diag(Tok, diag::err_argument_required_after_attribute);
2093 delete AttrList;
2094 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002095 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2096 // normal declarators, not for abstract-declarators.
2097 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002098 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002099 }
2100
2101 // Finally, a normal, non-empty parameter type list.
2102
2103 // Build up an array of information about the parsed arguments.
2104 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002105
2106 // Enter function-declaration scope, limiting any declarators to the
2107 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002108 ParseScope PrototypeScope(this,
2109 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002110
2111 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002112 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002113 while (1) {
2114 if (Tok.is(tok::ellipsis)) {
2115 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002116 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002117 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002118 }
2119
Chris Lattnerf97409f2008-04-06 06:57:35 +00002120 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002121
Chris Lattnerf97409f2008-04-06 06:57:35 +00002122 // Parse the declaration-specifiers.
2123 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002124
2125 // If the caller parsed attributes for the first argument, add them now.
2126 if (AttrList) {
2127 DS.AddAttributes(AttrList);
2128 AttrList = 0; // Only apply the attributes to the first parameter.
2129 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002130 ParseDeclarationSpecifiers(DS);
2131
Chris Lattnerf97409f2008-04-06 06:57:35 +00002132 // Parse the declarator. This is "PrototypeContext", because we must
2133 // accept either 'declarator' or 'abstract-declarator' here.
2134 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2135 ParseDeclarator(ParmDecl);
2136
2137 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002138 if (Tok.is(tok::kw___attribute)) {
2139 SourceLocation Loc;
2140 AttributeList *AttrList = ParseAttributes(&Loc);
2141 ParmDecl.AddAttributes(AttrList, Loc);
2142 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002143
Chris Lattnerf97409f2008-04-06 06:57:35 +00002144 // Remember this parsed parameter in ParamInfo.
2145 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2146
Douglas Gregor72b505b2008-12-16 21:30:33 +00002147 // DefArgToks is used when the parsing of default arguments needs
2148 // to be delayed.
2149 CachedTokens *DefArgToks = 0;
2150
Chris Lattnerf97409f2008-04-06 06:57:35 +00002151 // If no parameter was specified, verify that *something* was specified,
2152 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002153 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2154 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002155 // Completely missing, emit error.
2156 Diag(DSStart, diag::err_missing_param);
2157 } else {
2158 // Otherwise, we have something. Add it and let semantic analysis try
2159 // to grok it and add the result to the ParamInfo we are building.
2160
2161 // Inform the actions module about the parameter declarator, so it gets
2162 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00002163 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2164
2165 // Parse the default argument, if any. We parse the default
2166 // arguments in all dialects; the semantic analysis in
2167 // ActOnParamDefaultArgument will reject the default argument in
2168 // C.
2169 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002170 SourceLocation EqualLoc = Tok.getLocation();
2171
Chris Lattner04421082008-04-08 04:40:51 +00002172 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002173 if (D.getContext() == Declarator::MemberContext) {
2174 // If we're inside a class definition, cache the tokens
2175 // corresponding to the default argument. We'll actually parse
2176 // them when we see the end of the class definition.
2177 // FIXME: Templates will require something similar.
2178 // FIXME: Can we use a smart pointer for Toks?
2179 DefArgToks = new CachedTokens;
2180
2181 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2182 tok::semi, false)) {
2183 delete DefArgToks;
2184 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002185 Actions.ActOnParamDefaultArgumentError(Param);
2186 } else
2187 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner04421082008-04-08 04:40:51 +00002188 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002189 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002190 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002191
2192 OwningExprResult DefArgResult(ParseAssignmentExpression());
2193 if (DefArgResult.isInvalid()) {
2194 Actions.ActOnParamDefaultArgumentError(Param);
2195 SkipUntil(tok::comma, tok::r_paren, true, true);
2196 } else {
2197 // Inform the actions module about the default argument
2198 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002199 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002200 }
Chris Lattner04421082008-04-08 04:40:51 +00002201 }
2202 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002203
2204 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002205 ParmDecl.getIdentifierLoc(), Param,
2206 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002207 }
2208
2209 // If the next token is a comma, consume it and keep reading arguments.
2210 if (Tok.isNot(tok::comma)) break;
2211
2212 // Consume the comma.
2213 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002214 }
2215
Chris Lattnerf97409f2008-04-06 06:57:35 +00002216 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002217 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002218
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002219 // If we have the closing ')', eat it.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002220 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002221
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002222 DeclSpec DS;
2223 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002224 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002225 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002226 if (!DS.getSourceRange().getEnd().isInvalid())
2227 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002228
2229 // Parse exception-specification[opt].
2230 if (Tok.is(tok::kw_throw))
Sebastian Redlab197ba2009-02-09 18:23:29 +00002231 ParseExceptionSpecification(Loc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002232 }
2233
Reid Spencer5f016e22007-07-11 17:01:13 +00002234 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002235 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002236 EllipsisLoc,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002237 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002238 DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002239 LParenLoc, D),
2240 Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002241}
2242
Chris Lattner66d28652008-04-06 06:34:08 +00002243/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2244/// we found a K&R-style identifier list instead of a type argument list. The
2245/// current token is known to be the first identifier in the list.
2246///
2247/// identifier-list: [C99 6.7.5]
2248/// identifier
2249/// identifier-list ',' identifier
2250///
2251void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2252 Declarator &D) {
2253 // Build up an array of information about the parsed arguments.
2254 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2255 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2256
2257 // If there was no identifier specified for the declarator, either we are in
2258 // an abstract-declarator, or we are in a parameter declarator which was found
2259 // to be abstract. In abstract-declarators, identifier lists are not valid:
2260 // diagnose this.
2261 if (!D.getIdentifier())
2262 Diag(Tok, diag::ext_ident_list_in_param);
2263
2264 // Tok is known to be the first identifier in the list. Remember this
2265 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002266 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002267 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2268 Tok.getLocation(), 0));
2269
Chris Lattner50c64772008-04-06 06:39:19 +00002270 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002271
2272 while (Tok.is(tok::comma)) {
2273 // Eat the comma.
2274 ConsumeToken();
2275
Chris Lattner50c64772008-04-06 06:39:19 +00002276 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002277 if (Tok.isNot(tok::identifier)) {
2278 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002279 SkipUntil(tok::r_paren);
2280 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002281 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002282
Chris Lattner66d28652008-04-06 06:34:08 +00002283 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002284
2285 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002286 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002287 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002288
2289 // Verify that the argument identifier has not already been mentioned.
2290 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002291 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002292 } else {
2293 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002294 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2295 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00002296 }
Chris Lattner66d28652008-04-06 06:34:08 +00002297
2298 // Eat the identifier.
2299 ConsumeToken();
2300 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002301
2302 // If we have the closing ')', eat it and we're done.
2303 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2304
Chris Lattner50c64772008-04-06 06:39:19 +00002305 // Remember that we parsed a function type, and remember the attributes. This
2306 // function type is always a K&R style function type, which is not varargs and
2307 // has no prototype.
2308 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002309 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002310 &ParamInfo[0], ParamInfo.size(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002311 /*TypeQuals*/0, LParenLoc, D),
2312 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002313}
Chris Lattneref4715c2008-04-06 05:45:57 +00002314
Reid Spencer5f016e22007-07-11 17:01:13 +00002315/// [C90] direct-declarator '[' constant-expression[opt] ']'
2316/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2317/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2318/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2319/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2320void Parser::ParseBracketDeclarator(Declarator &D) {
2321 SourceLocation StartLoc = ConsumeBracket();
2322
Chris Lattner378c7e42008-12-18 07:27:21 +00002323 // C array syntax has many features, but by-far the most common is [] and [4].
2324 // This code does a fast path to handle some of the most obvious cases.
2325 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002326 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002327 // Remember that we parsed the empty array type.
2328 OwningExprResult NumElements(Actions);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002329 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2330 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002331 return;
2332 } else if (Tok.getKind() == tok::numeric_constant &&
2333 GetLookAheadToken(1).is(tok::r_square)) {
2334 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002335 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002336 ConsumeToken();
2337
Sebastian Redlab197ba2009-02-09 18:23:29 +00002338 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002339
2340 // If there was an error parsing the assignment-expression, recover.
2341 if (ExprRes.isInvalid())
2342 ExprRes.release(); // Deallocate expr, just use [].
2343
2344 // Remember that we parsed a array type, and remember its features.
2345 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002346 ExprRes.release(), StartLoc),
2347 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002348 return;
2349 }
2350
Reid Spencer5f016e22007-07-11 17:01:13 +00002351 // If valid, this location is the position where we read the 'static' keyword.
2352 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002353 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002354 StaticLoc = ConsumeToken();
2355
2356 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002357 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002358 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002359 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002360
2361 // If we haven't already read 'static', check to see if there is one after the
2362 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002363 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002364 StaticLoc = ConsumeToken();
2365
2366 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2367 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002368 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002369
2370 // Handle the case where we have '[*]' as the array size. However, a leading
2371 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2372 // the the token after the star is a ']'. Since stars in arrays are
2373 // infrequent, use of lookahead is not costly here.
2374 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002375 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002376
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002377 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002378 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002379 StaticLoc = SourceLocation(); // Drop the static.
2380 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002381 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002382 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002383 // Note, in C89, this production uses the constant-expr production instead
2384 // of assignment-expr. The only difference is that assignment-expr allows
2385 // things like '=' and '*='. Sema rejects these in C89 mode because they
2386 // are not i-c-e's, so we don't need to distinguish between the two here.
2387
Reid Spencer5f016e22007-07-11 17:01:13 +00002388 // Parse the assignment-expression now.
2389 NumElements = ParseAssignmentExpression();
2390 }
2391
2392 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002393 if (NumElements.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002394 // If the expression was invalid, skip it.
2395 SkipUntil(tok::r_square);
2396 return;
2397 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002398
2399 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2400
Chris Lattner378c7e42008-12-18 07:27:21 +00002401 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002402 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2403 StaticLoc.isValid(), isStar,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002404 NumElements.release(), StartLoc),
2405 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002406}
2407
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002408/// [GNU] typeof-specifier:
2409/// typeof ( expressions )
2410/// typeof ( type-name )
2411/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002412///
2413void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002414 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002415 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002416 SourceLocation StartLoc = ConsumeToken();
2417
Chris Lattner04d66662007-10-09 17:33:22 +00002418 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002419 if (!getLang().CPlusPlus) {
Chris Lattner08631c52008-11-23 21:45:46 +00002420 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002421 return;
2422 }
2423
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002424 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor809070a2009-02-18 17:45:20 +00002425 if (Result.isInvalid()) {
2426 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002427 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002428 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002429
2430 const char *PrevSpec = 0;
2431 // Check for duplicate type specifiers.
2432 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002433 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002434 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002435
2436 // FIXME: Not accurate, the range gets one token more than it should.
2437 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002438 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002439 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002440
Steve Naroffd1861fd2007-07-31 12:34:36 +00002441 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2442
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00002443 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00002444 Action::TypeResult Ty = ParseTypeName();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002445
Douglas Gregor809070a2009-02-18 17:45:20 +00002446 assert((Ty.isInvalid() || Ty.get()) &&
2447 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002448
Chris Lattner04d66662007-10-09 17:33:22 +00002449 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002450 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002451 return;
2452 }
2453 RParenLoc = ConsumeParen();
Douglas Gregor809070a2009-02-18 17:45:20 +00002454
2455 if (Ty.isInvalid())
2456 DS.SetTypeSpecError();
2457 else {
2458 const char *PrevSpec = 0;
2459 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2460 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2461 Ty.get()))
2462 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2463 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00002464 } else { // we have an expression.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002465 OwningExprResult Result(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002466
2467 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002468 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor809070a2009-02-18 17:45:20 +00002469 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002470 return;
2471 }
2472 RParenLoc = ConsumeParen();
2473 const char *PrevSpec = 0;
2474 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2475 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002476 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002477 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002478 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00002479 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002480}
2481
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002482