blob: 6e0ffe5e2b68858daf5fa4d9874979c3b6e52127 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000018#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Douglas Gregor809070a2009-02-18 17:45:20 +000031Action::TypeResult Parser::ParseTypeName() {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Douglas Gregor809070a2009-02-18 17:45:20 +000040 if (DeclaratorInfo.getInvalidType())
41 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000044}
45
46/// ParseAttributes - Parse a non-empty attributes list.
47///
48/// [GNU] attributes:
49/// attribute
50/// attributes attribute
51///
52/// [GNU] attribute:
53/// '__attribute__' '(' '(' attribute-list ')' ')'
54///
55/// [GNU] attribute-list:
56/// attrib
57/// attribute_list ',' attrib
58///
59/// [GNU] attrib:
60/// empty
61/// attrib-name
62/// attrib-name '(' identifier ')'
63/// attrib-name '(' identifier ',' nonempty-expr-list ')'
64/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
65///
66/// [GNU] attrib-name:
67/// identifier
68/// typespec
69/// typequal
70/// storageclass
71///
72/// FIXME: The GCC grammar/code for this construct implies we need two
73/// token lookahead. Comment from gcc: "If they start with an identifier
74/// which is followed by a comma or close parenthesis, then the arguments
75/// start with that identifier; otherwise they are an expression list."
76///
77/// At the moment, I am not doing 2 token lookahead. I am also unaware of
78/// any attributes that don't work (based on my limited testing). Most
79/// attributes are very simple in practice. Until we find a bug, I don't see
80/// a pressing need to implement the 2 token lookahead.
81
Sebastian Redlab197ba2009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000084
85 AttributeList *CurrAttr = 0;
86
Chris Lattner04d66662007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000088 ConsumeToken();
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
90 "attribute")) {
91 SkipUntil(tok::r_paren, true); // skip until ) or ;
92 return CurrAttr;
93 }
94 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
96 return CurrAttr;
97 }
98 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000101
Chris Lattner04d66662007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
104 ConsumeToken();
105 continue;
106 }
107 // we have an identifier or declaration specifier (const, int, etc.)
108 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
109 SourceLocation AttrNameLoc = ConsumeToken();
110
111 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 ConsumeParen(); // ignore the left paren loc for now
114
Chris Lattner04d66662007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner04d66662007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 // __attribute__(( mode(byte) ))
121 ConsumeParen(); // ignore the right paren loc for now
122 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
123 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 }
Chris Lattner04d66662007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner04d66662007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 // __attribute__(( nonnull() ))
154 ConsumeParen(); // ignore the right paren loc for now
155 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
156 0, SourceLocation(), 0, 0, CurrAttr);
157 } else {
158 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 }
Chris Lattner04d66662007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 CurrAttr);
182 }
183 }
184 }
185 } else {
186 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
187 0, SourceLocation(), 0, 0, CurrAttr);
188 }
189 }
190 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000192 SourceLocation Loc = Tok.getLocation();;
193 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
194 SkipUntil(tok::r_paren, false);
195 }
196 if (EndLoc)
197 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 }
199 return CurrAttr;
200}
201
Steve Narofff59e17e2008-12-24 20:59:21 +0000202/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
203/// routine is called to skip/ignore tokens that comprise the MS declspec.
204void Parser::FuzzyParseMicrosoftDeclSpec() {
205 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
206 ConsumeToken();
207 if (Tok.is(tok::l_paren)) {
208 unsigned short savedParenCount = ParenCount;
209 do {
210 ConsumeAnyToken();
211 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
212 }
213 return;
214}
215
Reid Spencer5f016e22007-07-11 17:01:13 +0000216/// ParseDeclaration - Parse a full 'declaration', which consists of
217/// declaration-specifiers, some number of declarators, and a semicolon.
218/// 'Context' should be a Declarator::TheContext value.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000219///
220/// declaration: [C99 6.7]
221/// block-declaration ->
222/// simple-declaration
223/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000224/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000225/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000226/// [C++] using-directive
227/// [C++] using-declaration [TODO]
Chris Lattner8f08cb72007-08-25 06:57:03 +0000228/// others... [FIXME]
229///
Reid Spencer5f016e22007-07-11 17:01:13 +0000230Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner8f08cb72007-08-25 06:57:03 +0000231 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000232 case tok::kw_export:
233 case tok::kw_template:
Douglas Gregorcc636682009-02-17 23:15:12 +0000234 return ParseTemplateDeclarationOrSpecialization(Context);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000235 case tok::kw_namespace:
236 return ParseNamespace(Context);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000237 case tok::kw_using:
238 return ParseUsingDirectiveOrDeclaration(Context);
Chris Lattner8f08cb72007-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) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000255 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner8f08cb72007-08-25 06:57:03 +0000266
Reid Spencer5f016e22007-07-11 17:01:13 +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///
Reid Spencer5f016e22007-07-11 17:01:13 +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
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000280/// [C++] declarator initializer[opt]
281///
282/// [C++] initializer:
283/// [C++] '=' initializer-clause
284/// [C++] '(' expression-list ')'
Reid Spencer5f016e22007-07-11 17:01:13 +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 Dunbara80f8742008-08-05 01:35:17 +0000297 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000298 SourceLocation Loc;
299 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000300 if (AsmLabel.isInvalid()) {
Daniel Dunbara80f8742008-08-05 01:35:17 +0000301 SkipUntil(tok::semi);
302 return 0;
303 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000304
Sebastian Redleffa8d12008-12-10 00:02:53 +0000305 D.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000306 D.SetRangeEnd(Loc);
Daniel Dunbara80f8742008-08-05 01:35:17 +0000307 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000308
309 // If attributes are present, parse them.
Sebastian Redlab197ba2009-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 Naroffbb204692007-09-12 14:07:44 +0000315
316 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar914701e2008-08-05 16:28:08 +0000317 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000318
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // Parse declarator '=' initializer.
Chris Lattner04d66662007-10-09 17:33:22 +0000320 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000322 OwningExprResult Init(ParseInitializer());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000323 if (Init.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 SkipUntil(tok::semi);
325 return 0;
326 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000327 Actions.AddInitializerToDecl(LastDeclInGroup, move(Init));
Argyrios Kyrtzidis73a0d882008-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 Redla55e52c2008-11-25 22:21:31 +0000331 ExprVector Exprs(Actions);
Argyrios Kyrtzidis73a0d882008-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 Redla55e52c2008-11-25 22:21:31 +0000346 Exprs.take(), Exprs.size(),
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000347 &CommaLocs[0], RParenLoc);
348 }
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000349 } else {
350 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 }
352
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 // If we don't have a comma, it is either the end of the list (a ';') or an
354 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000355 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 break;
357
358 // Consume the comma.
359 ConsumeToken();
360
361 // Parse the next declarator.
362 D.clear();
Chris Lattneraab740a2008-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 Redlab197ba2009-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 Lattneraab740a2008-10-20 04:57:38 +0000376
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 ParseDeclarator(D);
378 }
379
Chris Lattner04d66662007-10-09 17:33:22 +0000380 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 ConsumeToken();
Fariborz Jahanian41f2b322009-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 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
388 }
Fariborz Jahanianbdd15f72008-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 Jahanian335a2d42008-01-04 23:04:08 +0000392 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000393 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
394 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 Diag(Tok, diag::err_parse_error);
396 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000397 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000398 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 ParseDeclarationSpecifiers(DS);
413
414 // Validate declspec for type-name.
415 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000416 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregorb48fe382008-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);
Reid Spencer5f016e22007-07-11 17:01:13 +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]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl669d5d72008-11-14 23:42:31 +0000453/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000454/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000455/// function-specifier: [C99 6.7.4]
456/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000457/// [C++] 'virtual'
458/// [C++] 'explicit'
Reid Spencer5f016e22007-07-11 17:01:13 +0000459///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000460void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Chris Lattner5e02c472009-01-05 00:07:25 +0000461 TemplateParameterLists *TemplateParams){
Chris Lattner81c018d2008-03-13 06:29:04 +0000462 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 while (1) {
464 int isInvalid = false;
465 const char *PrevSpec = 0;
466 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000469 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000470 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 // If this is not a declaration specifier token, we're done reading decl
472 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000473 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 return;
Chris Lattner5e02c472009-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;
Argyrios Kyrtzidiseb83ecd2008-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 Gregorb696ea32009-02-04 17:00:24 +0000502 Token Next = NextToken();
503 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
504 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000505
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000506 if (TypeRep == 0)
507 goto DoneWithDeclSpec;
508
509 ConsumeToken(); // The C++ scope.
510
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000511 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-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 Lattner80d0c892009-01-21 19:48:37 +0000521
522 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000523 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner80d0c892009-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 Lattner3bd934a2008-07-26 01:18:38 +0000544 // typedef-name
545 case tok::identifier: {
Chris Lattner5e02c472009-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 Lattner837acd02009-01-21 19:19:26 +0000548 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
549 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000550
Chris Lattner3bd934a2008-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 Gregorb696ea32009-02-04 17:00:24 +0000558 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
559 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000560
Chris Lattner3bd934a2008-07-26 01:18:38 +0000561 if (TypeRep == 0)
562 goto DoneWithDeclSpec;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000563
Douglas Gregorb48fe382008-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 Gregor3218c4b2009-01-09 22:42:13 +0000569 CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000570 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
571 NextToken().getKind() == tok::l_paren)
572 goto DoneWithDeclSpec;
573
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000574 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner3bd934a2008-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 Lattnerae4da612008-07-26 01:53:50 +0000590 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000591 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000592 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000593
594 DS.SetRangeEnd(EndProtoLoc);
595
Steve Naroff4f9b9f12008-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 Lattner3bd934a2008-07-26 01:18:38 +0000599 }
Douglas Gregor39a8de12009-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 // GNU attributes support.
620 case tok::kw___attribute:
621 DS.AddAttributes(ParseAttributes());
622 continue;
Steve Narofff59e17e2008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000630
Steve Naroff239f0732008-12-25 14:16:32 +0000631 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000632 case tok::kw___forceinline:
633 case tok::kw___w64:
Steve Naroff239f0732008-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner1ab3b962008-11-18 07:48:38 +0000648 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
650 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000651 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000652 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
653 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000654 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 case tok::kw_static:
656 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000657 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl669d5d72008-11-14 23:42:31 +0000666 case tok::kw_mutable:
667 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
668 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 case tok::kw___thread:
670 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
671 break;
672
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 continue;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000674
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 // function-specifier
676 case tok::kw_inline:
677 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
678 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000679 case tok::kw_virtual:
680 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
681 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000682 case tok::kw_explicit:
683 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
684 break;
Chris Lattner80d0c892009-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 Naroffd3ded1f2008-06-05 00:02:44 +0000770 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000771 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000772 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
773 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000774 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000775 goto DoneWithDeclSpec;
776
777 {
778 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000779 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000780 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000781 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000782 DS.SetRangeEnd(EndProtoLoc);
783
Chris Lattner1ab3b962008-11-18 07:48:38 +0000784 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
785 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-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 Naroffd3ded1f2008-06-05 00:02:44 +0000789 }
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner1ab3b962008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000799 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 ConsumeToken();
801 }
802}
Douglas Gregoradcac882008-12-01 23:54:00 +0000803
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000804/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-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 Lattner7a0ab5f2009-01-06 06:59:53 +0000846bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
847 const char *&PrevSpec,
848 TemplateParameterLists *TemplateParams){
Douglas Gregor12e083c2008-11-07 15:42:26 +0000849 SourceLocation Loc = Tok.getLocation();
850
851 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-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 Lattner7a0ab5f2009-01-06 06:59:53 +0000856 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-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 Lattner7a0ab5f2009-01-06 06:59:53 +0000867 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000868 // Otherwise, not a type specifier.
869 return false;
870
Douglas Gregor12e083c2008-11-07 15:42:26 +0000871 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000872 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000873 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000874 Tok.getAnnotationValue());
875 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
876 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-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 Gregorc4b4e7b2008-12-24 02:52:09 +0000951 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor12e083c2008-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 Naroff239f0732008-12-25 14:16:32 +0000978 case tok::kw___cdecl:
979 case tok::kw___stdcall:
980 case tok::kw___fastcall:
Chris Lattner837acd02009-01-21 19:19:26 +0000981 if (!PP.getLangOptions().Microsoft) return false;
982 ConsumeToken();
983 return true;
Steve Naroff239f0732008-12-25 14:16:32 +0000984
Douglas Gregor12e083c2008-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 Lattner1ab3b962008-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 Gregor12e083c2008-11-07 15:42:26 +0000997 }
998 DS.SetRangeEnd(Tok.getLocation());
999 ConsumeToken(); // whatever we parsed above.
1000 return true;
1001}
Reid Spencer5f016e22007-07-11 17:01:13 +00001002
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001003/// ParseStructDeclaration - Parse a struct declaration without the terminating
1004/// semicolon.
1005///
Reid Spencer5f016e22007-07-11 17:01:13 +00001006/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001007/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001008/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001009/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnere1359422008-04-10 06:46:29 +00001020void Parser::
1021ParseStructDeclaration(DeclSpec &DS,
1022 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-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 Naroff28a7ca82007-08-20 22:28:22 +00001026 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001027 return ParseStructDeclaration(DS, Fields);
1028 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001029
1030 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001031 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001032 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001033
Douglas Gregor4920f1f2009-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 Lattner04d66662007-10-09 17:33:22 +00001036 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001037 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001038 return;
1039 }
1040
1041 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001042 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001043 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001044 FieldDeclarator &DeclaratorInfo = Fields.back();
1045
Steve Naroff28a7ca82007-08-20 22:28:22 +00001046 /// struct-declarator: declarator
1047 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001048 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001049 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001050
Chris Lattner04d66662007-10-09 17:33:22 +00001051 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001052 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001053 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001054 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001055 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001056 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001057 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001058 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001059
Steve Naroff28a7ca82007-08-20 22:28:22 +00001060 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-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 Naroff28a7ca82007-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 Lattner04d66662007-10-09 17:33:22 +00001069 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001070 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001071
Steve Naroff28a7ca82007-08-20 22:28:22 +00001072 // Consume the comma.
1073 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001074
Steve Naroff28a7ca82007-08-20 22:28:22 +00001075 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001076 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001077
Steve Naroff28a7ca82007-08-20 22:28:22 +00001078 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-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 Naroff28a7ca82007-08-20 22:28:22 +00001084 }
Steve Naroff28a7ca82007-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 Lattner5a6ddbf2008-06-21 19:39:06 +00001095/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001096///
Reid Spencer5f016e22007-07-11 17:01:13 +00001097void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1098 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner27b7f102009-03-05 02:25:03 +00001099 PrettyStackTraceDecl CrashInfo(TagDecl, RecordLoc, Actions,
1100 PP.getSourceManager(),
1101 "parsing struct/union body");
1102
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 SourceLocation LBraceLoc = ConsumeBrace();
1104
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001105 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001106 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1107
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1109 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001110 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001111 Diag(Tok, diag::ext_empty_struct_union_enum)
1112 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001113
1114 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001115 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1116
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001118 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 // Each iteration of this loop reads one struct-declaration.
1120
1121 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001122 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 Diag(Tok, diag::ext_extra_struct_semi);
1124 ConsumeToken();
1125 continue;
1126 }
Chris Lattnere1359422008-04-10 06:46:29 +00001127
1128 // Parse all the comma separated declarators.
1129 DeclSpec DS;
1130 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-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 Gregor44b43212008-12-11 16:49:14 +00001138 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner5a6ddbf2008-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 Gregor44b43212008-12-11 16:49:14 +00001158 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1159 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-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 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001164
Chris Lattner04d66662007-10-09 17:33:22 +00001165 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001167 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001168 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff60fccee2007-10-29 21:38:07 +00001177 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001178
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 AttributeList *AttrList = 0;
1180 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001181 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001182 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001183
1184 Actions.ActOnFields(CurScope,
1185 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1186 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001187 AttrList);
1188 StructScope.Exit();
1189 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001190}
1191
1192
1193/// ParseEnumSpecifier
1194/// enum-specifier: [C99 6.7.2.2]
1195/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001196///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001197/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1198/// '}' attributes[opt]
1199/// 'enum' identifier
1200/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001201///
1202/// [C++] elaborated-type-specifier:
1203/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1204///
Reid Spencer5f016e22007-07-11 17:01:13 +00001205void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001206 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 SourceLocation StartLoc = ConsumeToken();
1208
1209 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-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();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001215
1216 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001217 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-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 }
Argyrios Kyrtzidise281b4c2008-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);
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 return;
Argyrios Kyrtzidise281b4c2008-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 Gregorddc29e12009-02-06 22:42:48 +00001262 SS, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001263
Chris Lattner04d66662007-10-09 17:33:22 +00001264 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner1ab3b962008-11-18 07:48:38 +00001270 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregor074149e2009-01-05 19:45:36 +00001284 // Enter the scope of the enum body and start the definition.
1285 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001286 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001287
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 SourceLocation LBraceLoc = ConsumeBrace();
1289
Chris Lattner7946dd32007-08-27 17:24:30 +00001290 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001291 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001292 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001293
1294 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1295
1296 DeclTy *LastEnumConstDecl = 0;
1297
1298 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001299 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1301 SourceLocation IdentLoc = ConsumeToken();
1302
1303 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001304 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001305 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001306 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001307 AssignedVal = ParseConstantExpression();
1308 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 }
1311
1312 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +00001313 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 LastEnumConstDecl,
1315 IdentLoc, Ident,
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001316 EqualLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001317 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 EnumConstantDecls.push_back(EnumConstDecl);
1319 LastEnumConstDecl = EnumConstDecl;
1320
Chris Lattner04d66662007-10-09 17:33:22 +00001321 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 break;
1323 SourceLocation CommaLoc = ConsumeToken();
1324
Chris Lattner04d66662007-10-09 17:33:22 +00001325 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1327 }
1328
1329 // Eat the }.
1330 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1331
Steve Naroff08d92e42007-09-15 18:49:24 +00001332 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 EnumConstantDecls.size());
1334
1335 DeclTy *AttrList = 0;
1336 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001337 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001339
1340 EnumScope.Exit();
1341 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001342}
1343
1344/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-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
Reid Spencer5f016e22007-07-11 17:01:13 +00001358/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001359bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001360 switch (Tok.getKind()) {
1361 default: return false;
Chris Lattner166a8fc2009-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
Reid Spencer5f016e22007-07-11 17:01:13 +00001382 // GNU attributes support.
1383 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001384 // GNU typeof support.
1385 case tok::kw_typeof:
1386
Reid Spencer5f016e22007-07-11 17:01:13 +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:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001396 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001397 case tok::kw_int:
1398 case tok::kw_float:
1399 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001400 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 case tok::kw__Bool:
1402 case tok::kw__Decimal32:
1403 case tok::kw__Decimal64:
1404 case tok::kw__Decimal128:
1405
Chris Lattner99dc9142008-04-13 18:59:07 +00001406 // struct-or-union-specifier (C99) or class-specifier (C++)
1407 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +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:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001417
1418 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001419 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 return true;
Chris Lattner7c186be2008-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 Naroff239f0732008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 }
1431}
1432
1433/// isDeclarationSpecifier() - Return true if the current token is part of a
1434/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001435bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 switch (Tok.getKind()) {
1437 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001438
1439 case tok::identifier: // foo::bar
1440 // Annotate typenames and C++ scope specifiers. If we get one, just
1441 // recurse to handle whatever we get.
1442 if (TryAnnotateTypeOrScopeToken())
1443 return isDeclarationSpecifier();
1444 // Otherwise, not a declaration specifier.
1445 return false;
1446 case tok::coloncolon: // ::foo::bar
1447 if (NextToken().is(tok::kw_new) || // ::new
1448 NextToken().is(tok::kw_delete)) // ::delete
1449 return false;
1450
1451 // Annotate typenames and C++ scope specifiers. If we get one, just
1452 // recurse to handle whatever we get.
1453 if (TryAnnotateTypeOrScopeToken())
1454 return isDeclarationSpecifier();
1455 // Otherwise, not a declaration specifier.
1456 return false;
1457
Reid Spencer5f016e22007-07-11 17:01:13 +00001458 // storage-class-specifier
1459 case tok::kw_typedef:
1460 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001461 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 case tok::kw_static:
1463 case tok::kw_auto:
1464 case tok::kw_register:
1465 case tok::kw___thread:
1466
1467 // type-specifiers
1468 case tok::kw_short:
1469 case tok::kw_long:
1470 case tok::kw_signed:
1471 case tok::kw_unsigned:
1472 case tok::kw__Complex:
1473 case tok::kw__Imaginary:
1474 case tok::kw_void:
1475 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001476 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 case tok::kw_int:
1478 case tok::kw_float:
1479 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001480 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001481 case tok::kw__Bool:
1482 case tok::kw__Decimal32:
1483 case tok::kw__Decimal64:
1484 case tok::kw__Decimal128:
1485
Chris Lattner99dc9142008-04-13 18:59:07 +00001486 // struct-or-union-specifier (C99) or class-specifier (C++)
1487 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 case tok::kw_struct:
1489 case tok::kw_union:
1490 // enum-specifier
1491 case tok::kw_enum:
1492
1493 // type-qualifier
1494 case tok::kw_const:
1495 case tok::kw_volatile:
1496 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001497
Reid Spencer5f016e22007-07-11 17:01:13 +00001498 // function-specifier
1499 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001500 case tok::kw_virtual:
1501 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001502
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001503 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001504 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001505
Chris Lattner1ef08762007-08-09 17:01:07 +00001506 // GNU typeof support.
1507 case tok::kw_typeof:
1508
1509 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001510 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001511 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001512
1513 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1514 case tok::less:
1515 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001516
Steve Naroff47f52092009-01-06 19:34:12 +00001517 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001518 case tok::kw___cdecl:
1519 case tok::kw___stdcall:
1520 case tok::kw___fastcall:
1521 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001522 }
1523}
1524
1525
1526/// ParseTypeQualifierListOpt
1527/// type-qualifier-list: [C99 6.7.5]
1528/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001529/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001530/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001531/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001532///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001533void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001534 while (1) {
1535 int isInvalid = false;
1536 const char *PrevSpec = 0;
1537 SourceLocation Loc = Tok.getLocation();
1538
1539 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 case tok::kw_const:
1541 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1542 getLang())*2;
1543 break;
1544 case tok::kw_volatile:
1545 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1546 getLang())*2;
1547 break;
1548 case tok::kw_restrict:
1549 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1550 getLang())*2;
1551 break;
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001552 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001553 case tok::kw___cdecl:
1554 case tok::kw___stdcall:
1555 case tok::kw___fastcall:
1556 if (!PP.getLangOptions().Microsoft)
1557 goto DoneWithTypeQuals;
1558 // Just ignore it.
1559 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001560 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001561 if (AttributesAllowed) {
1562 DS.AddAttributes(ParseAttributes());
1563 continue; // do *not* consume the next token!
1564 }
1565 // otherwise, FALL THROUGH!
1566 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001567 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001568 // If this is not a type-qualifier token, we're done reading type
1569 // qualifiers. First verify that DeclSpec's are consistent.
1570 DS.Finish(Diags, PP.getSourceManager(), getLang());
1571 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001572 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001573
Reid Spencer5f016e22007-07-11 17:01:13 +00001574 // If the specifier combination wasn't legal, issue a diagnostic.
1575 if (isInvalid) {
1576 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001577 // Pick between error or extwarn.
1578 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1579 : diag::ext_duplicate_declspec;
1580 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001581 }
1582 ConsumeToken();
1583 }
1584}
1585
1586
1587/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1588///
1589void Parser::ParseDeclarator(Declarator &D) {
1590 /// This implements the 'declarator' production in the C grammar, then checks
1591 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001592 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001593}
1594
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001595/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1596/// is parsed by the function passed to it. Pass null, and the direct-declarator
1597/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001598/// ptr-operator production.
1599///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001600/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1601/// [C] pointer[opt] direct-declarator
1602/// [C++] direct-declarator
1603/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001604///
1605/// pointer: [C99 6.7.5]
1606/// '*' type-qualifier-list[opt]
1607/// '*' type-qualifier-list[opt] pointer
1608///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001609/// ptr-operator:
1610/// '*' cv-qualifier-seq[opt]
1611/// '&'
1612/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001613/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001614void Parser::ParseDeclaratorInternal(Declarator &D,
1615 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001616
Sebastian Redlf30208a2009-01-24 21:16:55 +00001617 // C++ member pointers start with a '::' or a nested-name.
1618 // Member pointers get special handling, since there's no place for the
1619 // scope spec in the generic path below.
1620 if ((Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1621 Tok.is(tok::annot_cxxscope)) && getLang().CPlusPlus) {
1622 CXXScopeSpec SS;
1623 if (ParseOptionalCXXScopeSpecifier(SS)) {
1624 if(Tok.isNot(tok::star)) {
1625 // The scope spec really belongs to the direct-declarator.
1626 D.getCXXScopeSpec() = SS;
1627 if (DirectDeclParser)
1628 (this->*DirectDeclParser)(D);
1629 return;
1630 }
1631
1632 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001633 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001634 DeclSpec DS;
1635 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001636 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001637
1638 // Recurse to parse whatever is left.
1639 ParseDeclaratorInternal(D, DirectDeclParser);
1640
1641 // Sema will have to catch (syntactically invalid) pointers into global
1642 // scope. It has to catch pointers into namespace scope anyway.
1643 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001644 Loc, DS.TakeAttributes()),
1645 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00001646 return;
1647 }
1648 }
1649
1650 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001651 // Not a pointer, C++ reference, or block.
1652 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001653 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001654 if (DirectDeclParser)
1655 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001656 return;
1657 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001658
Steve Naroff4ef1c992008-08-28 10:07:06 +00001659 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001660 SourceLocation Loc = ConsumeToken(); // Eat the *, ^ or &.
1661 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001662
Steve Naroff4ef1c992008-08-28 10:07:06 +00001663 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner76549142008-02-21 01:32:26 +00001664 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001666
Reid Spencer5f016e22007-07-11 17:01:13 +00001667 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001668 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001669
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001671 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001672 if (Kind == tok::star)
1673 // Remember that we parsed a pointer type, and remember the type-quals.
1674 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001675 DS.TakeAttributes()),
1676 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00001677 else
1678 // Remember that we parsed a Block type, and remember the type-quals.
1679 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001680 Loc),
1681 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001682 } else {
1683 // Is a reference
1684 DeclSpec DS;
1685
1686 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1687 // cv-qualifiers are introduced through the use of a typedef or of a
1688 // template type argument, in which case the cv-qualifiers are ignored.
1689 //
1690 // [GNU] Retricted references are allowed.
1691 // [GNU] Attributes on references are allowed.
1692 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001693 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001694
1695 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1696 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1697 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001698 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001699 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1700 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001701 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001702 }
1703
1704 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001705 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001706
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001707 if (D.getNumTypeObjects() > 0) {
1708 // C++ [dcl.ref]p4: There shall be no references to references.
1709 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1710 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001711 if (const IdentifierInfo *II = D.getIdentifier())
1712 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1713 << II;
1714 else
1715 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1716 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001717
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001718 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001719 // can go ahead and build the (technically ill-formed)
1720 // declarator: reference collapsing will take care of it.
1721 }
1722 }
1723
Reid Spencer5f016e22007-07-11 17:01:13 +00001724 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001725 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001726 DS.TakeAttributes()),
1727 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001728 }
1729}
1730
1731/// ParseDirectDeclarator
1732/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001733/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001734/// '(' declarator ')'
1735/// [GNU] '(' attributes declarator ')'
1736/// [C90] direct-declarator '[' constant-expression[opt] ']'
1737/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1738/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1739/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1740/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1741/// direct-declarator '(' parameter-type-list ')'
1742/// direct-declarator '(' identifier-list[opt] ')'
1743/// [GNU] direct-declarator '(' parameter-forward-declarations
1744/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001745/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1746/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001747/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001748///
1749/// declarator-id: [C++ 8]
1750/// id-expression
1751/// '::'[opt] nested-name-specifier[opt] type-name
1752///
1753/// id-expression: [C++ 5.1]
1754/// unqualified-id
1755/// qualified-id [TODO]
1756///
1757/// unqualified-id: [C++ 5.1]
1758/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001759/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001760/// conversion-function-id [TODO]
1761/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00001762/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001763///
Reid Spencer5f016e22007-07-11 17:01:13 +00001764void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001765 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001766
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001767 if (getLang().CPlusPlus) {
1768 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001769 // ParseDeclaratorInternal might already have parsed the scope.
1770 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1771 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001772 if (afterCXXScope) {
1773 // Change the declaration context for name lookup, until this function
1774 // is exited (and the declarator has been parsed).
1775 DeclScopeObj.EnterDeclaratorScope();
1776 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001777
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001778 if (Tok.is(tok::identifier)) {
1779 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001780
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001781 // If this identifier is the name of the current class, it's a
1782 // constructor name.
Douglas Gregor39a8de12009-02-25 19:37:18 +00001783 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroffb43a50f2009-01-28 19:39:02 +00001784 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +00001785 Tok.getLocation(), CurScope),
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001786 Tok.getLocation());
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001787 // This is a normal identifier.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001788 } else
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001789 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1790 ConsumeToken();
1791 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001792 } else if (Tok.is(tok::annot_template_id)) {
1793 TemplateIdAnnotation *TemplateId
1794 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1795
1796 // FIXME: Could this template-id name a constructor?
1797
1798 // FIXME: This is an egregious hack, where we silently ignore
1799 // the specialization (which should be a function template
1800 // specialization name) and use the name instead. This hack
1801 // will go away when we have support for function
1802 // specializations.
1803 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1804 TemplateId->Destroy();
1805 ConsumeToken();
1806 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00001807 } else if (Tok.is(tok::kw_operator)) {
1808 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001809 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001810
Douglas Gregor70316a02008-12-26 15:00:45 +00001811 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00001812 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1813 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00001814 } else {
1815 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00001816 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1817 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1818 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00001819 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001820 }
Douglas Gregor70316a02008-12-26 15:00:45 +00001821 }
1822 goto PastIdentifier;
1823 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001824 // This should be a C++ destructor.
1825 SourceLocation TildeLoc = ConsumeToken();
1826 if (Tok.is(tok::identifier)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001827 // FIXME: Inaccurate.
1828 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00001829 SourceLocation EndLoc;
1830 if (TypeTy *Type = ParseClassName(EndLoc)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001831 D.setDestructor(Type, TildeLoc, NameLoc);
1832 } else {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001833 D.SetIdentifier(0, TildeLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001834 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001835 } else {
1836 Diag(Tok, diag::err_expected_class_name);
1837 D.SetIdentifier(0, TildeLoc);
1838 }
1839 goto PastIdentifier;
1840 }
1841
1842 // If we reached this point, token is not identifier and not '~'.
1843
1844 if (afterCXXScope) {
1845 Diag(Tok, diag::err_expected_unqualified_id);
1846 D.SetIdentifier(0, Tok.getLocation());
1847 D.setInvalidType(true);
1848 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001849 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001850 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001851 }
1852
1853 // If we reached this point, we are either in C/ObjC or the token didn't
1854 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001855 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1856 assert(!getLang().CPlusPlus &&
1857 "There's a C++-specific check for tok::identifier above");
1858 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1859 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1860 ConsumeToken();
1861 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001862 // direct-declarator: '(' declarator ')'
1863 // direct-declarator: '(' attributes declarator ')'
1864 // Example: 'char (*X)' or 'int (*XX)(void)'
1865 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001866 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001867 // This could be something simple like "int" (in which case the declarator
1868 // portion is empty), if an abstract-declarator is allowed.
1869 D.SetIdentifier(0, Tok.getLocation());
1870 } else {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001871 if (getLang().CPlusPlus)
1872 Diag(Tok, diag::err_expected_unqualified_id);
1873 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00001874 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00001875 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00001876 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001877 }
1878
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001879 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00001880 assert(D.isPastIdentifier() &&
1881 "Haven't past the location of the identifier yet?");
1882
1883 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001884 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001885 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1886 // In such a case, check if we actually have a function declarator; if it
1887 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00001888 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1889 // When not in file scope, warn for ambiguous function declarators, just
1890 // in case the author intended it as a variable definition.
1891 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1892 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1893 break;
1894 }
Chris Lattneref4715c2008-04-06 05:45:57 +00001895 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001896 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 ParseBracketDeclarator(D);
1898 } else {
1899 break;
1900 }
1901 }
1902}
1903
Chris Lattneref4715c2008-04-06 05:45:57 +00001904/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1905/// only called before the identifier, so these are most likely just grouping
1906/// parens for precedence. If we find that these are actually function
1907/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1908///
1909/// direct-declarator:
1910/// '(' declarator ')'
1911/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00001912/// direct-declarator '(' parameter-type-list ')'
1913/// direct-declarator '(' identifier-list[opt] ')'
1914/// [GNU] direct-declarator '(' parameter-forward-declarations
1915/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00001916///
1917void Parser::ParseParenDeclarator(Declarator &D) {
1918 SourceLocation StartLoc = ConsumeParen();
1919 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1920
Chris Lattner7399ee02008-10-20 02:05:46 +00001921 // Eat any attributes before we look at whether this is a grouping or function
1922 // declarator paren. If this is a grouping paren, the attribute applies to
1923 // the type being built up, for example:
1924 // int (__attribute__(()) *x)(long y)
1925 // If this ends up not being a grouping paren, the attribute applies to the
1926 // first argument, for example:
1927 // int (__attribute__(()) int x)
1928 // In either case, we need to eat any attributes to be able to determine what
1929 // sort of paren this is.
1930 //
1931 AttributeList *AttrList = 0;
1932 bool RequiresArg = false;
1933 if (Tok.is(tok::kw___attribute)) {
1934 AttrList = ParseAttributes();
1935
1936 // We require that the argument list (if this is a non-grouping paren) be
1937 // present even if the attribute list was empty.
1938 RequiresArg = true;
1939 }
Steve Naroff239f0732008-12-25 14:16:32 +00001940 // Eat any Microsoft extensions.
Douglas Gregor5a2f5d32009-01-10 00:48:18 +00001941 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1942 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff239f0732008-12-25 14:16:32 +00001943 ConsumeToken();
Chris Lattner7399ee02008-10-20 02:05:46 +00001944
Chris Lattneref4715c2008-04-06 05:45:57 +00001945 // If we haven't past the identifier yet (or where the identifier would be
1946 // stored, if this is an abstract declarator), then this is probably just
1947 // grouping parens. However, if this could be an abstract-declarator, then
1948 // this could also be the start of function arguments (consider 'void()').
1949 bool isGrouping;
1950
1951 if (!D.mayOmitIdentifier()) {
1952 // If this can't be an abstract-declarator, this *must* be a grouping
1953 // paren, because we haven't seen the identifier yet.
1954 isGrouping = true;
1955 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001956 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00001957 isDeclarationSpecifier()) { // 'int(int)' is a function.
1958 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1959 // considered to be a type, not a K&R identifier-list.
1960 isGrouping = false;
1961 } else {
1962 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1963 isGrouping = true;
1964 }
1965
1966 // If this is a grouping paren, handle:
1967 // direct-declarator: '(' declarator ')'
1968 // direct-declarator: '(' attributes declarator ')'
1969 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001970 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001971 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00001972 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00001973 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001974
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001975 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00001976 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001977 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001978
1979 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001980 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00001981 return;
1982 }
1983
1984 // Okay, if this wasn't a grouping paren, it must be the start of a function
1985 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00001986 // identifier (and remember where it would have been), then call into
1987 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00001988 D.SetIdentifier(0, Tok.getLocation());
1989
Chris Lattner7399ee02008-10-20 02:05:46 +00001990 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00001991}
1992
1993/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1994/// declarator D up to a paren, which indicates that we are parsing function
1995/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00001996///
Chris Lattner7399ee02008-10-20 02:05:46 +00001997/// If AttrList is non-null, then the caller parsed those arguments immediately
1998/// after the open paren - they should be considered to be the first argument of
1999/// a parameter. If RequiresArg is true, then the first argument of the
2000/// function is required to be present and required to not be an identifier
2001/// list.
2002///
Reid Spencer5f016e22007-07-11 17:01:13 +00002003/// This method also handles this portion of the grammar:
2004/// parameter-type-list: [C99 6.7.5]
2005/// parameter-list
2006/// parameter-list ',' '...'
2007///
2008/// parameter-list: [C99 6.7.5]
2009/// parameter-declaration
2010/// parameter-list ',' parameter-declaration
2011///
2012/// parameter-declaration: [C99 6.7.5]
2013/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002014/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002015/// [GNU] declaration-specifiers declarator attributes
2016/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002017/// [C++] declaration-specifiers abstract-declarator[opt]
2018/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002019/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2020///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002021/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
2022/// and "exception-specification[opt]"(TODO).
2023///
Chris Lattner7399ee02008-10-20 02:05:46 +00002024void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2025 AttributeList *AttrList,
2026 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002027 // lparen is already consumed!
2028 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00002029
Chris Lattner7399ee02008-10-20 02:05:46 +00002030 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002031 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002032 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002033 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002034 delete AttrList;
2035 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002036
Sebastian Redlab197ba2009-02-09 18:23:29 +00002037 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002038
2039 // cv-qualifier-seq[opt].
2040 DeclSpec DS;
2041 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002042 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002043 if (!DS.getSourceRange().getEnd().isInvalid())
2044 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002045
2046 // Parse exception-specification[opt].
2047 if (Tok.is(tok::kw_throw))
Sebastian Redlab197ba2009-02-09 18:23:29 +00002048 ParseExceptionSpecification(Loc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002049 }
2050
Chris Lattnerf97409f2008-04-06 06:57:35 +00002051 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002053 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002054 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002055 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002056 /*arglist*/ 0, 0,
2057 DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002058 LParenLoc, D),
2059 Loc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002060 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00002061 }
2062
2063 // Alternatively, this parameter list may be an identifier list form for a
2064 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002065 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002066 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002067 // K&R identifier lists can't have typedefs as identifiers, per
2068 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002069 if (RequiresArg) {
2070 Diag(Tok, diag::err_argument_required_after_attribute);
2071 delete AttrList;
2072 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002073 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2074 // normal declarators, not for abstract-declarators.
2075 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002076 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002077 }
2078
2079 // Finally, a normal, non-empty parameter type list.
2080
2081 // Build up an array of information about the parsed arguments.
2082 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002083
2084 // Enter function-declaration scope, limiting any declarators to the
2085 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002086 ParseScope PrototypeScope(this,
2087 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002088
2089 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002090 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002091 while (1) {
2092 if (Tok.is(tok::ellipsis)) {
2093 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002094 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002095 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002096 }
2097
Chris Lattnerf97409f2008-04-06 06:57:35 +00002098 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002099
Chris Lattnerf97409f2008-04-06 06:57:35 +00002100 // Parse the declaration-specifiers.
2101 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002102
2103 // If the caller parsed attributes for the first argument, add them now.
2104 if (AttrList) {
2105 DS.AddAttributes(AttrList);
2106 AttrList = 0; // Only apply the attributes to the first parameter.
2107 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002108 ParseDeclarationSpecifiers(DS);
2109
Chris Lattnerf97409f2008-04-06 06:57:35 +00002110 // Parse the declarator. This is "PrototypeContext", because we must
2111 // accept either 'declarator' or 'abstract-declarator' here.
2112 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2113 ParseDeclarator(ParmDecl);
2114
2115 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002116 if (Tok.is(tok::kw___attribute)) {
2117 SourceLocation Loc;
2118 AttributeList *AttrList = ParseAttributes(&Loc);
2119 ParmDecl.AddAttributes(AttrList, Loc);
2120 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002121
Chris Lattnerf97409f2008-04-06 06:57:35 +00002122 // Remember this parsed parameter in ParamInfo.
2123 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2124
Douglas Gregor72b505b2008-12-16 21:30:33 +00002125 // DefArgToks is used when the parsing of default arguments needs
2126 // to be delayed.
2127 CachedTokens *DefArgToks = 0;
2128
Chris Lattnerf97409f2008-04-06 06:57:35 +00002129 // If no parameter was specified, verify that *something* was specified,
2130 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002131 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2132 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002133 // Completely missing, emit error.
2134 Diag(DSStart, diag::err_missing_param);
2135 } else {
2136 // Otherwise, we have something. Add it and let semantic analysis try
2137 // to grok it and add the result to the ParamInfo we are building.
2138
2139 // Inform the actions module about the parameter declarator, so it gets
2140 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00002141 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2142
2143 // Parse the default argument, if any. We parse the default
2144 // arguments in all dialects; the semantic analysis in
2145 // ActOnParamDefaultArgument will reject the default argument in
2146 // C.
2147 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002148 SourceLocation EqualLoc = Tok.getLocation();
2149
Chris Lattner04421082008-04-08 04:40:51 +00002150 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002151 if (D.getContext() == Declarator::MemberContext) {
2152 // If we're inside a class definition, cache the tokens
2153 // corresponding to the default argument. We'll actually parse
2154 // them when we see the end of the class definition.
2155 // FIXME: Templates will require something similar.
2156 // FIXME: Can we use a smart pointer for Toks?
2157 DefArgToks = new CachedTokens;
2158
2159 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2160 tok::semi, false)) {
2161 delete DefArgToks;
2162 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002163 Actions.ActOnParamDefaultArgumentError(Param);
2164 } else
2165 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner04421082008-04-08 04:40:51 +00002166 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002167 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002168 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002169
2170 OwningExprResult DefArgResult(ParseAssignmentExpression());
2171 if (DefArgResult.isInvalid()) {
2172 Actions.ActOnParamDefaultArgumentError(Param);
2173 SkipUntil(tok::comma, tok::r_paren, true, true);
2174 } else {
2175 // Inform the actions module about the default argument
2176 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
2177 DefArgResult.release());
2178 }
Chris Lattner04421082008-04-08 04:40:51 +00002179 }
2180 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002181
2182 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002183 ParmDecl.getIdentifierLoc(), Param,
2184 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002185 }
2186
2187 // If the next token is a comma, consume it and keep reading arguments.
2188 if (Tok.isNot(tok::comma)) break;
2189
2190 // Consume the comma.
2191 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002192 }
2193
Chris Lattnerf97409f2008-04-06 06:57:35 +00002194 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002195 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002196
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002197 // If we have the closing ')', eat it.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002198 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002199
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002200 DeclSpec DS;
2201 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002202 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002203 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002204 if (!DS.getSourceRange().getEnd().isInvalid())
2205 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002206
2207 // Parse exception-specification[opt].
2208 if (Tok.is(tok::kw_throw))
Sebastian Redlab197ba2009-02-09 18:23:29 +00002209 ParseExceptionSpecification(Loc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002210 }
2211
Reid Spencer5f016e22007-07-11 17:01:13 +00002212 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002213 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002214 EllipsisLoc,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002215 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002216 DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002217 LParenLoc, D),
2218 Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002219}
2220
Chris Lattner66d28652008-04-06 06:34:08 +00002221/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2222/// we found a K&R-style identifier list instead of a type argument list. The
2223/// current token is known to be the first identifier in the list.
2224///
2225/// identifier-list: [C99 6.7.5]
2226/// identifier
2227/// identifier-list ',' identifier
2228///
2229void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2230 Declarator &D) {
2231 // Build up an array of information about the parsed arguments.
2232 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2233 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2234
2235 // If there was no identifier specified for the declarator, either we are in
2236 // an abstract-declarator, or we are in a parameter declarator which was found
2237 // to be abstract. In abstract-declarators, identifier lists are not valid:
2238 // diagnose this.
2239 if (!D.getIdentifier())
2240 Diag(Tok, diag::ext_ident_list_in_param);
2241
2242 // Tok is known to be the first identifier in the list. Remember this
2243 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002244 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002245 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2246 Tok.getLocation(), 0));
2247
Chris Lattner50c64772008-04-06 06:39:19 +00002248 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002249
2250 while (Tok.is(tok::comma)) {
2251 // Eat the comma.
2252 ConsumeToken();
2253
Chris Lattner50c64772008-04-06 06:39:19 +00002254 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002255 if (Tok.isNot(tok::identifier)) {
2256 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002257 SkipUntil(tok::r_paren);
2258 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002259 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002260
Chris Lattner66d28652008-04-06 06:34:08 +00002261 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002262
2263 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002264 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002265 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002266
2267 // Verify that the argument identifier has not already been mentioned.
2268 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002269 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002270 } else {
2271 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002272 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2273 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00002274 }
Chris Lattner66d28652008-04-06 06:34:08 +00002275
2276 // Eat the identifier.
2277 ConsumeToken();
2278 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002279
2280 // If we have the closing ')', eat it and we're done.
2281 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2282
Chris Lattner50c64772008-04-06 06:39:19 +00002283 // Remember that we parsed a function type, and remember the attributes. This
2284 // function type is always a K&R style function type, which is not varargs and
2285 // has no prototype.
2286 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002287 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002288 &ParamInfo[0], ParamInfo.size(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002289 /*TypeQuals*/0, LParenLoc, D),
2290 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002291}
Chris Lattneref4715c2008-04-06 05:45:57 +00002292
Reid Spencer5f016e22007-07-11 17:01:13 +00002293/// [C90] direct-declarator '[' constant-expression[opt] ']'
2294/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2295/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2296/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2297/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2298void Parser::ParseBracketDeclarator(Declarator &D) {
2299 SourceLocation StartLoc = ConsumeBracket();
2300
Chris Lattner378c7e42008-12-18 07:27:21 +00002301 // C array syntax has many features, but by-far the most common is [] and [4].
2302 // This code does a fast path to handle some of the most obvious cases.
2303 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002304 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002305 // Remember that we parsed the empty array type.
2306 OwningExprResult NumElements(Actions);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002307 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2308 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002309 return;
2310 } else if (Tok.getKind() == tok::numeric_constant &&
2311 GetLookAheadToken(1).is(tok::r_square)) {
2312 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002313 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002314 ConsumeToken();
2315
Sebastian Redlab197ba2009-02-09 18:23:29 +00002316 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002317
2318 // If there was an error parsing the assignment-expression, recover.
2319 if (ExprRes.isInvalid())
2320 ExprRes.release(); // Deallocate expr, just use [].
2321
2322 // Remember that we parsed a array type, and remember its features.
2323 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002324 ExprRes.release(), StartLoc),
2325 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002326 return;
2327 }
2328
Reid Spencer5f016e22007-07-11 17:01:13 +00002329 // If valid, this location is the position where we read the 'static' keyword.
2330 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002331 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002332 StaticLoc = ConsumeToken();
2333
2334 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002335 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002336 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002337 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002338
2339 // If we haven't already read 'static', check to see if there is one after the
2340 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002341 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002342 StaticLoc = ConsumeToken();
2343
2344 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2345 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002346 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002347
2348 // Handle the case where we have '[*]' as the array size. However, a leading
2349 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2350 // the the token after the star is a ']'. Since stars in arrays are
2351 // infrequent, use of lookahead is not costly here.
2352 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002353 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002354
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002355 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002356 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002357 StaticLoc = SourceLocation(); // Drop the static.
2358 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002359 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002360 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002361 // Note, in C89, this production uses the constant-expr production instead
2362 // of assignment-expr. The only difference is that assignment-expr allows
2363 // things like '=' and '*='. Sema rejects these in C89 mode because they
2364 // are not i-c-e's, so we don't need to distinguish between the two here.
2365
Reid Spencer5f016e22007-07-11 17:01:13 +00002366 // Parse the assignment-expression now.
2367 NumElements = ParseAssignmentExpression();
2368 }
2369
2370 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002371 if (NumElements.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002372 // If the expression was invalid, skip it.
2373 SkipUntil(tok::r_square);
2374 return;
2375 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002376
2377 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2378
Chris Lattner378c7e42008-12-18 07:27:21 +00002379 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002380 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2381 StaticLoc.isValid(), isStar,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002382 NumElements.release(), StartLoc),
2383 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002384}
2385
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002386/// [GNU] typeof-specifier:
2387/// typeof ( expressions )
2388/// typeof ( type-name )
2389/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002390///
2391void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002392 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002393 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002394 SourceLocation StartLoc = ConsumeToken();
2395
Chris Lattner04d66662007-10-09 17:33:22 +00002396 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002397 if (!getLang().CPlusPlus) {
Chris Lattner08631c52008-11-23 21:45:46 +00002398 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002399 return;
2400 }
2401
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002402 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor809070a2009-02-18 17:45:20 +00002403 if (Result.isInvalid()) {
2404 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002405 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002406 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002407
2408 const char *PrevSpec = 0;
2409 // Check for duplicate type specifiers.
2410 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002411 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002412 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002413
2414 // FIXME: Not accurate, the range gets one token more than it should.
2415 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002416 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002417 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002418
Steve Naroffd1861fd2007-07-31 12:34:36 +00002419 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2420
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00002421 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00002422 Action::TypeResult Ty = ParseTypeName();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002423
Douglas Gregor809070a2009-02-18 17:45:20 +00002424 assert((Ty.isInvalid() || Ty.get()) &&
2425 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002426
Chris Lattner04d66662007-10-09 17:33:22 +00002427 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002428 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002429 return;
2430 }
2431 RParenLoc = ConsumeParen();
Douglas Gregor809070a2009-02-18 17:45:20 +00002432
2433 if (Ty.isInvalid())
2434 DS.SetTypeSpecError();
2435 else {
2436 const char *PrevSpec = 0;
2437 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2438 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2439 Ty.get()))
2440 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2441 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00002442 } else { // we have an expression.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002443 OwningExprResult Result(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002444
2445 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002446 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor809070a2009-02-18 17:45:20 +00002447 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002448 return;
2449 }
2450 RParenLoc = ConsumeParen();
2451 const char *PrevSpec = 0;
2452 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2453 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002454 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002455 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002456 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00002457 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002458}
2459
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002460