blob: 25565e657687dd092d550f6e0fdab58ee09700a1 [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;
494 SS.setScopeRep(Tok.getAnnotationValue());
495 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;
511
512 ConsumeToken(); // The C++ scope.
513
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000514 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000515 TypeRep);
516 if (isInvalid)
517 break;
518
519 DS.SetRangeEnd(Tok.getLocation());
520 ConsumeToken(); // The typename.
521
522 continue;
523 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000524
525 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000526 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner80d0c892009-01-21 19:48:37 +0000527 Tok.getAnnotationValue());
528 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
529 ConsumeToken(); // The typename
530
531 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
532 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
533 // Objective-C interface. If we don't have Objective-C or a '<', this is
534 // just a normal reference to a typedef name.
535 if (!Tok.is(tok::less) || !getLang().ObjC1)
536 continue;
537
538 SourceLocation EndProtoLoc;
539 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
540 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
541 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
542
543 DS.SetRangeEnd(EndProtoLoc);
544 continue;
545 }
546
Chris Lattner3bd934a2008-07-26 01:18:38 +0000547 // typedef-name
548 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000549 // In C++, check to see if this is a scope specifier like foo::bar::, if
550 // so handle it as such. This is important for ctor parsing.
Chris Lattner837acd02009-01-21 19:19:26 +0000551 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
552 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000553
Chris Lattner3bd934a2008-07-26 01:18:38 +0000554 // This identifier can only be a typedef name if we haven't already seen
555 // a type-specifier. Without this check we misparse:
556 // typedef int X; struct Y { short X; }; as 'short int'.
557 if (DS.hasTypeSpecifier())
558 goto DoneWithDeclSpec;
559
560 // It has to be available as a typedef too!
Douglas Gregorb696ea32009-02-04 17:00:24 +0000561 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
562 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000563
Chris Lattner3bd934a2008-07-26 01:18:38 +0000564 if (TypeRep == 0)
565 goto DoneWithDeclSpec;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000566
Douglas Gregorb48fe382008-10-31 09:07:45 +0000567 // C++: If the identifier is actually the name of the class type
568 // being defined and the next token is a '(', then this is a
569 // constructor declaration. We're done with the decl-specifiers
570 // and will treat this token as an identifier.
571 if (getLang().CPlusPlus &&
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000572 CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000573 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
574 NextToken().getKind() == tok::l_paren)
575 goto DoneWithDeclSpec;
576
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000577 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner3bd934a2008-07-26 01:18:38 +0000578 TypeRep);
579 if (isInvalid)
580 break;
581
582 DS.SetRangeEnd(Tok.getLocation());
583 ConsumeToken(); // The identifier
584
585 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
586 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
587 // Objective-C interface. If we don't have Objective-C or a '<', this is
588 // just a normal reference to a typedef name.
589 if (!Tok.is(tok::less) || !getLang().ObjC1)
590 continue;
591
592 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000593 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000594 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000595 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000596
597 DS.SetRangeEnd(EndProtoLoc);
598
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000599 // Need to support trailing type qualifiers (e.g. "id<p> const").
600 // If a type specifier follows, it will be diagnosed elsewhere.
601 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000602 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000603
604 // type-name
605 case tok::annot_template_id: {
606 TemplateIdAnnotation *TemplateId
607 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
608 if (TemplateId->Kind != TNK_Class_template) {
609 // This template-id does not refer to a type name, so we're
610 // done with the type-specifiers.
611 goto DoneWithDeclSpec;
612 }
613
614 // Turn the template-id annotation token into a type annotation
615 // token, then try again to parse it as a type-specifier.
616 if (AnnotateTemplateIdTokenAsType())
617 DS.SetTypeSpecError();
618
619 continue;
620 }
621
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 // GNU attributes support.
623 case tok::kw___attribute:
624 DS.AddAttributes(ParseAttributes());
625 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000626
627 // Microsoft declspec support.
628 case tok::kw___declspec:
629 if (!PP.getLangOptions().Microsoft)
630 goto DoneWithDeclSpec;
631 FuzzyParseMicrosoftDeclSpec();
632 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000633
Steve Naroff239f0732008-12-25 14:16:32 +0000634 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000635 case tok::kw___forceinline:
636 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000637 case tok::kw___cdecl:
638 case tok::kw___stdcall:
639 case tok::kw___fastcall:
640 if (!PP.getLangOptions().Microsoft)
641 goto DoneWithDeclSpec;
642 // Just ignore it.
643 break;
644
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 // storage-class-specifier
646 case tok::kw_typedef:
647 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
648 break;
649 case tok::kw_extern:
650 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000651 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
653 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000654 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000655 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
656 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000657 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 case tok::kw_static:
659 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000660 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
662 break;
663 case tok::kw_auto:
664 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
665 break;
666 case tok::kw_register:
667 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
668 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000669 case tok::kw_mutable:
670 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
671 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 case tok::kw___thread:
673 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
674 break;
675
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 continue;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000677
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 // function-specifier
679 case tok::kw_inline:
680 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
681 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000682 case tok::kw_virtual:
683 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
684 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000685 case tok::kw_explicit:
686 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
687 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000688
689 // type-specifier
690 case tok::kw_short:
691 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
692 break;
693 case tok::kw_long:
694 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
695 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
696 else
697 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
698 break;
699 case tok::kw_signed:
700 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
701 break;
702 case tok::kw_unsigned:
703 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
704 break;
705 case tok::kw__Complex:
706 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
707 break;
708 case tok::kw__Imaginary:
709 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
710 break;
711 case tok::kw_void:
712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
713 break;
714 case tok::kw_char:
715 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
716 break;
717 case tok::kw_int:
718 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
719 break;
720 case tok::kw_float:
721 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
722 break;
723 case tok::kw_double:
724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
725 break;
726 case tok::kw_wchar_t:
727 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
728 break;
729 case tok::kw_bool:
730 case tok::kw__Bool:
731 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
732 break;
733 case tok::kw__Decimal32:
734 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
735 break;
736 case tok::kw__Decimal64:
737 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
738 break;
739 case tok::kw__Decimal128:
740 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
741 break;
742
743 // class-specifier:
744 case tok::kw_class:
745 case tok::kw_struct:
746 case tok::kw_union:
747 ParseClassSpecifier(DS, TemplateParams);
748 continue;
749
750 // enum-specifier:
751 case tok::kw_enum:
752 ParseEnumSpecifier(DS);
753 continue;
754
755 // cv-qualifier:
756 case tok::kw_const:
757 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
758 break;
759 case tok::kw_volatile:
760 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
761 getLang())*2;
762 break;
763 case tok::kw_restrict:
764 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
765 getLang())*2;
766 break;
767
768 // GNU typeof support.
769 case tok::kw_typeof:
770 ParseTypeofSpecifier(DS);
771 continue;
772
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000773 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000774 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000775 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
776 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000777 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000778 goto DoneWithDeclSpec;
779
780 {
781 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000782 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000783 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000784 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000785 DS.SetRangeEnd(EndProtoLoc);
786
Chris Lattner1ab3b962008-11-18 07:48:38 +0000787 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
788 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000789 // Need to support trailing type qualifiers (e.g. "id<p> const").
790 // If a type specifier follows, it will be diagnosed elsewhere.
791 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000792 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 }
794 // If the specifier combination wasn't legal, issue a diagnostic.
795 if (isInvalid) {
796 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000797 // Pick between error or extwarn.
798 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
799 : diag::ext_duplicate_declspec;
800 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000802 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 ConsumeToken();
804 }
805}
Douglas Gregoradcac882008-12-01 23:54:00 +0000806
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000807/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +0000808/// primarily follow the C++ grammar with additions for C99 and GNU,
809/// which together subsume the C grammar. Note that the C++
810/// type-specifier also includes the C type-qualifier (for const,
811/// volatile, and C99 restrict). Returns true if a type-specifier was
812/// found (and parsed), false otherwise.
813///
814/// type-specifier: [C++ 7.1.5]
815/// simple-type-specifier
816/// class-specifier
817/// enum-specifier
818/// elaborated-type-specifier [TODO]
819/// cv-qualifier
820///
821/// cv-qualifier: [C++ 7.1.5.1]
822/// 'const'
823/// 'volatile'
824/// [C99] 'restrict'
825///
826/// simple-type-specifier: [ C++ 7.1.5.2]
827/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
828/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
829/// 'char'
830/// 'wchar_t'
831/// 'bool'
832/// 'short'
833/// 'int'
834/// 'long'
835/// 'signed'
836/// 'unsigned'
837/// 'float'
838/// 'double'
839/// 'void'
840/// [C99] '_Bool'
841/// [C99] '_Complex'
842/// [C99] '_Imaginary' // Removed in TC2?
843/// [GNU] '_Decimal32'
844/// [GNU] '_Decimal64'
845/// [GNU] '_Decimal128'
846/// [GNU] typeof-specifier
847/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
848/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000849bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
850 const char *&PrevSpec,
851 TemplateParameterLists *TemplateParams){
Douglas Gregor12e083c2008-11-07 15:42:26 +0000852 SourceLocation Loc = Tok.getLocation();
853
854 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +0000855 case tok::identifier: // foo::bar
856 // Annotate typenames and C++ scope specifiers. If we get one, just
857 // recurse to handle whatever we get.
858 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000859 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000860 // Otherwise, not a type specifier.
861 return false;
862 case tok::coloncolon: // ::foo::bar
863 if (NextToken().is(tok::kw_new) || // ::new
864 NextToken().is(tok::kw_delete)) // ::delete
865 return false;
866
867 // Annotate typenames and C++ scope specifiers. If we get one, just
868 // recurse to handle whatever we get.
869 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000870 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000871 // Otherwise, not a type specifier.
872 return false;
873
Douglas Gregor12e083c2008-11-07 15:42:26 +0000874 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000875 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000876 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000877 Tok.getAnnotationValue());
878 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
879 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +0000880
881 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
882 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
883 // Objective-C interface. If we don't have Objective-C or a '<', this is
884 // just a normal reference to a typedef name.
885 if (!Tok.is(tok::less) || !getLang().ObjC1)
886 return true;
887
888 SourceLocation EndProtoLoc;
889 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
890 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
891 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
892
893 DS.SetRangeEnd(EndProtoLoc);
894 return true;
895 }
896
897 case tok::kw_short:
898 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
899 break;
900 case tok::kw_long:
901 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
902 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
903 else
904 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
905 break;
906 case tok::kw_signed:
907 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
908 break;
909 case tok::kw_unsigned:
910 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
911 break;
912 case tok::kw__Complex:
913 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
914 break;
915 case tok::kw__Imaginary:
916 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
917 break;
918 case tok::kw_void:
919 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
920 break;
921 case tok::kw_char:
922 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
923 break;
924 case tok::kw_int:
925 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
926 break;
927 case tok::kw_float:
928 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
929 break;
930 case tok::kw_double:
931 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
932 break;
933 case tok::kw_wchar_t:
934 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
935 break;
936 case tok::kw_bool:
937 case tok::kw__Bool:
938 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
939 break;
940 case tok::kw__Decimal32:
941 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
942 break;
943 case tok::kw__Decimal64:
944 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
945 break;
946 case tok::kw__Decimal128:
947 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
948 break;
949
950 // class-specifier:
951 case tok::kw_class:
952 case tok::kw_struct:
953 case tok::kw_union:
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000954 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor12e083c2008-11-07 15:42:26 +0000955 return true;
956
957 // enum-specifier:
958 case tok::kw_enum:
959 ParseEnumSpecifier(DS);
960 return true;
961
962 // cv-qualifier:
963 case tok::kw_const:
964 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
965 getLang())*2;
966 break;
967 case tok::kw_volatile:
968 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
969 getLang())*2;
970 break;
971 case tok::kw_restrict:
972 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
973 getLang())*2;
974 break;
975
976 // GNU typeof support.
977 case tok::kw_typeof:
978 ParseTypeofSpecifier(DS);
979 return true;
980
Steve Naroff239f0732008-12-25 14:16:32 +0000981 case tok::kw___cdecl:
982 case tok::kw___stdcall:
983 case tok::kw___fastcall:
Chris Lattner837acd02009-01-21 19:19:26 +0000984 if (!PP.getLangOptions().Microsoft) return false;
985 ConsumeToken();
986 return true;
Steve Naroff239f0732008-12-25 14:16:32 +0000987
Douglas Gregor12e083c2008-11-07 15:42:26 +0000988 default:
989 // Not a type-specifier; do nothing.
990 return false;
991 }
992
993 // If the specifier combination wasn't legal, issue a diagnostic.
994 if (isInvalid) {
995 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000996 // Pick between error or extwarn.
997 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
998 : diag::ext_duplicate_declspec;
999 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001000 }
1001 DS.SetRangeEnd(Tok.getLocation());
1002 ConsumeToken(); // whatever we parsed above.
1003 return true;
1004}
Reid Spencer5f016e22007-07-11 17:01:13 +00001005
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001006/// ParseStructDeclaration - Parse a struct declaration without the terminating
1007/// semicolon.
1008///
Reid Spencer5f016e22007-07-11 17:01:13 +00001009/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001010/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001011/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001012/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001013/// struct-declarator-list:
1014/// struct-declarator
1015/// struct-declarator-list ',' struct-declarator
1016/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1017/// struct-declarator:
1018/// declarator
1019/// [GNU] declarator attributes[opt]
1020/// declarator[opt] ':' constant-expression
1021/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1022///
Chris Lattnere1359422008-04-10 06:46:29 +00001023void Parser::
1024ParseStructDeclaration(DeclSpec &DS,
1025 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001026 if (Tok.is(tok::kw___extension__)) {
1027 // __extension__ silences extension warnings in the subexpression.
1028 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001029 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001030 return ParseStructDeclaration(DS, Fields);
1031 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001032
1033 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001034 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001035 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001036
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001037 // If there are no declarators, this is a free-standing declaration
1038 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001039 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001040 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001041 return;
1042 }
1043
1044 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001045 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001046 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001047 FieldDeclarator &DeclaratorInfo = Fields.back();
1048
Steve Naroff28a7ca82007-08-20 22:28:22 +00001049 /// struct-declarator: declarator
1050 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001051 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001052 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001053
Chris Lattner04d66662007-10-09 17:33:22 +00001054 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001055 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001056 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001057 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001058 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001059 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001060 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001061 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001062
Steve Naroff28a7ca82007-08-20 22:28:22 +00001063 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001064 if (Tok.is(tok::kw___attribute)) {
1065 SourceLocation Loc;
1066 AttributeList *AttrList = ParseAttributes(&Loc);
1067 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1068 }
1069
Steve Naroff28a7ca82007-08-20 22:28:22 +00001070 // If we don't have a comma, it is either the end of the list (a ';')
1071 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001072 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001073 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001074
Steve Naroff28a7ca82007-08-20 22:28:22 +00001075 // Consume the comma.
1076 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001077
Steve Naroff28a7ca82007-08-20 22:28:22 +00001078 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001079 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001080
Steve Naroff28a7ca82007-08-20 22:28:22 +00001081 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001082 if (Tok.is(tok::kw___attribute)) {
1083 SourceLocation Loc;
1084 AttributeList *AttrList = ParseAttributes(&Loc);
1085 Fields.back().D.AddAttributes(AttrList, Loc);
1086 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001087 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001088}
1089
1090/// ParseStructUnionBody
1091/// struct-contents:
1092/// struct-declaration-list
1093/// [EXT] empty
1094/// [GNU] "struct-declaration-list" without terminatoring ';'
1095/// struct-declaration-list:
1096/// struct-declaration
1097/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001098/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001099///
Reid Spencer5f016e22007-07-11 17:01:13 +00001100void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1101 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001102 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1103 PP.getSourceManager(),
1104 "parsing struct/union body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001105
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 SourceLocation LBraceLoc = ConsumeBrace();
1107
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001108 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001109 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1110
Reid Spencer5f016e22007-07-11 17:01:13 +00001111 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1112 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001113 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001114 Diag(Tok, diag::ext_empty_struct_union_enum)
1115 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001116
1117 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001118 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1119
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001121 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 // Each iteration of this loop reads one struct-declaration.
1123
1124 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001125 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001126 Diag(Tok, diag::ext_extra_struct_semi);
1127 ConsumeToken();
1128 continue;
1129 }
Chris Lattnere1359422008-04-10 06:46:29 +00001130
1131 // Parse all the comma separated declarators.
1132 DeclSpec DS;
1133 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001134 if (!Tok.is(tok::at)) {
1135 ParseStructDeclaration(DS, FieldDeclarators);
1136
1137 // Convert them all to fields.
1138 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1139 FieldDeclarator &FD = FieldDeclarators[i];
1140 // Install the declarator into the current TagDecl.
Douglas Gregor44b43212008-12-11 16:49:14 +00001141 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001142 DS.getSourceRange().getBegin(),
1143 FD.D, FD.BitfieldSize);
1144 FieldDecls.push_back(Field);
1145 }
1146 } else { // Handle @defs
1147 ConsumeToken();
1148 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1149 Diag(Tok, diag::err_unexpected_at);
1150 SkipUntil(tok::semi, true, true);
1151 continue;
1152 }
1153 ConsumeToken();
1154 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1155 if (!Tok.is(tok::identifier)) {
1156 Diag(Tok, diag::err_expected_ident);
1157 SkipUntil(tok::semi, true, true);
1158 continue;
1159 }
1160 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001161 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1162 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001163 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1164 ConsumeToken();
1165 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1166 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001167
Chris Lattner04d66662007-10-09 17:33:22 +00001168 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001170 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001171 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 break;
1173 } else {
1174 Diag(Tok, diag::err_expected_semi_decl_list);
1175 // Skip to end of block or statement
1176 SkipUntil(tok::r_brace, true, true);
1177 }
1178 }
1179
Steve Naroff60fccee2007-10-29 21:38:07 +00001180 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001181
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 AttributeList *AttrList = 0;
1183 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001184 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001185 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001186
1187 Actions.ActOnFields(CurScope,
1188 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1189 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001190 AttrList);
1191 StructScope.Exit();
1192 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001193}
1194
1195
1196/// ParseEnumSpecifier
1197/// enum-specifier: [C99 6.7.2.2]
1198/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001199///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001200/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1201/// '}' attributes[opt]
1202/// 'enum' identifier
1203/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001204///
1205/// [C++] elaborated-type-specifier:
1206/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1207///
Reid Spencer5f016e22007-07-11 17:01:13 +00001208void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001209 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 SourceLocation StartLoc = ConsumeToken();
1211
1212 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001213
1214 AttributeList *Attr = 0;
1215 // If attributes exist after tag, parse them.
1216 if (Tok.is(tok::kw___attribute))
1217 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001218
1219 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001220 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001221 if (Tok.isNot(tok::identifier)) {
1222 Diag(Tok, diag::err_expected_ident);
1223 if (Tok.isNot(tok::l_brace)) {
1224 // Has no name and is not a definition.
1225 // Skip the rest of this declarator, up until the comma or semicolon.
1226 SkipUntil(tok::comma, true);
1227 return;
1228 }
1229 }
1230 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001231
1232 // Must have either 'enum name' or 'enum {...}'.
1233 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1234 Diag(Tok, diag::err_expected_ident_lbrace);
1235
1236 // Skip the rest of this declarator, up until the comma or semicolon.
1237 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001239 }
1240
1241 // If an identifier is present, consume and remember it.
1242 IdentifierInfo *Name = 0;
1243 SourceLocation NameLoc;
1244 if (Tok.is(tok::identifier)) {
1245 Name = Tok.getIdentifierInfo();
1246 NameLoc = ConsumeToken();
1247 }
1248
1249 // There are three options here. If we have 'enum foo;', then this is a
1250 // forward declaration. If we have 'enum foo {...' then this is a
1251 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1252 //
1253 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1254 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1255 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1256 //
1257 Action::TagKind TK;
1258 if (Tok.is(tok::l_brace))
1259 TK = Action::TK_Definition;
1260 else if (Tok.is(tok::semi))
1261 TK = Action::TK_Declaration;
1262 else
1263 TK = Action::TK_Reference;
1264 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregorddc29e12009-02-06 22:42:48 +00001265 SS, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001266
Chris Lattner04d66662007-10-09 17:33:22 +00001267 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 ParseEnumBody(StartLoc, TagDecl);
1269
1270 // TODO: semantic analysis on the declspec for enums.
1271 const char *PrevSpec = 0;
1272 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001273 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001274}
1275
1276/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1277/// enumerator-list:
1278/// enumerator
1279/// enumerator-list ',' enumerator
1280/// enumerator:
1281/// enumeration-constant
1282/// enumeration-constant '=' constant-expression
1283/// enumeration-constant:
1284/// identifier
1285///
1286void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001287 // Enter the scope of the enum body and start the definition.
1288 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001289 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001290
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 SourceLocation LBraceLoc = ConsumeBrace();
1292
Chris Lattner7946dd32007-08-27 17:24:30 +00001293 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001294 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001295 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001296
1297 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1298
1299 DeclTy *LastEnumConstDecl = 0;
1300
1301 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001302 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1304 SourceLocation IdentLoc = ConsumeToken();
1305
1306 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001307 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001308 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001310 AssignedVal = ParseConstantExpression();
1311 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 }
1314
1315 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +00001316 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 LastEnumConstDecl,
1318 IdentLoc, Ident,
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001319 EqualLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001320 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001321 EnumConstantDecls.push_back(EnumConstDecl);
1322 LastEnumConstDecl = EnumConstDecl;
1323
Chris Lattner04d66662007-10-09 17:33:22 +00001324 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001325 break;
1326 SourceLocation CommaLoc = ConsumeToken();
1327
Chris Lattner04d66662007-10-09 17:33:22 +00001328 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1330 }
1331
1332 // Eat the }.
1333 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1334
Steve Naroff08d92e42007-09-15 18:49:24 +00001335 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001336 EnumConstantDecls.size());
1337
1338 DeclTy *AttrList = 0;
1339 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001340 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001342
1343 EnumScope.Exit();
1344 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001345}
1346
1347/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001348/// start of a type-qualifier-list.
1349bool Parser::isTypeQualifier() const {
1350 switch (Tok.getKind()) {
1351 default: return false;
1352 // type-qualifier
1353 case tok::kw_const:
1354 case tok::kw_volatile:
1355 case tok::kw_restrict:
1356 return true;
1357 }
1358}
1359
1360/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001361/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001362bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 switch (Tok.getKind()) {
1364 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001365
1366 case tok::identifier: // foo::bar
1367 // Annotate typenames and C++ scope specifiers. If we get one, just
1368 // recurse to handle whatever we get.
1369 if (TryAnnotateTypeOrScopeToken())
1370 return isTypeSpecifierQualifier();
1371 // Otherwise, not a type specifier.
1372 return false;
1373 case tok::coloncolon: // ::foo::bar
1374 if (NextToken().is(tok::kw_new) || // ::new
1375 NextToken().is(tok::kw_delete)) // ::delete
1376 return false;
1377
1378 // Annotate typenames and C++ scope specifiers. If we get one, just
1379 // recurse to handle whatever we get.
1380 if (TryAnnotateTypeOrScopeToken())
1381 return isTypeSpecifierQualifier();
1382 // Otherwise, not a type specifier.
1383 return false;
1384
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 // GNU attributes support.
1386 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001387 // GNU typeof support.
1388 case tok::kw_typeof:
1389
Reid Spencer5f016e22007-07-11 17:01:13 +00001390 // type-specifiers
1391 case tok::kw_short:
1392 case tok::kw_long:
1393 case tok::kw_signed:
1394 case tok::kw_unsigned:
1395 case tok::kw__Complex:
1396 case tok::kw__Imaginary:
1397 case tok::kw_void:
1398 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001399 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001400 case tok::kw_int:
1401 case tok::kw_float:
1402 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001403 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001404 case tok::kw__Bool:
1405 case tok::kw__Decimal32:
1406 case tok::kw__Decimal64:
1407 case tok::kw__Decimal128:
1408
Chris Lattner99dc9142008-04-13 18:59:07 +00001409 // struct-or-union-specifier (C99) or class-specifier (C++)
1410 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 case tok::kw_struct:
1412 case tok::kw_union:
1413 // enum-specifier
1414 case tok::kw_enum:
1415
1416 // type-qualifier
1417 case tok::kw_const:
1418 case tok::kw_volatile:
1419 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001420
1421 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001422 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001423 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001424
1425 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1426 case tok::less:
1427 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001428
1429 case tok::kw___cdecl:
1430 case tok::kw___stdcall:
1431 case tok::kw___fastcall:
1432 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001433 }
1434}
1435
1436/// isDeclarationSpecifier() - Return true if the current token is part of a
1437/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001438bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001439 switch (Tok.getKind()) {
1440 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001441
1442 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001443 // Unfortunate hack to support "Class.factoryMethod" notation.
1444 if (getLang().ObjC1 && NextToken().is(tok::period))
1445 return false;
1446
Chris Lattner166a8fc2009-01-04 23:41:41 +00001447 // Annotate typenames and C++ scope specifiers. If we get one, just
1448 // recurse to handle whatever we get.
1449 if (TryAnnotateTypeOrScopeToken())
1450 return isDeclarationSpecifier();
1451 // Otherwise, not a declaration specifier.
1452 return false;
1453 case tok::coloncolon: // ::foo::bar
1454 if (NextToken().is(tok::kw_new) || // ::new
1455 NextToken().is(tok::kw_delete)) // ::delete
1456 return false;
1457
1458 // Annotate typenames and C++ scope specifiers. If we get one, just
1459 // recurse to handle whatever we get.
1460 if (TryAnnotateTypeOrScopeToken())
1461 return isDeclarationSpecifier();
1462 // Otherwise, not a declaration specifier.
1463 return false;
1464
Reid Spencer5f016e22007-07-11 17:01:13 +00001465 // storage-class-specifier
1466 case tok::kw_typedef:
1467 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001468 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001469 case tok::kw_static:
1470 case tok::kw_auto:
1471 case tok::kw_register:
1472 case tok::kw___thread:
1473
1474 // type-specifiers
1475 case tok::kw_short:
1476 case tok::kw_long:
1477 case tok::kw_signed:
1478 case tok::kw_unsigned:
1479 case tok::kw__Complex:
1480 case tok::kw__Imaginary:
1481 case tok::kw_void:
1482 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001483 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001484 case tok::kw_int:
1485 case tok::kw_float:
1486 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001487 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 case tok::kw__Bool:
1489 case tok::kw__Decimal32:
1490 case tok::kw__Decimal64:
1491 case tok::kw__Decimal128:
1492
Chris Lattner99dc9142008-04-13 18:59:07 +00001493 // struct-or-union-specifier (C99) or class-specifier (C++)
1494 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001495 case tok::kw_struct:
1496 case tok::kw_union:
1497 // enum-specifier
1498 case tok::kw_enum:
1499
1500 // type-qualifier
1501 case tok::kw_const:
1502 case tok::kw_volatile:
1503 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001504
Reid Spencer5f016e22007-07-11 17:01:13 +00001505 // function-specifier
1506 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001507 case tok::kw_virtual:
1508 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001509
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001510 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001511 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001512
Chris Lattner1ef08762007-08-09 17:01:07 +00001513 // GNU typeof support.
1514 case tok::kw_typeof:
1515
1516 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001517 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001518 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001519
1520 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1521 case tok::less:
1522 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001523
Steve Naroff47f52092009-01-06 19:34:12 +00001524 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001525 case tok::kw___cdecl:
1526 case tok::kw___stdcall:
1527 case tok::kw___fastcall:
1528 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 }
1530}
1531
1532
1533/// ParseTypeQualifierListOpt
1534/// type-qualifier-list: [C99 6.7.5]
1535/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001536/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001537/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001538/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001539///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001540void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001541 while (1) {
1542 int isInvalid = false;
1543 const char *PrevSpec = 0;
1544 SourceLocation Loc = Tok.getLocation();
1545
1546 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001547 case tok::kw_const:
1548 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1549 getLang())*2;
1550 break;
1551 case tok::kw_volatile:
1552 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1553 getLang())*2;
1554 break;
1555 case tok::kw_restrict:
1556 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1557 getLang())*2;
1558 break;
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001559 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001560 case tok::kw___cdecl:
1561 case tok::kw___stdcall:
1562 case tok::kw___fastcall:
1563 if (!PP.getLangOptions().Microsoft)
1564 goto DoneWithTypeQuals;
1565 // Just ignore it.
1566 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001568 if (AttributesAllowed) {
1569 DS.AddAttributes(ParseAttributes());
1570 continue; // do *not* consume the next token!
1571 }
1572 // otherwise, FALL THROUGH!
1573 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001574 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001575 // If this is not a type-qualifier token, we're done reading type
1576 // qualifiers. First verify that DeclSpec's are consistent.
1577 DS.Finish(Diags, PP.getSourceManager(), getLang());
1578 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001579 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001580
Reid Spencer5f016e22007-07-11 17:01:13 +00001581 // If the specifier combination wasn't legal, issue a diagnostic.
1582 if (isInvalid) {
1583 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001584 // Pick between error or extwarn.
1585 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1586 : diag::ext_duplicate_declspec;
1587 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001588 }
1589 ConsumeToken();
1590 }
1591}
1592
1593
1594/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1595///
1596void Parser::ParseDeclarator(Declarator &D) {
1597 /// This implements the 'declarator' production in the C grammar, then checks
1598 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001599 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001600}
1601
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001602/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1603/// is parsed by the function passed to it. Pass null, and the direct-declarator
1604/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001605/// ptr-operator production.
1606///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001607/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1608/// [C] pointer[opt] direct-declarator
1609/// [C++] direct-declarator
1610/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001611///
1612/// pointer: [C99 6.7.5]
1613/// '*' type-qualifier-list[opt]
1614/// '*' type-qualifier-list[opt] pointer
1615///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001616/// ptr-operator:
1617/// '*' cv-qualifier-seq[opt]
1618/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00001619/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001620/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00001621/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001622/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001623void Parser::ParseDeclaratorInternal(Declarator &D,
1624 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001625
Sebastian Redlf30208a2009-01-24 21:16:55 +00001626 // C++ member pointers start with a '::' or a nested-name.
1627 // Member pointers get special handling, since there's no place for the
1628 // scope spec in the generic path below.
1629 if ((Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1630 Tok.is(tok::annot_cxxscope)) && getLang().CPlusPlus) {
1631 CXXScopeSpec SS;
1632 if (ParseOptionalCXXScopeSpecifier(SS)) {
1633 if(Tok.isNot(tok::star)) {
1634 // The scope spec really belongs to the direct-declarator.
1635 D.getCXXScopeSpec() = SS;
1636 if (DirectDeclParser)
1637 (this->*DirectDeclParser)(D);
1638 return;
1639 }
1640
1641 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001642 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001643 DeclSpec DS;
1644 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001645 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001646
1647 // Recurse to parse whatever is left.
1648 ParseDeclaratorInternal(D, DirectDeclParser);
1649
1650 // Sema will have to catch (syntactically invalid) pointers into global
1651 // scope. It has to catch pointers into namespace scope anyway.
1652 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001653 Loc, DS.TakeAttributes()),
1654 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00001655 return;
1656 }
1657 }
1658
1659 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001660 // Not a pointer, C++ reference, or block.
1661 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl05532f22009-03-15 22:02:01 +00001662 (Kind != tok::ampamp || !getLang().CPlusPlus0x) &&
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001663 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001664 if (DirectDeclParser)
1665 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001666 return;
1667 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001668
Sebastian Redl05532f22009-03-15 22:02:01 +00001669 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1670 // '&&' -> rvalue reference
Sebastian Redlab197ba2009-02-09 18:23:29 +00001671 SourceLocation Loc = ConsumeToken(); // Eat the *, ^ or &.
1672 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001673
Steve Naroff4ef1c992008-08-28 10:07:06 +00001674 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner76549142008-02-21 01:32:26 +00001675 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001676 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001677
Reid Spencer5f016e22007-07-11 17:01:13 +00001678 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001679 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001680
Reid Spencer5f016e22007-07-11 17:01:13 +00001681 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001682 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001683 if (Kind == tok::star)
1684 // Remember that we parsed a pointer type, and remember the type-quals.
1685 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001686 DS.TakeAttributes()),
1687 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00001688 else
1689 // Remember that we parsed a Block type, and remember the type-quals.
1690 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001691 Loc),
1692 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001693 } else {
1694 // Is a reference
1695 DeclSpec DS;
1696
1697 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1698 // cv-qualifiers are introduced through the use of a typedef or of a
1699 // template type argument, in which case the cv-qualifiers are ignored.
1700 //
1701 // [GNU] Retricted references are allowed.
1702 // [GNU] Attributes on references are allowed.
1703 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001704 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001705
1706 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1707 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1708 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001709 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1711 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001712 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001713 }
1714
1715 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001716 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001717
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001718 if (D.getNumTypeObjects() > 0) {
1719 // C++ [dcl.ref]p4: There shall be no references to references.
1720 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1721 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001722 if (const IdentifierInfo *II = D.getIdentifier())
1723 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1724 << II;
1725 else
1726 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1727 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001728
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001729 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001730 // can go ahead and build the (technically ill-formed)
1731 // declarator: reference collapsing will take care of it.
1732 }
1733 }
1734
Reid Spencer5f016e22007-07-11 17:01:13 +00001735 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001736 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00001737 DS.TakeAttributes(),
1738 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001739 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001740 }
1741}
1742
1743/// ParseDirectDeclarator
1744/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001745/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001746/// '(' declarator ')'
1747/// [GNU] '(' attributes declarator ')'
1748/// [C90] direct-declarator '[' constant-expression[opt] ']'
1749/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1750/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1751/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1752/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1753/// direct-declarator '(' parameter-type-list ')'
1754/// direct-declarator '(' identifier-list[opt] ')'
1755/// [GNU] direct-declarator '(' parameter-forward-declarations
1756/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001757/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1758/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001759/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001760///
1761/// declarator-id: [C++ 8]
1762/// id-expression
1763/// '::'[opt] nested-name-specifier[opt] type-name
1764///
1765/// id-expression: [C++ 5.1]
1766/// unqualified-id
1767/// qualified-id [TODO]
1768///
1769/// unqualified-id: [C++ 5.1]
1770/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001771/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001772/// conversion-function-id [TODO]
1773/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00001774/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001775///
Reid Spencer5f016e22007-07-11 17:01:13 +00001776void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001777 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001778
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001779 if (getLang().CPlusPlus) {
1780 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001781 // ParseDeclaratorInternal might already have parsed the scope.
1782 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1783 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001784 if (afterCXXScope) {
1785 // Change the declaration context for name lookup, until this function
1786 // is exited (and the declarator has been parsed).
1787 DeclScopeObj.EnterDeclaratorScope();
1788 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001789
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001790 if (Tok.is(tok::identifier)) {
1791 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001792
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001793 // If this identifier is the name of the current class, it's a
1794 // constructor name.
Douglas Gregor39a8de12009-02-25 19:37:18 +00001795 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroffb43a50f2009-01-28 19:39:02 +00001796 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +00001797 Tok.getLocation(), CurScope),
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001798 Tok.getLocation());
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001799 // This is a normal identifier.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001800 } else
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001801 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1802 ConsumeToken();
1803 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001804 } else if (Tok.is(tok::annot_template_id)) {
1805 TemplateIdAnnotation *TemplateId
1806 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1807
1808 // FIXME: Could this template-id name a constructor?
1809
1810 // FIXME: This is an egregious hack, where we silently ignore
1811 // the specialization (which should be a function template
1812 // specialization name) and use the name instead. This hack
1813 // will go away when we have support for function
1814 // specializations.
1815 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1816 TemplateId->Destroy();
1817 ConsumeToken();
1818 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00001819 } else if (Tok.is(tok::kw_operator)) {
1820 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001821 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001822
Douglas Gregor70316a02008-12-26 15:00:45 +00001823 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00001824 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1825 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00001826 } else {
1827 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00001828 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1829 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1830 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00001831 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001832 }
Douglas Gregor70316a02008-12-26 15:00:45 +00001833 }
1834 goto PastIdentifier;
1835 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001836 // This should be a C++ destructor.
1837 SourceLocation TildeLoc = ConsumeToken();
1838 if (Tok.is(tok::identifier)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001839 // FIXME: Inaccurate.
1840 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00001841 SourceLocation EndLoc;
1842 if (TypeTy *Type = ParseClassName(EndLoc)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001843 D.setDestructor(Type, TildeLoc, NameLoc);
1844 } else {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001845 D.SetIdentifier(0, TildeLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001846 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001847 } else {
1848 Diag(Tok, diag::err_expected_class_name);
1849 D.SetIdentifier(0, TildeLoc);
1850 }
1851 goto PastIdentifier;
1852 }
1853
1854 // If we reached this point, token is not identifier and not '~'.
1855
1856 if (afterCXXScope) {
1857 Diag(Tok, diag::err_expected_unqualified_id);
1858 D.SetIdentifier(0, Tok.getLocation());
1859 D.setInvalidType(true);
1860 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001861 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001862 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001863 }
1864
1865 // If we reached this point, we are either in C/ObjC or the token didn't
1866 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001867 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1868 assert(!getLang().CPlusPlus &&
1869 "There's a C++-specific check for tok::identifier above");
1870 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1871 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1872 ConsumeToken();
1873 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 // direct-declarator: '(' declarator ')'
1875 // direct-declarator: '(' attributes declarator ')'
1876 // Example: 'char (*X)' or 'int (*XX)(void)'
1877 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001878 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001879 // This could be something simple like "int" (in which case the declarator
1880 // portion is empty), if an abstract-declarator is allowed.
1881 D.SetIdentifier(0, Tok.getLocation());
1882 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00001883 if (D.getContext() == Declarator::MemberContext)
1884 Diag(Tok, diag::err_expected_member_name_or_semi)
1885 << D.getDeclSpec().getSourceRange();
1886 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001887 Diag(Tok, diag::err_expected_unqualified_id);
1888 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00001889 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00001890 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00001891 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001892 }
1893
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001894 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00001895 assert(D.isPastIdentifier() &&
1896 "Haven't past the location of the identifier yet?");
1897
1898 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001899 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001900 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1901 // In such a case, check if we actually have a function declarator; if it
1902 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00001903 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1904 // When not in file scope, warn for ambiguous function declarators, just
1905 // in case the author intended it as a variable definition.
1906 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1907 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1908 break;
1909 }
Chris Lattneref4715c2008-04-06 05:45:57 +00001910 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001911 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001912 ParseBracketDeclarator(D);
1913 } else {
1914 break;
1915 }
1916 }
1917}
1918
Chris Lattneref4715c2008-04-06 05:45:57 +00001919/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1920/// only called before the identifier, so these are most likely just grouping
1921/// parens for precedence. If we find that these are actually function
1922/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1923///
1924/// direct-declarator:
1925/// '(' declarator ')'
1926/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00001927/// direct-declarator '(' parameter-type-list ')'
1928/// direct-declarator '(' identifier-list[opt] ')'
1929/// [GNU] direct-declarator '(' parameter-forward-declarations
1930/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00001931///
1932void Parser::ParseParenDeclarator(Declarator &D) {
1933 SourceLocation StartLoc = ConsumeParen();
1934 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1935
Chris Lattner7399ee02008-10-20 02:05:46 +00001936 // Eat any attributes before we look at whether this is a grouping or function
1937 // declarator paren. If this is a grouping paren, the attribute applies to
1938 // the type being built up, for example:
1939 // int (__attribute__(()) *x)(long y)
1940 // If this ends up not being a grouping paren, the attribute applies to the
1941 // first argument, for example:
1942 // int (__attribute__(()) int x)
1943 // In either case, we need to eat any attributes to be able to determine what
1944 // sort of paren this is.
1945 //
1946 AttributeList *AttrList = 0;
1947 bool RequiresArg = false;
1948 if (Tok.is(tok::kw___attribute)) {
1949 AttrList = ParseAttributes();
1950
1951 // We require that the argument list (if this is a non-grouping paren) be
1952 // present even if the attribute list was empty.
1953 RequiresArg = true;
1954 }
Steve Naroff239f0732008-12-25 14:16:32 +00001955 // Eat any Microsoft extensions.
Douglas Gregor5a2f5d32009-01-10 00:48:18 +00001956 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1957 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff239f0732008-12-25 14:16:32 +00001958 ConsumeToken();
Chris Lattner7399ee02008-10-20 02:05:46 +00001959
Chris Lattneref4715c2008-04-06 05:45:57 +00001960 // If we haven't past the identifier yet (or where the identifier would be
1961 // stored, if this is an abstract declarator), then this is probably just
1962 // grouping parens. However, if this could be an abstract-declarator, then
1963 // this could also be the start of function arguments (consider 'void()').
1964 bool isGrouping;
1965
1966 if (!D.mayOmitIdentifier()) {
1967 // If this can't be an abstract-declarator, this *must* be a grouping
1968 // paren, because we haven't seen the identifier yet.
1969 isGrouping = true;
1970 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001971 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00001972 isDeclarationSpecifier()) { // 'int(int)' is a function.
1973 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1974 // considered to be a type, not a K&R identifier-list.
1975 isGrouping = false;
1976 } else {
1977 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1978 isGrouping = true;
1979 }
1980
1981 // If this is a grouping paren, handle:
1982 // direct-declarator: '(' declarator ')'
1983 // direct-declarator: '(' attributes declarator ')'
1984 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001985 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001986 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00001987 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00001988 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001989
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001990 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00001991 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001992 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001993
1994 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001995 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00001996 return;
1997 }
1998
1999 // Okay, if this wasn't a grouping paren, it must be the start of a function
2000 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002001 // identifier (and remember where it would have been), then call into
2002 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002003 D.SetIdentifier(0, Tok.getLocation());
2004
Chris Lattner7399ee02008-10-20 02:05:46 +00002005 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002006}
2007
2008/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2009/// declarator D up to a paren, which indicates that we are parsing function
2010/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002011///
Chris Lattner7399ee02008-10-20 02:05:46 +00002012/// If AttrList is non-null, then the caller parsed those arguments immediately
2013/// after the open paren - they should be considered to be the first argument of
2014/// a parameter. If RequiresArg is true, then the first argument of the
2015/// function is required to be present and required to not be an identifier
2016/// list.
2017///
Reid Spencer5f016e22007-07-11 17:01:13 +00002018/// This method also handles this portion of the grammar:
2019/// parameter-type-list: [C99 6.7.5]
2020/// parameter-list
2021/// parameter-list ',' '...'
2022///
2023/// parameter-list: [C99 6.7.5]
2024/// parameter-declaration
2025/// parameter-list ',' parameter-declaration
2026///
2027/// parameter-declaration: [C99 6.7.5]
2028/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002029/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002030/// [GNU] declaration-specifiers declarator attributes
2031/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002032/// [C++] declaration-specifiers abstract-declarator[opt]
2033/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002034/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2035///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002036/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
2037/// and "exception-specification[opt]"(TODO).
2038///
Chris Lattner7399ee02008-10-20 02:05:46 +00002039void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2040 AttributeList *AttrList,
2041 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002042 // lparen is already consumed!
2043 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00002044
Chris Lattner7399ee02008-10-20 02:05:46 +00002045 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002046 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002047 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002048 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002049 delete AttrList;
2050 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002051
Sebastian Redlab197ba2009-02-09 18:23:29 +00002052 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002053
2054 // cv-qualifier-seq[opt].
2055 DeclSpec DS;
2056 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002057 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002058 if (!DS.getSourceRange().getEnd().isInvalid())
2059 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002060
2061 // Parse exception-specification[opt].
2062 if (Tok.is(tok::kw_throw))
Sebastian Redlab197ba2009-02-09 18:23:29 +00002063 ParseExceptionSpecification(Loc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002064 }
2065
Chris Lattnerf97409f2008-04-06 06:57:35 +00002066 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002067 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002068 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002069 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002070 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002071 /*arglist*/ 0, 0,
2072 DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002073 LParenLoc, D),
2074 Loc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002075 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00002076 }
2077
2078 // Alternatively, this parameter list may be an identifier list form for a
2079 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002080 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002081 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002082 // K&R identifier lists can't have typedefs as identifiers, per
2083 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002084 if (RequiresArg) {
2085 Diag(Tok, diag::err_argument_required_after_attribute);
2086 delete AttrList;
2087 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002088 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2089 // normal declarators, not for abstract-declarators.
2090 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002091 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002092 }
2093
2094 // Finally, a normal, non-empty parameter type list.
2095
2096 // Build up an array of information about the parsed arguments.
2097 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002098
2099 // Enter function-declaration scope, limiting any declarators to the
2100 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002101 ParseScope PrototypeScope(this,
2102 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002103
2104 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002105 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002106 while (1) {
2107 if (Tok.is(tok::ellipsis)) {
2108 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002109 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002110 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002111 }
2112
Chris Lattnerf97409f2008-04-06 06:57:35 +00002113 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002114
Chris Lattnerf97409f2008-04-06 06:57:35 +00002115 // Parse the declaration-specifiers.
2116 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002117
2118 // If the caller parsed attributes for the first argument, add them now.
2119 if (AttrList) {
2120 DS.AddAttributes(AttrList);
2121 AttrList = 0; // Only apply the attributes to the first parameter.
2122 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002123 ParseDeclarationSpecifiers(DS);
2124
Chris Lattnerf97409f2008-04-06 06:57:35 +00002125 // Parse the declarator. This is "PrototypeContext", because we must
2126 // accept either 'declarator' or 'abstract-declarator' here.
2127 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2128 ParseDeclarator(ParmDecl);
2129
2130 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002131 if (Tok.is(tok::kw___attribute)) {
2132 SourceLocation Loc;
2133 AttributeList *AttrList = ParseAttributes(&Loc);
2134 ParmDecl.AddAttributes(AttrList, Loc);
2135 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002136
Chris Lattnerf97409f2008-04-06 06:57:35 +00002137 // Remember this parsed parameter in ParamInfo.
2138 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2139
Douglas Gregor72b505b2008-12-16 21:30:33 +00002140 // DefArgToks is used when the parsing of default arguments needs
2141 // to be delayed.
2142 CachedTokens *DefArgToks = 0;
2143
Chris Lattnerf97409f2008-04-06 06:57:35 +00002144 // If no parameter was specified, verify that *something* was specified,
2145 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002146 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2147 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002148 // Completely missing, emit error.
2149 Diag(DSStart, diag::err_missing_param);
2150 } else {
2151 // Otherwise, we have something. Add it and let semantic analysis try
2152 // to grok it and add the result to the ParamInfo we are building.
2153
2154 // Inform the actions module about the parameter declarator, so it gets
2155 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00002156 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2157
2158 // Parse the default argument, if any. We parse the default
2159 // arguments in all dialects; the semantic analysis in
2160 // ActOnParamDefaultArgument will reject the default argument in
2161 // C.
2162 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002163 SourceLocation EqualLoc = Tok.getLocation();
2164
Chris Lattner04421082008-04-08 04:40:51 +00002165 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002166 if (D.getContext() == Declarator::MemberContext) {
2167 // If we're inside a class definition, cache the tokens
2168 // corresponding to the default argument. We'll actually parse
2169 // them when we see the end of the class definition.
2170 // FIXME: Templates will require something similar.
2171 // FIXME: Can we use a smart pointer for Toks?
2172 DefArgToks = new CachedTokens;
2173
2174 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2175 tok::semi, false)) {
2176 delete DefArgToks;
2177 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002178 Actions.ActOnParamDefaultArgumentError(Param);
2179 } else
2180 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner04421082008-04-08 04:40:51 +00002181 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002182 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002183 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002184
2185 OwningExprResult DefArgResult(ParseAssignmentExpression());
2186 if (DefArgResult.isInvalid()) {
2187 Actions.ActOnParamDefaultArgumentError(Param);
2188 SkipUntil(tok::comma, tok::r_paren, true, true);
2189 } else {
2190 // Inform the actions module about the default argument
2191 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002192 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002193 }
Chris Lattner04421082008-04-08 04:40:51 +00002194 }
2195 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002196
2197 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002198 ParmDecl.getIdentifierLoc(), Param,
2199 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002200 }
2201
2202 // If the next token is a comma, consume it and keep reading arguments.
2203 if (Tok.isNot(tok::comma)) break;
2204
2205 // Consume the comma.
2206 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002207 }
2208
Chris Lattnerf97409f2008-04-06 06:57:35 +00002209 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002210 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002211
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002212 // If we have the closing ')', eat it.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002213 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002214
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002215 DeclSpec DS;
2216 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002217 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002218 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002219 if (!DS.getSourceRange().getEnd().isInvalid())
2220 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002221
2222 // Parse exception-specification[opt].
2223 if (Tok.is(tok::kw_throw))
Sebastian Redlab197ba2009-02-09 18:23:29 +00002224 ParseExceptionSpecification(Loc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002225 }
2226
Reid Spencer5f016e22007-07-11 17:01:13 +00002227 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002228 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002229 EllipsisLoc,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002230 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002231 DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002232 LParenLoc, D),
2233 Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002234}
2235
Chris Lattner66d28652008-04-06 06:34:08 +00002236/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2237/// we found a K&R-style identifier list instead of a type argument list. The
2238/// current token is known to be the first identifier in the list.
2239///
2240/// identifier-list: [C99 6.7.5]
2241/// identifier
2242/// identifier-list ',' identifier
2243///
2244void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2245 Declarator &D) {
2246 // Build up an array of information about the parsed arguments.
2247 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2248 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2249
2250 // If there was no identifier specified for the declarator, either we are in
2251 // an abstract-declarator, or we are in a parameter declarator which was found
2252 // to be abstract. In abstract-declarators, identifier lists are not valid:
2253 // diagnose this.
2254 if (!D.getIdentifier())
2255 Diag(Tok, diag::ext_ident_list_in_param);
2256
2257 // Tok is known to be the first identifier in the list. Remember this
2258 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002259 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002260 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2261 Tok.getLocation(), 0));
2262
Chris Lattner50c64772008-04-06 06:39:19 +00002263 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002264
2265 while (Tok.is(tok::comma)) {
2266 // Eat the comma.
2267 ConsumeToken();
2268
Chris Lattner50c64772008-04-06 06:39:19 +00002269 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002270 if (Tok.isNot(tok::identifier)) {
2271 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002272 SkipUntil(tok::r_paren);
2273 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002274 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002275
Chris Lattner66d28652008-04-06 06:34:08 +00002276 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002277
2278 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002279 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002280 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002281
2282 // Verify that the argument identifier has not already been mentioned.
2283 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002284 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002285 } else {
2286 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002287 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2288 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00002289 }
Chris Lattner66d28652008-04-06 06:34:08 +00002290
2291 // Eat the identifier.
2292 ConsumeToken();
2293 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002294
2295 // If we have the closing ')', eat it and we're done.
2296 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2297
Chris Lattner50c64772008-04-06 06:39:19 +00002298 // Remember that we parsed a function type, and remember the attributes. This
2299 // function type is always a K&R style function type, which is not varargs and
2300 // has no prototype.
2301 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002302 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002303 &ParamInfo[0], ParamInfo.size(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002304 /*TypeQuals*/0, LParenLoc, D),
2305 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002306}
Chris Lattneref4715c2008-04-06 05:45:57 +00002307
Reid Spencer5f016e22007-07-11 17:01:13 +00002308/// [C90] direct-declarator '[' constant-expression[opt] ']'
2309/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2310/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2311/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2312/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2313void Parser::ParseBracketDeclarator(Declarator &D) {
2314 SourceLocation StartLoc = ConsumeBracket();
2315
Chris Lattner378c7e42008-12-18 07:27:21 +00002316 // C array syntax has many features, but by-far the most common is [] and [4].
2317 // This code does a fast path to handle some of the most obvious cases.
2318 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002319 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002320 // Remember that we parsed the empty array type.
2321 OwningExprResult NumElements(Actions);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002322 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2323 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002324 return;
2325 } else if (Tok.getKind() == tok::numeric_constant &&
2326 GetLookAheadToken(1).is(tok::r_square)) {
2327 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002328 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002329 ConsumeToken();
2330
Sebastian Redlab197ba2009-02-09 18:23:29 +00002331 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002332
2333 // If there was an error parsing the assignment-expression, recover.
2334 if (ExprRes.isInvalid())
2335 ExprRes.release(); // Deallocate expr, just use [].
2336
2337 // Remember that we parsed a array type, and remember its features.
2338 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002339 ExprRes.release(), StartLoc),
2340 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002341 return;
2342 }
2343
Reid Spencer5f016e22007-07-11 17:01:13 +00002344 // If valid, this location is the position where we read the 'static' keyword.
2345 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002346 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002347 StaticLoc = ConsumeToken();
2348
2349 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002350 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002351 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002352 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002353
2354 // If we haven't already read 'static', check to see if there is one after the
2355 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002356 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002357 StaticLoc = ConsumeToken();
2358
2359 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2360 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002361 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002362
2363 // Handle the case where we have '[*]' as the array size. However, a leading
2364 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2365 // the the token after the star is a ']'. Since stars in arrays are
2366 // infrequent, use of lookahead is not costly here.
2367 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002368 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002369
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002370 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002371 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002372 StaticLoc = SourceLocation(); // Drop the static.
2373 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002374 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002375 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002376 // Note, in C89, this production uses the constant-expr production instead
2377 // of assignment-expr. The only difference is that assignment-expr allows
2378 // things like '=' and '*='. Sema rejects these in C89 mode because they
2379 // are not i-c-e's, so we don't need to distinguish between the two here.
2380
Reid Spencer5f016e22007-07-11 17:01:13 +00002381 // Parse the assignment-expression now.
2382 NumElements = ParseAssignmentExpression();
2383 }
2384
2385 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002386 if (NumElements.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002387 // If the expression was invalid, skip it.
2388 SkipUntil(tok::r_square);
2389 return;
2390 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002391
2392 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2393
Chris Lattner378c7e42008-12-18 07:27:21 +00002394 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002395 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2396 StaticLoc.isValid(), isStar,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002397 NumElements.release(), StartLoc),
2398 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002399}
2400
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002401/// [GNU] typeof-specifier:
2402/// typeof ( expressions )
2403/// typeof ( type-name )
2404/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002405///
2406void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002407 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002408 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002409 SourceLocation StartLoc = ConsumeToken();
2410
Chris Lattner04d66662007-10-09 17:33:22 +00002411 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002412 if (!getLang().CPlusPlus) {
Chris Lattner08631c52008-11-23 21:45:46 +00002413 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002414 return;
2415 }
2416
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002417 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor809070a2009-02-18 17:45:20 +00002418 if (Result.isInvalid()) {
2419 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002420 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002421 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002422
2423 const char *PrevSpec = 0;
2424 // Check for duplicate type specifiers.
2425 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002426 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002427 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002428
2429 // FIXME: Not accurate, the range gets one token more than it should.
2430 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002431 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002432 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002433
Steve Naroffd1861fd2007-07-31 12:34:36 +00002434 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2435
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00002436 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00002437 Action::TypeResult Ty = ParseTypeName();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002438
Douglas Gregor809070a2009-02-18 17:45:20 +00002439 assert((Ty.isInvalid() || Ty.get()) &&
2440 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002441
Chris Lattner04d66662007-10-09 17:33:22 +00002442 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002443 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002444 return;
2445 }
2446 RParenLoc = ConsumeParen();
Douglas Gregor809070a2009-02-18 17:45:20 +00002447
2448 if (Ty.isInvalid())
2449 DS.SetTypeSpecError();
2450 else {
2451 const char *PrevSpec = 0;
2452 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2453 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2454 Ty.get()))
2455 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2456 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00002457 } else { // we have an expression.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002458 OwningExprResult Result(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002459
2460 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002461 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor809070a2009-02-18 17:45:20 +00002462 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002463 return;
2464 }
2465 RParenLoc = ConsumeParen();
2466 const char *PrevSpec = 0;
2467 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2468 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002469 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002470 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002471 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00002472 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002473}
2474
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002475