blob: a6e7d52fc1372d99b6d14ef77ea0bae4a09daaad [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl19fec9d2008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Douglas Gregor6c0f4062009-02-18 17:45:20 +000031Action::TypeResult Parser::ParseTypeName() {
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregor6c0f4062009-02-18 17:45:20 +000040 if (DeclaratorInfo.getInvalidType())
41 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl0c986032009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000084
85 AttributeList *CurrAttr = 0;
86
Chris Lattner34a01ad2007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000101
Chris Lattner34a01ad2007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 ConsumeParen(); // ignore the left paren loc for now
114
Chris Lattner34a01ad2007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner34a01ad2007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000139 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl6008ac32008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000171 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +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))
Chris Lattner4b009652007-07-25 00:24:17 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redl0c986032009-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000198 }
199 return CurrAttr;
200}
201
Steve Naroffc5ab14f2008-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
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerf7b2e552007-08-25 06:57:03 +0000219///
220/// declaration: [C99 6.7]
221/// block-declaration ->
222/// simple-declaration
223/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000224/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000225/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000226/// [C++] using-directive
227/// [C++] using-declaration [TODO]
Sebastian Redla8cecf62009-03-24 22:27:57 +0000228/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000229/// others... [FIXME]
230///
Chris Lattner4b009652007-07-25 00:24:17 +0000231Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000232 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000233 case tok::kw_export:
234 case tok::kw_template:
Douglas Gregora08b6c72009-02-17 23:15:12 +0000235 return ParseTemplateDeclarationOrSpecialization(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000236 case tok::kw_namespace:
237 return ParseNamespace(Context);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000238 case tok::kw_using:
239 return ParseUsingDirectiveOrDeclaration(Context);
Anders Carlssonab041982009-03-11 16:27:10 +0000240 case tok::kw_static_assert:
241 return ParseStaticAssertDeclaration();
Chris Lattnerf7b2e552007-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) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +0000258 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerf7b2e552007-08-25 06:57:03 +0000269
Chris Lattner4b009652007-07-25 00:24:17 +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///
Chris Lattner4b009652007-07-25 00:24:17 +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
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000283/// [C++] declarator initializer[opt]
284///
285/// [C++] initializer:
286/// [C++] '=' initializer-clause
287/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000288/// [C++0x] '=' 'default' [TODO]
289/// [C++0x] '=' 'delete'
290///
291/// According to the standard grammar, =default and =delete are function
292/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000293///
294Parser::DeclTy *Parser::
295ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
296
297 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
298 // that they can be chained properly if the actions want this.
299 Parser::DeclTy *LastDeclInGroup = 0;
300
301 // At this point, we know that it is not a function definition. Parse the
302 // rest of the init-declarator-list.
303 while (1) {
304 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000305 if (Tok.is(tok::kw_asm)) {
Sebastian Redl0c986032009-02-09 18:23:29 +0000306 SourceLocation Loc;
307 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000308 if (AsmLabel.isInvalid()) {
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000309 SkipUntil(tok::semi);
310 return 0;
311 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000312
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000313 D.setAsmLabel(AsmLabel.release());
Sebastian Redl0c986032009-02-09 18:23:29 +0000314 D.SetRangeEnd(Loc);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000315 }
Chris Lattner4b009652007-07-25 00:24:17 +0000316
317 // If attributes are present, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000318 if (Tok.is(tok::kw___attribute)) {
319 SourceLocation Loc;
320 AttributeList *AttrList = ParseAttributes(&Loc);
321 D.AddAttributes(AttrList, Loc);
322 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000323
324 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000325 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000326
Chris Lattner4b009652007-07-25 00:24:17 +0000327 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000328 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000329 ConsumeToken();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000330 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
331 SourceLocation DelLoc = ConsumeToken();
332 Actions.SetDeclDeleted(LastDeclInGroup, DelLoc);
333 } else {
334 OwningExprResult Init(ParseInitializer());
335 if (Init.isInvalid()) {
336 SkipUntil(tok::semi);
337 return 0;
338 }
339 Actions.AddInitializerToDecl(LastDeclInGroup, move(Init));
Chris Lattner4b009652007-07-25 00:24:17 +0000340 }
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000341 } else if (Tok.is(tok::l_paren)) {
342 // Parse C++ direct initializer: '(' expression-list ')'
343 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000344 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000345 CommaLocsTy CommaLocs;
346
347 bool InvalidExpr = false;
348 if (ParseExpressionList(Exprs, CommaLocs)) {
349 SkipUntil(tok::r_paren);
350 InvalidExpr = true;
351 }
352 // Match the ')'.
353 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
354
355 if (!InvalidExpr) {
356 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
357 "Unexpected number of commas!");
358 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000359 move_arg(Exprs),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000360 &CommaLocs[0], RParenLoc);
361 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000362 } else {
363 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Chris Lattner4b009652007-07-25 00:24:17 +0000364 }
365
Chris Lattner4b009652007-07-25 00:24:17 +0000366 // If we don't have a comma, it is either the end of the list (a ';') or an
367 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000368 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000369 break;
370
371 // Consume the comma.
372 ConsumeToken();
373
374 // Parse the next declarator.
375 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000376
377 // Accept attributes in an init-declarator. In the first declarator in a
378 // declaration, these would be part of the declspec. In subsequent
379 // declarators, they become part of the declarator itself, so that they
380 // don't apply to declarators after *this* one. Examples:
381 // short __attribute__((common)) var; -> declspec
382 // short var __attribute__((common)); -> declarator
383 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000384 if (Tok.is(tok::kw___attribute)) {
385 SourceLocation Loc;
386 AttributeList *AttrList = ParseAttributes(&Loc);
387 D.AddAttributes(AttrList, Loc);
388 }
Chris Lattner926cf542008-10-20 04:57:38 +0000389
Chris Lattner4b009652007-07-25 00:24:17 +0000390 ParseDeclarator(D);
391 }
392
Chris Lattner34a01ad2007-10-09 17:33:22 +0000393 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000394 ConsumeToken();
Fariborz Jahanianc1509b02009-01-17 00:00:40 +0000395 // for(is key; in keys) is error.
396 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
397 Diag(Tok, diag::err_parse_error);
398 return 0;
399 }
Chris Lattner4b009652007-07-25 00:24:17 +0000400 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
401 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000402 // If this is an ObjC2 for-each loop, this is a successful declarator
403 // parse. The syntax for these looks like:
404 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000405 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000406 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
407 }
Chris Lattner4b009652007-07-25 00:24:17 +0000408 Diag(Tok, diag::err_parse_error);
409 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000410 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000411 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000412 ConsumeToken();
413 return 0;
414}
415
416/// ParseSpecifierQualifierList
417/// specifier-qualifier-list:
418/// type-specifier specifier-qualifier-list[opt]
419/// type-qualifier specifier-qualifier-list[opt]
420/// [GNU] attributes specifier-qualifier-list[opt]
421///
422void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
423 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
424 /// parse declaration-specifiers and complain about extra stuff.
425 ParseDeclarationSpecifiers(DS);
426
427 // Validate declspec for type-name.
428 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000429 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000430 Diag(Tok, diag::err_typename_requires_specqual);
431
432 // Issue diagnostic and remove storage class if present.
433 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
434 if (DS.getStorageClassSpecLoc().isValid())
435 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
436 else
437 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
438 DS.ClearStorageClassSpecs();
439 }
440
441 // Issue diagnostic and remove function specfier if present.
442 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000443 if (DS.isInlineSpecified())
444 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
445 if (DS.isVirtualSpecified())
446 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
447 if (DS.isExplicitSpecified())
448 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000449 DS.ClearFunctionSpecs();
450 }
451}
452
453/// ParseDeclarationSpecifiers
454/// declaration-specifiers: [C99 6.7]
455/// storage-class-specifier declaration-specifiers[opt]
456/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000457/// [C99] function-specifier declaration-specifiers[opt]
458/// [GNU] attributes declaration-specifiers[opt]
459///
460/// storage-class-specifier: [C99 6.7.1]
461/// 'typedef'
462/// 'extern'
463/// 'static'
464/// 'auto'
465/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000466/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000467/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000468/// function-specifier: [C99 6.7.4]
469/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000470/// [C++] 'virtual'
471/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000472///
Douglas Gregor52473432008-12-24 02:52:09 +0000473void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Chris Lattner712f9a32009-01-05 00:07:25 +0000474 TemplateParameterLists *TemplateParams){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000475 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000476 while (1) {
477 int isInvalid = false;
478 const char *PrevSpec = 0;
479 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000480
Chris Lattner4b009652007-07-25 00:24:17 +0000481 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000482 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000483 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000484 // If this is not a declaration specifier token, we're done reading decl
485 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000486 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000487 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000488
489 case tok::coloncolon: // ::foo::bar
490 // Annotate C++ scope specifiers. If we get one, loop.
491 if (TryAnnotateCXXScopeToken())
492 continue;
493 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000494
495 case tok::annot_cxxscope: {
496 if (DS.hasTypeSpecifier())
497 goto DoneWithDeclSpec;
498
499 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000500 Token Next = NextToken();
501 if (Next.is(tok::annot_template_id) &&
502 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
503 ->Kind == TNK_Class_template) {
504 // We have a qualified template-id, e.g., N::A<int>
505 CXXScopeSpec SS;
506 ParseOptionalCXXScopeSpecifier(SS);
507 assert(Tok.is(tok::annot_template_id) &&
508 "ParseOptionalCXXScopeSpecifier not working");
509 AnnotateTemplateIdTokenAsType(&SS);
510 continue;
511 }
512
513 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000514 goto DoneWithDeclSpec;
515
516 CXXScopeSpec SS;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000517 SS.setFromAnnotationData(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000518 SS.setRange(Tok.getAnnotationRange());
519
520 // If the next token is the name of the class type that the C++ scope
521 // denotes, followed by a '(', then this is a constructor declaration.
522 // We're done with the decl-specifiers.
523 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
524 CurScope, &SS) &&
525 GetLookAheadToken(2).is(tok::l_paren))
526 goto DoneWithDeclSpec;
527
Douglas Gregor1075a162009-02-04 17:00:24 +0000528 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
529 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000530
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000531 if (TypeRep == 0)
532 goto DoneWithDeclSpec;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000533
534 CXXScopeSpec::freeAnnotationData(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000535 ConsumeToken(); // The C++ scope.
536
Douglas Gregora60c62e2009-02-09 15:09:02 +0000537 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000538 TypeRep);
539 if (isInvalid)
540 break;
541
542 DS.SetRangeEnd(Tok.getLocation());
543 ConsumeToken(); // The typename.
544
545 continue;
546 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000547
548 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerc297b722009-01-21 19:48:37 +0000550 Tok.getAnnotationValue());
551 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
552 ConsumeToken(); // The typename
553
554 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
555 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
556 // Objective-C interface. If we don't have Objective-C or a '<', this is
557 // just a normal reference to a typedef name.
558 if (!Tok.is(tok::less) || !getLang().ObjC1)
559 continue;
560
561 SourceLocation EndProtoLoc;
562 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
563 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
564 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
565
566 DS.SetRangeEnd(EndProtoLoc);
567 continue;
568 }
569
Chris Lattnerfda18db2008-07-26 01:18:38 +0000570 // typedef-name
571 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000572 // In C++, check to see if this is a scope specifier like foo::bar::, if
573 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000574 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
575 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000576
Chris Lattnerfda18db2008-07-26 01:18:38 +0000577 // This identifier can only be a typedef name if we haven't already seen
578 // a type-specifier. Without this check we misparse:
579 // typedef int X; struct Y { short X; }; as 'short int'.
580 if (DS.hasTypeSpecifier())
581 goto DoneWithDeclSpec;
582
583 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000584 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
585 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000586
Chris Lattnerfda18db2008-07-26 01:18:38 +0000587 if (TypeRep == 0)
588 goto DoneWithDeclSpec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000589
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000590 // C++: If the identifier is actually the name of the class type
591 // being defined and the next token is a '(', then this is a
592 // constructor declaration. We're done with the decl-specifiers
593 // and will treat this token as an identifier.
594 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000595 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000596 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
597 NextToken().getKind() == tok::l_paren)
598 goto DoneWithDeclSpec;
599
Douglas Gregora60c62e2009-02-09 15:09:02 +0000600 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000601 TypeRep);
602 if (isInvalid)
603 break;
604
605 DS.SetRangeEnd(Tok.getLocation());
606 ConsumeToken(); // The identifier
607
608 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
609 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
610 // Objective-C interface. If we don't have Objective-C or a '<', this is
611 // just a normal reference to a typedef name.
612 if (!Tok.is(tok::less) || !getLang().ObjC1)
613 continue;
614
615 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000616 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000617 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000618 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000619
620 DS.SetRangeEnd(EndProtoLoc);
621
Steve Narofff7683302008-09-22 10:28:57 +0000622 // Need to support trailing type qualifiers (e.g. "id<p> const").
623 // If a type specifier follows, it will be diagnosed elsewhere.
624 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000625 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000626
627 // type-name
628 case tok::annot_template_id: {
629 TemplateIdAnnotation *TemplateId
630 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
631 if (TemplateId->Kind != TNK_Class_template) {
632 // This template-id does not refer to a type name, so we're
633 // done with the type-specifiers.
634 goto DoneWithDeclSpec;
635 }
636
637 // Turn the template-id annotation token into a type annotation
638 // token, then try again to parse it as a type-specifier.
639 if (AnnotateTemplateIdTokenAsType())
640 DS.SetTypeSpecError();
641
642 continue;
643 }
644
Chris Lattner4b009652007-07-25 00:24:17 +0000645 // GNU attributes support.
646 case tok::kw___attribute:
647 DS.AddAttributes(ParseAttributes());
648 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000649
650 // Microsoft declspec support.
651 case tok::kw___declspec:
652 if (!PP.getLangOptions().Microsoft)
653 goto DoneWithDeclSpec;
654 FuzzyParseMicrosoftDeclSpec();
655 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000656
Steve Naroffedd04d52008-12-25 14:16:32 +0000657 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000658 case tok::kw___forceinline:
659 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000660 case tok::kw___cdecl:
661 case tok::kw___stdcall:
662 case tok::kw___fastcall:
663 if (!PP.getLangOptions().Microsoft)
664 goto DoneWithDeclSpec;
665 // Just ignore it.
666 break;
667
Chris Lattner4b009652007-07-25 00:24:17 +0000668 // storage-class-specifier
669 case tok::kw_typedef:
670 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
671 break;
672 case tok::kw_extern:
673 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000674 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000675 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
676 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000677 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000678 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
679 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000680 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000681 case tok::kw_static:
682 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000683 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000684 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
685 break;
686 case tok::kw_auto:
687 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
688 break;
689 case tok::kw_register:
690 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
691 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000692 case tok::kw_mutable:
693 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
694 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000695 case tok::kw___thread:
696 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
697 break;
698
Chris Lattner4b009652007-07-25 00:24:17 +0000699 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000700
Chris Lattner4b009652007-07-25 00:24:17 +0000701 // function-specifier
702 case tok::kw_inline:
703 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
704 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000705 case tok::kw_virtual:
706 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
707 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000708 case tok::kw_explicit:
709 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
710 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000711
712 // type-specifier
713 case tok::kw_short:
714 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
715 break;
716 case tok::kw_long:
717 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
718 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
719 else
720 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
721 break;
722 case tok::kw_signed:
723 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
724 break;
725 case tok::kw_unsigned:
726 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
727 break;
728 case tok::kw__Complex:
729 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
730 break;
731 case tok::kw__Imaginary:
732 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
733 break;
734 case tok::kw_void:
735 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
736 break;
737 case tok::kw_char:
738 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
739 break;
740 case tok::kw_int:
741 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
742 break;
743 case tok::kw_float:
744 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
745 break;
746 case tok::kw_double:
747 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
748 break;
749 case tok::kw_wchar_t:
750 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
751 break;
752 case tok::kw_bool:
753 case tok::kw__Bool:
754 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
755 break;
756 case tok::kw__Decimal32:
757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
758 break;
759 case tok::kw__Decimal64:
760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
761 break;
762 case tok::kw__Decimal128:
763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
764 break;
765
766 // class-specifier:
767 case tok::kw_class:
768 case tok::kw_struct:
769 case tok::kw_union:
770 ParseClassSpecifier(DS, TemplateParams);
771 continue;
772
773 // enum-specifier:
774 case tok::kw_enum:
775 ParseEnumSpecifier(DS);
776 continue;
777
778 // cv-qualifier:
779 case tok::kw_const:
780 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
781 break;
782 case tok::kw_volatile:
783 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
784 getLang())*2;
785 break;
786 case tok::kw_restrict:
787 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
788 getLang())*2;
789 break;
790
791 // GNU typeof support.
792 case tok::kw_typeof:
793 ParseTypeofSpecifier(DS);
794 continue;
795
Steve Naroff5f0466b2008-06-05 00:02:44 +0000796 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000797 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000798 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
799 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000800 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000801 goto DoneWithDeclSpec;
802
803 {
804 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000805 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000806 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000807 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000808 DS.SetRangeEnd(EndProtoLoc);
809
Chris Lattnerf006a222008-11-18 07:48:38 +0000810 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
811 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000812 // Need to support trailing type qualifiers (e.g. "id<p> const").
813 // If a type specifier follows, it will be diagnosed elsewhere.
814 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000815 }
Chris Lattner4b009652007-07-25 00:24:17 +0000816 }
817 // If the specifier combination wasn't legal, issue a diagnostic.
818 if (isInvalid) {
819 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000820 // Pick between error or extwarn.
821 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
822 : diag::ext_duplicate_declspec;
823 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000824 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000825 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000826 ConsumeToken();
827 }
828}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000829
Chris Lattnerd706dc82009-01-06 06:59:53 +0000830/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000831/// primarily follow the C++ grammar with additions for C99 and GNU,
832/// which together subsume the C grammar. Note that the C++
833/// type-specifier also includes the C type-qualifier (for const,
834/// volatile, and C99 restrict). Returns true if a type-specifier was
835/// found (and parsed), false otherwise.
836///
837/// type-specifier: [C++ 7.1.5]
838/// simple-type-specifier
839/// class-specifier
840/// enum-specifier
841/// elaborated-type-specifier [TODO]
842/// cv-qualifier
843///
844/// cv-qualifier: [C++ 7.1.5.1]
845/// 'const'
846/// 'volatile'
847/// [C99] 'restrict'
848///
849/// simple-type-specifier: [ C++ 7.1.5.2]
850/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
851/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
852/// 'char'
853/// 'wchar_t'
854/// 'bool'
855/// 'short'
856/// 'int'
857/// 'long'
858/// 'signed'
859/// 'unsigned'
860/// 'float'
861/// 'double'
862/// 'void'
863/// [C99] '_Bool'
864/// [C99] '_Complex'
865/// [C99] '_Imaginary' // Removed in TC2?
866/// [GNU] '_Decimal32'
867/// [GNU] '_Decimal64'
868/// [GNU] '_Decimal128'
869/// [GNU] typeof-specifier
870/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
871/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000872bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
873 const char *&PrevSpec,
874 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000875 SourceLocation Loc = Tok.getLocation();
876
877 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000878 case tok::identifier: // foo::bar
879 // Annotate typenames and C++ scope specifiers. If we get one, just
880 // recurse to handle whatever we get.
881 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000882 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000883 // Otherwise, not a type specifier.
884 return false;
885 case tok::coloncolon: // ::foo::bar
886 if (NextToken().is(tok::kw_new) || // ::new
887 NextToken().is(tok::kw_delete)) // ::delete
888 return false;
889
890 // Annotate typenames and C++ scope specifiers. If we get one, just
891 // recurse to handle whatever we get.
892 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000893 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000894 // Otherwise, not a type specifier.
895 return false;
896
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000897 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000898 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000899 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000900 Tok.getAnnotationValue());
901 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
902 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000903
904 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
905 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
906 // Objective-C interface. If we don't have Objective-C or a '<', this is
907 // just a normal reference to a typedef name.
908 if (!Tok.is(tok::less) || !getLang().ObjC1)
909 return true;
910
911 SourceLocation EndProtoLoc;
912 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
913 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
914 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
915
916 DS.SetRangeEnd(EndProtoLoc);
917 return true;
918 }
919
920 case tok::kw_short:
921 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
922 break;
923 case tok::kw_long:
924 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
925 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
926 else
927 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
928 break;
929 case tok::kw_signed:
930 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
931 break;
932 case tok::kw_unsigned:
933 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
934 break;
935 case tok::kw__Complex:
936 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
937 break;
938 case tok::kw__Imaginary:
939 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
940 break;
941 case tok::kw_void:
942 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
943 break;
944 case tok::kw_char:
945 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
946 break;
947 case tok::kw_int:
948 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
949 break;
950 case tok::kw_float:
951 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
952 break;
953 case tok::kw_double:
954 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
955 break;
956 case tok::kw_wchar_t:
957 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
958 break;
959 case tok::kw_bool:
960 case tok::kw__Bool:
961 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
962 break;
963 case tok::kw__Decimal32:
964 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
965 break;
966 case tok::kw__Decimal64:
967 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
968 break;
969 case tok::kw__Decimal128:
970 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
971 break;
972
973 // class-specifier:
974 case tok::kw_class:
975 case tok::kw_struct:
976 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000977 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000978 return true;
979
980 // enum-specifier:
981 case tok::kw_enum:
982 ParseEnumSpecifier(DS);
983 return true;
984
985 // cv-qualifier:
986 case tok::kw_const:
987 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
988 getLang())*2;
989 break;
990 case tok::kw_volatile:
991 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
992 getLang())*2;
993 break;
994 case tok::kw_restrict:
995 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
996 getLang())*2;
997 break;
998
999 // GNU typeof support.
1000 case tok::kw_typeof:
1001 ParseTypeofSpecifier(DS);
1002 return true;
1003
Steve Naroffedd04d52008-12-25 14:16:32 +00001004 case tok::kw___cdecl:
1005 case tok::kw___stdcall:
1006 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001007 if (!PP.getLangOptions().Microsoft) return false;
1008 ConsumeToken();
1009 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001010
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001011 default:
1012 // Not a type-specifier; do nothing.
1013 return false;
1014 }
1015
1016 // If the specifier combination wasn't legal, issue a diagnostic.
1017 if (isInvalid) {
1018 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001019 // Pick between error or extwarn.
1020 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1021 : diag::ext_duplicate_declspec;
1022 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001023 }
1024 DS.SetRangeEnd(Tok.getLocation());
1025 ConsumeToken(); // whatever we parsed above.
1026 return true;
1027}
Chris Lattner4b009652007-07-25 00:24:17 +00001028
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001029/// ParseStructDeclaration - Parse a struct declaration without the terminating
1030/// semicolon.
1031///
Chris Lattner4b009652007-07-25 00:24:17 +00001032/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001033/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001034/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001035/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001036/// struct-declarator-list:
1037/// struct-declarator
1038/// struct-declarator-list ',' struct-declarator
1039/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1040/// struct-declarator:
1041/// declarator
1042/// [GNU] declarator attributes[opt]
1043/// declarator[opt] ':' constant-expression
1044/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1045///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001046void Parser::
1047ParseStructDeclaration(DeclSpec &DS,
1048 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001049 if (Tok.is(tok::kw___extension__)) {
1050 // __extension__ silences extension warnings in the subexpression.
1051 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001052 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001053 return ParseStructDeclaration(DS, Fields);
1054 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001055
1056 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001057 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001058 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001059
Douglas Gregorb748fc52009-01-12 22:49:06 +00001060 // If there are no declarators, this is a free-standing declaration
1061 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001062 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001063 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001064 return;
1065 }
1066
1067 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001068 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001069 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001070 FieldDeclarator &DeclaratorInfo = Fields.back();
1071
Steve Naroffa9adf112007-08-20 22:28:22 +00001072 /// struct-declarator: declarator
1073 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001074 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001075 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001076
Chris Lattner34a01ad2007-10-09 17:33:22 +00001077 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001078 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001079 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001080 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001081 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001082 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001083 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001084 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001085
Steve Naroffa9adf112007-08-20 22:28:22 +00001086 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001087 if (Tok.is(tok::kw___attribute)) {
1088 SourceLocation Loc;
1089 AttributeList *AttrList = ParseAttributes(&Loc);
1090 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1091 }
1092
Steve Naroffa9adf112007-08-20 22:28:22 +00001093 // If we don't have a comma, it is either the end of the list (a ';')
1094 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001095 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001096 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001097
Steve Naroffa9adf112007-08-20 22:28:22 +00001098 // Consume the comma.
1099 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001100
Steve Naroffa9adf112007-08-20 22:28:22 +00001101 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001102 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001103
Steve Naroffa9adf112007-08-20 22:28:22 +00001104 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001105 if (Tok.is(tok::kw___attribute)) {
1106 SourceLocation Loc;
1107 AttributeList *AttrList = ParseAttributes(&Loc);
1108 Fields.back().D.AddAttributes(AttrList, Loc);
1109 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001110 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001111}
1112
1113/// ParseStructUnionBody
1114/// struct-contents:
1115/// struct-declaration-list
1116/// [EXT] empty
1117/// [GNU] "struct-declaration-list" without terminatoring ';'
1118/// struct-declaration-list:
1119/// struct-declaration
1120/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001121/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001122///
Chris Lattner4b009652007-07-25 00:24:17 +00001123void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1124 unsigned TagType, DeclTy *TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001125 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1126 PP.getSourceManager(),
1127 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001128
Chris Lattner4b009652007-07-25 00:24:17 +00001129 SourceLocation LBraceLoc = ConsumeBrace();
1130
Douglas Gregorcab994d2009-01-09 22:42:13 +00001131 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001132 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1133
Chris Lattner4b009652007-07-25 00:24:17 +00001134 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1135 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001136 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001137 Diag(Tok, diag::ext_empty_struct_union_enum)
1138 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001139
1140 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001141 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1142
Chris Lattner4b009652007-07-25 00:24:17 +00001143 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001144 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001145 // Each iteration of this loop reads one struct-declaration.
1146
1147 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001148 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001149 Diag(Tok, diag::ext_extra_struct_semi);
1150 ConsumeToken();
1151 continue;
1152 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001153
1154 // Parse all the comma separated declarators.
1155 DeclSpec DS;
1156 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001157 if (!Tok.is(tok::at)) {
1158 ParseStructDeclaration(DS, FieldDeclarators);
1159
1160 // Convert them all to fields.
1161 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1162 FieldDeclarator &FD = FieldDeclarators[i];
1163 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +00001164 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +00001165 DS.getSourceRange().getBegin(),
1166 FD.D, FD.BitfieldSize);
1167 FieldDecls.push_back(Field);
1168 }
1169 } else { // Handle @defs
1170 ConsumeToken();
1171 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1172 Diag(Tok, diag::err_unexpected_at);
1173 SkipUntil(tok::semi, true, true);
1174 continue;
1175 }
1176 ConsumeToken();
1177 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1178 if (!Tok.is(tok::identifier)) {
1179 Diag(Tok, diag::err_expected_ident);
1180 SkipUntil(tok::semi, true, true);
1181 continue;
1182 }
1183 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001184 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1185 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001186 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1187 ConsumeToken();
1188 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1189 }
Chris Lattner4b009652007-07-25 00:24:17 +00001190
Chris Lattner34a01ad2007-10-09 17:33:22 +00001191 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001192 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001193 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001194 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001195 break;
1196 } else {
1197 Diag(Tok, diag::err_expected_semi_decl_list);
1198 // Skip to end of block or statement
1199 SkipUntil(tok::r_brace, true, true);
1200 }
1201 }
1202
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001203 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001204
Chris Lattner4b009652007-07-25 00:24:17 +00001205 AttributeList *AttrList = 0;
1206 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001207 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001208 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001209
1210 Actions.ActOnFields(CurScope,
1211 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1212 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001213 AttrList);
1214 StructScope.Exit();
1215 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001216}
1217
1218
1219/// ParseEnumSpecifier
1220/// enum-specifier: [C99 6.7.2.2]
1221/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001222///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001223/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1224/// '}' attributes[opt]
1225/// 'enum' identifier
1226/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001227///
1228/// [C++] elaborated-type-specifier:
1229/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1230///
Chris Lattner4b009652007-07-25 00:24:17 +00001231void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001232 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001233 SourceLocation StartLoc = ConsumeToken();
1234
1235 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001236
1237 AttributeList *Attr = 0;
1238 // If attributes exist after tag, parse them.
1239 if (Tok.is(tok::kw___attribute))
1240 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001241
1242 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001243 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001244 if (Tok.isNot(tok::identifier)) {
1245 Diag(Tok, diag::err_expected_ident);
1246 if (Tok.isNot(tok::l_brace)) {
1247 // Has no name and is not a definition.
1248 // Skip the rest of this declarator, up until the comma or semicolon.
1249 SkipUntil(tok::comma, true);
1250 return;
1251 }
1252 }
1253 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001254
1255 // Must have either 'enum name' or 'enum {...}'.
1256 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1257 Diag(Tok, diag::err_expected_ident_lbrace);
1258
1259 // Skip the rest of this declarator, up until the comma or semicolon.
1260 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001261 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001262 }
1263
1264 // If an identifier is present, consume and remember it.
1265 IdentifierInfo *Name = 0;
1266 SourceLocation NameLoc;
1267 if (Tok.is(tok::identifier)) {
1268 Name = Tok.getIdentifierInfo();
1269 NameLoc = ConsumeToken();
1270 }
1271
1272 // There are three options here. If we have 'enum foo;', then this is a
1273 // forward declaration. If we have 'enum foo {...' then this is a
1274 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1275 //
1276 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1277 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1278 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1279 //
1280 Action::TagKind TK;
1281 if (Tok.is(tok::l_brace))
1282 TK = Action::TK_Definition;
1283 else if (Tok.is(tok::semi))
1284 TK = Action::TK_Declaration;
1285 else
1286 TK = Action::TK_Reference;
1287 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregord406b032009-02-06 22:42:48 +00001288 SS, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +00001289
Chris Lattner34a01ad2007-10-09 17:33:22 +00001290 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001291 ParseEnumBody(StartLoc, TagDecl);
1292
1293 // TODO: semantic analysis on the declspec for enums.
1294 const char *PrevSpec = 0;
1295 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001296 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001297}
1298
1299/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1300/// enumerator-list:
1301/// enumerator
1302/// enumerator-list ',' enumerator
1303/// enumerator:
1304/// enumeration-constant
1305/// enumeration-constant '=' constant-expression
1306/// enumeration-constant:
1307/// identifier
1308///
1309void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001310 // Enter the scope of the enum body and start the definition.
1311 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001312 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001313
Chris Lattner4b009652007-07-25 00:24:17 +00001314 SourceLocation LBraceLoc = ConsumeBrace();
1315
Chris Lattnerc9a92452007-08-27 17:24:30 +00001316 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001317 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001318 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001319
1320 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1321
1322 DeclTy *LastEnumConstDecl = 0;
1323
1324 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001325 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001326 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1327 SourceLocation IdentLoc = ConsumeToken();
1328
1329 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001330 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001331 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001332 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001333 AssignedVal = ParseConstantExpression();
1334 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001335 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001336 }
1337
1338 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001339 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001340 LastEnumConstDecl,
1341 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001342 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001343 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001344 EnumConstantDecls.push_back(EnumConstDecl);
1345 LastEnumConstDecl = EnumConstDecl;
1346
Chris Lattner34a01ad2007-10-09 17:33:22 +00001347 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001348 break;
1349 SourceLocation CommaLoc = ConsumeToken();
1350
Chris Lattner34a01ad2007-10-09 17:33:22 +00001351 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001352 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1353 }
1354
1355 // Eat the }.
1356 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1357
Steve Naroff0acc9c92007-09-15 18:49:24 +00001358 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001359 EnumConstantDecls.size());
1360
1361 DeclTy *AttrList = 0;
1362 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001363 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001364 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001365
1366 EnumScope.Exit();
1367 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001368}
1369
1370/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001371/// start of a type-qualifier-list.
1372bool Parser::isTypeQualifier() const {
1373 switch (Tok.getKind()) {
1374 default: return false;
1375 // type-qualifier
1376 case tok::kw_const:
1377 case tok::kw_volatile:
1378 case tok::kw_restrict:
1379 return true;
1380 }
1381}
1382
1383/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001384/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001385bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001386 switch (Tok.getKind()) {
1387 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001388
1389 case tok::identifier: // foo::bar
1390 // Annotate typenames and C++ scope specifiers. If we get one, just
1391 // recurse to handle whatever we get.
1392 if (TryAnnotateTypeOrScopeToken())
1393 return isTypeSpecifierQualifier();
1394 // Otherwise, not a type specifier.
1395 return false;
1396 case tok::coloncolon: // ::foo::bar
1397 if (NextToken().is(tok::kw_new) || // ::new
1398 NextToken().is(tok::kw_delete)) // ::delete
1399 return false;
1400
1401 // Annotate typenames and C++ scope specifiers. If we get one, just
1402 // recurse to handle whatever we get.
1403 if (TryAnnotateTypeOrScopeToken())
1404 return isTypeSpecifierQualifier();
1405 // Otherwise, not a type specifier.
1406 return false;
1407
Chris Lattner4b009652007-07-25 00:24:17 +00001408 // GNU attributes support.
1409 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001410 // GNU typeof support.
1411 case tok::kw_typeof:
1412
Chris Lattner4b009652007-07-25 00:24:17 +00001413 // type-specifiers
1414 case tok::kw_short:
1415 case tok::kw_long:
1416 case tok::kw_signed:
1417 case tok::kw_unsigned:
1418 case tok::kw__Complex:
1419 case tok::kw__Imaginary:
1420 case tok::kw_void:
1421 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001422 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001423 case tok::kw_int:
1424 case tok::kw_float:
1425 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001426 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001427 case tok::kw__Bool:
1428 case tok::kw__Decimal32:
1429 case tok::kw__Decimal64:
1430 case tok::kw__Decimal128:
1431
Chris Lattner2e78db32008-04-13 18:59:07 +00001432 // struct-or-union-specifier (C99) or class-specifier (C++)
1433 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001434 case tok::kw_struct:
1435 case tok::kw_union:
1436 // enum-specifier
1437 case tok::kw_enum:
1438
1439 // type-qualifier
1440 case tok::kw_const:
1441 case tok::kw_volatile:
1442 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001443
1444 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001445 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001446 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001447
1448 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1449 case tok::less:
1450 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001451
1452 case tok::kw___cdecl:
1453 case tok::kw___stdcall:
1454 case tok::kw___fastcall:
1455 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001456 }
1457}
1458
1459/// isDeclarationSpecifier() - Return true if the current token is part of a
1460/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001461bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001462 switch (Tok.getKind()) {
1463 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001464
1465 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001466 // Unfortunate hack to support "Class.factoryMethod" notation.
1467 if (getLang().ObjC1 && NextToken().is(tok::period))
1468 return false;
1469
Chris Lattnerb75fde62009-01-04 23:41:41 +00001470 // Annotate typenames and C++ scope specifiers. If we get one, just
1471 // recurse to handle whatever we get.
1472 if (TryAnnotateTypeOrScopeToken())
1473 return isDeclarationSpecifier();
1474 // Otherwise, not a declaration specifier.
1475 return false;
1476 case tok::coloncolon: // ::foo::bar
1477 if (NextToken().is(tok::kw_new) || // ::new
1478 NextToken().is(tok::kw_delete)) // ::delete
1479 return false;
1480
1481 // Annotate typenames and C++ scope specifiers. If we get one, just
1482 // recurse to handle whatever we get.
1483 if (TryAnnotateTypeOrScopeToken())
1484 return isDeclarationSpecifier();
1485 // Otherwise, not a declaration specifier.
1486 return false;
1487
Chris Lattner4b009652007-07-25 00:24:17 +00001488 // storage-class-specifier
1489 case tok::kw_typedef:
1490 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001491 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001492 case tok::kw_static:
1493 case tok::kw_auto:
1494 case tok::kw_register:
1495 case tok::kw___thread:
1496
1497 // type-specifiers
1498 case tok::kw_short:
1499 case tok::kw_long:
1500 case tok::kw_signed:
1501 case tok::kw_unsigned:
1502 case tok::kw__Complex:
1503 case tok::kw__Imaginary:
1504 case tok::kw_void:
1505 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001506 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001507 case tok::kw_int:
1508 case tok::kw_float:
1509 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001510 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001511 case tok::kw__Bool:
1512 case tok::kw__Decimal32:
1513 case tok::kw__Decimal64:
1514 case tok::kw__Decimal128:
1515
Chris Lattner2e78db32008-04-13 18:59:07 +00001516 // struct-or-union-specifier (C99) or class-specifier (C++)
1517 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001518 case tok::kw_struct:
1519 case tok::kw_union:
1520 // enum-specifier
1521 case tok::kw_enum:
1522
1523 // type-qualifier
1524 case tok::kw_const:
1525 case tok::kw_volatile:
1526 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001527
Chris Lattner4b009652007-07-25 00:24:17 +00001528 // function-specifier
1529 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001530 case tok::kw_virtual:
1531 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001532
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001533 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001534 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001535
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001536 // GNU typeof support.
1537 case tok::kw_typeof:
1538
1539 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001540 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001541 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001542
1543 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1544 case tok::less:
1545 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001546
Steve Naroffab1a3632009-01-06 19:34:12 +00001547 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001548 case tok::kw___cdecl:
1549 case tok::kw___stdcall:
1550 case tok::kw___fastcall:
1551 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001552 }
1553}
1554
1555
1556/// ParseTypeQualifierListOpt
1557/// type-qualifier-list: [C99 6.7.5]
1558/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001559/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001560/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001561/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001562///
Chris Lattner460696f2008-12-18 07:02:59 +00001563void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001564 while (1) {
1565 int isInvalid = false;
1566 const char *PrevSpec = 0;
1567 SourceLocation Loc = Tok.getLocation();
1568
1569 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001570 case tok::kw_const:
1571 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1572 getLang())*2;
1573 break;
1574 case tok::kw_volatile:
1575 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1576 getLang())*2;
1577 break;
1578 case tok::kw_restrict:
1579 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1580 getLang())*2;
1581 break;
Steve Naroffad620402008-12-25 14:41:26 +00001582 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001583 case tok::kw___cdecl:
1584 case tok::kw___stdcall:
1585 case tok::kw___fastcall:
1586 if (!PP.getLangOptions().Microsoft)
1587 goto DoneWithTypeQuals;
1588 // Just ignore it.
1589 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001590 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001591 if (AttributesAllowed) {
1592 DS.AddAttributes(ParseAttributes());
1593 continue; // do *not* consume the next token!
1594 }
1595 // otherwise, FALL THROUGH!
1596 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001597 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001598 // If this is not a type-qualifier token, we're done reading type
1599 // qualifiers. First verify that DeclSpec's are consistent.
1600 DS.Finish(Diags, PP.getSourceManager(), getLang());
1601 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001602 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001603
Chris Lattner4b009652007-07-25 00:24:17 +00001604 // If the specifier combination wasn't legal, issue a diagnostic.
1605 if (isInvalid) {
1606 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001607 // Pick between error or extwarn.
1608 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1609 : diag::ext_duplicate_declspec;
1610 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001611 }
1612 ConsumeToken();
1613 }
1614}
1615
1616
1617/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1618///
1619void Parser::ParseDeclarator(Declarator &D) {
1620 /// This implements the 'declarator' production in the C grammar, then checks
1621 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001622 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001623}
1624
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001625/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1626/// is parsed by the function passed to it. Pass null, and the direct-declarator
1627/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001628/// ptr-operator production.
1629///
Sebastian Redl75555032009-01-24 21:16:55 +00001630/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1631/// [C] pointer[opt] direct-declarator
1632/// [C++] direct-declarator
1633/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001634///
1635/// pointer: [C99 6.7.5]
1636/// '*' type-qualifier-list[opt]
1637/// '*' type-qualifier-list[opt] pointer
1638///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001639/// ptr-operator:
1640/// '*' cv-qualifier-seq[opt]
1641/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001642/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001643/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001644/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001645/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001646void Parser::ParseDeclaratorInternal(Declarator &D,
1647 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001648
Sebastian Redl75555032009-01-24 21:16:55 +00001649 // C++ member pointers start with a '::' or a nested-name.
1650 // Member pointers get special handling, since there's no place for the
1651 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001652 if (getLang().CPlusPlus &&
1653 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1654 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001655 CXXScopeSpec SS;
1656 if (ParseOptionalCXXScopeSpecifier(SS)) {
1657 if(Tok.isNot(tok::star)) {
1658 // The scope spec really belongs to the direct-declarator.
1659 D.getCXXScopeSpec() = SS;
1660 if (DirectDeclParser)
1661 (this->*DirectDeclParser)(D);
1662 return;
1663 }
1664
1665 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001666 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001667 DeclSpec DS;
1668 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001669 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001670
1671 // Recurse to parse whatever is left.
1672 ParseDeclaratorInternal(D, DirectDeclParser);
1673
1674 // Sema will have to catch (syntactically invalid) pointers into global
1675 // scope. It has to catch pointers into namespace scope anyway.
1676 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001677 Loc, DS.TakeAttributes()),
1678 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001679 return;
1680 }
1681 }
1682
1683 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001684 // Not a pointer, C++ reference, or block.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001685 if (Kind != tok::star &&
1686 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001687 // We parse rvalue refs in C++03, because otherwise the errors are scary.
1688 (Kind != tok::ampamp || !getLang().CPlusPlus) &&
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001689 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001690 if (DirectDeclParser)
1691 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001692 return;
1693 }
Sebastian Redl75555032009-01-24 21:16:55 +00001694
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001695 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1696 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001697 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001698 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001699
Steve Naroffdc22f212008-08-28 10:07:06 +00001700 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001701 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001702 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001703
Chris Lattner4b009652007-07-25 00:24:17 +00001704 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001705 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001706
Chris Lattner4b009652007-07-25 00:24:17 +00001707 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001708 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001709 if (Kind == tok::star)
1710 // Remember that we parsed a pointer type, and remember the type-quals.
1711 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001712 DS.TakeAttributes()),
1713 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001714 else
1715 // Remember that we parsed a Block type, and remember the type-quals.
1716 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001717 Loc),
1718 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001719 } else {
1720 // Is a reference
1721 DeclSpec DS;
1722
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001723 // Complain about rvalue references in C++03, but then go on and build
1724 // the declarator.
1725 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1726 Diag(Loc, diag::err_rvalue_reference);
1727
Chris Lattner4b009652007-07-25 00:24:17 +00001728 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1729 // cv-qualifiers are introduced through the use of a typedef or of a
1730 // template type argument, in which case the cv-qualifiers are ignored.
1731 //
1732 // [GNU] Retricted references are allowed.
1733 // [GNU] Attributes on references are allowed.
1734 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001735 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001736
1737 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1738 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1739 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001740 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001741 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1742 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001743 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001744 }
1745
1746 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001747 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001748
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001749 if (D.getNumTypeObjects() > 0) {
1750 // C++ [dcl.ref]p4: There shall be no references to references.
1751 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1752 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001753 if (const IdentifierInfo *II = D.getIdentifier())
1754 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1755 << II;
1756 else
1757 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1758 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001759
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001760 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001761 // can go ahead and build the (technically ill-formed)
1762 // declarator: reference collapsing will take care of it.
1763 }
1764 }
1765
Chris Lattner4b009652007-07-25 00:24:17 +00001766 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001767 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001768 DS.TakeAttributes(),
1769 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001770 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001771 }
1772}
1773
1774/// ParseDirectDeclarator
1775/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001776/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001777/// '(' declarator ')'
1778/// [GNU] '(' attributes declarator ')'
1779/// [C90] direct-declarator '[' constant-expression[opt] ']'
1780/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1781/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1782/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1783/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1784/// direct-declarator '(' parameter-type-list ')'
1785/// direct-declarator '(' identifier-list[opt] ')'
1786/// [GNU] direct-declarator '(' parameter-forward-declarations
1787/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001788/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1789/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001790/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001791///
1792/// declarator-id: [C++ 8]
1793/// id-expression
1794/// '::'[opt] nested-name-specifier[opt] type-name
1795///
1796/// id-expression: [C++ 5.1]
1797/// unqualified-id
1798/// qualified-id [TODO]
1799///
1800/// unqualified-id: [C++ 5.1]
1801/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001802/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001803/// conversion-function-id [TODO]
1804/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001805/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001806///
Chris Lattner4b009652007-07-25 00:24:17 +00001807void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001808 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001809
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001810 if (getLang().CPlusPlus) {
1811 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001812 // ParseDeclaratorInternal might already have parsed the scope.
1813 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1814 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001815 if (afterCXXScope) {
1816 // Change the declaration context for name lookup, until this function
1817 // is exited (and the declarator has been parsed).
1818 DeclScopeObj.EnterDeclaratorScope();
1819 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001820
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001821 if (Tok.is(tok::identifier)) {
1822 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001823
Douglas Gregor2fa10442008-12-18 19:37:40 +00001824 // If this identifier is the name of the current class, it's a
1825 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001826 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001827 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001828 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001829 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001830 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001831 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001832 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1833 ConsumeToken();
1834 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001835 } else if (Tok.is(tok::annot_template_id)) {
1836 TemplateIdAnnotation *TemplateId
1837 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1838
1839 // FIXME: Could this template-id name a constructor?
1840
1841 // FIXME: This is an egregious hack, where we silently ignore
1842 // the specialization (which should be a function template
1843 // specialization name) and use the name instead. This hack
1844 // will go away when we have support for function
1845 // specializations.
1846 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1847 TemplateId->Destroy();
1848 ConsumeToken();
1849 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001850 } else if (Tok.is(tok::kw_operator)) {
1851 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001852 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001853
Douglas Gregor853dd392008-12-26 15:00:45 +00001854 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001855 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1856 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001857 } else {
1858 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001859 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1860 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1861 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001862 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001863 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001864 }
1865 goto PastIdentifier;
1866 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001867 // This should be a C++ destructor.
1868 SourceLocation TildeLoc = ConsumeToken();
1869 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001870 // FIXME: Inaccurate.
1871 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001872 SourceLocation EndLoc;
1873 if (TypeTy *Type = ParseClassName(EndLoc)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001874 D.setDestructor(Type, TildeLoc, NameLoc);
1875 } else {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001876 D.SetIdentifier(0, TildeLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001877 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001878 } else {
1879 Diag(Tok, diag::err_expected_class_name);
1880 D.SetIdentifier(0, TildeLoc);
1881 }
1882 goto PastIdentifier;
1883 }
1884
1885 // If we reached this point, token is not identifier and not '~'.
1886
1887 if (afterCXXScope) {
1888 Diag(Tok, diag::err_expected_unqualified_id);
1889 D.SetIdentifier(0, Tok.getLocation());
1890 D.setInvalidType(true);
1891 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001892 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001893 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001894 }
1895
1896 // If we reached this point, we are either in C/ObjC or the token didn't
1897 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001898 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1899 assert(!getLang().CPlusPlus &&
1900 "There's a C++-specific check for tok::identifier above");
1901 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1902 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1903 ConsumeToken();
1904 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001905 // direct-declarator: '(' declarator ')'
1906 // direct-declarator: '(' attributes declarator ')'
1907 // Example: 'char (*X)' or 'int (*XX)(void)'
1908 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001909 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001910 // This could be something simple like "int" (in which case the declarator
1911 // portion is empty), if an abstract-declarator is allowed.
1912 D.SetIdentifier(0, Tok.getLocation());
1913 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00001914 if (D.getContext() == Declarator::MemberContext)
1915 Diag(Tok, diag::err_expected_member_name_or_semi)
1916 << D.getDeclSpec().getSourceRange();
1917 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001918 Diag(Tok, diag::err_expected_unqualified_id);
1919 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001920 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001921 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001922 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001923 }
1924
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001925 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001926 assert(D.isPastIdentifier() &&
1927 "Haven't past the location of the identifier yet?");
1928
1929 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001930 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001931 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1932 // In such a case, check if we actually have a function declarator; if it
1933 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001934 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1935 // When not in file scope, warn for ambiguous function declarators, just
1936 // in case the author intended it as a variable definition.
1937 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1938 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1939 break;
1940 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001941 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001942 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001943 ParseBracketDeclarator(D);
1944 } else {
1945 break;
1946 }
1947 }
1948}
1949
Chris Lattnera0d056d2008-04-06 05:45:57 +00001950/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1951/// only called before the identifier, so these are most likely just grouping
1952/// parens for precedence. If we find that these are actually function
1953/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1954///
1955/// direct-declarator:
1956/// '(' declarator ')'
1957/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001958/// direct-declarator '(' parameter-type-list ')'
1959/// direct-declarator '(' identifier-list[opt] ')'
1960/// [GNU] direct-declarator '(' parameter-forward-declarations
1961/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001962///
1963void Parser::ParseParenDeclarator(Declarator &D) {
1964 SourceLocation StartLoc = ConsumeParen();
1965 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1966
Chris Lattner1f185292008-10-20 02:05:46 +00001967 // Eat any attributes before we look at whether this is a grouping or function
1968 // declarator paren. If this is a grouping paren, the attribute applies to
1969 // the type being built up, for example:
1970 // int (__attribute__(()) *x)(long y)
1971 // If this ends up not being a grouping paren, the attribute applies to the
1972 // first argument, for example:
1973 // int (__attribute__(()) int x)
1974 // In either case, we need to eat any attributes to be able to determine what
1975 // sort of paren this is.
1976 //
1977 AttributeList *AttrList = 0;
1978 bool RequiresArg = false;
1979 if (Tok.is(tok::kw___attribute)) {
1980 AttrList = ParseAttributes();
1981
1982 // We require that the argument list (if this is a non-grouping paren) be
1983 // present even if the attribute list was empty.
1984 RequiresArg = true;
1985 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001986 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00001987 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1988 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00001989 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00001990
Chris Lattnera0d056d2008-04-06 05:45:57 +00001991 // If we haven't past the identifier yet (or where the identifier would be
1992 // stored, if this is an abstract declarator), then this is probably just
1993 // grouping parens. However, if this could be an abstract-declarator, then
1994 // this could also be the start of function arguments (consider 'void()').
1995 bool isGrouping;
1996
1997 if (!D.mayOmitIdentifier()) {
1998 // If this can't be an abstract-declarator, this *must* be a grouping
1999 // paren, because we haven't seen the identifier yet.
2000 isGrouping = true;
2001 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002002 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002003 isDeclarationSpecifier()) { // 'int(int)' is a function.
2004 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2005 // considered to be a type, not a K&R identifier-list.
2006 isGrouping = false;
2007 } else {
2008 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2009 isGrouping = true;
2010 }
2011
2012 // If this is a grouping paren, handle:
2013 // direct-declarator: '(' declarator ')'
2014 // direct-declarator: '(' attributes declarator ')'
2015 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002016 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002017 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002018 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002019 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002020
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002021 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002022 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002023 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002024
2025 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002026 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002027 return;
2028 }
2029
2030 // Okay, if this wasn't a grouping paren, it must be the start of a function
2031 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002032 // identifier (and remember where it would have been), then call into
2033 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002034 D.SetIdentifier(0, Tok.getLocation());
2035
Chris Lattner1f185292008-10-20 02:05:46 +00002036 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002037}
2038
2039/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2040/// declarator D up to a paren, which indicates that we are parsing function
2041/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002042///
Chris Lattner1f185292008-10-20 02:05:46 +00002043/// If AttrList is non-null, then the caller parsed those arguments immediately
2044/// after the open paren - they should be considered to be the first argument of
2045/// a parameter. If RequiresArg is true, then the first argument of the
2046/// function is required to be present and required to not be an identifier
2047/// list.
2048///
Chris Lattner4b009652007-07-25 00:24:17 +00002049/// This method also handles this portion of the grammar:
2050/// parameter-type-list: [C99 6.7.5]
2051/// parameter-list
2052/// parameter-list ',' '...'
2053///
2054/// parameter-list: [C99 6.7.5]
2055/// parameter-declaration
2056/// parameter-list ',' parameter-declaration
2057///
2058/// parameter-declaration: [C99 6.7.5]
2059/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002060/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002061/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002062/// declaration-specifiers abstract-declarator[opt]
2063/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002064/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002065/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2066///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002067/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002068/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002069///
Chris Lattner1f185292008-10-20 02:05:46 +00002070void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2071 AttributeList *AttrList,
2072 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002073 // lparen is already consumed!
2074 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002075
Chris Lattner1f185292008-10-20 02:05:46 +00002076 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002077 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002078 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002079 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002080 delete AttrList;
2081 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002082
Sebastian Redl0c986032009-02-09 18:23:29 +00002083 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002084
2085 // cv-qualifier-seq[opt].
2086 DeclSpec DS;
2087 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002088 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002089 if (!DS.getSourceRange().getEnd().isInvalid())
2090 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002091
2092 // Parse exception-specification[opt].
2093 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002094 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002095 }
2096
Chris Lattner9f7564b2008-04-06 06:57:35 +00002097 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002098 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002099 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002100 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002101 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002102 /*arglist*/ 0, 0,
2103 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002104 LParenLoc, D),
2105 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002106 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002107 }
2108
2109 // Alternatively, this parameter list may be an identifier list form for a
2110 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002111 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002112 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002113 // K&R identifier lists can't have typedefs as identifiers, per
2114 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002115 if (RequiresArg) {
2116 Diag(Tok, diag::err_argument_required_after_attribute);
2117 delete AttrList;
2118 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002119 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2120 // normal declarators, not for abstract-declarators.
2121 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002122 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002123 }
2124
2125 // Finally, a normal, non-empty parameter type list.
2126
2127 // Build up an array of information about the parsed arguments.
2128 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002129
2130 // Enter function-declaration scope, limiting any declarators to the
2131 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002132 ParseScope PrototypeScope(this,
2133 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002134
2135 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002136 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002137 while (1) {
2138 if (Tok.is(tok::ellipsis)) {
2139 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002140 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002141 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002142 }
2143
Chris Lattner9f7564b2008-04-06 06:57:35 +00002144 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002145
Chris Lattner9f7564b2008-04-06 06:57:35 +00002146 // Parse the declaration-specifiers.
2147 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002148
2149 // If the caller parsed attributes for the first argument, add them now.
2150 if (AttrList) {
2151 DS.AddAttributes(AttrList);
2152 AttrList = 0; // Only apply the attributes to the first parameter.
2153 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002154 ParseDeclarationSpecifiers(DS);
2155
Chris Lattner9f7564b2008-04-06 06:57:35 +00002156 // Parse the declarator. This is "PrototypeContext", because we must
2157 // accept either 'declarator' or 'abstract-declarator' here.
2158 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2159 ParseDeclarator(ParmDecl);
2160
2161 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002162 if (Tok.is(tok::kw___attribute)) {
2163 SourceLocation Loc;
2164 AttributeList *AttrList = ParseAttributes(&Loc);
2165 ParmDecl.AddAttributes(AttrList, Loc);
2166 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002167
Chris Lattner9f7564b2008-04-06 06:57:35 +00002168 // Remember this parsed parameter in ParamInfo.
2169 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2170
Douglas Gregor605de8d2008-12-16 21:30:33 +00002171 // DefArgToks is used when the parsing of default arguments needs
2172 // to be delayed.
2173 CachedTokens *DefArgToks = 0;
2174
Chris Lattner9f7564b2008-04-06 06:57:35 +00002175 // If no parameter was specified, verify that *something* was specified,
2176 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002177 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2178 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002179 // Completely missing, emit error.
2180 Diag(DSStart, diag::err_missing_param);
2181 } else {
2182 // Otherwise, we have something. Add it and let semantic analysis try
2183 // to grok it and add the result to the ParamInfo we are building.
2184
2185 // Inform the actions module about the parameter declarator, so it gets
2186 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00002187 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2188
2189 // Parse the default argument, if any. We parse the default
2190 // arguments in all dialects; the semantic analysis in
2191 // ActOnParamDefaultArgument will reject the default argument in
2192 // C.
2193 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002194 SourceLocation EqualLoc = Tok.getLocation();
2195
Chris Lattner3e254fb2008-04-08 04:40:51 +00002196 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002197 if (D.getContext() == Declarator::MemberContext) {
2198 // If we're inside a class definition, cache the tokens
2199 // corresponding to the default argument. We'll actually parse
2200 // them when we see the end of the class definition.
2201 // FIXME: Templates will require something similar.
2202 // FIXME: Can we use a smart pointer for Toks?
2203 DefArgToks = new CachedTokens;
2204
2205 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2206 tok::semi, false)) {
2207 delete DefArgToks;
2208 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002209 Actions.ActOnParamDefaultArgumentError(Param);
2210 } else
2211 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002212 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002213 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002214 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002215
2216 OwningExprResult DefArgResult(ParseAssignmentExpression());
2217 if (DefArgResult.isInvalid()) {
2218 Actions.ActOnParamDefaultArgumentError(Param);
2219 SkipUntil(tok::comma, tok::r_paren, true, true);
2220 } else {
2221 // Inform the actions module about the default argument
2222 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002223 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002224 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002225 }
2226 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002227
2228 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002229 ParmDecl.getIdentifierLoc(), Param,
2230 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002231 }
2232
2233 // If the next token is a comma, consume it and keep reading arguments.
2234 if (Tok.isNot(tok::comma)) break;
2235
2236 // Consume the comma.
2237 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002238 }
2239
Chris Lattner9f7564b2008-04-06 06:57:35 +00002240 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002241 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002242
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002243 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002244 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002245
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002246 DeclSpec DS;
2247 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002248 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002249 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002250 if (!DS.getSourceRange().getEnd().isInvalid())
2251 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002252
2253 // Parse exception-specification[opt].
2254 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002255 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002256 }
2257
Chris Lattner4b009652007-07-25 00:24:17 +00002258 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002259 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002260 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002261 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002262 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002263 LParenLoc, D),
2264 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002265}
2266
Chris Lattner35d9c912008-04-06 06:34:08 +00002267/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2268/// we found a K&R-style identifier list instead of a type argument list. The
2269/// current token is known to be the first identifier in the list.
2270///
2271/// identifier-list: [C99 6.7.5]
2272/// identifier
2273/// identifier-list ',' identifier
2274///
2275void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2276 Declarator &D) {
2277 // Build up an array of information about the parsed arguments.
2278 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2279 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2280
2281 // If there was no identifier specified for the declarator, either we are in
2282 // an abstract-declarator, or we are in a parameter declarator which was found
2283 // to be abstract. In abstract-declarators, identifier lists are not valid:
2284 // diagnose this.
2285 if (!D.getIdentifier())
2286 Diag(Tok, diag::ext_ident_list_in_param);
2287
2288 // Tok is known to be the first identifier in the list. Remember this
2289 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002290 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002291 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2292 Tok.getLocation(), 0));
2293
Chris Lattner113a56b2008-04-06 06:39:19 +00002294 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002295
2296 while (Tok.is(tok::comma)) {
2297 // Eat the comma.
2298 ConsumeToken();
2299
Chris Lattner113a56b2008-04-06 06:39:19 +00002300 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002301 if (Tok.isNot(tok::identifier)) {
2302 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002303 SkipUntil(tok::r_paren);
2304 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002305 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002306
Chris Lattner35d9c912008-04-06 06:34:08 +00002307 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002308
2309 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002310 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002311 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002312
2313 // Verify that the argument identifier has not already been mentioned.
2314 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002315 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002316 } else {
2317 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002318 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2319 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00002320 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002321
2322 // Eat the identifier.
2323 ConsumeToken();
2324 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002325
2326 // If we have the closing ')', eat it and we're done.
2327 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2328
Chris Lattner113a56b2008-04-06 06:39:19 +00002329 // Remember that we parsed a function type, and remember the attributes. This
2330 // function type is always a K&R style function type, which is not varargs and
2331 // has no prototype.
2332 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002333 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002334 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002335 /*TypeQuals*/0, LParenLoc, D),
2336 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002337}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002338
Chris Lattner4b009652007-07-25 00:24:17 +00002339/// [C90] direct-declarator '[' constant-expression[opt] ']'
2340/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2341/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2342/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2343/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2344void Parser::ParseBracketDeclarator(Declarator &D) {
2345 SourceLocation StartLoc = ConsumeBracket();
2346
Chris Lattner1525c3a2008-12-18 07:27:21 +00002347 // C array syntax has many features, but by-far the most common is [] and [4].
2348 // This code does a fast path to handle some of the most obvious cases.
2349 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002350 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002351 // Remember that we parsed the empty array type.
2352 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002353 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2354 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002355 return;
2356 } else if (Tok.getKind() == tok::numeric_constant &&
2357 GetLookAheadToken(1).is(tok::r_square)) {
2358 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002359 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002360 ConsumeToken();
2361
Sebastian Redl0c986032009-02-09 18:23:29 +00002362 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002363
2364 // If there was an error parsing the assignment-expression, recover.
2365 if (ExprRes.isInvalid())
2366 ExprRes.release(); // Deallocate expr, just use [].
2367
2368 // Remember that we parsed a array type, and remember its features.
2369 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002370 ExprRes.release(), StartLoc),
2371 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002372 return;
2373 }
2374
Chris Lattner4b009652007-07-25 00:24:17 +00002375 // If valid, this location is the position where we read the 'static' keyword.
2376 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002377 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002378 StaticLoc = ConsumeToken();
2379
2380 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002381 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002382 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002383 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002384
2385 // If we haven't already read 'static', check to see if there is one after the
2386 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002387 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002388 StaticLoc = ConsumeToken();
2389
2390 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2391 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002392 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002393
2394 // Handle the case where we have '[*]' as the array size. However, a leading
2395 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2396 // the the token after the star is a ']'. Since stars in arrays are
2397 // infrequent, use of lookahead is not costly here.
2398 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002399 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002400
Chris Lattner306d4df2008-12-18 06:50:14 +00002401 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002402 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002403 StaticLoc = SourceLocation(); // Drop the static.
2404 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002405 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002406 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002407 // Note, in C89, this production uses the constant-expr production instead
2408 // of assignment-expr. The only difference is that assignment-expr allows
2409 // things like '=' and '*='. Sema rejects these in C89 mode because they
2410 // are not i-c-e's, so we don't need to distinguish between the two here.
2411
Chris Lattner4b009652007-07-25 00:24:17 +00002412 // Parse the assignment-expression now.
2413 NumElements = ParseAssignmentExpression();
2414 }
2415
2416 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002417 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002418 // If the expression was invalid, skip it.
2419 SkipUntil(tok::r_square);
2420 return;
2421 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002422
2423 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2424
Chris Lattner1525c3a2008-12-18 07:27:21 +00002425 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002426 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2427 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002428 NumElements.release(), StartLoc),
2429 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002430}
2431
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002432/// [GNU] typeof-specifier:
2433/// typeof ( expressions )
2434/// typeof ( type-name )
2435/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002436///
2437void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002438 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002439 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002440 SourceLocation StartLoc = ConsumeToken();
2441
Chris Lattner34a01ad2007-10-09 17:33:22 +00002442 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002443 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002444 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002445 return;
2446 }
2447
Sebastian Redl14ca7412008-12-11 21:36:32 +00002448 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002449 if (Result.isInvalid()) {
2450 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002451 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002452 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002453
2454 const char *PrevSpec = 0;
2455 // Check for duplicate type specifiers.
2456 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002457 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002458 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002459
2460 // FIXME: Not accurate, the range gets one token more than it should.
2461 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002462 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002463 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002464
Steve Naroff7cbb1462007-07-31 12:34:36 +00002465 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2466
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002467 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002468 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002469
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002470 assert((Ty.isInvalid() || Ty.get()) &&
2471 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002472
Chris Lattner34a01ad2007-10-09 17:33:22 +00002473 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002474 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002475 return;
2476 }
2477 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002478
2479 if (Ty.isInvalid())
2480 DS.SetTypeSpecError();
2481 else {
2482 const char *PrevSpec = 0;
2483 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2484 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2485 Ty.get()))
2486 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2487 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002488 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002489 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002490
2491 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002492 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002493 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002494 return;
2495 }
2496 RParenLoc = ConsumeParen();
2497 const char *PrevSpec = 0;
2498 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2499 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002500 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002501 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002502 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002503 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002504}
2505
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002506