blob: 0ab7042f5eb12597a1b9038c8732bffbceac7e8d [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.
Chris Lattner7f6c2872009-03-28 06:13:37 +0000396 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanianc1509b02009-01-17 00:00:40 +0000397 Diag(Tok, diag::err_parse_error);
398 return 0;
399 }
Chris Lattner4b009652007-07-25 00:24:17 +0000400 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
401 }
Chris Lattner7f6c2872009-03-28 06:13:37 +0000402
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000403 // If this is an ObjC2 for-each loop, this is a successful declarator
404 // parse. The syntax for these looks like:
405 // 'for' '(' declaration 'in' expr ')' statement
Chris Lattner7f6c2872009-03-28 06:13:37 +0000406 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in())
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000407 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner7f6c2872009-03-28 06:13:37 +0000408
Chris Lattner4b009652007-07-25 00:24:17 +0000409 Diag(Tok, diag::err_parse_error);
410 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000411 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000412 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000413 ConsumeToken();
414 return 0;
415}
416
417/// ParseSpecifierQualifierList
418/// specifier-qualifier-list:
419/// type-specifier specifier-qualifier-list[opt]
420/// type-qualifier specifier-qualifier-list[opt]
421/// [GNU] attributes specifier-qualifier-list[opt]
422///
423void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
424 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
425 /// parse declaration-specifiers and complain about extra stuff.
426 ParseDeclarationSpecifiers(DS);
427
428 // Validate declspec for type-name.
429 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000430 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000431 Diag(Tok, diag::err_typename_requires_specqual);
432
433 // Issue diagnostic and remove storage class if present.
434 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
435 if (DS.getStorageClassSpecLoc().isValid())
436 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
437 else
438 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
439 DS.ClearStorageClassSpecs();
440 }
441
442 // Issue diagnostic and remove function specfier if present.
443 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000444 if (DS.isInlineSpecified())
445 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
446 if (DS.isVirtualSpecified())
447 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
448 if (DS.isExplicitSpecified())
449 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000450 DS.ClearFunctionSpecs();
451 }
452}
453
454/// ParseDeclarationSpecifiers
455/// declaration-specifiers: [C99 6.7]
456/// storage-class-specifier declaration-specifiers[opt]
457/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000458/// [C99] function-specifier declaration-specifiers[opt]
459/// [GNU] attributes declaration-specifiers[opt]
460///
461/// storage-class-specifier: [C99 6.7.1]
462/// 'typedef'
463/// 'extern'
464/// 'static'
465/// 'auto'
466/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000467/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000468/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000469/// function-specifier: [C99 6.7.4]
470/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000471/// [C++] 'virtual'
472/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000473///
Douglas Gregor52473432008-12-24 02:52:09 +0000474void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000475 TemplateParameterLists *TemplateParams,
476 AccessSpecifier AS){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000477 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000478 while (1) {
479 int isInvalid = false;
480 const char *PrevSpec = 0;
481 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000482
Chris Lattner4b009652007-07-25 00:24:17 +0000483 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000484 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000485 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000486 // If this is not a declaration specifier token, we're done reading decl
487 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000488 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000489 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000490
491 case tok::coloncolon: // ::foo::bar
492 // Annotate C++ scope specifiers. If we get one, loop.
493 if (TryAnnotateCXXScopeToken())
494 continue;
495 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000496
497 case tok::annot_cxxscope: {
498 if (DS.hasTypeSpecifier())
499 goto DoneWithDeclSpec;
500
501 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000502 Token Next = NextToken();
503 if (Next.is(tok::annot_template_id) &&
504 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
505 ->Kind == TNK_Class_template) {
506 // We have a qualified template-id, e.g., N::A<int>
507 CXXScopeSpec SS;
508 ParseOptionalCXXScopeSpecifier(SS);
509 assert(Tok.is(tok::annot_template_id) &&
510 "ParseOptionalCXXScopeSpecifier not working");
511 AnnotateTemplateIdTokenAsType(&SS);
512 continue;
513 }
514
515 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000516 goto DoneWithDeclSpec;
517
518 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000519 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000520 SS.setRange(Tok.getAnnotationRange());
521
522 // If the next token is the name of the class type that the C++ scope
523 // denotes, followed by a '(', then this is a constructor declaration.
524 // We're done with the decl-specifiers.
525 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
526 CurScope, &SS) &&
527 GetLookAheadToken(2).is(tok::l_paren))
528 goto DoneWithDeclSpec;
529
Douglas Gregor1075a162009-02-04 17:00:24 +0000530 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
531 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000532
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000533 if (TypeRep == 0)
534 goto DoneWithDeclSpec;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000535
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000536 ConsumeToken(); // The C++ scope.
537
Douglas Gregora60c62e2009-02-09 15:09:02 +0000538 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000539 TypeRep);
540 if (isInvalid)
541 break;
542
543 DS.SetRangeEnd(Tok.getLocation());
544 ConsumeToken(); // The typename.
545
546 continue;
547 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000548
549 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000550 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerc297b722009-01-21 19:48:37 +0000551 Tok.getAnnotationValue());
552 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
553 ConsumeToken(); // The typename
554
555 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
556 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
557 // Objective-C interface. If we don't have Objective-C or a '<', this is
558 // just a normal reference to a typedef name.
559 if (!Tok.is(tok::less) || !getLang().ObjC1)
560 continue;
561
562 SourceLocation EndProtoLoc;
563 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
564 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
565 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
566
567 DS.SetRangeEnd(EndProtoLoc);
568 continue;
569 }
570
Chris Lattnerfda18db2008-07-26 01:18:38 +0000571 // typedef-name
572 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000573 // In C++, check to see if this is a scope specifier like foo::bar::, if
574 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000575 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
576 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000577
Chris Lattnerfda18db2008-07-26 01:18:38 +0000578 // This identifier can only be a typedef name if we haven't already seen
579 // a type-specifier. Without this check we misparse:
580 // typedef int X; struct Y { short X; }; as 'short int'.
581 if (DS.hasTypeSpecifier())
582 goto DoneWithDeclSpec;
583
584 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000585 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
586 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000587
Chris Lattnerfda18db2008-07-26 01:18:38 +0000588 if (TypeRep == 0)
589 goto DoneWithDeclSpec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000590
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000591 // C++: If the identifier is actually the name of the class type
592 // being defined and the next token is a '(', then this is a
593 // constructor declaration. We're done with the decl-specifiers
594 // and will treat this token as an identifier.
595 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000596 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000597 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
598 NextToken().getKind() == tok::l_paren)
599 goto DoneWithDeclSpec;
600
Douglas Gregora60c62e2009-02-09 15:09:02 +0000601 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000602 TypeRep);
603 if (isInvalid)
604 break;
605
606 DS.SetRangeEnd(Tok.getLocation());
607 ConsumeToken(); // The identifier
608
609 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
610 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
611 // Objective-C interface. If we don't have Objective-C or a '<', this is
612 // just a normal reference to a typedef name.
613 if (!Tok.is(tok::less) || !getLang().ObjC1)
614 continue;
615
616 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000617 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000618 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000619 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000620
621 DS.SetRangeEnd(EndProtoLoc);
622
Steve Narofff7683302008-09-22 10:28:57 +0000623 // Need to support trailing type qualifiers (e.g. "id<p> const").
624 // If a type specifier follows, it will be diagnosed elsewhere.
625 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000626 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000627
628 // type-name
629 case tok::annot_template_id: {
630 TemplateIdAnnotation *TemplateId
631 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
632 if (TemplateId->Kind != TNK_Class_template) {
633 // This template-id does not refer to a type name, so we're
634 // done with the type-specifiers.
635 goto DoneWithDeclSpec;
636 }
637
638 // Turn the template-id annotation token into a type annotation
639 // token, then try again to parse it as a type-specifier.
640 if (AnnotateTemplateIdTokenAsType())
641 DS.SetTypeSpecError();
642
643 continue;
644 }
645
Chris Lattner4b009652007-07-25 00:24:17 +0000646 // GNU attributes support.
647 case tok::kw___attribute:
648 DS.AddAttributes(ParseAttributes());
649 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000650
651 // Microsoft declspec support.
652 case tok::kw___declspec:
653 if (!PP.getLangOptions().Microsoft)
654 goto DoneWithDeclSpec;
655 FuzzyParseMicrosoftDeclSpec();
656 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000657
Steve Naroffedd04d52008-12-25 14:16:32 +0000658 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000659 case tok::kw___forceinline:
660 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000661 case tok::kw___cdecl:
662 case tok::kw___stdcall:
663 case tok::kw___fastcall:
664 if (!PP.getLangOptions().Microsoft)
665 goto DoneWithDeclSpec;
666 // Just ignore it.
667 break;
668
Chris Lattner4b009652007-07-25 00:24:17 +0000669 // storage-class-specifier
670 case tok::kw_typedef:
671 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
672 break;
673 case tok::kw_extern:
674 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000675 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000676 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
677 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000678 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000679 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
680 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000681 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000682 case tok::kw_static:
683 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000684 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000685 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
686 break;
687 case tok::kw_auto:
688 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
689 break;
690 case tok::kw_register:
691 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
692 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000693 case tok::kw_mutable:
694 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
695 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000696 case tok::kw___thread:
697 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
698 break;
699
Chris Lattner4b009652007-07-25 00:24:17 +0000700 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000701
Chris Lattner4b009652007-07-25 00:24:17 +0000702 // function-specifier
703 case tok::kw_inline:
704 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
705 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000706 case tok::kw_virtual:
707 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
708 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000709 case tok::kw_explicit:
710 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
711 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000712
713 // type-specifier
714 case tok::kw_short:
715 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
716 break;
717 case tok::kw_long:
718 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
719 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
720 else
721 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
722 break;
723 case tok::kw_signed:
724 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
725 break;
726 case tok::kw_unsigned:
727 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
728 break;
729 case tok::kw__Complex:
730 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
731 break;
732 case tok::kw__Imaginary:
733 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
734 break;
735 case tok::kw_void:
736 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
737 break;
738 case tok::kw_char:
739 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
740 break;
741 case tok::kw_int:
742 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
743 break;
744 case tok::kw_float:
745 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
746 break;
747 case tok::kw_double:
748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
749 break;
750 case tok::kw_wchar_t:
751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
752 break;
753 case tok::kw_bool:
754 case tok::kw__Bool:
755 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
756 break;
757 case tok::kw__Decimal32:
758 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
759 break;
760 case tok::kw__Decimal64:
761 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
762 break;
763 case tok::kw__Decimal128:
764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
765 break;
766
767 // class-specifier:
768 case tok::kw_class:
769 case tok::kw_struct:
770 case tok::kw_union:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000771 ParseClassSpecifier(DS, TemplateParams, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000772 continue;
773
774 // enum-specifier:
775 case tok::kw_enum:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000776 ParseEnumSpecifier(DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000777 continue;
778
779 // cv-qualifier:
780 case tok::kw_const:
781 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
782 break;
783 case tok::kw_volatile:
784 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
785 getLang())*2;
786 break;
787 case tok::kw_restrict:
788 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
789 getLang())*2;
790 break;
791
Douglas Gregord3022602009-03-27 23:10:48 +0000792 // C++ typename-specifier:
793 case tok::kw_typename:
794 if (TryAnnotateTypeOrScopeToken())
795 continue;
796 break;
797
Chris Lattnerc297b722009-01-21 19:48:37 +0000798 // GNU typeof support.
799 case tok::kw_typeof:
800 ParseTypeofSpecifier(DS);
801 continue;
802
Steve Naroff5f0466b2008-06-05 00:02:44 +0000803 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000804 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000805 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
806 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000807 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000808 goto DoneWithDeclSpec;
809
810 {
811 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000812 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000813 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000814 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000815 DS.SetRangeEnd(EndProtoLoc);
816
Chris Lattnerf006a222008-11-18 07:48:38 +0000817 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
818 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000819 // Need to support trailing type qualifiers (e.g. "id<p> const").
820 // If a type specifier follows, it will be diagnosed elsewhere.
821 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000822 }
Chris Lattner4b009652007-07-25 00:24:17 +0000823 }
824 // If the specifier combination wasn't legal, issue a diagnostic.
825 if (isInvalid) {
826 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000827 // Pick between error or extwarn.
828 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
829 : diag::ext_duplicate_declspec;
830 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000831 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000832 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000833 ConsumeToken();
834 }
835}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000836
Chris Lattnerd706dc82009-01-06 06:59:53 +0000837/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000838/// primarily follow the C++ grammar with additions for C99 and GNU,
839/// which together subsume the C grammar. Note that the C++
840/// type-specifier also includes the C type-qualifier (for const,
841/// volatile, and C99 restrict). Returns true if a type-specifier was
842/// found (and parsed), false otherwise.
843///
844/// type-specifier: [C++ 7.1.5]
845/// simple-type-specifier
846/// class-specifier
847/// enum-specifier
848/// elaborated-type-specifier [TODO]
849/// cv-qualifier
850///
851/// cv-qualifier: [C++ 7.1.5.1]
852/// 'const'
853/// 'volatile'
854/// [C99] 'restrict'
855///
856/// simple-type-specifier: [ C++ 7.1.5.2]
857/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
858/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
859/// 'char'
860/// 'wchar_t'
861/// 'bool'
862/// 'short'
863/// 'int'
864/// 'long'
865/// 'signed'
866/// 'unsigned'
867/// 'float'
868/// 'double'
869/// 'void'
870/// [C99] '_Bool'
871/// [C99] '_Complex'
872/// [C99] '_Imaginary' // Removed in TC2?
873/// [GNU] '_Decimal32'
874/// [GNU] '_Decimal64'
875/// [GNU] '_Decimal128'
876/// [GNU] typeof-specifier
877/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
878/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000879bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
880 const char *&PrevSpec,
881 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000882 SourceLocation Loc = Tok.getLocation();
883
884 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000885 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +0000886 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +0000887 // Annotate typenames and C++ scope specifiers. If we get one, just
888 // recurse to handle whatever we get.
889 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000890 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000891 // Otherwise, not a type specifier.
892 return false;
893 case tok::coloncolon: // ::foo::bar
894 if (NextToken().is(tok::kw_new) || // ::new
895 NextToken().is(tok::kw_delete)) // ::delete
896 return false;
897
898 // Annotate typenames and C++ scope specifiers. If we get one, just
899 // recurse to handle whatever we get.
900 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000901 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000902 // Otherwise, not a type specifier.
903 return false;
904
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000905 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000906 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000907 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000908 Tok.getAnnotationValue());
909 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
910 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000911
912 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
913 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
914 // Objective-C interface. If we don't have Objective-C or a '<', this is
915 // just a normal reference to a typedef name.
916 if (!Tok.is(tok::less) || !getLang().ObjC1)
917 return true;
918
919 SourceLocation EndProtoLoc;
920 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
921 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
922 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
923
924 DS.SetRangeEnd(EndProtoLoc);
925 return true;
926 }
927
928 case tok::kw_short:
929 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
930 break;
931 case tok::kw_long:
932 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
933 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
934 else
935 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
936 break;
937 case tok::kw_signed:
938 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
939 break;
940 case tok::kw_unsigned:
941 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
942 break;
943 case tok::kw__Complex:
944 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
945 break;
946 case tok::kw__Imaginary:
947 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
948 break;
949 case tok::kw_void:
950 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
951 break;
952 case tok::kw_char:
953 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
954 break;
955 case tok::kw_int:
956 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
957 break;
958 case tok::kw_float:
959 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
960 break;
961 case tok::kw_double:
962 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
963 break;
964 case tok::kw_wchar_t:
965 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
966 break;
967 case tok::kw_bool:
968 case tok::kw__Bool:
969 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
970 break;
971 case tok::kw__Decimal32:
972 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
973 break;
974 case tok::kw__Decimal64:
975 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
976 break;
977 case tok::kw__Decimal128:
978 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
979 break;
980
981 // class-specifier:
982 case tok::kw_class:
983 case tok::kw_struct:
984 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000985 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000986 return true;
987
988 // enum-specifier:
989 case tok::kw_enum:
990 ParseEnumSpecifier(DS);
991 return true;
992
993 // cv-qualifier:
994 case tok::kw_const:
995 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
996 getLang())*2;
997 break;
998 case tok::kw_volatile:
999 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1000 getLang())*2;
1001 break;
1002 case tok::kw_restrict:
1003 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1004 getLang())*2;
1005 break;
1006
1007 // GNU typeof support.
1008 case tok::kw_typeof:
1009 ParseTypeofSpecifier(DS);
1010 return true;
1011
Steve Naroffedd04d52008-12-25 14:16:32 +00001012 case tok::kw___cdecl:
1013 case tok::kw___stdcall:
1014 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001015 if (!PP.getLangOptions().Microsoft) return false;
1016 ConsumeToken();
1017 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001018
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001019 default:
1020 // Not a type-specifier; do nothing.
1021 return false;
1022 }
1023
1024 // If the specifier combination wasn't legal, issue a diagnostic.
1025 if (isInvalid) {
1026 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001027 // Pick between error or extwarn.
1028 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1029 : diag::ext_duplicate_declspec;
1030 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001031 }
1032 DS.SetRangeEnd(Tok.getLocation());
1033 ConsumeToken(); // whatever we parsed above.
1034 return true;
1035}
Chris Lattner4b009652007-07-25 00:24:17 +00001036
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001037/// ParseStructDeclaration - Parse a struct declaration without the terminating
1038/// semicolon.
1039///
Chris Lattner4b009652007-07-25 00:24:17 +00001040/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001041/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001042/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001043/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001044/// struct-declarator-list:
1045/// struct-declarator
1046/// struct-declarator-list ',' struct-declarator
1047/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1048/// struct-declarator:
1049/// declarator
1050/// [GNU] declarator attributes[opt]
1051/// declarator[opt] ':' constant-expression
1052/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1053///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001054void Parser::
1055ParseStructDeclaration(DeclSpec &DS,
1056 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001057 if (Tok.is(tok::kw___extension__)) {
1058 // __extension__ silences extension warnings in the subexpression.
1059 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001060 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001061 return ParseStructDeclaration(DS, Fields);
1062 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001063
1064 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001065 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001066 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001067
Douglas Gregorb748fc52009-01-12 22:49:06 +00001068 // If there are no declarators, this is a free-standing declaration
1069 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001070 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001071 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001072 return;
1073 }
1074
1075 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001076 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001077 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001078 FieldDeclarator &DeclaratorInfo = Fields.back();
1079
Steve Naroffa9adf112007-08-20 22:28:22 +00001080 /// struct-declarator: declarator
1081 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001082 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001083 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001084
Chris Lattner34a01ad2007-10-09 17:33:22 +00001085 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001086 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001087 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001088 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001089 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001090 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001091 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001092 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001093
Steve Naroffa9adf112007-08-20 22:28:22 +00001094 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001095 if (Tok.is(tok::kw___attribute)) {
1096 SourceLocation Loc;
1097 AttributeList *AttrList = ParseAttributes(&Loc);
1098 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1099 }
1100
Steve Naroffa9adf112007-08-20 22:28:22 +00001101 // If we don't have a comma, it is either the end of the list (a ';')
1102 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001103 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001104 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001105
Steve Naroffa9adf112007-08-20 22:28:22 +00001106 // Consume the comma.
1107 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001108
Steve Naroffa9adf112007-08-20 22:28:22 +00001109 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001110 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001111
Steve Naroffa9adf112007-08-20 22:28:22 +00001112 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001113 if (Tok.is(tok::kw___attribute)) {
1114 SourceLocation Loc;
1115 AttributeList *AttrList = ParseAttributes(&Loc);
1116 Fields.back().D.AddAttributes(AttrList, Loc);
1117 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001118 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001119}
1120
1121/// ParseStructUnionBody
1122/// struct-contents:
1123/// struct-declaration-list
1124/// [EXT] empty
1125/// [GNU] "struct-declaration-list" without terminatoring ';'
1126/// struct-declaration-list:
1127/// struct-declaration
1128/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001129/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001130///
Chris Lattner4b009652007-07-25 00:24:17 +00001131void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1132 unsigned TagType, DeclTy *TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001133 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1134 PP.getSourceManager(),
1135 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001136
Chris Lattner4b009652007-07-25 00:24:17 +00001137 SourceLocation LBraceLoc = ConsumeBrace();
1138
Douglas Gregorcab994d2009-01-09 22:42:13 +00001139 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001140 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1141
Chris Lattner4b009652007-07-25 00:24:17 +00001142 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1143 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001144 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001145 Diag(Tok, diag::ext_empty_struct_union_enum)
1146 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001147
1148 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001149 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1150
Chris Lattner4b009652007-07-25 00:24:17 +00001151 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001152 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001153 // Each iteration of this loop reads one struct-declaration.
1154
1155 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001156 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001157 Diag(Tok, diag::ext_extra_struct_semi);
1158 ConsumeToken();
1159 continue;
1160 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001161
1162 // Parse all the comma separated declarators.
1163 DeclSpec DS;
1164 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001165 if (!Tok.is(tok::at)) {
1166 ParseStructDeclaration(DS, FieldDeclarators);
1167
1168 // Convert them all to fields.
1169 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1170 FieldDeclarator &FD = FieldDeclarators[i];
1171 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +00001172 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +00001173 DS.getSourceRange().getBegin(),
1174 FD.D, FD.BitfieldSize);
1175 FieldDecls.push_back(Field);
1176 }
1177 } else { // Handle @defs
1178 ConsumeToken();
1179 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1180 Diag(Tok, diag::err_unexpected_at);
1181 SkipUntil(tok::semi, true, true);
1182 continue;
1183 }
1184 ConsumeToken();
1185 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1186 if (!Tok.is(tok::identifier)) {
1187 Diag(Tok, diag::err_expected_ident);
1188 SkipUntil(tok::semi, true, true);
1189 continue;
1190 }
1191 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001192 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1193 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001194 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1195 ConsumeToken();
1196 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1197 }
Chris Lattner4b009652007-07-25 00:24:17 +00001198
Chris Lattner34a01ad2007-10-09 17:33:22 +00001199 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001200 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001201 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001202 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001203 break;
1204 } else {
1205 Diag(Tok, diag::err_expected_semi_decl_list);
1206 // Skip to end of block or statement
1207 SkipUntil(tok::r_brace, true, true);
1208 }
1209 }
1210
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001211 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001212
Chris Lattner4b009652007-07-25 00:24:17 +00001213 AttributeList *AttrList = 0;
1214 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001215 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001216 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001217
1218 Actions.ActOnFields(CurScope,
1219 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1220 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001221 AttrList);
1222 StructScope.Exit();
1223 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001224}
1225
1226
1227/// ParseEnumSpecifier
1228/// enum-specifier: [C99 6.7.2.2]
1229/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001230///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001231/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1232/// '}' attributes[opt]
1233/// 'enum' identifier
1234/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001235///
1236/// [C++] elaborated-type-specifier:
1237/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1238///
Douglas Gregor0c793bb2009-03-25 22:00:53 +00001239void Parser::ParseEnumSpecifier(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001240 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001241 SourceLocation StartLoc = ConsumeToken();
1242
1243 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001244
1245 AttributeList *Attr = 0;
1246 // If attributes exist after tag, parse them.
1247 if (Tok.is(tok::kw___attribute))
1248 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001249
1250 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001251 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001252 if (Tok.isNot(tok::identifier)) {
1253 Diag(Tok, diag::err_expected_ident);
1254 if (Tok.isNot(tok::l_brace)) {
1255 // Has no name and is not a definition.
1256 // Skip the rest of this declarator, up until the comma or semicolon.
1257 SkipUntil(tok::comma, true);
1258 return;
1259 }
1260 }
1261 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001262
1263 // Must have either 'enum name' or 'enum {...}'.
1264 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1265 Diag(Tok, diag::err_expected_ident_lbrace);
1266
1267 // Skip the rest of this declarator, up until the comma or semicolon.
1268 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001269 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001270 }
1271
1272 // If an identifier is present, consume and remember it.
1273 IdentifierInfo *Name = 0;
1274 SourceLocation NameLoc;
1275 if (Tok.is(tok::identifier)) {
1276 Name = Tok.getIdentifierInfo();
1277 NameLoc = ConsumeToken();
1278 }
1279
1280 // There are three options here. If we have 'enum foo;', then this is a
1281 // forward declaration. If we have 'enum foo {...' then this is a
1282 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1283 //
1284 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1285 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1286 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1287 //
1288 Action::TagKind TK;
1289 if (Tok.is(tok::l_brace))
1290 TK = Action::TK_Definition;
1291 else if (Tok.is(tok::semi))
1292 TK = Action::TK_Declaration;
1293 else
1294 TK = Action::TK_Reference;
1295 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregor0c793bb2009-03-25 22:00:53 +00001296 SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001297
Chris Lattner34a01ad2007-10-09 17:33:22 +00001298 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001299 ParseEnumBody(StartLoc, TagDecl);
1300
1301 // TODO: semantic analysis on the declspec for enums.
1302 const char *PrevSpec = 0;
1303 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001304 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001305}
1306
1307/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1308/// enumerator-list:
1309/// enumerator
1310/// enumerator-list ',' enumerator
1311/// enumerator:
1312/// enumeration-constant
1313/// enumeration-constant '=' constant-expression
1314/// enumeration-constant:
1315/// identifier
1316///
1317void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001318 // Enter the scope of the enum body and start the definition.
1319 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001320 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001321
Chris Lattner4b009652007-07-25 00:24:17 +00001322 SourceLocation LBraceLoc = ConsumeBrace();
1323
Chris Lattnerc9a92452007-08-27 17:24:30 +00001324 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001325 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001326 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001327
1328 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1329
1330 DeclTy *LastEnumConstDecl = 0;
1331
1332 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001333 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001334 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1335 SourceLocation IdentLoc = ConsumeToken();
1336
1337 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001338 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001339 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001340 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001341 AssignedVal = ParseConstantExpression();
1342 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001343 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001344 }
1345
1346 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001347 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001348 LastEnumConstDecl,
1349 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001350 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001351 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001352 EnumConstantDecls.push_back(EnumConstDecl);
1353 LastEnumConstDecl = EnumConstDecl;
1354
Chris Lattner34a01ad2007-10-09 17:33:22 +00001355 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001356 break;
1357 SourceLocation CommaLoc = ConsumeToken();
1358
Chris Lattner34a01ad2007-10-09 17:33:22 +00001359 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001360 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1361 }
1362
1363 // Eat the }.
1364 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1365
Steve Naroff0acc9c92007-09-15 18:49:24 +00001366 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001367 EnumConstantDecls.size());
1368
1369 DeclTy *AttrList = 0;
1370 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001371 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001372 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001373
1374 EnumScope.Exit();
1375 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001376}
1377
1378/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001379/// start of a type-qualifier-list.
1380bool Parser::isTypeQualifier() const {
1381 switch (Tok.getKind()) {
1382 default: return false;
1383 // type-qualifier
1384 case tok::kw_const:
1385 case tok::kw_volatile:
1386 case tok::kw_restrict:
1387 return true;
1388 }
1389}
1390
1391/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001392/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001393bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001394 switch (Tok.getKind()) {
1395 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001396
1397 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001398 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001399 // Annotate typenames and C++ scope specifiers. If we get one, just
1400 // recurse to handle whatever we get.
1401 if (TryAnnotateTypeOrScopeToken())
1402 return isTypeSpecifierQualifier();
1403 // Otherwise, not a type specifier.
1404 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001405
Chris Lattnerb75fde62009-01-04 23:41:41 +00001406 case tok::coloncolon: // ::foo::bar
1407 if (NextToken().is(tok::kw_new) || // ::new
1408 NextToken().is(tok::kw_delete)) // ::delete
1409 return false;
1410
1411 // Annotate typenames and C++ scope specifiers. If we get one, just
1412 // recurse to handle whatever we get.
1413 if (TryAnnotateTypeOrScopeToken())
1414 return isTypeSpecifierQualifier();
1415 // Otherwise, not a type specifier.
1416 return false;
1417
Chris Lattner4b009652007-07-25 00:24:17 +00001418 // GNU attributes support.
1419 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001420 // GNU typeof support.
1421 case tok::kw_typeof:
1422
Chris Lattner4b009652007-07-25 00:24:17 +00001423 // type-specifiers
1424 case tok::kw_short:
1425 case tok::kw_long:
1426 case tok::kw_signed:
1427 case tok::kw_unsigned:
1428 case tok::kw__Complex:
1429 case tok::kw__Imaginary:
1430 case tok::kw_void:
1431 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001432 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001433 case tok::kw_int:
1434 case tok::kw_float:
1435 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001436 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001437 case tok::kw__Bool:
1438 case tok::kw__Decimal32:
1439 case tok::kw__Decimal64:
1440 case tok::kw__Decimal128:
1441
Chris Lattner2e78db32008-04-13 18:59:07 +00001442 // struct-or-union-specifier (C99) or class-specifier (C++)
1443 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001444 case tok::kw_struct:
1445 case tok::kw_union:
1446 // enum-specifier
1447 case tok::kw_enum:
1448
1449 // type-qualifier
1450 case tok::kw_const:
1451 case tok::kw_volatile:
1452 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001453
1454 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001455 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001456 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001457
1458 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1459 case tok::less:
1460 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001461
1462 case tok::kw___cdecl:
1463 case tok::kw___stdcall:
1464 case tok::kw___fastcall:
1465 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001466 }
1467}
1468
1469/// isDeclarationSpecifier() - Return true if the current token is part of a
1470/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001471bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001472 switch (Tok.getKind()) {
1473 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001474
1475 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001476 // Unfortunate hack to support "Class.factoryMethod" notation.
1477 if (getLang().ObjC1 && NextToken().is(tok::period))
1478 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001479 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001480
Douglas Gregord3022602009-03-27 23:10:48 +00001481 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001482 // Annotate typenames and C++ scope specifiers. If we get one, just
1483 // recurse to handle whatever we get.
1484 if (TryAnnotateTypeOrScopeToken())
1485 return isDeclarationSpecifier();
1486 // Otherwise, not a declaration specifier.
1487 return false;
1488 case tok::coloncolon: // ::foo::bar
1489 if (NextToken().is(tok::kw_new) || // ::new
1490 NextToken().is(tok::kw_delete)) // ::delete
1491 return false;
1492
1493 // Annotate typenames and C++ scope specifiers. If we get one, just
1494 // recurse to handle whatever we get.
1495 if (TryAnnotateTypeOrScopeToken())
1496 return isDeclarationSpecifier();
1497 // Otherwise, not a declaration specifier.
1498 return false;
1499
Chris Lattner4b009652007-07-25 00:24:17 +00001500 // storage-class-specifier
1501 case tok::kw_typedef:
1502 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001503 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001504 case tok::kw_static:
1505 case tok::kw_auto:
1506 case tok::kw_register:
1507 case tok::kw___thread:
1508
1509 // type-specifiers
1510 case tok::kw_short:
1511 case tok::kw_long:
1512 case tok::kw_signed:
1513 case tok::kw_unsigned:
1514 case tok::kw__Complex:
1515 case tok::kw__Imaginary:
1516 case tok::kw_void:
1517 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001518 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001519 case tok::kw_int:
1520 case tok::kw_float:
1521 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001522 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001523 case tok::kw__Bool:
1524 case tok::kw__Decimal32:
1525 case tok::kw__Decimal64:
1526 case tok::kw__Decimal128:
1527
Chris Lattner2e78db32008-04-13 18:59:07 +00001528 // struct-or-union-specifier (C99) or class-specifier (C++)
1529 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001530 case tok::kw_struct:
1531 case tok::kw_union:
1532 // enum-specifier
1533 case tok::kw_enum:
1534
1535 // type-qualifier
1536 case tok::kw_const:
1537 case tok::kw_volatile:
1538 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001539
Chris Lattner4b009652007-07-25 00:24:17 +00001540 // function-specifier
1541 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001542 case tok::kw_virtual:
1543 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001544
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001545 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001546 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001547
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001548 // GNU typeof support.
1549 case tok::kw_typeof:
1550
1551 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001552 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001553 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001554
1555 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1556 case tok::less:
1557 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001558
Steve Naroffab1a3632009-01-06 19:34:12 +00001559 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001560 case tok::kw___cdecl:
1561 case tok::kw___stdcall:
1562 case tok::kw___fastcall:
1563 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001564 }
1565}
1566
1567
1568/// ParseTypeQualifierListOpt
1569/// type-qualifier-list: [C99 6.7.5]
1570/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001571/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001572/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001573/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001574///
Chris Lattner460696f2008-12-18 07:02:59 +00001575void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001576 while (1) {
1577 int isInvalid = false;
1578 const char *PrevSpec = 0;
1579 SourceLocation Loc = Tok.getLocation();
1580
1581 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001582 case tok::kw_const:
1583 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1584 getLang())*2;
1585 break;
1586 case tok::kw_volatile:
1587 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1588 getLang())*2;
1589 break;
1590 case tok::kw_restrict:
1591 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1592 getLang())*2;
1593 break;
Steve Naroffad620402008-12-25 14:41:26 +00001594 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001595 case tok::kw___cdecl:
1596 case tok::kw___stdcall:
1597 case tok::kw___fastcall:
1598 if (!PP.getLangOptions().Microsoft)
1599 goto DoneWithTypeQuals;
1600 // Just ignore it.
1601 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001602 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001603 if (AttributesAllowed) {
1604 DS.AddAttributes(ParseAttributes());
1605 continue; // do *not* consume the next token!
1606 }
1607 // otherwise, FALL THROUGH!
1608 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001609 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001610 // If this is not a type-qualifier token, we're done reading type
1611 // qualifiers. First verify that DeclSpec's are consistent.
1612 DS.Finish(Diags, PP.getSourceManager(), getLang());
1613 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001614 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001615
Chris Lattner4b009652007-07-25 00:24:17 +00001616 // If the specifier combination wasn't legal, issue a diagnostic.
1617 if (isInvalid) {
1618 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001619 // Pick between error or extwarn.
1620 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1621 : diag::ext_duplicate_declspec;
1622 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001623 }
1624 ConsumeToken();
1625 }
1626}
1627
1628
1629/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1630///
1631void Parser::ParseDeclarator(Declarator &D) {
1632 /// This implements the 'declarator' production in the C grammar, then checks
1633 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001634 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001635}
1636
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001637/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1638/// is parsed by the function passed to it. Pass null, and the direct-declarator
1639/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001640/// ptr-operator production.
1641///
Sebastian Redl75555032009-01-24 21:16:55 +00001642/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1643/// [C] pointer[opt] direct-declarator
1644/// [C++] direct-declarator
1645/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001646///
1647/// pointer: [C99 6.7.5]
1648/// '*' type-qualifier-list[opt]
1649/// '*' type-qualifier-list[opt] pointer
1650///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001651/// ptr-operator:
1652/// '*' cv-qualifier-seq[opt]
1653/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001654/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001655/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001656/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001657/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001658void Parser::ParseDeclaratorInternal(Declarator &D,
1659 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001660
Sebastian Redl75555032009-01-24 21:16:55 +00001661 // C++ member pointers start with a '::' or a nested-name.
1662 // Member pointers get special handling, since there's no place for the
1663 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001664 if (getLang().CPlusPlus &&
1665 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1666 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001667 CXXScopeSpec SS;
1668 if (ParseOptionalCXXScopeSpecifier(SS)) {
1669 if(Tok.isNot(tok::star)) {
1670 // The scope spec really belongs to the direct-declarator.
1671 D.getCXXScopeSpec() = SS;
1672 if (DirectDeclParser)
1673 (this->*DirectDeclParser)(D);
1674 return;
1675 }
1676
1677 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001678 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001679 DeclSpec DS;
1680 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001681 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001682
1683 // Recurse to parse whatever is left.
1684 ParseDeclaratorInternal(D, DirectDeclParser);
1685
1686 // Sema will have to catch (syntactically invalid) pointers into global
1687 // scope. It has to catch pointers into namespace scope anyway.
1688 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001689 Loc, DS.TakeAttributes()),
1690 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001691 return;
1692 }
1693 }
1694
1695 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001696 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001697 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001698 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001699 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001700 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001701 if (DirectDeclParser)
1702 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001703 return;
1704 }
Sebastian Redl75555032009-01-24 21:16:55 +00001705
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001706 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1707 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001708 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001709 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001710
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001711 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001712 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001713 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001714
Chris Lattner4b009652007-07-25 00:24:17 +00001715 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001716 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001717
Chris Lattner4b009652007-07-25 00:24:17 +00001718 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001719 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001720 if (Kind == tok::star)
1721 // Remember that we parsed a pointer type, and remember the type-quals.
1722 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001723 DS.TakeAttributes()),
1724 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001725 else
1726 // Remember that we parsed a Block type, and remember the type-quals.
1727 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001728 Loc),
1729 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001730 } else {
1731 // Is a reference
1732 DeclSpec DS;
1733
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001734 // Complain about rvalue references in C++03, but then go on and build
1735 // the declarator.
1736 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1737 Diag(Loc, diag::err_rvalue_reference);
1738
Chris Lattner4b009652007-07-25 00:24:17 +00001739 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1740 // cv-qualifiers are introduced through the use of a typedef or of a
1741 // template type argument, in which case the cv-qualifiers are ignored.
1742 //
1743 // [GNU] Retricted references are allowed.
1744 // [GNU] Attributes on references are allowed.
1745 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001746 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001747
1748 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1749 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1750 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001751 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001752 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1753 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001754 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001755 }
1756
1757 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001758 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001759
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001760 if (D.getNumTypeObjects() > 0) {
1761 // C++ [dcl.ref]p4: There shall be no references to references.
1762 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1763 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001764 if (const IdentifierInfo *II = D.getIdentifier())
1765 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1766 << II;
1767 else
1768 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1769 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001770
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001771 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001772 // can go ahead and build the (technically ill-formed)
1773 // declarator: reference collapsing will take care of it.
1774 }
1775 }
1776
Chris Lattner4b009652007-07-25 00:24:17 +00001777 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001778 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001779 DS.TakeAttributes(),
1780 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001781 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001782 }
1783}
1784
1785/// ParseDirectDeclarator
1786/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001787/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001788/// '(' declarator ')'
1789/// [GNU] '(' attributes declarator ')'
1790/// [C90] direct-declarator '[' constant-expression[opt] ']'
1791/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1792/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1793/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1794/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1795/// direct-declarator '(' parameter-type-list ')'
1796/// direct-declarator '(' identifier-list[opt] ')'
1797/// [GNU] direct-declarator '(' parameter-forward-declarations
1798/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001799/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1800/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001801/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001802///
1803/// declarator-id: [C++ 8]
1804/// id-expression
1805/// '::'[opt] nested-name-specifier[opt] type-name
1806///
1807/// id-expression: [C++ 5.1]
1808/// unqualified-id
1809/// qualified-id [TODO]
1810///
1811/// unqualified-id: [C++ 5.1]
1812/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001813/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001814/// conversion-function-id [TODO]
1815/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001816/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001817///
Chris Lattner4b009652007-07-25 00:24:17 +00001818void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001819 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001820
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001821 if (getLang().CPlusPlus) {
1822 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001823 // ParseDeclaratorInternal might already have parsed the scope.
1824 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1825 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001826 if (afterCXXScope) {
1827 // Change the declaration context for name lookup, until this function
1828 // is exited (and the declarator has been parsed).
1829 DeclScopeObj.EnterDeclaratorScope();
1830 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001831
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001832 if (Tok.is(tok::identifier)) {
1833 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001834
Douglas Gregor2fa10442008-12-18 19:37:40 +00001835 // If this identifier is the name of the current class, it's a
1836 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001837 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001838 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001839 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001840 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001841 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001842 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001843 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1844 ConsumeToken();
1845 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001846 } else if (Tok.is(tok::annot_template_id)) {
1847 TemplateIdAnnotation *TemplateId
1848 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1849
1850 // FIXME: Could this template-id name a constructor?
1851
1852 // FIXME: This is an egregious hack, where we silently ignore
1853 // the specialization (which should be a function template
1854 // specialization name) and use the name instead. This hack
1855 // will go away when we have support for function
1856 // specializations.
1857 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1858 TemplateId->Destroy();
1859 ConsumeToken();
1860 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001861 } else if (Tok.is(tok::kw_operator)) {
1862 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001863 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001864
Douglas Gregor853dd392008-12-26 15:00:45 +00001865 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001866 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1867 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001868 } else {
1869 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001870 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1871 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1872 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001873 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001874 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001875 }
1876 goto PastIdentifier;
1877 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001878 // This should be a C++ destructor.
1879 SourceLocation TildeLoc = ConsumeToken();
1880 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001881 // FIXME: Inaccurate.
1882 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001883 SourceLocation EndLoc;
1884 if (TypeTy *Type = ParseClassName(EndLoc)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001885 D.setDestructor(Type, TildeLoc, NameLoc);
1886 } else {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001887 D.SetIdentifier(0, TildeLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001888 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001889 } else {
1890 Diag(Tok, diag::err_expected_class_name);
1891 D.SetIdentifier(0, TildeLoc);
1892 }
1893 goto PastIdentifier;
1894 }
1895
1896 // If we reached this point, token is not identifier and not '~'.
1897
1898 if (afterCXXScope) {
1899 Diag(Tok, diag::err_expected_unqualified_id);
1900 D.SetIdentifier(0, Tok.getLocation());
1901 D.setInvalidType(true);
1902 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001903 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001904 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001905 }
1906
1907 // If we reached this point, we are either in C/ObjC or the token didn't
1908 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001909 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1910 assert(!getLang().CPlusPlus &&
1911 "There's a C++-specific check for tok::identifier above");
1912 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1913 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1914 ConsumeToken();
1915 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001916 // direct-declarator: '(' declarator ')'
1917 // direct-declarator: '(' attributes declarator ')'
1918 // Example: 'char (*X)' or 'int (*XX)(void)'
1919 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001920 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001921 // This could be something simple like "int" (in which case the declarator
1922 // portion is empty), if an abstract-declarator is allowed.
1923 D.SetIdentifier(0, Tok.getLocation());
1924 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00001925 if (D.getContext() == Declarator::MemberContext)
1926 Diag(Tok, diag::err_expected_member_name_or_semi)
1927 << D.getDeclSpec().getSourceRange();
1928 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001929 Diag(Tok, diag::err_expected_unqualified_id);
1930 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001931 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001932 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001933 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001934 }
1935
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001936 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001937 assert(D.isPastIdentifier() &&
1938 "Haven't past the location of the identifier yet?");
1939
1940 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001941 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001942 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1943 // In such a case, check if we actually have a function declarator; if it
1944 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001945 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1946 // When not in file scope, warn for ambiguous function declarators, just
1947 // in case the author intended it as a variable definition.
1948 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1949 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1950 break;
1951 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001952 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001953 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001954 ParseBracketDeclarator(D);
1955 } else {
1956 break;
1957 }
1958 }
1959}
1960
Chris Lattnera0d056d2008-04-06 05:45:57 +00001961/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1962/// only called before the identifier, so these are most likely just grouping
1963/// parens for precedence. If we find that these are actually function
1964/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1965///
1966/// direct-declarator:
1967/// '(' declarator ')'
1968/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001969/// direct-declarator '(' parameter-type-list ')'
1970/// direct-declarator '(' identifier-list[opt] ')'
1971/// [GNU] direct-declarator '(' parameter-forward-declarations
1972/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001973///
1974void Parser::ParseParenDeclarator(Declarator &D) {
1975 SourceLocation StartLoc = ConsumeParen();
1976 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1977
Chris Lattner1f185292008-10-20 02:05:46 +00001978 // Eat any attributes before we look at whether this is a grouping or function
1979 // declarator paren. If this is a grouping paren, the attribute applies to
1980 // the type being built up, for example:
1981 // int (__attribute__(()) *x)(long y)
1982 // If this ends up not being a grouping paren, the attribute applies to the
1983 // first argument, for example:
1984 // int (__attribute__(()) int x)
1985 // In either case, we need to eat any attributes to be able to determine what
1986 // sort of paren this is.
1987 //
1988 AttributeList *AttrList = 0;
1989 bool RequiresArg = false;
1990 if (Tok.is(tok::kw___attribute)) {
1991 AttrList = ParseAttributes();
1992
1993 // We require that the argument list (if this is a non-grouping paren) be
1994 // present even if the attribute list was empty.
1995 RequiresArg = true;
1996 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001997 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00001998 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1999 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00002000 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002001
Chris Lattnera0d056d2008-04-06 05:45:57 +00002002 // If we haven't past the identifier yet (or where the identifier would be
2003 // stored, if this is an abstract declarator), then this is probably just
2004 // grouping parens. However, if this could be an abstract-declarator, then
2005 // this could also be the start of function arguments (consider 'void()').
2006 bool isGrouping;
2007
2008 if (!D.mayOmitIdentifier()) {
2009 // If this can't be an abstract-declarator, this *must* be a grouping
2010 // paren, because we haven't seen the identifier yet.
2011 isGrouping = true;
2012 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002013 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002014 isDeclarationSpecifier()) { // 'int(int)' is a function.
2015 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2016 // considered to be a type, not a K&R identifier-list.
2017 isGrouping = false;
2018 } else {
2019 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2020 isGrouping = true;
2021 }
2022
2023 // If this is a grouping paren, handle:
2024 // direct-declarator: '(' declarator ')'
2025 // direct-declarator: '(' attributes declarator ')'
2026 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002027 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002028 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002029 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002030 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002031
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002032 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002033 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002034 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002035
2036 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002037 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002038 return;
2039 }
2040
2041 // Okay, if this wasn't a grouping paren, it must be the start of a function
2042 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002043 // identifier (and remember where it would have been), then call into
2044 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002045 D.SetIdentifier(0, Tok.getLocation());
2046
Chris Lattner1f185292008-10-20 02:05:46 +00002047 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002048}
2049
2050/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2051/// declarator D up to a paren, which indicates that we are parsing function
2052/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002053///
Chris Lattner1f185292008-10-20 02:05:46 +00002054/// If AttrList is non-null, then the caller parsed those arguments immediately
2055/// after the open paren - they should be considered to be the first argument of
2056/// a parameter. If RequiresArg is true, then the first argument of the
2057/// function is required to be present and required to not be an identifier
2058/// list.
2059///
Chris Lattner4b009652007-07-25 00:24:17 +00002060/// This method also handles this portion of the grammar:
2061/// parameter-type-list: [C99 6.7.5]
2062/// parameter-list
2063/// parameter-list ',' '...'
2064///
2065/// parameter-list: [C99 6.7.5]
2066/// parameter-declaration
2067/// parameter-list ',' parameter-declaration
2068///
2069/// parameter-declaration: [C99 6.7.5]
2070/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002071/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002072/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002073/// declaration-specifiers abstract-declarator[opt]
2074/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002075/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002076/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2077///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002078/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002079/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002080///
Chris Lattner1f185292008-10-20 02:05:46 +00002081void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2082 AttributeList *AttrList,
2083 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002084 // lparen is already consumed!
2085 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002086
Chris Lattner1f185292008-10-20 02:05:46 +00002087 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002088 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002089 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002090 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002091 delete AttrList;
2092 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002093
Sebastian Redl0c986032009-02-09 18:23:29 +00002094 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002095
2096 // cv-qualifier-seq[opt].
2097 DeclSpec DS;
2098 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002099 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002100 if (!DS.getSourceRange().getEnd().isInvalid())
2101 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002102
2103 // Parse exception-specification[opt].
2104 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002105 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002106 }
2107
Chris Lattner9f7564b2008-04-06 06:57:35 +00002108 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002109 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002110 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002111 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002112 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002113 /*arglist*/ 0, 0,
2114 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002115 LParenLoc, D),
2116 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002117 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002118 }
2119
2120 // Alternatively, this parameter list may be an identifier list form for a
2121 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002122 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002123 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002124 // K&R identifier lists can't have typedefs as identifiers, per
2125 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002126 if (RequiresArg) {
2127 Diag(Tok, diag::err_argument_required_after_attribute);
2128 delete AttrList;
2129 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002130 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2131 // normal declarators, not for abstract-declarators.
2132 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002133 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002134 }
2135
2136 // Finally, a normal, non-empty parameter type list.
2137
2138 // Build up an array of information about the parsed arguments.
2139 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002140
2141 // Enter function-declaration scope, limiting any declarators to the
2142 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002143 ParseScope PrototypeScope(this,
2144 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002145
2146 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002147 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002148 while (1) {
2149 if (Tok.is(tok::ellipsis)) {
2150 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002151 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002152 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002153 }
2154
Chris Lattner9f7564b2008-04-06 06:57:35 +00002155 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002156
Chris Lattner9f7564b2008-04-06 06:57:35 +00002157 // Parse the declaration-specifiers.
2158 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002159
2160 // If the caller parsed attributes for the first argument, add them now.
2161 if (AttrList) {
2162 DS.AddAttributes(AttrList);
2163 AttrList = 0; // Only apply the attributes to the first parameter.
2164 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002165 ParseDeclarationSpecifiers(DS);
2166
Chris Lattner9f7564b2008-04-06 06:57:35 +00002167 // Parse the declarator. This is "PrototypeContext", because we must
2168 // accept either 'declarator' or 'abstract-declarator' here.
2169 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2170 ParseDeclarator(ParmDecl);
2171
2172 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002173 if (Tok.is(tok::kw___attribute)) {
2174 SourceLocation Loc;
2175 AttributeList *AttrList = ParseAttributes(&Loc);
2176 ParmDecl.AddAttributes(AttrList, Loc);
2177 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002178
Chris Lattner9f7564b2008-04-06 06:57:35 +00002179 // Remember this parsed parameter in ParamInfo.
2180 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2181
Douglas Gregor605de8d2008-12-16 21:30:33 +00002182 // DefArgToks is used when the parsing of default arguments needs
2183 // to be delayed.
2184 CachedTokens *DefArgToks = 0;
2185
Chris Lattner9f7564b2008-04-06 06:57:35 +00002186 // If no parameter was specified, verify that *something* was specified,
2187 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002188 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2189 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002190 // Completely missing, emit error.
2191 Diag(DSStart, diag::err_missing_param);
2192 } else {
2193 // Otherwise, we have something. Add it and let semantic analysis try
2194 // to grok it and add the result to the ParamInfo we are building.
2195
2196 // Inform the actions module about the parameter declarator, so it gets
2197 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00002198 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2199
2200 // Parse the default argument, if any. We parse the default
2201 // arguments in all dialects; the semantic analysis in
2202 // ActOnParamDefaultArgument will reject the default argument in
2203 // C.
2204 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002205 SourceLocation EqualLoc = Tok.getLocation();
2206
Chris Lattner3e254fb2008-04-08 04:40:51 +00002207 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002208 if (D.getContext() == Declarator::MemberContext) {
2209 // If we're inside a class definition, cache the tokens
2210 // corresponding to the default argument. We'll actually parse
2211 // them when we see the end of the class definition.
2212 // FIXME: Templates will require something similar.
2213 // FIXME: Can we use a smart pointer for Toks?
2214 DefArgToks = new CachedTokens;
2215
2216 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2217 tok::semi, false)) {
2218 delete DefArgToks;
2219 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002220 Actions.ActOnParamDefaultArgumentError(Param);
2221 } else
2222 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002223 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002224 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002225 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002226
2227 OwningExprResult DefArgResult(ParseAssignmentExpression());
2228 if (DefArgResult.isInvalid()) {
2229 Actions.ActOnParamDefaultArgumentError(Param);
2230 SkipUntil(tok::comma, tok::r_paren, true, true);
2231 } else {
2232 // Inform the actions module about the default argument
2233 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002234 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002235 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002236 }
2237 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002238
2239 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002240 ParmDecl.getIdentifierLoc(), Param,
2241 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002242 }
2243
2244 // If the next token is a comma, consume it and keep reading arguments.
2245 if (Tok.isNot(tok::comma)) break;
2246
2247 // Consume the comma.
2248 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002249 }
2250
Chris Lattner9f7564b2008-04-06 06:57:35 +00002251 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002252 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002253
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002254 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002255 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002256
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002257 DeclSpec DS;
2258 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002259 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002260 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002261 if (!DS.getSourceRange().getEnd().isInvalid())
2262 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002263
2264 // Parse exception-specification[opt].
2265 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002266 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002267 }
2268
Chris Lattner4b009652007-07-25 00:24:17 +00002269 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002270 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002271 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002272 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002273 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002274 LParenLoc, D),
2275 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002276}
2277
Chris Lattner35d9c912008-04-06 06:34:08 +00002278/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2279/// we found a K&R-style identifier list instead of a type argument list. The
2280/// current token is known to be the first identifier in the list.
2281///
2282/// identifier-list: [C99 6.7.5]
2283/// identifier
2284/// identifier-list ',' identifier
2285///
2286void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2287 Declarator &D) {
2288 // Build up an array of information about the parsed arguments.
2289 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2290 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2291
2292 // If there was no identifier specified for the declarator, either we are in
2293 // an abstract-declarator, or we are in a parameter declarator which was found
2294 // to be abstract. In abstract-declarators, identifier lists are not valid:
2295 // diagnose this.
2296 if (!D.getIdentifier())
2297 Diag(Tok, diag::ext_ident_list_in_param);
2298
2299 // Tok is known to be the first identifier in the list. Remember this
2300 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002301 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002302 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2303 Tok.getLocation(), 0));
2304
Chris Lattner113a56b2008-04-06 06:39:19 +00002305 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002306
2307 while (Tok.is(tok::comma)) {
2308 // Eat the comma.
2309 ConsumeToken();
2310
Chris Lattner113a56b2008-04-06 06:39:19 +00002311 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002312 if (Tok.isNot(tok::identifier)) {
2313 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002314 SkipUntil(tok::r_paren);
2315 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002316 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002317
Chris Lattner35d9c912008-04-06 06:34:08 +00002318 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002319
2320 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002321 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002322 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002323
2324 // Verify that the argument identifier has not already been mentioned.
2325 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002326 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002327 } else {
2328 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002329 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2330 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00002331 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002332
2333 // Eat the identifier.
2334 ConsumeToken();
2335 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002336
2337 // If we have the closing ')', eat it and we're done.
2338 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2339
Chris Lattner113a56b2008-04-06 06:39:19 +00002340 // Remember that we parsed a function type, and remember the attributes. This
2341 // function type is always a K&R style function type, which is not varargs and
2342 // has no prototype.
2343 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002344 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002345 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002346 /*TypeQuals*/0, LParenLoc, D),
2347 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002348}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002349
Chris Lattner4b009652007-07-25 00:24:17 +00002350/// [C90] direct-declarator '[' constant-expression[opt] ']'
2351/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2352/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2353/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2354/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2355void Parser::ParseBracketDeclarator(Declarator &D) {
2356 SourceLocation StartLoc = ConsumeBracket();
2357
Chris Lattner1525c3a2008-12-18 07:27:21 +00002358 // C array syntax has many features, but by-far the most common is [] and [4].
2359 // This code does a fast path to handle some of the most obvious cases.
2360 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002361 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002362 // Remember that we parsed the empty array type.
2363 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002364 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2365 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002366 return;
2367 } else if (Tok.getKind() == tok::numeric_constant &&
2368 GetLookAheadToken(1).is(tok::r_square)) {
2369 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002370 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002371 ConsumeToken();
2372
Sebastian Redl0c986032009-02-09 18:23:29 +00002373 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002374
2375 // If there was an error parsing the assignment-expression, recover.
2376 if (ExprRes.isInvalid())
2377 ExprRes.release(); // Deallocate expr, just use [].
2378
2379 // Remember that we parsed a array type, and remember its features.
2380 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002381 ExprRes.release(), StartLoc),
2382 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002383 return;
2384 }
2385
Chris Lattner4b009652007-07-25 00:24:17 +00002386 // If valid, this location is the position where we read the 'static' keyword.
2387 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002388 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002389 StaticLoc = ConsumeToken();
2390
2391 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002392 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002393 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002394 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002395
2396 // If we haven't already read 'static', check to see if there is one after the
2397 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002398 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002399 StaticLoc = ConsumeToken();
2400
2401 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2402 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002403 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002404
2405 // Handle the case where we have '[*]' as the array size. However, a leading
2406 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2407 // the the token after the star is a ']'. Since stars in arrays are
2408 // infrequent, use of lookahead is not costly here.
2409 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002410 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002411
Chris Lattner306d4df2008-12-18 06:50:14 +00002412 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002413 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002414 StaticLoc = SourceLocation(); // Drop the static.
2415 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002416 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002417 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002418 // Note, in C89, this production uses the constant-expr production instead
2419 // of assignment-expr. The only difference is that assignment-expr allows
2420 // things like '=' and '*='. Sema rejects these in C89 mode because they
2421 // are not i-c-e's, so we don't need to distinguish between the two here.
2422
Chris Lattner4b009652007-07-25 00:24:17 +00002423 // Parse the assignment-expression now.
2424 NumElements = ParseAssignmentExpression();
2425 }
2426
2427 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002428 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002429 // If the expression was invalid, skip it.
2430 SkipUntil(tok::r_square);
2431 return;
2432 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002433
2434 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2435
Chris Lattner1525c3a2008-12-18 07:27:21 +00002436 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002437 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2438 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002439 NumElements.release(), StartLoc),
2440 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002441}
2442
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002443/// [GNU] typeof-specifier:
2444/// typeof ( expressions )
2445/// typeof ( type-name )
2446/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002447///
2448void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002449 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002450 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002451 SourceLocation StartLoc = ConsumeToken();
2452
Chris Lattner34a01ad2007-10-09 17:33:22 +00002453 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002454 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002455 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002456 return;
2457 }
2458
Sebastian Redl14ca7412008-12-11 21:36:32 +00002459 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002460 if (Result.isInvalid()) {
2461 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002462 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002463 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002464
2465 const char *PrevSpec = 0;
2466 // Check for duplicate type specifiers.
2467 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002468 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002469 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002470
2471 // FIXME: Not accurate, the range gets one token more than it should.
2472 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002473 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002474 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002475
Steve Naroff7cbb1462007-07-31 12:34:36 +00002476 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2477
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002478 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002479 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002480
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002481 assert((Ty.isInvalid() || Ty.get()) &&
2482 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002483
Chris Lattner34a01ad2007-10-09 17:33:22 +00002484 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002485 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002486 return;
2487 }
2488 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002489
2490 if (Ty.isInvalid())
2491 DS.SetTypeSpecError();
2492 else {
2493 const char *PrevSpec = 0;
2494 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2495 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2496 Ty.get()))
2497 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2498 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002499 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002500 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002501
2502 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002503 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002504 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002505 return;
2506 }
2507 RParenLoc = ConsumeParen();
2508 const char *PrevSpec = 0;
2509 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2510 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002511 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002512 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002513 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002514 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002515}
2516
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002517