blob: 5bf9783cfbeebe705bae394aee0e109155a64bb8 [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]
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000228/// others... [FIXME]
229///
Chris Lattner4b009652007-07-25 00:24:17 +0000230Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000231 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000232 case tok::kw_export:
233 case tok::kw_template:
Douglas Gregora08b6c72009-02-17 23:15:12 +0000234 return ParseTemplateDeclarationOrSpecialization(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000235 case tok::kw_namespace:
236 return ParseNamespace(Context);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000237 case tok::kw_using:
238 return ParseUsingDirectiveOrDeclaration(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000239 default:
240 return ParseSimpleDeclaration(Context);
241 }
242}
243
244/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
245/// declaration-specifiers init-declarator-list[opt] ';'
246///[C90/C++]init-declarator-list ';' [TODO]
247/// [OMP] threadprivate-directive [TODO]
248Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000249 // Parse the common declaration-specifiers piece.
250 DeclSpec DS;
251 ParseDeclarationSpecifiers(DS);
252
253 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
254 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000255 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000256 ConsumeToken();
257 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
258 }
259
260 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
261 ParseDeclarator(DeclaratorInfo);
262
263 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
264}
265
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000266
Chris Lattner4b009652007-07-25 00:24:17 +0000267/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
268/// parsing 'declaration-specifiers declarator'. This method is split out this
269/// way to handle the ambiguity between top-level function-definitions and
270/// declarations.
271///
Chris Lattner4b009652007-07-25 00:24:17 +0000272/// init-declarator-list: [C99 6.7]
273/// init-declarator
274/// init-declarator-list ',' init-declarator
275/// init-declarator: [C99 6.7]
276/// declarator
277/// declarator '=' initializer
278/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
279/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000280/// [C++] declarator initializer[opt]
281///
282/// [C++] initializer:
283/// [C++] '=' initializer-clause
284/// [C++] '(' expression-list ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000285///
286Parser::DeclTy *Parser::
287ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
288
289 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
290 // that they can be chained properly if the actions want this.
291 Parser::DeclTy *LastDeclInGroup = 0;
292
293 // At this point, we know that it is not a function definition. Parse the
294 // rest of the init-declarator-list.
295 while (1) {
296 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000297 if (Tok.is(tok::kw_asm)) {
Sebastian Redl0c986032009-02-09 18:23:29 +0000298 SourceLocation Loc;
299 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000300 if (AsmLabel.isInvalid()) {
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000301 SkipUntil(tok::semi);
302 return 0;
303 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000304
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000305 D.setAsmLabel(AsmLabel.release());
Sebastian Redl0c986032009-02-09 18:23:29 +0000306 D.SetRangeEnd(Loc);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000307 }
Chris Lattner4b009652007-07-25 00:24:17 +0000308
309 // If attributes are present, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000310 if (Tok.is(tok::kw___attribute)) {
311 SourceLocation Loc;
312 AttributeList *AttrList = ParseAttributes(&Loc);
313 D.AddAttributes(AttrList, Loc);
314 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000315
316 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000317 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000318
Chris Lattner4b009652007-07-25 00:24:17 +0000319 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000320 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000321 ConsumeToken();
Sebastian Redl39d4f022008-12-11 22:51:44 +0000322 OwningExprResult Init(ParseInitializer());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000323 if (Init.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000324 SkipUntil(tok::semi);
325 return 0;
326 }
Sebastian Redl81db6682009-02-05 15:02:23 +0000327 Actions.AddInitializerToDecl(LastDeclInGroup, move(Init));
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000328 } else if (Tok.is(tok::l_paren)) {
329 // Parse C++ direct initializer: '(' expression-list ')'
330 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000331 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000332 CommaLocsTy CommaLocs;
333
334 bool InvalidExpr = false;
335 if (ParseExpressionList(Exprs, CommaLocs)) {
336 SkipUntil(tok::r_paren);
337 InvalidExpr = true;
338 }
339 // Match the ')'.
340 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
341
342 if (!InvalidExpr) {
343 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
344 "Unexpected number of commas!");
345 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000346 Exprs.take(), Exprs.size(),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000347 &CommaLocs[0], RParenLoc);
348 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000349 } else {
350 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Chris Lattner4b009652007-07-25 00:24:17 +0000351 }
352
Chris Lattner4b009652007-07-25 00:24:17 +0000353 // If we don't have a comma, it is either the end of the list (a ';') or an
354 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000355 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000356 break;
357
358 // Consume the comma.
359 ConsumeToken();
360
361 // Parse the next declarator.
362 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000363
364 // Accept attributes in an init-declarator. In the first declarator in a
365 // declaration, these would be part of the declspec. In subsequent
366 // declarators, they become part of the declarator itself, so that they
367 // don't apply to declarators after *this* one. Examples:
368 // short __attribute__((common)) var; -> declspec
369 // short var __attribute__((common)); -> declarator
370 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000371 if (Tok.is(tok::kw___attribute)) {
372 SourceLocation Loc;
373 AttributeList *AttrList = ParseAttributes(&Loc);
374 D.AddAttributes(AttrList, Loc);
375 }
Chris Lattner926cf542008-10-20 04:57:38 +0000376
Chris Lattner4b009652007-07-25 00:24:17 +0000377 ParseDeclarator(D);
378 }
379
Chris Lattner34a01ad2007-10-09 17:33:22 +0000380 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000381 ConsumeToken();
Fariborz Jahanianc1509b02009-01-17 00:00:40 +0000382 // for(is key; in keys) is error.
383 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
384 Diag(Tok, diag::err_parse_error);
385 return 0;
386 }
Chris Lattner4b009652007-07-25 00:24:17 +0000387 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
388 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000389 // If this is an ObjC2 for-each loop, this is a successful declarator
390 // parse. The syntax for these looks like:
391 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000392 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000393 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
394 }
Chris Lattner4b009652007-07-25 00:24:17 +0000395 Diag(Tok, diag::err_parse_error);
396 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000397 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000398 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000399 ConsumeToken();
400 return 0;
401}
402
403/// ParseSpecifierQualifierList
404/// specifier-qualifier-list:
405/// type-specifier specifier-qualifier-list[opt]
406/// type-qualifier specifier-qualifier-list[opt]
407/// [GNU] attributes specifier-qualifier-list[opt]
408///
409void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
410 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
411 /// parse declaration-specifiers and complain about extra stuff.
412 ParseDeclarationSpecifiers(DS);
413
414 // Validate declspec for type-name.
415 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000416 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000417 Diag(Tok, diag::err_typename_requires_specqual);
418
419 // Issue diagnostic and remove storage class if present.
420 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
421 if (DS.getStorageClassSpecLoc().isValid())
422 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
423 else
424 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
425 DS.ClearStorageClassSpecs();
426 }
427
428 // Issue diagnostic and remove function specfier if present.
429 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000430 if (DS.isInlineSpecified())
431 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
432 if (DS.isVirtualSpecified())
433 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
434 if (DS.isExplicitSpecified())
435 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000436 DS.ClearFunctionSpecs();
437 }
438}
439
440/// ParseDeclarationSpecifiers
441/// declaration-specifiers: [C99 6.7]
442/// storage-class-specifier declaration-specifiers[opt]
443/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000444/// [C99] function-specifier declaration-specifiers[opt]
445/// [GNU] attributes declaration-specifiers[opt]
446///
447/// storage-class-specifier: [C99 6.7.1]
448/// 'typedef'
449/// 'extern'
450/// 'static'
451/// 'auto'
452/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000453/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000454/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000455/// function-specifier: [C99 6.7.4]
456/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000457/// [C++] 'virtual'
458/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000459///
Douglas Gregor52473432008-12-24 02:52:09 +0000460void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Chris Lattner712f9a32009-01-05 00:07:25 +0000461 TemplateParameterLists *TemplateParams){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000462 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000463 while (1) {
464 int isInvalid = false;
465 const char *PrevSpec = 0;
466 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000467
Chris Lattner4b009652007-07-25 00:24:17 +0000468 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000469 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000470 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000471 // If this is not a declaration specifier token, we're done reading decl
472 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000473 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000474 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000475
476 case tok::coloncolon: // ::foo::bar
477 // Annotate C++ scope specifiers. If we get one, loop.
478 if (TryAnnotateCXXScopeToken())
479 continue;
480 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000481
482 case tok::annot_cxxscope: {
483 if (DS.hasTypeSpecifier())
484 goto DoneWithDeclSpec;
485
486 // We are looking for a qualified typename.
487 if (NextToken().isNot(tok::identifier))
488 goto DoneWithDeclSpec;
489
490 CXXScopeSpec SS;
491 SS.setScopeRep(Tok.getAnnotationValue());
492 SS.setRange(Tok.getAnnotationRange());
493
494 // If the next token is the name of the class type that the C++ scope
495 // denotes, followed by a '(', then this is a constructor declaration.
496 // We're done with the decl-specifiers.
497 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
498 CurScope, &SS) &&
499 GetLookAheadToken(2).is(tok::l_paren))
500 goto DoneWithDeclSpec;
501
Douglas Gregor1075a162009-02-04 17:00:24 +0000502 Token Next = NextToken();
503 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
504 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000505
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000506 if (TypeRep == 0)
507 goto DoneWithDeclSpec;
508
509 ConsumeToken(); // The C++ scope.
510
Douglas Gregora60c62e2009-02-09 15:09:02 +0000511 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000512 TypeRep);
513 if (isInvalid)
514 break;
515
516 DS.SetRangeEnd(Tok.getLocation());
517 ConsumeToken(); // The typename.
518
519 continue;
520 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000521
522 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000523 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerc297b722009-01-21 19:48:37 +0000524 Tok.getAnnotationValue());
525 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
526 ConsumeToken(); // The typename
527
528 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
529 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
530 // Objective-C interface. If we don't have Objective-C or a '<', this is
531 // just a normal reference to a typedef name.
532 if (!Tok.is(tok::less) || !getLang().ObjC1)
533 continue;
534
535 SourceLocation EndProtoLoc;
536 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
537 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
538 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
539
540 DS.SetRangeEnd(EndProtoLoc);
541 continue;
542 }
543
Chris Lattnerfda18db2008-07-26 01:18:38 +0000544 // typedef-name
545 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000546 // In C++, check to see if this is a scope specifier like foo::bar::, if
547 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000548 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
549 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000550
Chris Lattnerfda18db2008-07-26 01:18:38 +0000551 // This identifier can only be a typedef name if we haven't already seen
552 // a type-specifier. Without this check we misparse:
553 // typedef int X; struct Y { short X; }; as 'short int'.
554 if (DS.hasTypeSpecifier())
555 goto DoneWithDeclSpec;
556
557 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000558 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
559 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000560
Chris Lattnerfda18db2008-07-26 01:18:38 +0000561 if (TypeRep == 0)
562 goto DoneWithDeclSpec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000563
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000564 // C++: If the identifier is actually the name of the class type
565 // being defined and the next token is a '(', then this is a
566 // constructor declaration. We're done with the decl-specifiers
567 // and will treat this token as an identifier.
568 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000569 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000570 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
571 NextToken().getKind() == tok::l_paren)
572 goto DoneWithDeclSpec;
573
Douglas Gregora60c62e2009-02-09 15:09:02 +0000574 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000575 TypeRep);
576 if (isInvalid)
577 break;
578
579 DS.SetRangeEnd(Tok.getLocation());
580 ConsumeToken(); // The identifier
581
582 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
583 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
584 // Objective-C interface. If we don't have Objective-C or a '<', this is
585 // just a normal reference to a typedef name.
586 if (!Tok.is(tok::less) || !getLang().ObjC1)
587 continue;
588
589 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000590 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000591 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000592 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000593
594 DS.SetRangeEnd(EndProtoLoc);
595
Steve Narofff7683302008-09-22 10:28:57 +0000596 // Need to support trailing type qualifiers (e.g. "id<p> const").
597 // If a type specifier follows, it will be diagnosed elsewhere.
598 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000599 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000600
601 // type-name
602 case tok::annot_template_id: {
603 TemplateIdAnnotation *TemplateId
604 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
605 if (TemplateId->Kind != TNK_Class_template) {
606 // This template-id does not refer to a type name, so we're
607 // done with the type-specifiers.
608 goto DoneWithDeclSpec;
609 }
610
611 // Turn the template-id annotation token into a type annotation
612 // token, then try again to parse it as a type-specifier.
613 if (AnnotateTemplateIdTokenAsType())
614 DS.SetTypeSpecError();
615
616 continue;
617 }
618
Chris Lattner4b009652007-07-25 00:24:17 +0000619 // GNU attributes support.
620 case tok::kw___attribute:
621 DS.AddAttributes(ParseAttributes());
622 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000623
624 // Microsoft declspec support.
625 case tok::kw___declspec:
626 if (!PP.getLangOptions().Microsoft)
627 goto DoneWithDeclSpec;
628 FuzzyParseMicrosoftDeclSpec();
629 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000630
Steve Naroffedd04d52008-12-25 14:16:32 +0000631 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000632 case tok::kw___forceinline:
633 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000634 case tok::kw___cdecl:
635 case tok::kw___stdcall:
636 case tok::kw___fastcall:
637 if (!PP.getLangOptions().Microsoft)
638 goto DoneWithDeclSpec;
639 // Just ignore it.
640 break;
641
Chris Lattner4b009652007-07-25 00:24:17 +0000642 // storage-class-specifier
643 case tok::kw_typedef:
644 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
645 break;
646 case tok::kw_extern:
647 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000648 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000649 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
650 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000651 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000652 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
653 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000654 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000655 case tok::kw_static:
656 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000657 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000658 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
659 break;
660 case tok::kw_auto:
661 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
662 break;
663 case tok::kw_register:
664 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
665 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000666 case tok::kw_mutable:
667 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
668 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000669 case tok::kw___thread:
670 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
671 break;
672
Chris Lattner4b009652007-07-25 00:24:17 +0000673 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000674
Chris Lattner4b009652007-07-25 00:24:17 +0000675 // function-specifier
676 case tok::kw_inline:
677 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
678 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000679 case tok::kw_virtual:
680 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
681 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000682 case tok::kw_explicit:
683 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
684 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000685
686 // type-specifier
687 case tok::kw_short:
688 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
689 break;
690 case tok::kw_long:
691 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
692 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
693 else
694 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
695 break;
696 case tok::kw_signed:
697 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
698 break;
699 case tok::kw_unsigned:
700 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
701 break;
702 case tok::kw__Complex:
703 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
704 break;
705 case tok::kw__Imaginary:
706 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
707 break;
708 case tok::kw_void:
709 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
710 break;
711 case tok::kw_char:
712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
713 break;
714 case tok::kw_int:
715 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
716 break;
717 case tok::kw_float:
718 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
719 break;
720 case tok::kw_double:
721 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
722 break;
723 case tok::kw_wchar_t:
724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
725 break;
726 case tok::kw_bool:
727 case tok::kw__Bool:
728 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
729 break;
730 case tok::kw__Decimal32:
731 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
732 break;
733 case tok::kw__Decimal64:
734 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
735 break;
736 case tok::kw__Decimal128:
737 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
738 break;
739
740 // class-specifier:
741 case tok::kw_class:
742 case tok::kw_struct:
743 case tok::kw_union:
744 ParseClassSpecifier(DS, TemplateParams);
745 continue;
746
747 // enum-specifier:
748 case tok::kw_enum:
749 ParseEnumSpecifier(DS);
750 continue;
751
752 // cv-qualifier:
753 case tok::kw_const:
754 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
755 break;
756 case tok::kw_volatile:
757 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
758 getLang())*2;
759 break;
760 case tok::kw_restrict:
761 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
762 getLang())*2;
763 break;
764
765 // GNU typeof support.
766 case tok::kw_typeof:
767 ParseTypeofSpecifier(DS);
768 continue;
769
Steve Naroff5f0466b2008-06-05 00:02:44 +0000770 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000771 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000772 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
773 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000774 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000775 goto DoneWithDeclSpec;
776
777 {
778 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000779 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000780 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000781 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000782 DS.SetRangeEnd(EndProtoLoc);
783
Chris Lattnerf006a222008-11-18 07:48:38 +0000784 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
785 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000786 // Need to support trailing type qualifiers (e.g. "id<p> const").
787 // If a type specifier follows, it will be diagnosed elsewhere.
788 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000789 }
Chris Lattner4b009652007-07-25 00:24:17 +0000790 }
791 // If the specifier combination wasn't legal, issue a diagnostic.
792 if (isInvalid) {
793 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000794 // Pick between error or extwarn.
795 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
796 : diag::ext_duplicate_declspec;
797 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000798 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000799 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000800 ConsumeToken();
801 }
802}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000803
Chris Lattnerd706dc82009-01-06 06:59:53 +0000804/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000805/// primarily follow the C++ grammar with additions for C99 and GNU,
806/// which together subsume the C grammar. Note that the C++
807/// type-specifier also includes the C type-qualifier (for const,
808/// volatile, and C99 restrict). Returns true if a type-specifier was
809/// found (and parsed), false otherwise.
810///
811/// type-specifier: [C++ 7.1.5]
812/// simple-type-specifier
813/// class-specifier
814/// enum-specifier
815/// elaborated-type-specifier [TODO]
816/// cv-qualifier
817///
818/// cv-qualifier: [C++ 7.1.5.1]
819/// 'const'
820/// 'volatile'
821/// [C99] 'restrict'
822///
823/// simple-type-specifier: [ C++ 7.1.5.2]
824/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
825/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
826/// 'char'
827/// 'wchar_t'
828/// 'bool'
829/// 'short'
830/// 'int'
831/// 'long'
832/// 'signed'
833/// 'unsigned'
834/// 'float'
835/// 'double'
836/// 'void'
837/// [C99] '_Bool'
838/// [C99] '_Complex'
839/// [C99] '_Imaginary' // Removed in TC2?
840/// [GNU] '_Decimal32'
841/// [GNU] '_Decimal64'
842/// [GNU] '_Decimal128'
843/// [GNU] typeof-specifier
844/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
845/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000846bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
847 const char *&PrevSpec,
848 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000849 SourceLocation Loc = Tok.getLocation();
850
851 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000852 case tok::identifier: // foo::bar
853 // Annotate typenames and C++ scope specifiers. If we get one, just
854 // recurse to handle whatever we get.
855 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000856 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000857 // Otherwise, not a type specifier.
858 return false;
859 case tok::coloncolon: // ::foo::bar
860 if (NextToken().is(tok::kw_new) || // ::new
861 NextToken().is(tok::kw_delete)) // ::delete
862 return false;
863
864 // Annotate typenames and C++ scope specifiers. If we get one, just
865 // recurse to handle whatever we get.
866 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000867 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000868 // Otherwise, not a type specifier.
869 return false;
870
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000871 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000872 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000873 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000874 Tok.getAnnotationValue());
875 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
876 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000877
878 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
879 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
880 // Objective-C interface. If we don't have Objective-C or a '<', this is
881 // just a normal reference to a typedef name.
882 if (!Tok.is(tok::less) || !getLang().ObjC1)
883 return true;
884
885 SourceLocation EndProtoLoc;
886 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
887 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
888 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
889
890 DS.SetRangeEnd(EndProtoLoc);
891 return true;
892 }
893
894 case tok::kw_short:
895 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
896 break;
897 case tok::kw_long:
898 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
899 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
900 else
901 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
902 break;
903 case tok::kw_signed:
904 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
905 break;
906 case tok::kw_unsigned:
907 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
908 break;
909 case tok::kw__Complex:
910 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
911 break;
912 case tok::kw__Imaginary:
913 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
914 break;
915 case tok::kw_void:
916 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
917 break;
918 case tok::kw_char:
919 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
920 break;
921 case tok::kw_int:
922 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
923 break;
924 case tok::kw_float:
925 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
926 break;
927 case tok::kw_double:
928 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
929 break;
930 case tok::kw_wchar_t:
931 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
932 break;
933 case tok::kw_bool:
934 case tok::kw__Bool:
935 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
936 break;
937 case tok::kw__Decimal32:
938 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
939 break;
940 case tok::kw__Decimal64:
941 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
942 break;
943 case tok::kw__Decimal128:
944 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
945 break;
946
947 // class-specifier:
948 case tok::kw_class:
949 case tok::kw_struct:
950 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000951 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000952 return true;
953
954 // enum-specifier:
955 case tok::kw_enum:
956 ParseEnumSpecifier(DS);
957 return true;
958
959 // cv-qualifier:
960 case tok::kw_const:
961 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
962 getLang())*2;
963 break;
964 case tok::kw_volatile:
965 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
966 getLang())*2;
967 break;
968 case tok::kw_restrict:
969 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
970 getLang())*2;
971 break;
972
973 // GNU typeof support.
974 case tok::kw_typeof:
975 ParseTypeofSpecifier(DS);
976 return true;
977
Steve Naroffedd04d52008-12-25 14:16:32 +0000978 case tok::kw___cdecl:
979 case tok::kw___stdcall:
980 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +0000981 if (!PP.getLangOptions().Microsoft) return false;
982 ConsumeToken();
983 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +0000984
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000985 default:
986 // Not a type-specifier; do nothing.
987 return false;
988 }
989
990 // If the specifier combination wasn't legal, issue a diagnostic.
991 if (isInvalid) {
992 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000993 // Pick between error or extwarn.
994 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
995 : diag::ext_duplicate_declspec;
996 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000997 }
998 DS.SetRangeEnd(Tok.getLocation());
999 ConsumeToken(); // whatever we parsed above.
1000 return true;
1001}
Chris Lattner4b009652007-07-25 00:24:17 +00001002
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001003/// ParseStructDeclaration - Parse a struct declaration without the terminating
1004/// semicolon.
1005///
Chris Lattner4b009652007-07-25 00:24:17 +00001006/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001007/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001008/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001009/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001010/// struct-declarator-list:
1011/// struct-declarator
1012/// struct-declarator-list ',' struct-declarator
1013/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1014/// struct-declarator:
1015/// declarator
1016/// [GNU] declarator attributes[opt]
1017/// declarator[opt] ':' constant-expression
1018/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1019///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001020void Parser::
1021ParseStructDeclaration(DeclSpec &DS,
1022 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001023 if (Tok.is(tok::kw___extension__)) {
1024 // __extension__ silences extension warnings in the subexpression.
1025 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001026 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001027 return ParseStructDeclaration(DS, Fields);
1028 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001029
1030 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001031 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001032 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001033
Douglas Gregorb748fc52009-01-12 22:49:06 +00001034 // If there are no declarators, this is a free-standing declaration
1035 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001036 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001037 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001038 return;
1039 }
1040
1041 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001042 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001043 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001044 FieldDeclarator &DeclaratorInfo = Fields.back();
1045
Steve Naroffa9adf112007-08-20 22:28:22 +00001046 /// struct-declarator: declarator
1047 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001048 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001049 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001050
Chris Lattner34a01ad2007-10-09 17:33:22 +00001051 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001052 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001053 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001054 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001055 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001056 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001057 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001058 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001059
Steve Naroffa9adf112007-08-20 22:28:22 +00001060 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001061 if (Tok.is(tok::kw___attribute)) {
1062 SourceLocation Loc;
1063 AttributeList *AttrList = ParseAttributes(&Loc);
1064 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1065 }
1066
Steve Naroffa9adf112007-08-20 22:28:22 +00001067 // If we don't have a comma, it is either the end of the list (a ';')
1068 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001069 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001070 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001071
Steve Naroffa9adf112007-08-20 22:28:22 +00001072 // Consume the comma.
1073 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001074
Steve Naroffa9adf112007-08-20 22:28:22 +00001075 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001076 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001077
Steve Naroffa9adf112007-08-20 22:28:22 +00001078 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001079 if (Tok.is(tok::kw___attribute)) {
1080 SourceLocation Loc;
1081 AttributeList *AttrList = ParseAttributes(&Loc);
1082 Fields.back().D.AddAttributes(AttrList, Loc);
1083 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001084 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001085}
1086
1087/// ParseStructUnionBody
1088/// struct-contents:
1089/// struct-declaration-list
1090/// [EXT] empty
1091/// [GNU] "struct-declaration-list" without terminatoring ';'
1092/// struct-declaration-list:
1093/// struct-declaration
1094/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001095/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001096///
Chris Lattner4b009652007-07-25 00:24:17 +00001097void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1098 unsigned TagType, DeclTy *TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001099 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1100 PP.getSourceManager(),
1101 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001102
Chris Lattner4b009652007-07-25 00:24:17 +00001103 SourceLocation LBraceLoc = ConsumeBrace();
1104
Douglas Gregorcab994d2009-01-09 22:42:13 +00001105 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001106 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1107
Chris Lattner4b009652007-07-25 00:24:17 +00001108 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1109 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001110 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001111 Diag(Tok, diag::ext_empty_struct_union_enum)
1112 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001113
1114 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001115 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1116
Chris Lattner4b009652007-07-25 00:24:17 +00001117 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001118 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001119 // Each iteration of this loop reads one struct-declaration.
1120
1121 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001122 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001123 Diag(Tok, diag::ext_extra_struct_semi);
1124 ConsumeToken();
1125 continue;
1126 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001127
1128 // Parse all the comma separated declarators.
1129 DeclSpec DS;
1130 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001131 if (!Tok.is(tok::at)) {
1132 ParseStructDeclaration(DS, FieldDeclarators);
1133
1134 // Convert them all to fields.
1135 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1136 FieldDeclarator &FD = FieldDeclarators[i];
1137 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +00001138 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +00001139 DS.getSourceRange().getBegin(),
1140 FD.D, FD.BitfieldSize);
1141 FieldDecls.push_back(Field);
1142 }
1143 } else { // Handle @defs
1144 ConsumeToken();
1145 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1146 Diag(Tok, diag::err_unexpected_at);
1147 SkipUntil(tok::semi, true, true);
1148 continue;
1149 }
1150 ConsumeToken();
1151 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1152 if (!Tok.is(tok::identifier)) {
1153 Diag(Tok, diag::err_expected_ident);
1154 SkipUntil(tok::semi, true, true);
1155 continue;
1156 }
1157 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001158 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1159 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001160 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1161 ConsumeToken();
1162 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1163 }
Chris Lattner4b009652007-07-25 00:24:17 +00001164
Chris Lattner34a01ad2007-10-09 17:33:22 +00001165 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001166 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001167 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001168 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001169 break;
1170 } else {
1171 Diag(Tok, diag::err_expected_semi_decl_list);
1172 // Skip to end of block or statement
1173 SkipUntil(tok::r_brace, true, true);
1174 }
1175 }
1176
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001177 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001178
Chris Lattner4b009652007-07-25 00:24:17 +00001179 AttributeList *AttrList = 0;
1180 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001181 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001182 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001183
1184 Actions.ActOnFields(CurScope,
1185 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1186 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001187 AttrList);
1188 StructScope.Exit();
1189 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001190}
1191
1192
1193/// ParseEnumSpecifier
1194/// enum-specifier: [C99 6.7.2.2]
1195/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001196///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001197/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1198/// '}' attributes[opt]
1199/// 'enum' identifier
1200/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001201///
1202/// [C++] elaborated-type-specifier:
1203/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1204///
Chris Lattner4b009652007-07-25 00:24:17 +00001205void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001206 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001207 SourceLocation StartLoc = ConsumeToken();
1208
1209 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001210
1211 AttributeList *Attr = 0;
1212 // If attributes exist after tag, parse them.
1213 if (Tok.is(tok::kw___attribute))
1214 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001215
1216 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001217 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001218 if (Tok.isNot(tok::identifier)) {
1219 Diag(Tok, diag::err_expected_ident);
1220 if (Tok.isNot(tok::l_brace)) {
1221 // Has no name and is not a definition.
1222 // Skip the rest of this declarator, up until the comma or semicolon.
1223 SkipUntil(tok::comma, true);
1224 return;
1225 }
1226 }
1227 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001228
1229 // Must have either 'enum name' or 'enum {...}'.
1230 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1231 Diag(Tok, diag::err_expected_ident_lbrace);
1232
1233 // Skip the rest of this declarator, up until the comma or semicolon.
1234 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001235 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001236 }
1237
1238 // If an identifier is present, consume and remember it.
1239 IdentifierInfo *Name = 0;
1240 SourceLocation NameLoc;
1241 if (Tok.is(tok::identifier)) {
1242 Name = Tok.getIdentifierInfo();
1243 NameLoc = ConsumeToken();
1244 }
1245
1246 // There are three options here. If we have 'enum foo;', then this is a
1247 // forward declaration. If we have 'enum foo {...' then this is a
1248 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1249 //
1250 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1251 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1252 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1253 //
1254 Action::TagKind TK;
1255 if (Tok.is(tok::l_brace))
1256 TK = Action::TK_Definition;
1257 else if (Tok.is(tok::semi))
1258 TK = Action::TK_Declaration;
1259 else
1260 TK = Action::TK_Reference;
1261 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregord406b032009-02-06 22:42:48 +00001262 SS, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +00001263
Chris Lattner34a01ad2007-10-09 17:33:22 +00001264 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001265 ParseEnumBody(StartLoc, TagDecl);
1266
1267 // TODO: semantic analysis on the declspec for enums.
1268 const char *PrevSpec = 0;
1269 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001270 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001271}
1272
1273/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1274/// enumerator-list:
1275/// enumerator
1276/// enumerator-list ',' enumerator
1277/// enumerator:
1278/// enumeration-constant
1279/// enumeration-constant '=' constant-expression
1280/// enumeration-constant:
1281/// identifier
1282///
1283void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001284 // Enter the scope of the enum body and start the definition.
1285 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001286 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001287
Chris Lattner4b009652007-07-25 00:24:17 +00001288 SourceLocation LBraceLoc = ConsumeBrace();
1289
Chris Lattnerc9a92452007-08-27 17:24:30 +00001290 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001291 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001292 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001293
1294 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1295
1296 DeclTy *LastEnumConstDecl = 0;
1297
1298 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001299 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001300 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1301 SourceLocation IdentLoc = ConsumeToken();
1302
1303 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001304 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001305 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001306 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001307 AssignedVal = ParseConstantExpression();
1308 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001309 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001310 }
1311
1312 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001313 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001314 LastEnumConstDecl,
1315 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001316 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001317 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001318 EnumConstantDecls.push_back(EnumConstDecl);
1319 LastEnumConstDecl = EnumConstDecl;
1320
Chris Lattner34a01ad2007-10-09 17:33:22 +00001321 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001322 break;
1323 SourceLocation CommaLoc = ConsumeToken();
1324
Chris Lattner34a01ad2007-10-09 17:33:22 +00001325 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001326 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1327 }
1328
1329 // Eat the }.
1330 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1331
Steve Naroff0acc9c92007-09-15 18:49:24 +00001332 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001333 EnumConstantDecls.size());
1334
1335 DeclTy *AttrList = 0;
1336 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001337 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001338 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001339
1340 EnumScope.Exit();
1341 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001342}
1343
1344/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001345/// start of a type-qualifier-list.
1346bool Parser::isTypeQualifier() const {
1347 switch (Tok.getKind()) {
1348 default: return false;
1349 // type-qualifier
1350 case tok::kw_const:
1351 case tok::kw_volatile:
1352 case tok::kw_restrict:
1353 return true;
1354 }
1355}
1356
1357/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001358/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001359bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001360 switch (Tok.getKind()) {
1361 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001362
1363 case tok::identifier: // foo::bar
1364 // Annotate typenames and C++ scope specifiers. If we get one, just
1365 // recurse to handle whatever we get.
1366 if (TryAnnotateTypeOrScopeToken())
1367 return isTypeSpecifierQualifier();
1368 // Otherwise, not a type specifier.
1369 return false;
1370 case tok::coloncolon: // ::foo::bar
1371 if (NextToken().is(tok::kw_new) || // ::new
1372 NextToken().is(tok::kw_delete)) // ::delete
1373 return false;
1374
1375 // Annotate typenames and C++ scope specifiers. If we get one, just
1376 // recurse to handle whatever we get.
1377 if (TryAnnotateTypeOrScopeToken())
1378 return isTypeSpecifierQualifier();
1379 // Otherwise, not a type specifier.
1380 return false;
1381
Chris Lattner4b009652007-07-25 00:24:17 +00001382 // GNU attributes support.
1383 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001384 // GNU typeof support.
1385 case tok::kw_typeof:
1386
Chris Lattner4b009652007-07-25 00:24:17 +00001387 // type-specifiers
1388 case tok::kw_short:
1389 case tok::kw_long:
1390 case tok::kw_signed:
1391 case tok::kw_unsigned:
1392 case tok::kw__Complex:
1393 case tok::kw__Imaginary:
1394 case tok::kw_void:
1395 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001396 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001397 case tok::kw_int:
1398 case tok::kw_float:
1399 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001400 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001401 case tok::kw__Bool:
1402 case tok::kw__Decimal32:
1403 case tok::kw__Decimal64:
1404 case tok::kw__Decimal128:
1405
Chris Lattner2e78db32008-04-13 18:59:07 +00001406 // struct-or-union-specifier (C99) or class-specifier (C++)
1407 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001408 case tok::kw_struct:
1409 case tok::kw_union:
1410 // enum-specifier
1411 case tok::kw_enum:
1412
1413 // type-qualifier
1414 case tok::kw_const:
1415 case tok::kw_volatile:
1416 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001417
1418 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001419 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001420 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001421
1422 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1423 case tok::less:
1424 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001425
1426 case tok::kw___cdecl:
1427 case tok::kw___stdcall:
1428 case tok::kw___fastcall:
1429 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001430 }
1431}
1432
1433/// isDeclarationSpecifier() - Return true if the current token is part of a
1434/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001435bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001436 switch (Tok.getKind()) {
1437 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001438
1439 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001440 // Unfortunate hack to support "Class.factoryMethod" notation.
1441 if (getLang().ObjC1 && NextToken().is(tok::period))
1442 return false;
1443
Chris Lattnerb75fde62009-01-04 23:41:41 +00001444 // Annotate typenames and C++ scope specifiers. If we get one, just
1445 // recurse to handle whatever we get.
1446 if (TryAnnotateTypeOrScopeToken())
1447 return isDeclarationSpecifier();
1448 // Otherwise, not a declaration specifier.
1449 return false;
1450 case tok::coloncolon: // ::foo::bar
1451 if (NextToken().is(tok::kw_new) || // ::new
1452 NextToken().is(tok::kw_delete)) // ::delete
1453 return false;
1454
1455 // Annotate typenames and C++ scope specifiers. If we get one, just
1456 // recurse to handle whatever we get.
1457 if (TryAnnotateTypeOrScopeToken())
1458 return isDeclarationSpecifier();
1459 // Otherwise, not a declaration specifier.
1460 return false;
1461
Chris Lattner4b009652007-07-25 00:24:17 +00001462 // storage-class-specifier
1463 case tok::kw_typedef:
1464 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001465 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001466 case tok::kw_static:
1467 case tok::kw_auto:
1468 case tok::kw_register:
1469 case tok::kw___thread:
1470
1471 // type-specifiers
1472 case tok::kw_short:
1473 case tok::kw_long:
1474 case tok::kw_signed:
1475 case tok::kw_unsigned:
1476 case tok::kw__Complex:
1477 case tok::kw__Imaginary:
1478 case tok::kw_void:
1479 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001480 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001481 case tok::kw_int:
1482 case tok::kw_float:
1483 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001484 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001485 case tok::kw__Bool:
1486 case tok::kw__Decimal32:
1487 case tok::kw__Decimal64:
1488 case tok::kw__Decimal128:
1489
Chris Lattner2e78db32008-04-13 18:59:07 +00001490 // struct-or-union-specifier (C99) or class-specifier (C++)
1491 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001492 case tok::kw_struct:
1493 case tok::kw_union:
1494 // enum-specifier
1495 case tok::kw_enum:
1496
1497 // type-qualifier
1498 case tok::kw_const:
1499 case tok::kw_volatile:
1500 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001501
Chris Lattner4b009652007-07-25 00:24:17 +00001502 // function-specifier
1503 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001504 case tok::kw_virtual:
1505 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001506
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001507 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001508 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001509
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001510 // GNU typeof support.
1511 case tok::kw_typeof:
1512
1513 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001514 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001515 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001516
1517 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1518 case tok::less:
1519 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001520
Steve Naroffab1a3632009-01-06 19:34:12 +00001521 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001522 case tok::kw___cdecl:
1523 case tok::kw___stdcall:
1524 case tok::kw___fastcall:
1525 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001526 }
1527}
1528
1529
1530/// ParseTypeQualifierListOpt
1531/// type-qualifier-list: [C99 6.7.5]
1532/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001533/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001534/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001535/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001536///
Chris Lattner460696f2008-12-18 07:02:59 +00001537void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001538 while (1) {
1539 int isInvalid = false;
1540 const char *PrevSpec = 0;
1541 SourceLocation Loc = Tok.getLocation();
1542
1543 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001544 case tok::kw_const:
1545 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1546 getLang())*2;
1547 break;
1548 case tok::kw_volatile:
1549 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1550 getLang())*2;
1551 break;
1552 case tok::kw_restrict:
1553 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1554 getLang())*2;
1555 break;
Steve Naroffad620402008-12-25 14:41:26 +00001556 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001557 case tok::kw___cdecl:
1558 case tok::kw___stdcall:
1559 case tok::kw___fastcall:
1560 if (!PP.getLangOptions().Microsoft)
1561 goto DoneWithTypeQuals;
1562 // Just ignore it.
1563 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001564 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001565 if (AttributesAllowed) {
1566 DS.AddAttributes(ParseAttributes());
1567 continue; // do *not* consume the next token!
1568 }
1569 // otherwise, FALL THROUGH!
1570 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001571 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001572 // If this is not a type-qualifier token, we're done reading type
1573 // qualifiers. First verify that DeclSpec's are consistent.
1574 DS.Finish(Diags, PP.getSourceManager(), getLang());
1575 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001576 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001577
Chris Lattner4b009652007-07-25 00:24:17 +00001578 // If the specifier combination wasn't legal, issue a diagnostic.
1579 if (isInvalid) {
1580 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001581 // Pick between error or extwarn.
1582 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1583 : diag::ext_duplicate_declspec;
1584 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001585 }
1586 ConsumeToken();
1587 }
1588}
1589
1590
1591/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1592///
1593void Parser::ParseDeclarator(Declarator &D) {
1594 /// This implements the 'declarator' production in the C grammar, then checks
1595 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001596 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001597}
1598
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001599/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1600/// is parsed by the function passed to it. Pass null, and the direct-declarator
1601/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001602/// ptr-operator production.
1603///
Sebastian Redl75555032009-01-24 21:16:55 +00001604/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1605/// [C] pointer[opt] direct-declarator
1606/// [C++] direct-declarator
1607/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001608///
1609/// pointer: [C99 6.7.5]
1610/// '*' type-qualifier-list[opt]
1611/// '*' type-qualifier-list[opt] pointer
1612///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001613/// ptr-operator:
1614/// '*' cv-qualifier-seq[opt]
1615/// '&'
1616/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001617/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001618void Parser::ParseDeclaratorInternal(Declarator &D,
1619 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001620
Sebastian Redl75555032009-01-24 21:16:55 +00001621 // C++ member pointers start with a '::' or a nested-name.
1622 // Member pointers get special handling, since there's no place for the
1623 // scope spec in the generic path below.
1624 if ((Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1625 Tok.is(tok::annot_cxxscope)) && getLang().CPlusPlus) {
1626 CXXScopeSpec SS;
1627 if (ParseOptionalCXXScopeSpecifier(SS)) {
1628 if(Tok.isNot(tok::star)) {
1629 // The scope spec really belongs to the direct-declarator.
1630 D.getCXXScopeSpec() = SS;
1631 if (DirectDeclParser)
1632 (this->*DirectDeclParser)(D);
1633 return;
1634 }
1635
1636 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001637 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001638 DeclSpec DS;
1639 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001640 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001641
1642 // Recurse to parse whatever is left.
1643 ParseDeclaratorInternal(D, DirectDeclParser);
1644
1645 // Sema will have to catch (syntactically invalid) pointers into global
1646 // scope. It has to catch pointers into namespace scope anyway.
1647 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001648 Loc, DS.TakeAttributes()),
1649 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001650 return;
1651 }
1652 }
1653
1654 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001655 // Not a pointer, C++ reference, or block.
1656 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001657 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001658 if (DirectDeclParser)
1659 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001660 return;
1661 }
Sebastian Redl75555032009-01-24 21:16:55 +00001662
Steve Naroffdc22f212008-08-28 10:07:06 +00001663 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Sebastian Redl0c986032009-02-09 18:23:29 +00001664 SourceLocation Loc = ConsumeToken(); // Eat the *, ^ or &.
1665 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001666
Steve Naroffdc22f212008-08-28 10:07:06 +00001667 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001668 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001669 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001670
Chris Lattner4b009652007-07-25 00:24:17 +00001671 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001672 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001673
Chris Lattner4b009652007-07-25 00:24:17 +00001674 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001675 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001676 if (Kind == tok::star)
1677 // Remember that we parsed a pointer type, and remember the type-quals.
1678 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001679 DS.TakeAttributes()),
1680 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001681 else
1682 // Remember that we parsed a Block type, and remember the type-quals.
1683 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001684 Loc),
1685 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001686 } else {
1687 // Is a reference
1688 DeclSpec DS;
1689
1690 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1691 // cv-qualifiers are introduced through the use of a typedef or of a
1692 // template type argument, in which case the cv-qualifiers are ignored.
1693 //
1694 // [GNU] Retricted references are allowed.
1695 // [GNU] Attributes on references are allowed.
1696 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001697 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001698
1699 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1700 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1701 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001702 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001703 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1704 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001705 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001706 }
1707
1708 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001709 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001710
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001711 if (D.getNumTypeObjects() > 0) {
1712 // C++ [dcl.ref]p4: There shall be no references to references.
1713 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1714 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001715 if (const IdentifierInfo *II = D.getIdentifier())
1716 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1717 << II;
1718 else
1719 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1720 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001721
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001722 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001723 // can go ahead and build the (technically ill-formed)
1724 // declarator: reference collapsing will take care of it.
1725 }
1726 }
1727
Chris Lattner4b009652007-07-25 00:24:17 +00001728 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001729 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001730 DS.TakeAttributes()),
1731 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001732 }
1733}
1734
1735/// ParseDirectDeclarator
1736/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001737/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001738/// '(' declarator ')'
1739/// [GNU] '(' attributes declarator ')'
1740/// [C90] direct-declarator '[' constant-expression[opt] ']'
1741/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1742/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1743/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1744/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1745/// direct-declarator '(' parameter-type-list ')'
1746/// direct-declarator '(' identifier-list[opt] ')'
1747/// [GNU] direct-declarator '(' parameter-forward-declarations
1748/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001749/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1750/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001751/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001752///
1753/// declarator-id: [C++ 8]
1754/// id-expression
1755/// '::'[opt] nested-name-specifier[opt] type-name
1756///
1757/// id-expression: [C++ 5.1]
1758/// unqualified-id
1759/// qualified-id [TODO]
1760///
1761/// unqualified-id: [C++ 5.1]
1762/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001763/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001764/// conversion-function-id [TODO]
1765/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001766/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001767///
Chris Lattner4b009652007-07-25 00:24:17 +00001768void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001769 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001770
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001771 if (getLang().CPlusPlus) {
1772 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001773 // ParseDeclaratorInternal might already have parsed the scope.
1774 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1775 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001776 if (afterCXXScope) {
1777 // Change the declaration context for name lookup, until this function
1778 // is exited (and the declarator has been parsed).
1779 DeclScopeObj.EnterDeclaratorScope();
1780 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001781
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001782 if (Tok.is(tok::identifier)) {
1783 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001784
Douglas Gregor2fa10442008-12-18 19:37:40 +00001785 // If this identifier is the name of the current class, it's a
1786 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001787 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001788 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001789 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001790 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001791 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001792 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001793 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1794 ConsumeToken();
1795 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001796 } else if (Tok.is(tok::annot_template_id)) {
1797 TemplateIdAnnotation *TemplateId
1798 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1799
1800 // FIXME: Could this template-id name a constructor?
1801
1802 // FIXME: This is an egregious hack, where we silently ignore
1803 // the specialization (which should be a function template
1804 // specialization name) and use the name instead. This hack
1805 // will go away when we have support for function
1806 // specializations.
1807 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1808 TemplateId->Destroy();
1809 ConsumeToken();
1810 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001811 } else if (Tok.is(tok::kw_operator)) {
1812 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001813 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001814
Douglas Gregor853dd392008-12-26 15:00:45 +00001815 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001816 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1817 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001818 } else {
1819 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001820 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1821 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1822 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001823 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001824 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001825 }
1826 goto PastIdentifier;
1827 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001828 // This should be a C++ destructor.
1829 SourceLocation TildeLoc = ConsumeToken();
1830 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001831 // FIXME: Inaccurate.
1832 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001833 SourceLocation EndLoc;
1834 if (TypeTy *Type = ParseClassName(EndLoc)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001835 D.setDestructor(Type, TildeLoc, NameLoc);
1836 } else {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001837 D.SetIdentifier(0, TildeLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001838 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001839 } else {
1840 Diag(Tok, diag::err_expected_class_name);
1841 D.SetIdentifier(0, TildeLoc);
1842 }
1843 goto PastIdentifier;
1844 }
1845
1846 // If we reached this point, token is not identifier and not '~'.
1847
1848 if (afterCXXScope) {
1849 Diag(Tok, diag::err_expected_unqualified_id);
1850 D.SetIdentifier(0, Tok.getLocation());
1851 D.setInvalidType(true);
1852 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001853 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001854 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001855 }
1856
1857 // If we reached this point, we are either in C/ObjC or the token didn't
1858 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001859 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1860 assert(!getLang().CPlusPlus &&
1861 "There's a C++-specific check for tok::identifier above");
1862 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1863 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1864 ConsumeToken();
1865 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001866 // direct-declarator: '(' declarator ')'
1867 // direct-declarator: '(' attributes declarator ')'
1868 // Example: 'char (*X)' or 'int (*XX)(void)'
1869 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001870 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001871 // This could be something simple like "int" (in which case the declarator
1872 // portion is empty), if an abstract-declarator is allowed.
1873 D.SetIdentifier(0, Tok.getLocation());
1874 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00001875 if (D.getContext() == Declarator::MemberContext)
1876 Diag(Tok, diag::err_expected_member_name_or_semi)
1877 << D.getDeclSpec().getSourceRange();
1878 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001879 Diag(Tok, diag::err_expected_unqualified_id);
1880 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001881 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001882 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001883 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001884 }
1885
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001886 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001887 assert(D.isPastIdentifier() &&
1888 "Haven't past the location of the identifier yet?");
1889
1890 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001891 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001892 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1893 // In such a case, check if we actually have a function declarator; if it
1894 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001895 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1896 // When not in file scope, warn for ambiguous function declarators, just
1897 // in case the author intended it as a variable definition.
1898 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1899 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1900 break;
1901 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001902 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001903 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001904 ParseBracketDeclarator(D);
1905 } else {
1906 break;
1907 }
1908 }
1909}
1910
Chris Lattnera0d056d2008-04-06 05:45:57 +00001911/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1912/// only called before the identifier, so these are most likely just grouping
1913/// parens for precedence. If we find that these are actually function
1914/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1915///
1916/// direct-declarator:
1917/// '(' declarator ')'
1918/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001919/// direct-declarator '(' parameter-type-list ')'
1920/// direct-declarator '(' identifier-list[opt] ')'
1921/// [GNU] direct-declarator '(' parameter-forward-declarations
1922/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001923///
1924void Parser::ParseParenDeclarator(Declarator &D) {
1925 SourceLocation StartLoc = ConsumeParen();
1926 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1927
Chris Lattner1f185292008-10-20 02:05:46 +00001928 // Eat any attributes before we look at whether this is a grouping or function
1929 // declarator paren. If this is a grouping paren, the attribute applies to
1930 // the type being built up, for example:
1931 // int (__attribute__(()) *x)(long y)
1932 // If this ends up not being a grouping paren, the attribute applies to the
1933 // first argument, for example:
1934 // int (__attribute__(()) int x)
1935 // In either case, we need to eat any attributes to be able to determine what
1936 // sort of paren this is.
1937 //
1938 AttributeList *AttrList = 0;
1939 bool RequiresArg = false;
1940 if (Tok.is(tok::kw___attribute)) {
1941 AttrList = ParseAttributes();
1942
1943 // We require that the argument list (if this is a non-grouping paren) be
1944 // present even if the attribute list was empty.
1945 RequiresArg = true;
1946 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001947 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00001948 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1949 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00001950 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00001951
Chris Lattnera0d056d2008-04-06 05:45:57 +00001952 // If we haven't past the identifier yet (or where the identifier would be
1953 // stored, if this is an abstract declarator), then this is probably just
1954 // grouping parens. However, if this could be an abstract-declarator, then
1955 // this could also be the start of function arguments (consider 'void()').
1956 bool isGrouping;
1957
1958 if (!D.mayOmitIdentifier()) {
1959 // If this can't be an abstract-declarator, this *must* be a grouping
1960 // paren, because we haven't seen the identifier yet.
1961 isGrouping = true;
1962 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001963 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001964 isDeclarationSpecifier()) { // 'int(int)' is a function.
1965 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1966 // considered to be a type, not a K&R identifier-list.
1967 isGrouping = false;
1968 } else {
1969 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1970 isGrouping = true;
1971 }
1972
1973 // If this is a grouping paren, handle:
1974 // direct-declarator: '(' declarator ')'
1975 // direct-declarator: '(' attributes declarator ')'
1976 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001977 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001978 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001979 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00001980 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001981
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001982 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001983 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00001984 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001985
1986 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00001987 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001988 return;
1989 }
1990
1991 // Okay, if this wasn't a grouping paren, it must be the start of a function
1992 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001993 // identifier (and remember where it would have been), then call into
1994 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001995 D.SetIdentifier(0, Tok.getLocation());
1996
Chris Lattner1f185292008-10-20 02:05:46 +00001997 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001998}
1999
2000/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2001/// declarator D up to a paren, which indicates that we are parsing function
2002/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002003///
Chris Lattner1f185292008-10-20 02:05:46 +00002004/// If AttrList is non-null, then the caller parsed those arguments immediately
2005/// after the open paren - they should be considered to be the first argument of
2006/// a parameter. If RequiresArg is true, then the first argument of the
2007/// function is required to be present and required to not be an identifier
2008/// list.
2009///
Chris Lattner4b009652007-07-25 00:24:17 +00002010/// This method also handles this portion of the grammar:
2011/// parameter-type-list: [C99 6.7.5]
2012/// parameter-list
2013/// parameter-list ',' '...'
2014///
2015/// parameter-list: [C99 6.7.5]
2016/// parameter-declaration
2017/// parameter-list ',' parameter-declaration
2018///
2019/// parameter-declaration: [C99 6.7.5]
2020/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002021/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002022/// [GNU] declaration-specifiers declarator attributes
2023/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002024/// [C++] declaration-specifiers abstract-declarator[opt]
2025/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002026/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2027///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002028/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
2029/// and "exception-specification[opt]"(TODO).
2030///
Chris Lattner1f185292008-10-20 02:05:46 +00002031void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2032 AttributeList *AttrList,
2033 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002034 // lparen is already consumed!
2035 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002036
Chris Lattner1f185292008-10-20 02:05:46 +00002037 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002038 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002039 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002040 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002041 delete AttrList;
2042 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002043
Sebastian Redl0c986032009-02-09 18:23:29 +00002044 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002045
2046 // cv-qualifier-seq[opt].
2047 DeclSpec DS;
2048 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002049 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002050 if (!DS.getSourceRange().getEnd().isInvalid())
2051 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002052
2053 // Parse exception-specification[opt].
2054 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002055 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002056 }
2057
Chris Lattner9f7564b2008-04-06 06:57:35 +00002058 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002059 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002060 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002061 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002062 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002063 /*arglist*/ 0, 0,
2064 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002065 LParenLoc, D),
2066 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002067 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002068 }
2069
2070 // Alternatively, this parameter list may be an identifier list form for a
2071 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002072 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002073 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002074 // K&R identifier lists can't have typedefs as identifiers, per
2075 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002076 if (RequiresArg) {
2077 Diag(Tok, diag::err_argument_required_after_attribute);
2078 delete AttrList;
2079 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002080 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2081 // normal declarators, not for abstract-declarators.
2082 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002083 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002084 }
2085
2086 // Finally, a normal, non-empty parameter type list.
2087
2088 // Build up an array of information about the parsed arguments.
2089 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002090
2091 // Enter function-declaration scope, limiting any declarators to the
2092 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002093 ParseScope PrototypeScope(this,
2094 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002095
2096 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002097 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002098 while (1) {
2099 if (Tok.is(tok::ellipsis)) {
2100 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002101 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002102 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002103 }
2104
Chris Lattner9f7564b2008-04-06 06:57:35 +00002105 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002106
Chris Lattner9f7564b2008-04-06 06:57:35 +00002107 // Parse the declaration-specifiers.
2108 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002109
2110 // If the caller parsed attributes for the first argument, add them now.
2111 if (AttrList) {
2112 DS.AddAttributes(AttrList);
2113 AttrList = 0; // Only apply the attributes to the first parameter.
2114 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002115 ParseDeclarationSpecifiers(DS);
2116
Chris Lattner9f7564b2008-04-06 06:57:35 +00002117 // Parse the declarator. This is "PrototypeContext", because we must
2118 // accept either 'declarator' or 'abstract-declarator' here.
2119 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2120 ParseDeclarator(ParmDecl);
2121
2122 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002123 if (Tok.is(tok::kw___attribute)) {
2124 SourceLocation Loc;
2125 AttributeList *AttrList = ParseAttributes(&Loc);
2126 ParmDecl.AddAttributes(AttrList, Loc);
2127 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002128
Chris Lattner9f7564b2008-04-06 06:57:35 +00002129 // Remember this parsed parameter in ParamInfo.
2130 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2131
Douglas Gregor605de8d2008-12-16 21:30:33 +00002132 // DefArgToks is used when the parsing of default arguments needs
2133 // to be delayed.
2134 CachedTokens *DefArgToks = 0;
2135
Chris Lattner9f7564b2008-04-06 06:57:35 +00002136 // If no parameter was specified, verify that *something* was specified,
2137 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002138 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2139 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002140 // Completely missing, emit error.
2141 Diag(DSStart, diag::err_missing_param);
2142 } else {
2143 // Otherwise, we have something. Add it and let semantic analysis try
2144 // to grok it and add the result to the ParamInfo we are building.
2145
2146 // Inform the actions module about the parameter declarator, so it gets
2147 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00002148 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2149
2150 // Parse the default argument, if any. We parse the default
2151 // arguments in all dialects; the semantic analysis in
2152 // ActOnParamDefaultArgument will reject the default argument in
2153 // C.
2154 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002155 SourceLocation EqualLoc = Tok.getLocation();
2156
Chris Lattner3e254fb2008-04-08 04:40:51 +00002157 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002158 if (D.getContext() == Declarator::MemberContext) {
2159 // If we're inside a class definition, cache the tokens
2160 // corresponding to the default argument. We'll actually parse
2161 // them when we see the end of the class definition.
2162 // FIXME: Templates will require something similar.
2163 // FIXME: Can we use a smart pointer for Toks?
2164 DefArgToks = new CachedTokens;
2165
2166 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2167 tok::semi, false)) {
2168 delete DefArgToks;
2169 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002170 Actions.ActOnParamDefaultArgumentError(Param);
2171 } else
2172 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002173 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002174 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002175 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002176
2177 OwningExprResult DefArgResult(ParseAssignmentExpression());
2178 if (DefArgResult.isInvalid()) {
2179 Actions.ActOnParamDefaultArgumentError(Param);
2180 SkipUntil(tok::comma, tok::r_paren, true, true);
2181 } else {
2182 // Inform the actions module about the default argument
2183 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
2184 DefArgResult.release());
2185 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002186 }
2187 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002188
2189 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002190 ParmDecl.getIdentifierLoc(), Param,
2191 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002192 }
2193
2194 // If the next token is a comma, consume it and keep reading arguments.
2195 if (Tok.isNot(tok::comma)) break;
2196
2197 // Consume the comma.
2198 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002199 }
2200
Chris Lattner9f7564b2008-04-06 06:57:35 +00002201 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002202 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002203
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002204 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002205 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002206
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002207 DeclSpec DS;
2208 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002209 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002210 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002211 if (!DS.getSourceRange().getEnd().isInvalid())
2212 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002213
2214 // Parse exception-specification[opt].
2215 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002216 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002217 }
2218
Chris Lattner4b009652007-07-25 00:24:17 +00002219 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002220 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002221 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002222 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002223 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002224 LParenLoc, D),
2225 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002226}
2227
Chris Lattner35d9c912008-04-06 06:34:08 +00002228/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2229/// we found a K&R-style identifier list instead of a type argument list. The
2230/// current token is known to be the first identifier in the list.
2231///
2232/// identifier-list: [C99 6.7.5]
2233/// identifier
2234/// identifier-list ',' identifier
2235///
2236void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2237 Declarator &D) {
2238 // Build up an array of information about the parsed arguments.
2239 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2240 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2241
2242 // If there was no identifier specified for the declarator, either we are in
2243 // an abstract-declarator, or we are in a parameter declarator which was found
2244 // to be abstract. In abstract-declarators, identifier lists are not valid:
2245 // diagnose this.
2246 if (!D.getIdentifier())
2247 Diag(Tok, diag::ext_ident_list_in_param);
2248
2249 // Tok is known to be the first identifier in the list. Remember this
2250 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002251 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002252 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2253 Tok.getLocation(), 0));
2254
Chris Lattner113a56b2008-04-06 06:39:19 +00002255 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002256
2257 while (Tok.is(tok::comma)) {
2258 // Eat the comma.
2259 ConsumeToken();
2260
Chris Lattner113a56b2008-04-06 06:39:19 +00002261 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002262 if (Tok.isNot(tok::identifier)) {
2263 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002264 SkipUntil(tok::r_paren);
2265 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002266 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002267
Chris Lattner35d9c912008-04-06 06:34:08 +00002268 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002269
2270 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002271 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002272 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002273
2274 // Verify that the argument identifier has not already been mentioned.
2275 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002276 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002277 } else {
2278 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002279 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2280 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00002281 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002282
2283 // Eat the identifier.
2284 ConsumeToken();
2285 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002286
2287 // If we have the closing ')', eat it and we're done.
2288 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2289
Chris Lattner113a56b2008-04-06 06:39:19 +00002290 // Remember that we parsed a function type, and remember the attributes. This
2291 // function type is always a K&R style function type, which is not varargs and
2292 // has no prototype.
2293 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002294 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002295 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002296 /*TypeQuals*/0, LParenLoc, D),
2297 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002298}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002299
Chris Lattner4b009652007-07-25 00:24:17 +00002300/// [C90] direct-declarator '[' constant-expression[opt] ']'
2301/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2302/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2303/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2304/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2305void Parser::ParseBracketDeclarator(Declarator &D) {
2306 SourceLocation StartLoc = ConsumeBracket();
2307
Chris Lattner1525c3a2008-12-18 07:27:21 +00002308 // C array syntax has many features, but by-far the most common is [] and [4].
2309 // This code does a fast path to handle some of the most obvious cases.
2310 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002311 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002312 // Remember that we parsed the empty array type.
2313 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002314 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2315 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002316 return;
2317 } else if (Tok.getKind() == tok::numeric_constant &&
2318 GetLookAheadToken(1).is(tok::r_square)) {
2319 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002320 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002321 ConsumeToken();
2322
Sebastian Redl0c986032009-02-09 18:23:29 +00002323 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002324
2325 // If there was an error parsing the assignment-expression, recover.
2326 if (ExprRes.isInvalid())
2327 ExprRes.release(); // Deallocate expr, just use [].
2328
2329 // Remember that we parsed a array type, and remember its features.
2330 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002331 ExprRes.release(), StartLoc),
2332 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002333 return;
2334 }
2335
Chris Lattner4b009652007-07-25 00:24:17 +00002336 // If valid, this location is the position where we read the 'static' keyword.
2337 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002338 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002339 StaticLoc = ConsumeToken();
2340
2341 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002342 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002343 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002344 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002345
2346 // If we haven't already read 'static', check to see if there is one after the
2347 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002348 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002349 StaticLoc = ConsumeToken();
2350
2351 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2352 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002353 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002354
2355 // Handle the case where we have '[*]' as the array size. However, a leading
2356 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2357 // the the token after the star is a ']'. Since stars in arrays are
2358 // infrequent, use of lookahead is not costly here.
2359 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002360 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002361
Chris Lattner306d4df2008-12-18 06:50:14 +00002362 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002363 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002364 StaticLoc = SourceLocation(); // Drop the static.
2365 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002366 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002367 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002368 // Note, in C89, this production uses the constant-expr production instead
2369 // of assignment-expr. The only difference is that assignment-expr allows
2370 // things like '=' and '*='. Sema rejects these in C89 mode because they
2371 // are not i-c-e's, so we don't need to distinguish between the two here.
2372
Chris Lattner4b009652007-07-25 00:24:17 +00002373 // Parse the assignment-expression now.
2374 NumElements = ParseAssignmentExpression();
2375 }
2376
2377 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002378 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002379 // If the expression was invalid, skip it.
2380 SkipUntil(tok::r_square);
2381 return;
2382 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002383
2384 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2385
Chris Lattner1525c3a2008-12-18 07:27:21 +00002386 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002387 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2388 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002389 NumElements.release(), StartLoc),
2390 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002391}
2392
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002393/// [GNU] typeof-specifier:
2394/// typeof ( expressions )
2395/// typeof ( type-name )
2396/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002397///
2398void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002399 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002400 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002401 SourceLocation StartLoc = ConsumeToken();
2402
Chris Lattner34a01ad2007-10-09 17:33:22 +00002403 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002404 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002405 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002406 return;
2407 }
2408
Sebastian Redl14ca7412008-12-11 21:36:32 +00002409 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002410 if (Result.isInvalid()) {
2411 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002412 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002413 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002414
2415 const char *PrevSpec = 0;
2416 // Check for duplicate type specifiers.
2417 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002418 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002419 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002420
2421 // FIXME: Not accurate, the range gets one token more than it should.
2422 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002423 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002424 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002425
Steve Naroff7cbb1462007-07-31 12:34:36 +00002426 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2427
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002428 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002429 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002430
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002431 assert((Ty.isInvalid() || Ty.get()) &&
2432 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002433
Chris Lattner34a01ad2007-10-09 17:33:22 +00002434 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002435 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002436 return;
2437 }
2438 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002439
2440 if (Ty.isInvalid())
2441 DS.SetTypeSpecError();
2442 else {
2443 const char *PrevSpec = 0;
2444 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2445 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2446 Ty.get()))
2447 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2448 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002449 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002450 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002451
2452 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002453 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002454 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002455 return;
2456 }
2457 RParenLoc = ConsumeParen();
2458 const char *PrevSpec = 0;
2459 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2460 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002461 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002462 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002463 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002464 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002465}
2466
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002467