blob: 243a15f3dbd6b25f7c86718cf28436712708e2ce [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattnera7549902007-08-26 06:24:45 +000017#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000018#include "ExtensionRAIIObject.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
29Parser::TypeTy *Parser::ParseTypeName() {
30 // Parse the common declaration-specifiers piece.
31 DeclSpec DS;
32 ParseSpecifierQualifierList(DS);
33
34 // Parse the abstract-declarator, if present.
35 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
36 ParseDeclarator(DeclaratorInfo);
37
Steve Naroff0acc9c92007-09-15 18:49:24 +000038 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattner4b009652007-07-25 00:24:17 +000039}
40
41/// ParseAttributes - Parse a non-empty attributes list.
42///
43/// [GNU] attributes:
44/// attribute
45/// attributes attribute
46///
47/// [GNU] attribute:
48/// '__attribute__' '(' '(' attribute-list ')' ')'
49///
50/// [GNU] attribute-list:
51/// attrib
52/// attribute_list ',' attrib
53///
54/// [GNU] attrib:
55/// empty
56/// attrib-name
57/// attrib-name '(' identifier ')'
58/// attrib-name '(' identifier ',' nonempty-expr-list ')'
59/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
60///
61/// [GNU] attrib-name:
62/// identifier
63/// typespec
64/// typequal
65/// storageclass
66///
67/// FIXME: The GCC grammar/code for this construct implies we need two
68/// token lookahead. Comment from gcc: "If they start with an identifier
69/// which is followed by a comma or close parenthesis, then the arguments
70/// start with that identifier; otherwise they are an expression list."
71///
72/// At the moment, I am not doing 2 token lookahead. I am also unaware of
73/// any attributes that don't work (based on my limited testing). Most
74/// attributes are very simple in practice. Until we find a bug, I don't see
75/// a pressing need to implement the 2 token lookahead.
76
77AttributeList *Parser::ParseAttributes() {
Chris Lattner34a01ad2007-10-09 17:33:22 +000078 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000079
80 AttributeList *CurrAttr = 0;
81
Chris Lattner34a01ad2007-10-09 17:33:22 +000082 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000083 ConsumeToken();
84 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
85 "attribute")) {
86 SkipUntil(tok::r_paren, true); // skip until ) or ;
87 return CurrAttr;
88 }
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
90 SkipUntil(tok::r_paren, true); // skip until ) or ;
91 return CurrAttr;
92 }
93 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000094 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
95 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000096
Chris Lattner34a01ad2007-10-09 17:33:22 +000097 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000098 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
99 ConsumeToken();
100 continue;
101 }
102 // we have an identifier or declaration specifier (const, int, etc.)
103 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
104 SourceLocation AttrNameLoc = ConsumeToken();
105
106 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000107 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000108 ConsumeParen(); // ignore the left paren loc for now
109
Chris Lattner34a01ad2007-10-09 17:33:22 +0000110 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000111 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
112 SourceLocation ParmLoc = ConsumeToken();
113
Chris Lattner34a01ad2007-10-09 17:33:22 +0000114 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000115 // __attribute__(( mode(byte) ))
116 ConsumeParen(); // ignore the right paren loc for now
117 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
118 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000119 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000120 ConsumeToken();
121 // __attribute__(( format(printf, 1, 2) ))
122 llvm::SmallVector<ExprTy*, 8> ArgExprs;
123 bool ArgExprsOk = true;
124
125 // now parse the non-empty comma separated list of expressions
126 while (1) {
127 ExprResult ArgExpr = ParseAssignmentExpression();
128 if (ArgExpr.isInvalid) {
129 ArgExprsOk = false;
130 SkipUntil(tok::r_paren);
131 break;
132 } else {
133 ArgExprs.push_back(ArgExpr.Val);
134 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000135 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000136 break;
137 ConsumeToken(); // Eat the comma, move to the next argument
138 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000139 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000140 ConsumeParen(); // ignore the right paren loc for now
141 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
142 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
143 }
144 }
145 } else { // not an identifier
146 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000147 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000148 // __attribute__(( nonnull() ))
149 ConsumeParen(); // ignore the right paren loc for now
150 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
151 0, SourceLocation(), 0, 0, CurrAttr);
152 } else {
153 // __attribute__(( aligned(16) ))
154 llvm::SmallVector<ExprTy*, 8> ArgExprs;
155 bool ArgExprsOk = true;
156
157 // now parse the list of expressions
158 while (1) {
159 ExprResult ArgExpr = ParseAssignmentExpression();
160 if (ArgExpr.isInvalid) {
161 ArgExprsOk = false;
162 SkipUntil(tok::r_paren);
163 break;
164 } else {
165 ArgExprs.push_back(ArgExpr.Val);
166 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000167 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000168 break;
169 ConsumeToken(); // Eat the comma, move to the next argument
170 }
171 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000172 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000173 ConsumeParen(); // ignore the right paren loc for now
174 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
175 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
176 CurrAttr);
177 }
178 }
179 }
180 } else {
181 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
182 0, SourceLocation(), 0, 0, CurrAttr);
183 }
184 }
185 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
186 SkipUntil(tok::r_paren, false);
187 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
188 SkipUntil(tok::r_paren, false);
189 }
190 return CurrAttr;
191}
192
193/// ParseDeclaration - Parse a full 'declaration', which consists of
194/// declaration-specifiers, some number of declarators, and a semicolon.
195/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000196///
197/// declaration: [C99 6.7]
198/// block-declaration ->
199/// simple-declaration
200/// others [FIXME]
201/// [C++] namespace-definition
202/// others... [FIXME]
203///
Chris Lattner4b009652007-07-25 00:24:17 +0000204Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000205 switch (Tok.getKind()) {
206 case tok::kw_namespace:
207 return ParseNamespace(Context);
208 default:
209 return ParseSimpleDeclaration(Context);
210 }
211}
212
213/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
214/// declaration-specifiers init-declarator-list[opt] ';'
215///[C90/C++]init-declarator-list ';' [TODO]
216/// [OMP] threadprivate-directive [TODO]
217Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000218 // Parse the common declaration-specifiers piece.
219 DeclSpec DS;
220 ParseDeclarationSpecifiers(DS);
221
222 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
223 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000224 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000225 ConsumeToken();
226 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
227 }
228
229 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
230 ParseDeclarator(DeclaratorInfo);
231
232 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
233}
234
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000235
Chris Lattner4b009652007-07-25 00:24:17 +0000236/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
237/// parsing 'declaration-specifiers declarator'. This method is split out this
238/// way to handle the ambiguity between top-level function-definitions and
239/// declarations.
240///
Chris Lattner4b009652007-07-25 00:24:17 +0000241/// init-declarator-list: [C99 6.7]
242/// init-declarator
243/// init-declarator-list ',' init-declarator
244/// init-declarator: [C99 6.7]
245/// declarator
246/// declarator '=' initializer
247/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
248/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000249/// [C++] declarator initializer[opt]
250///
251/// [C++] initializer:
252/// [C++] '=' initializer-clause
253/// [C++] '(' expression-list ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000254///
255Parser::DeclTy *Parser::
256ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
257
258 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
259 // that they can be chained properly if the actions want this.
260 Parser::DeclTy *LastDeclInGroup = 0;
261
262 // At this point, we know that it is not a function definition. Parse the
263 // rest of the init-declarator-list.
264 while (1) {
265 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000266 if (Tok.is(tok::kw_asm)) {
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000267 ExprResult AsmLabel = ParseSimpleAsm();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000268 if (AsmLabel.isInvalid) {
269 SkipUntil(tok::semi);
270 return 0;
271 }
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000272
273 D.setAsmLabel(AsmLabel.Val);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000274 }
Chris Lattner4b009652007-07-25 00:24:17 +0000275
276 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000277 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000278 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000279
280 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000281 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000282
Chris Lattner4b009652007-07-25 00:24:17 +0000283 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000284 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000285 ConsumeToken();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000286 ExprResult Init = ParseInitializer();
Chris Lattner4b009652007-07-25 00:24:17 +0000287 if (Init.isInvalid) {
288 SkipUntil(tok::semi);
289 return 0;
290 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000291 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000292 } else if (Tok.is(tok::l_paren)) {
293 // Parse C++ direct initializer: '(' expression-list ')'
294 SourceLocation LParenLoc = ConsumeParen();
295 ExprListTy Exprs;
296 CommaLocsTy CommaLocs;
297
298 bool InvalidExpr = false;
299 if (ParseExpressionList(Exprs, CommaLocs)) {
300 SkipUntil(tok::r_paren);
301 InvalidExpr = true;
302 }
303 // Match the ')'.
304 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
305
306 if (!InvalidExpr) {
307 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
308 "Unexpected number of commas!");
309 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
310 &Exprs[0], Exprs.size(),
311 &CommaLocs[0], RParenLoc);
312 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000313 } else {
314 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Chris Lattner4b009652007-07-25 00:24:17 +0000315 }
316
Chris Lattner4b009652007-07-25 00:24:17 +0000317 // If we don't have a comma, it is either the end of the list (a ';') or an
318 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000319 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000320 break;
321
322 // Consume the comma.
323 ConsumeToken();
324
325 // Parse the next declarator.
326 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000327
328 // Accept attributes in an init-declarator. In the first declarator in a
329 // declaration, these would be part of the declspec. In subsequent
330 // declarators, they become part of the declarator itself, so that they
331 // don't apply to declarators after *this* one. Examples:
332 // short __attribute__((common)) var; -> declspec
333 // short var __attribute__((common)); -> declarator
334 // short x, __attribute__((common)) var; -> declarator
335 if (Tok.is(tok::kw___attribute))
336 D.AddAttributes(ParseAttributes());
337
Chris Lattner4b009652007-07-25 00:24:17 +0000338 ParseDeclarator(D);
339 }
340
Chris Lattner34a01ad2007-10-09 17:33:22 +0000341 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000342 ConsumeToken();
343 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
344 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000345 // If this is an ObjC2 for-each loop, this is a successful declarator
346 // parse. The syntax for these looks like:
347 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000348 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000349 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
350 }
Chris Lattner4b009652007-07-25 00:24:17 +0000351 Diag(Tok, diag::err_parse_error);
352 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000353 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000354 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000355 ConsumeToken();
356 return 0;
357}
358
359/// ParseSpecifierQualifierList
360/// specifier-qualifier-list:
361/// type-specifier specifier-qualifier-list[opt]
362/// type-qualifier specifier-qualifier-list[opt]
363/// [GNU] attributes specifier-qualifier-list[opt]
364///
365void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
366 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
367 /// parse declaration-specifiers and complain about extra stuff.
368 ParseDeclarationSpecifiers(DS);
369
370 // Validate declspec for type-name.
371 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000372 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000373 Diag(Tok, diag::err_typename_requires_specqual);
374
375 // Issue diagnostic and remove storage class if present.
376 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
377 if (DS.getStorageClassSpecLoc().isValid())
378 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
379 else
380 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
381 DS.ClearStorageClassSpecs();
382 }
383
384 // Issue diagnostic and remove function specfier if present.
385 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
386 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
387 DS.ClearFunctionSpecs();
388 }
389}
390
391/// ParseDeclarationSpecifiers
392/// declaration-specifiers: [C99 6.7]
393/// storage-class-specifier declaration-specifiers[opt]
394/// type-specifier declaration-specifiers[opt]
395/// type-qualifier declaration-specifiers[opt]
396/// [C99] function-specifier declaration-specifiers[opt]
397/// [GNU] attributes declaration-specifiers[opt]
398///
399/// storage-class-specifier: [C99 6.7.1]
400/// 'typedef'
401/// 'extern'
402/// 'static'
403/// 'auto'
404/// 'register'
405/// [GNU] '__thread'
406/// type-specifier: [C99 6.7.2]
407/// 'void'
408/// 'char'
409/// 'short'
410/// 'int'
411/// 'long'
412/// 'float'
413/// 'double'
414/// 'signed'
415/// 'unsigned'
416/// struct-or-union-specifier
417/// enum-specifier
418/// typedef-name
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000419/// [C++] 'wchar_t'
Chris Lattner4b009652007-07-25 00:24:17 +0000420/// [C++] 'bool'
421/// [C99] '_Bool'
422/// [C99] '_Complex'
423/// [C99] '_Imaginary' // Removed in TC2?
424/// [GNU] '_Decimal32'
425/// [GNU] '_Decimal64'
426/// [GNU] '_Decimal128'
Steve Naroff4c255ab2007-07-31 23:56:32 +0000427/// [GNU] typeof-specifier
Chris Lattner4b009652007-07-25 00:24:17 +0000428/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroffa8ee2262007-08-22 23:18:22 +0000429/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000430/// type-qualifier:
431/// 'const'
432/// 'volatile'
433/// [C99] 'restrict'
434/// function-specifier: [C99 6.7.4]
435/// [C99] 'inline'
436///
437void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000438 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000439 while (1) {
440 int isInvalid = false;
441 const char *PrevSpec = 0;
442 SourceLocation Loc = Tok.getLocation();
443
444 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000445 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000446 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000447 // If this is not a declaration specifier token, we're done reading decl
448 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000449 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000450 return;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000451
452 // typedef-name
453 case tok::identifier: {
454 // This identifier can only be a typedef name if we haven't already seen
455 // a type-specifier. Without this check we misparse:
456 // typedef int X; struct Y { short X; }; as 'short int'.
457 if (DS.hasTypeSpecifier())
458 goto DoneWithDeclSpec;
459
460 // It has to be available as a typedef too!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000461 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000462 if (TypeRep == 0)
463 goto DoneWithDeclSpec;
464
465 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
466 TypeRep);
467 if (isInvalid)
468 break;
469
470 DS.SetRangeEnd(Tok.getLocation());
471 ConsumeToken(); // The identifier
472
473 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
474 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
475 // Objective-C interface. If we don't have Objective-C or a '<', this is
476 // just a normal reference to a typedef name.
477 if (!Tok.is(tok::less) || !getLang().ObjC1)
478 continue;
479
480 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000481 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000482 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000483 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000484
485 DS.SetRangeEnd(EndProtoLoc);
486
Steve Narofff7683302008-09-22 10:28:57 +0000487 // Need to support trailing type qualifiers (e.g. "id<p> const").
488 // If a type specifier follows, it will be diagnosed elsewhere.
489 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000490 }
Chris Lattner4b009652007-07-25 00:24:17 +0000491 // GNU attributes support.
492 case tok::kw___attribute:
493 DS.AddAttributes(ParseAttributes());
494 continue;
495
496 // storage-class-specifier
497 case tok::kw_typedef:
498 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
499 break;
500 case tok::kw_extern:
501 if (DS.isThreadSpecified())
502 Diag(Tok, diag::ext_thread_before, "extern");
503 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
504 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000505 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000506 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
507 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000508 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000509 case tok::kw_static:
510 if (DS.isThreadSpecified())
511 Diag(Tok, diag::ext_thread_before, "static");
512 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
513 break;
514 case tok::kw_auto:
515 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
516 break;
517 case tok::kw_register:
518 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
519 break;
520 case tok::kw___thread:
521 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
522 break;
523
524 // type-specifiers
525 case tok::kw_short:
526 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
527 break;
528 case tok::kw_long:
529 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
530 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
531 else
532 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
533 break;
534 case tok::kw_signed:
535 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
536 break;
537 case tok::kw_unsigned:
538 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
539 break;
540 case tok::kw__Complex:
541 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
542 break;
543 case tok::kw__Imaginary:
544 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
545 break;
546 case tok::kw_void:
547 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
548 break;
549 case tok::kw_char:
550 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
551 break;
552 case tok::kw_int:
553 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
554 break;
555 case tok::kw_float:
556 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
557 break;
558 case tok::kw_double:
559 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
560 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000561 case tok::kw_wchar_t: // [C++ 2.11p1]
562 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
563 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000564 case tok::kw_bool: // [C++ 2.11p1]
565 case tok::kw__Bool:
566 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
567 break;
568 case tok::kw__Decimal32:
569 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
570 break;
571 case tok::kw__Decimal64:
572 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
573 break;
574 case tok::kw__Decimal128:
575 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
576 break;
Chris Lattner2e78db32008-04-13 18:59:07 +0000577
578 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000579 case tok::kw_struct:
580 case tok::kw_union:
Douglas Gregorec93f442008-04-13 21:30:24 +0000581 ParseClassSpecifier(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000582 continue;
583 case tok::kw_enum:
584 ParseEnumSpecifier(DS);
585 continue;
586
Steve Naroff7cbb1462007-07-31 12:34:36 +0000587 // GNU typeof support.
588 case tok::kw_typeof:
589 ParseTypeofSpecifier(DS);
590 continue;
591
Chris Lattner4b009652007-07-25 00:24:17 +0000592 // type-qualifier
593 case tok::kw_const:
594 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
595 getLang())*2;
596 break;
597 case tok::kw_volatile:
598 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
599 getLang())*2;
600 break;
601 case tok::kw_restrict:
602 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
603 getLang())*2;
604 break;
605
606 // function-specifier
607 case tok::kw_inline:
608 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
609 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000610
Steve Naroff5f0466b2008-06-05 00:02:44 +0000611 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000612 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000613 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
614 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000615 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000616 goto DoneWithDeclSpec;
617
618 {
619 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000620 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000621 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000622 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000623 DS.SetRangeEnd(EndProtoLoc);
624
Chris Lattnerb99d7492008-07-26 00:20:22 +0000625 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
626 SourceRange(Loc, EndProtoLoc));
Steve Narofff7683302008-09-22 10:28:57 +0000627 // Need to support trailing type qualifiers (e.g. "id<p> const").
628 // If a type specifier follows, it will be diagnosed elsewhere.
629 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000630 }
Chris Lattner4b009652007-07-25 00:24:17 +0000631 }
632 // If the specifier combination wasn't legal, issue a diagnostic.
633 if (isInvalid) {
634 assert(PrevSpec && "Method did not return previous specifier!");
635 if (isInvalid == 1) // Error.
636 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
637 else // extwarn.
638 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
639 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000640 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000641 ConsumeToken();
642 }
643}
644
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000645/// ParseStructDeclaration - Parse a struct declaration without the terminating
646/// semicolon.
647///
Chris Lattner4b009652007-07-25 00:24:17 +0000648/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000649/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000650/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000651/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000652/// struct-declarator-list:
653/// struct-declarator
654/// struct-declarator-list ',' struct-declarator
655/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
656/// struct-declarator:
657/// declarator
658/// [GNU] declarator attributes[opt]
659/// declarator[opt] ':' constant-expression
660/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
661///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000662void Parser::
663ParseStructDeclaration(DeclSpec &DS,
664 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000665 if (Tok.is(tok::kw___extension__)) {
666 // __extension__ silences extension warnings in the subexpression.
667 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +0000668 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000669 return ParseStructDeclaration(DS, Fields);
670 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000671
672 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000673 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000674 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000675
676 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000677 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000678 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000679 return;
680 }
681
682 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000683 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000684 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000685 FieldDeclarator &DeclaratorInfo = Fields.back();
686
Steve Naroffa9adf112007-08-20 22:28:22 +0000687 /// struct-declarator: declarator
688 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000689 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000690 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000691
Chris Lattner34a01ad2007-10-09 17:33:22 +0000692 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000693 ConsumeToken();
694 ExprResult Res = ParseConstantExpression();
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000695 if (Res.isInvalid)
Steve Naroffa9adf112007-08-20 22:28:22 +0000696 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000697 else
Chris Lattner3dd8d392008-04-10 06:46:29 +0000698 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroffa9adf112007-08-20 22:28:22 +0000699 }
700
701 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000702 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000703 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000704
705 // If we don't have a comma, it is either the end of the list (a ';')
706 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000707 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000708 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000709
710 // Consume the comma.
711 ConsumeToken();
712
713 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000714 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000715
716 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000717 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000718 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000719 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000720}
721
722/// ParseStructUnionBody
723/// struct-contents:
724/// struct-declaration-list
725/// [EXT] empty
726/// [GNU] "struct-declaration-list" without terminatoring ';'
727/// struct-declaration-list:
728/// struct-declaration
729/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000730/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000731///
Chris Lattner4b009652007-07-25 00:24:17 +0000732void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
733 unsigned TagType, DeclTy *TagDecl) {
734 SourceLocation LBraceLoc = ConsumeBrace();
735
736 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
737 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000738 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000739 Diag(Tok, diag::ext_empty_struct_union_enum,
740 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
741
742 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000743 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
744
Chris Lattner4b009652007-07-25 00:24:17 +0000745 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000746 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000747 // Each iteration of this loop reads one struct-declaration.
748
749 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000750 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000751 Diag(Tok, diag::ext_extra_struct_semi);
752 ConsumeToken();
753 continue;
754 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000755
756 // Parse all the comma separated declarators.
757 DeclSpec DS;
758 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000759 if (!Tok.is(tok::at)) {
760 ParseStructDeclaration(DS, FieldDeclarators);
761
762 // Convert them all to fields.
763 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
764 FieldDeclarator &FD = FieldDeclarators[i];
765 // Install the declarator into the current TagDecl.
766 DeclTy *Field = Actions.ActOnField(CurScope,
767 DS.getSourceRange().getBegin(),
768 FD.D, FD.BitfieldSize);
769 FieldDecls.push_back(Field);
770 }
771 } else { // Handle @defs
772 ConsumeToken();
773 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
774 Diag(Tok, diag::err_unexpected_at);
775 SkipUntil(tok::semi, true, true);
776 continue;
777 }
778 ConsumeToken();
779 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
780 if (!Tok.is(tok::identifier)) {
781 Diag(Tok, diag::err_expected_ident);
782 SkipUntil(tok::semi, true, true);
783 continue;
784 }
785 llvm::SmallVector<DeclTy*, 16> Fields;
786 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
787 Fields);
788 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
789 ConsumeToken();
790 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
791 }
Chris Lattner4b009652007-07-25 00:24:17 +0000792
Chris Lattner34a01ad2007-10-09 17:33:22 +0000793 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000794 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000795 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000796 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
797 break;
798 } else {
799 Diag(Tok, diag::err_expected_semi_decl_list);
800 // Skip to end of block or statement
801 SkipUntil(tok::r_brace, true, true);
802 }
803 }
804
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000805 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000806
Chris Lattner4b009652007-07-25 00:24:17 +0000807 AttributeList *AttrList = 0;
808 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000809 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +0000810 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +0000811
812 Actions.ActOnFields(CurScope,
813 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
814 LBraceLoc, RBraceLoc,
815 AttrList);
Chris Lattner4b009652007-07-25 00:24:17 +0000816}
817
818
819/// ParseEnumSpecifier
820/// enum-specifier: [C99 6.7.2.2]
821/// 'enum' identifier[opt] '{' enumerator-list '}'
822/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
823/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
824/// '}' attributes[opt]
825/// 'enum' identifier
826/// [GNU] 'enum' attributes[opt] identifier
827void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000828 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000829 SourceLocation StartLoc = ConsumeToken();
830
831 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +0000832
833 AttributeList *Attr = 0;
834 // If attributes exist after tag, parse them.
835 if (Tok.is(tok::kw___attribute))
836 Attr = ParseAttributes();
837
838 // Must have either 'enum name' or 'enum {...}'.
839 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
840 Diag(Tok, diag::err_expected_ident_lbrace);
841
842 // Skip the rest of this declarator, up until the comma or semicolon.
843 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +0000844 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +0000845 }
846
847 // If an identifier is present, consume and remember it.
848 IdentifierInfo *Name = 0;
849 SourceLocation NameLoc;
850 if (Tok.is(tok::identifier)) {
851 Name = Tok.getIdentifierInfo();
852 NameLoc = ConsumeToken();
853 }
854
855 // There are three options here. If we have 'enum foo;', then this is a
856 // forward declaration. If we have 'enum foo {...' then this is a
857 // definition. Otherwise we have something like 'enum foo xyz', a reference.
858 //
859 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
860 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
861 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
862 //
863 Action::TagKind TK;
864 if (Tok.is(tok::l_brace))
865 TK = Action::TK_Definition;
866 else if (Tok.is(tok::semi))
867 TK = Action::TK_Declaration;
868 else
869 TK = Action::TK_Reference;
870 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
871 Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000872
Chris Lattner34a01ad2007-10-09 17:33:22 +0000873 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000874 ParseEnumBody(StartLoc, TagDecl);
875
876 // TODO: semantic analysis on the declspec for enums.
877 const char *PrevSpec = 0;
878 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
879 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
880}
881
882/// ParseEnumBody - Parse a {} enclosed enumerator-list.
883/// enumerator-list:
884/// enumerator
885/// enumerator-list ',' enumerator
886/// enumerator:
887/// enumeration-constant
888/// enumeration-constant '=' constant-expression
889/// enumeration-constant:
890/// identifier
891///
892void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
893 SourceLocation LBraceLoc = ConsumeBrace();
894
Chris Lattnerc9a92452007-08-27 17:24:30 +0000895 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000896 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000897 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
898
899 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
900
901 DeclTy *LastEnumConstDecl = 0;
902
903 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000904 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000905 IdentifierInfo *Ident = Tok.getIdentifierInfo();
906 SourceLocation IdentLoc = ConsumeToken();
907
908 SourceLocation EqualLoc;
909 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000910 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000911 EqualLoc = ConsumeToken();
912 ExprResult Res = ParseConstantExpression();
913 if (Res.isInvalid)
914 SkipUntil(tok::comma, tok::r_brace, true, true);
915 else
916 AssignedVal = Res.Val;
917 }
918
919 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000920 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000921 LastEnumConstDecl,
922 IdentLoc, Ident,
923 EqualLoc, AssignedVal);
924 EnumConstantDecls.push_back(EnumConstDecl);
925 LastEnumConstDecl = EnumConstDecl;
926
Chris Lattner34a01ad2007-10-09 17:33:22 +0000927 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000928 break;
929 SourceLocation CommaLoc = ConsumeToken();
930
Chris Lattner34a01ad2007-10-09 17:33:22 +0000931 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000932 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
933 }
934
935 // Eat the }.
936 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
937
Steve Naroff0acc9c92007-09-15 18:49:24 +0000938 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000939 EnumConstantDecls.size());
940
941 DeclTy *AttrList = 0;
942 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000943 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000944 AttrList = ParseAttributes(); // FIXME: where do they do?
945}
946
947/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +0000948/// start of a type-qualifier-list.
949bool Parser::isTypeQualifier() const {
950 switch (Tok.getKind()) {
951 default: return false;
952 // type-qualifier
953 case tok::kw_const:
954 case tok::kw_volatile:
955 case tok::kw_restrict:
956 return true;
957 }
958}
959
960/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +0000961/// start of a specifier-qualifier-list.
962bool Parser::isTypeSpecifierQualifier() const {
963 switch (Tok.getKind()) {
964 default: return false;
965 // GNU attributes support.
966 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000967 // GNU typeof support.
968 case tok::kw_typeof:
969
Chris Lattner4b009652007-07-25 00:24:17 +0000970 // type-specifiers
971 case tok::kw_short:
972 case tok::kw_long:
973 case tok::kw_signed:
974 case tok::kw_unsigned:
975 case tok::kw__Complex:
976 case tok::kw__Imaginary:
977 case tok::kw_void:
978 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000979 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +0000980 case tok::kw_int:
981 case tok::kw_float:
982 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000983 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000984 case tok::kw__Bool:
985 case tok::kw__Decimal32:
986 case tok::kw__Decimal64:
987 case tok::kw__Decimal128:
988
Chris Lattner2e78db32008-04-13 18:59:07 +0000989 // struct-or-union-specifier (C99) or class-specifier (C++)
990 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000991 case tok::kw_struct:
992 case tok::kw_union:
993 // enum-specifier
994 case tok::kw_enum:
995
996 // type-qualifier
997 case tok::kw_const:
998 case tok::kw_volatile:
999 case tok::kw_restrict:
1000 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001001
1002 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1003 case tok::less:
1004 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001005
1006 // typedef-name
1007 case tok::identifier:
1008 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001009 }
1010}
1011
1012/// isDeclarationSpecifier() - Return true if the current token is part of a
1013/// declaration specifier.
1014bool Parser::isDeclarationSpecifier() const {
1015 switch (Tok.getKind()) {
1016 default: return false;
1017 // storage-class-specifier
1018 case tok::kw_typedef:
1019 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001020 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001021 case tok::kw_static:
1022 case tok::kw_auto:
1023 case tok::kw_register:
1024 case tok::kw___thread:
1025
1026 // type-specifiers
1027 case tok::kw_short:
1028 case tok::kw_long:
1029 case tok::kw_signed:
1030 case tok::kw_unsigned:
1031 case tok::kw__Complex:
1032 case tok::kw__Imaginary:
1033 case tok::kw_void:
1034 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001035 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001036 case tok::kw_int:
1037 case tok::kw_float:
1038 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001039 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001040 case tok::kw__Bool:
1041 case tok::kw__Decimal32:
1042 case tok::kw__Decimal64:
1043 case tok::kw__Decimal128:
1044
Chris Lattner2e78db32008-04-13 18:59:07 +00001045 // struct-or-union-specifier (C99) or class-specifier (C++)
1046 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001047 case tok::kw_struct:
1048 case tok::kw_union:
1049 // enum-specifier
1050 case tok::kw_enum:
1051
1052 // type-qualifier
1053 case tok::kw_const:
1054 case tok::kw_volatile:
1055 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001056
Chris Lattner4b009652007-07-25 00:24:17 +00001057 // function-specifier
1058 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +00001059
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001060 // GNU typeof support.
1061 case tok::kw_typeof:
1062
1063 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001064 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001065 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001066
1067 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1068 case tok::less:
1069 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001070
1071 // typedef-name
1072 case tok::identifier:
1073 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001074 }
1075}
1076
1077
1078/// ParseTypeQualifierListOpt
1079/// type-qualifier-list: [C99 6.7.5]
1080/// type-qualifier
1081/// [GNU] attributes
1082/// type-qualifier-list type-qualifier
1083/// [GNU] type-qualifier-list attributes
1084///
1085void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1086 while (1) {
1087 int isInvalid = false;
1088 const char *PrevSpec = 0;
1089 SourceLocation Loc = Tok.getLocation();
1090
1091 switch (Tok.getKind()) {
1092 default:
1093 // If this is not a type-qualifier token, we're done reading type
1094 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001095 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001096 return;
1097 case tok::kw_const:
1098 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1099 getLang())*2;
1100 break;
1101 case tok::kw_volatile:
1102 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1103 getLang())*2;
1104 break;
1105 case tok::kw_restrict:
1106 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1107 getLang())*2;
1108 break;
1109 case tok::kw___attribute:
1110 DS.AddAttributes(ParseAttributes());
1111 continue; // do *not* consume the next token!
1112 }
1113
1114 // If the specifier combination wasn't legal, issue a diagnostic.
1115 if (isInvalid) {
1116 assert(PrevSpec && "Method did not return previous specifier!");
1117 if (isInvalid == 1) // Error.
1118 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1119 else // extwarn.
1120 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1121 }
1122 ConsumeToken();
1123 }
1124}
1125
1126
1127/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1128///
1129void Parser::ParseDeclarator(Declarator &D) {
1130 /// This implements the 'declarator' production in the C grammar, then checks
1131 /// for well-formedness and issues diagnostics.
1132 ParseDeclaratorInternal(D);
Chris Lattner4b009652007-07-25 00:24:17 +00001133}
1134
1135/// ParseDeclaratorInternal
1136/// declarator: [C99 6.7.5]
1137/// pointer[opt] direct-declarator
1138/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1139/// [GNU] '&' restrict[opt] attributes[opt] declarator
1140///
1141/// pointer: [C99 6.7.5]
1142/// '*' type-qualifier-list[opt]
1143/// '*' type-qualifier-list[opt] pointer
1144///
1145void Parser::ParseDeclaratorInternal(Declarator &D) {
1146 tok::TokenKind Kind = Tok.getKind();
1147
Steve Naroff7aa54752008-08-27 16:04:49 +00001148 // Not a pointer, C++ reference, or block.
1149 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
1150 (Kind != tok::caret || !getLang().Blocks))
Chris Lattner4b009652007-07-25 00:24:17 +00001151 return ParseDirectDeclarator(D);
1152
Steve Naroffdc22f212008-08-28 10:07:06 +00001153 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Chris Lattner4b009652007-07-25 00:24:17 +00001154 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1155
Steve Naroffdc22f212008-08-28 10:07:06 +00001156 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001157 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001158 DeclSpec DS;
1159
1160 ParseTypeQualifierListOpt(DS);
1161
1162 // Recursively parse the declarator.
1163 ParseDeclaratorInternal(D);
Steve Naroff7aa54752008-08-27 16:04:49 +00001164 if (Kind == tok::star)
1165 // Remember that we parsed a pointer type, and remember the type-quals.
1166 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1167 DS.TakeAttributes()));
1168 else
1169 // Remember that we parsed a Block type, and remember the type-quals.
1170 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1171 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001172 } else {
1173 // Is a reference
1174 DeclSpec DS;
1175
1176 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1177 // cv-qualifiers are introduced through the use of a typedef or of a
1178 // template type argument, in which case the cv-qualifiers are ignored.
1179 //
1180 // [GNU] Retricted references are allowed.
1181 // [GNU] Attributes on references are allowed.
1182 ParseTypeQualifierListOpt(DS);
1183
1184 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1185 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1186 Diag(DS.getConstSpecLoc(),
1187 diag::err_invalid_reference_qualifier_application,
1188 "const");
1189 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1190 Diag(DS.getVolatileSpecLoc(),
1191 diag::err_invalid_reference_qualifier_application,
1192 "volatile");
1193 }
1194
1195 // Recursively parse the declarator.
1196 ParseDeclaratorInternal(D);
1197
1198 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001199 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1200 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001201 }
1202}
1203
1204/// ParseDirectDeclarator
1205/// direct-declarator: [C99 6.7.5]
1206/// identifier
1207/// '(' declarator ')'
1208/// [GNU] '(' attributes declarator ')'
1209/// [C90] direct-declarator '[' constant-expression[opt] ']'
1210/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1211/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1212/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1213/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1214/// direct-declarator '(' parameter-type-list ')'
1215/// direct-declarator '(' identifier-list[opt] ')'
1216/// [GNU] direct-declarator '(' parameter-forward-declarations
1217/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001218/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1219/// cv-qualifier-seq[opt] exception-specification[opt]
Chris Lattner4b009652007-07-25 00:24:17 +00001220///
1221void Parser::ParseDirectDeclarator(Declarator &D) {
1222 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001223 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001224 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1225 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1226 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001227 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001228 // direct-declarator: '(' declarator ')'
1229 // direct-declarator: '(' attributes declarator ')'
1230 // Example: 'char (*X)' or 'int (*XX)(void)'
1231 ParseParenDeclarator(D);
1232 } else if (D.mayOmitIdentifier()) {
1233 // This could be something simple like "int" (in which case the declarator
1234 // portion is empty), if an abstract-declarator is allowed.
1235 D.SetIdentifier(0, Tok.getLocation());
1236 } else {
1237 // Expected identifier or '('.
1238 Diag(Tok, diag::err_expected_ident_lparen);
1239 D.SetIdentifier(0, Tok.getLocation());
1240 }
1241
1242 assert(D.isPastIdentifier() &&
1243 "Haven't past the location of the identifier yet?");
1244
1245 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001246 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001247 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1248 // In such a case, check if we actually have a function declarator; if it
1249 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001250 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1251 // When not in file scope, warn for ambiguous function declarators, just
1252 // in case the author intended it as a variable definition.
1253 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1254 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1255 break;
1256 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001257 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001258 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001259 ParseBracketDeclarator(D);
1260 } else {
1261 break;
1262 }
1263 }
1264}
1265
Chris Lattnera0d056d2008-04-06 05:45:57 +00001266/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1267/// only called before the identifier, so these are most likely just grouping
1268/// parens for precedence. If we find that these are actually function
1269/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1270///
1271/// direct-declarator:
1272/// '(' declarator ')'
1273/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001274/// direct-declarator '(' parameter-type-list ')'
1275/// direct-declarator '(' identifier-list[opt] ')'
1276/// [GNU] direct-declarator '(' parameter-forward-declarations
1277/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001278///
1279void Parser::ParseParenDeclarator(Declarator &D) {
1280 SourceLocation StartLoc = ConsumeParen();
1281 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1282
Chris Lattner1f185292008-10-20 02:05:46 +00001283 // Eat any attributes before we look at whether this is a grouping or function
1284 // declarator paren. If this is a grouping paren, the attribute applies to
1285 // the type being built up, for example:
1286 // int (__attribute__(()) *x)(long y)
1287 // If this ends up not being a grouping paren, the attribute applies to the
1288 // first argument, for example:
1289 // int (__attribute__(()) int x)
1290 // In either case, we need to eat any attributes to be able to determine what
1291 // sort of paren this is.
1292 //
1293 AttributeList *AttrList = 0;
1294 bool RequiresArg = false;
1295 if (Tok.is(tok::kw___attribute)) {
1296 AttrList = ParseAttributes();
1297
1298 // We require that the argument list (if this is a non-grouping paren) be
1299 // present even if the attribute list was empty.
1300 RequiresArg = true;
1301 }
1302
Chris Lattnera0d056d2008-04-06 05:45:57 +00001303 // If we haven't past the identifier yet (or where the identifier would be
1304 // stored, if this is an abstract declarator), then this is probably just
1305 // grouping parens. However, if this could be an abstract-declarator, then
1306 // this could also be the start of function arguments (consider 'void()').
1307 bool isGrouping;
1308
1309 if (!D.mayOmitIdentifier()) {
1310 // If this can't be an abstract-declarator, this *must* be a grouping
1311 // paren, because we haven't seen the identifier yet.
1312 isGrouping = true;
1313 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001314 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001315 isDeclarationSpecifier()) { // 'int(int)' is a function.
1316 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1317 // considered to be a type, not a K&R identifier-list.
1318 isGrouping = false;
1319 } else {
1320 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1321 isGrouping = true;
1322 }
1323
1324 // If this is a grouping paren, handle:
1325 // direct-declarator: '(' declarator ')'
1326 // direct-declarator: '(' attributes declarator ')'
1327 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001328 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001329 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001330 if (AttrList)
1331 D.AddAttributes(AttrList);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001332
Chris Lattnera0d056d2008-04-06 05:45:57 +00001333 ParseDeclaratorInternal(D);
1334 // Match the ')'.
1335 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001336
1337 D.setGroupingParens(hadGroupingParens);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001338 return;
1339 }
1340
1341 // Okay, if this wasn't a grouping paren, it must be the start of a function
1342 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001343 // identifier (and remember where it would have been), then call into
1344 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001345 D.SetIdentifier(0, Tok.getLocation());
1346
Chris Lattner1f185292008-10-20 02:05:46 +00001347 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001348}
1349
1350/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1351/// declarator D up to a paren, which indicates that we are parsing function
1352/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001353///
Chris Lattner1f185292008-10-20 02:05:46 +00001354/// If AttrList is non-null, then the caller parsed those arguments immediately
1355/// after the open paren - they should be considered to be the first argument of
1356/// a parameter. If RequiresArg is true, then the first argument of the
1357/// function is required to be present and required to not be an identifier
1358/// list.
1359///
Chris Lattner4b009652007-07-25 00:24:17 +00001360/// This method also handles this portion of the grammar:
1361/// parameter-type-list: [C99 6.7.5]
1362/// parameter-list
1363/// parameter-list ',' '...'
1364///
1365/// parameter-list: [C99 6.7.5]
1366/// parameter-declaration
1367/// parameter-list ',' parameter-declaration
1368///
1369/// parameter-declaration: [C99 6.7.5]
1370/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001371/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001372/// [GNU] declaration-specifiers declarator attributes
1373/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001374/// [C++] declaration-specifiers abstract-declarator[opt]
1375/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001376/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1377///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001378/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1379/// and "exception-specification[opt]"(TODO).
1380///
Chris Lattner1f185292008-10-20 02:05:46 +00001381void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1382 AttributeList *AttrList,
1383 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001384 // lparen is already consumed!
1385 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001386
Chris Lattner1f185292008-10-20 02:05:46 +00001387 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001388 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00001389 if (RequiresArg) {
1390 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1391 delete AttrList;
1392 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001393
1394 ConsumeParen(); // Eat the closing ')'.
1395
1396 // cv-qualifier-seq[opt].
1397 DeclSpec DS;
1398 if (getLang().CPlusPlus) {
1399 ParseTypeQualifierListOpt(DS);
1400 // FIXME: Parse exception-specification[opt].
1401 }
1402
Chris Lattner9f7564b2008-04-06 06:57:35 +00001403 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001404 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001405 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00001406 /*variadic*/ false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001407 /*arglist*/ 0, 0,
1408 DS.getTypeQualifiers(),
1409 LParenLoc));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001410 return;
Chris Lattner1f185292008-10-20 02:05:46 +00001411 }
1412
1413 // Alternatively, this parameter list may be an identifier list form for a
1414 // K&R-style function: void foo(a,b,c)
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001415 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner1f185292008-10-20 02:05:46 +00001416 // K&R identifier lists can't have typedefs as identifiers, per
1417 // C99 6.7.5.3p11.
1418 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1419 if (RequiresArg) {
1420 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1421 delete AttrList;
1422 }
1423
Chris Lattner4b009652007-07-25 00:24:17 +00001424 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1425 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001426 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001427 }
1428
1429 // Finally, a normal, non-empty parameter type list.
1430
1431 // Build up an array of information about the parsed arguments.
1432 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001433
1434 // Enter function-declaration scope, limiting any declarators to the
1435 // function prototype scope, including parameter declarators.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001436 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001437
1438 bool IsVariadic = false;
1439 while (1) {
1440 if (Tok.is(tok::ellipsis)) {
1441 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001442
Chris Lattner9f7564b2008-04-06 06:57:35 +00001443 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001444 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001445 // Otherwise, parse parameter type list. If it starts with an
1446 // ellipsis, diagnose the malformed function.
1447 Diag(Tok, diag::err_ellipsis_first_arg);
1448 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001449 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001450
Chris Lattner9f7564b2008-04-06 06:57:35 +00001451 ConsumeToken(); // Consume the ellipsis.
1452 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001453 }
1454
Chris Lattner9f7564b2008-04-06 06:57:35 +00001455 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001456
Chris Lattner9f7564b2008-04-06 06:57:35 +00001457 // Parse the declaration-specifiers.
1458 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00001459
1460 // If the caller parsed attributes for the first argument, add them now.
1461 if (AttrList) {
1462 DS.AddAttributes(AttrList);
1463 AttrList = 0; // Only apply the attributes to the first parameter.
1464 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001465 ParseDeclarationSpecifiers(DS);
1466
1467 // Parse the declarator. This is "PrototypeContext", because we must
1468 // accept either 'declarator' or 'abstract-declarator' here.
1469 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1470 ParseDeclarator(ParmDecl);
1471
1472 // Parse GNU attributes, if present.
1473 if (Tok.is(tok::kw___attribute))
1474 ParmDecl.AddAttributes(ParseAttributes());
1475
Chris Lattner9f7564b2008-04-06 06:57:35 +00001476 // Remember this parsed parameter in ParamInfo.
1477 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1478
Chris Lattner9f7564b2008-04-06 06:57:35 +00001479 // If no parameter was specified, verify that *something* was specified,
1480 // otherwise we have a missing type and identifier.
1481 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1482 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1483 // Completely missing, emit error.
1484 Diag(DSStart, diag::err_missing_param);
1485 } else {
1486 // Otherwise, we have something. Add it and let semantic analysis try
1487 // to grok it and add the result to the ParamInfo we are building.
1488
1489 // Inform the actions module about the parameter declarator, so it gets
1490 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001491 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1492
1493 // Parse the default argument, if any. We parse the default
1494 // arguments in all dialects; the semantic analysis in
1495 // ActOnParamDefaultArgument will reject the default argument in
1496 // C.
1497 if (Tok.is(tok::equal)) {
1498 SourceLocation EqualLoc = Tok.getLocation();
1499
1500 // Consume the '='.
1501 ConsumeToken();
1502
1503 // Parse the default argument
Chris Lattner3e254fb2008-04-08 04:40:51 +00001504 ExprResult DefArgResult = ParseAssignmentExpression();
1505 if (DefArgResult.isInvalid) {
1506 SkipUntil(tok::comma, tok::r_paren, true, true);
1507 } else {
1508 // Inform the actions module about the default argument
1509 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1510 }
1511 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001512
1513 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001514 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001515 }
1516
1517 // If the next token is a comma, consume it and keep reading arguments.
1518 if (Tok.isNot(tok::comma)) break;
1519
1520 // Consume the comma.
1521 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001522 }
1523
Chris Lattner9f7564b2008-04-06 06:57:35 +00001524 // Leave prototype scope.
1525 ExitScope();
1526
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001527 // If we have the closing ')', eat it.
1528 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1529
1530 // cv-qualifier-seq[opt].
1531 DeclSpec DS;
1532 if (getLang().CPlusPlus) {
1533 ParseTypeQualifierListOpt(DS);
1534 // FIXME: Parse exception-specification[opt].
1535 }
1536
Chris Lattner4b009652007-07-25 00:24:17 +00001537 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001538 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1539 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001540 DS.getTypeQualifiers(),
Chris Lattner9f7564b2008-04-06 06:57:35 +00001541 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001542}
1543
Chris Lattner35d9c912008-04-06 06:34:08 +00001544/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1545/// we found a K&R-style identifier list instead of a type argument list. The
1546/// current token is known to be the first identifier in the list.
1547///
1548/// identifier-list: [C99 6.7.5]
1549/// identifier
1550/// identifier-list ',' identifier
1551///
1552void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1553 Declarator &D) {
1554 // Build up an array of information about the parsed arguments.
1555 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1556 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1557
1558 // If there was no identifier specified for the declarator, either we are in
1559 // an abstract-declarator, or we are in a parameter declarator which was found
1560 // to be abstract. In abstract-declarators, identifier lists are not valid:
1561 // diagnose this.
1562 if (!D.getIdentifier())
1563 Diag(Tok, diag::ext_ident_list_in_param);
1564
1565 // Tok is known to be the first identifier in the list. Remember this
1566 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001567 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001568 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1569 Tok.getLocation(), 0));
1570
Chris Lattner113a56b2008-04-06 06:39:19 +00001571 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001572
1573 while (Tok.is(tok::comma)) {
1574 // Eat the comma.
1575 ConsumeToken();
1576
Chris Lattner113a56b2008-04-06 06:39:19 +00001577 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001578 if (Tok.isNot(tok::identifier)) {
1579 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001580 SkipUntil(tok::r_paren);
1581 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001582 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001583
Chris Lattner35d9c912008-04-06 06:34:08 +00001584 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001585
1586 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1587 if (Actions.isTypeName(*ParmII, CurScope))
1588 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001589
1590 // Verify that the argument identifier has not already been mentioned.
1591 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001592 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1593 } else {
1594 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001595 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1596 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001597 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001598
1599 // Eat the identifier.
1600 ConsumeToken();
1601 }
1602
Chris Lattner113a56b2008-04-06 06:39:19 +00001603 // Remember that we parsed a function type, and remember the attributes. This
1604 // function type is always a K&R style function type, which is not varargs and
1605 // has no prototype.
1606 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1607 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001608 /*TypeQuals*/0, LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001609
1610 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001611 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001612}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001613
Chris Lattner4b009652007-07-25 00:24:17 +00001614/// [C90] direct-declarator '[' constant-expression[opt] ']'
1615/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1616/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1617/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1618/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1619void Parser::ParseBracketDeclarator(Declarator &D) {
1620 SourceLocation StartLoc = ConsumeBracket();
1621
1622 // If valid, this location is the position where we read the 'static' keyword.
1623 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001624 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001625 StaticLoc = ConsumeToken();
1626
1627 // If there is a type-qualifier-list, read it now.
1628 DeclSpec DS;
1629 ParseTypeQualifierListOpt(DS);
1630
1631 // If we haven't already read 'static', check to see if there is one after the
1632 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001633 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001634 StaticLoc = ConsumeToken();
1635
1636 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1637 bool isStar = false;
1638 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001639
1640 // Handle the case where we have '[*]' as the array size. However, a leading
1641 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1642 // the the token after the star is a ']'. Since stars in arrays are
1643 // infrequent, use of lookahead is not costly here.
1644 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001645 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001646
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001647 if (StaticLoc.isValid())
1648 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1649 StaticLoc = SourceLocation(); // Drop the static.
1650 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001651 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001652 // Parse the assignment-expression now.
1653 NumElements = ParseAssignmentExpression();
1654 }
1655
1656 // If there was an error parsing the assignment-expression, recover.
1657 if (NumElements.isInvalid) {
1658 // If the expression was invalid, skip it.
1659 SkipUntil(tok::r_square);
1660 return;
1661 }
1662
1663 MatchRHSPunctuation(tok::r_square, StartLoc);
1664
1665 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1666 // it was not a constant expression.
1667 if (!getLang().C99) {
1668 // TODO: check C90 array constant exprness.
1669 if (isStar || StaticLoc.isValid() ||
1670 0/*TODO: NumElts is not a C90 constantexpr */)
1671 Diag(StartLoc, diag::ext_c99_array_usage);
1672 }
1673
1674 // Remember that we parsed a pointer type, and remember the type-quals.
1675 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1676 StaticLoc.isValid(), isStar,
1677 NumElements.Val, StartLoc));
1678}
1679
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001680/// [GNU] typeof-specifier:
1681/// typeof ( expressions )
1682/// typeof ( type-name )
1683/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00001684///
1685void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001686 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001687 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001688 SourceLocation StartLoc = ConsumeToken();
1689
Chris Lattner34a01ad2007-10-09 17:33:22 +00001690 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001691 if (!getLang().CPlusPlus) {
1692 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1693 return;
1694 }
1695
1696 ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/);
1697 if (Result.isInvalid)
1698 return;
1699
1700 const char *PrevSpec = 0;
1701 // Check for duplicate type specifiers.
1702 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1703 Result.Val))
1704 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1705
1706 // FIXME: Not accurate, the range gets one token more than it should.
1707 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00001708 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001709 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001710
Steve Naroff7cbb1462007-07-31 12:34:36 +00001711 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1712
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00001713 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00001714 TypeTy *Ty = ParseTypeName();
1715
Steve Naroff4c255ab2007-07-31 23:56:32 +00001716 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1717
Chris Lattner34a01ad2007-10-09 17:33:22 +00001718 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001719 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001720 return;
1721 }
1722 RParenLoc = ConsumeParen();
1723 const char *PrevSpec = 0;
1724 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1725 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1726 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001727 } else { // we have an expression.
1728 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001729
Chris Lattner34a01ad2007-10-09 17:33:22 +00001730 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001731 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001732 return;
1733 }
1734 RParenLoc = ConsumeParen();
1735 const char *PrevSpec = 0;
1736 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1737 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1738 Result.Val))
1739 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001740 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00001741 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001742}
1743
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001744