blob: 1cf331702d99424e75a9c278fd3613ceb6dfdbda [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattner1a76a3c2007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000017#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000018using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// C99 6.7: Declarations.
22//===----------------------------------------------------------------------===//
23
Chris Lattnerf5fbd792006-08-10 23:56:11 +000024/// ParseTypeName
25/// type-name: [C99 6.7.6]
26/// specifier-qualifier-list abstract-declarator[opt]
Chris Lattnere550a4e2006-08-24 06:37:51 +000027Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000028 // Parse the common declaration-specifiers piece.
29 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000030 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000031
32 // Parse the abstract-declarator, if present.
33 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
34 ParseDeclarator(DeclaratorInfo);
Chris Lattnere550a4e2006-08-24 06:37:51 +000035
Steve Naroff30d242c2007-09-15 18:49:24 +000036 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattnerf5fbd792006-08-10 23:56:11 +000037}
38
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000039/// ParseAttributes - Parse a non-empty attributes list.
40///
41/// [GNU] attributes:
42/// attribute
43/// attributes attribute
44///
45/// [GNU] attribute:
46/// '__attribute__' '(' '(' attribute-list ')' ')'
47///
48/// [GNU] attribute-list:
49/// attrib
50/// attribute_list ',' attrib
51///
52/// [GNU] attrib:
53/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000054/// attrib-name
55/// attrib-name '(' identifier ')'
56/// attrib-name '(' identifier ',' nonempty-expr-list ')'
57/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000058///
Steve Naroff0f2fe172007-06-01 17:11:19 +000059/// [GNU] attrib-name:
60/// identifier
61/// typespec
62/// typequal
63/// storageclass
64///
65/// FIXME: The GCC grammar/code for this construct implies we need two
66/// token lookahead. Comment from gcc: "If they start with an identifier
67/// which is followed by a comma or close parenthesis, then the arguments
68/// start with that identifier; otherwise they are an expression list."
69///
70/// At the moment, I am not doing 2 token lookahead. I am also unaware of
71/// any attributes that don't work (based on my limited testing). Most
72/// attributes are very simple in practice. Until we find a bug, I don't see
73/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000074
Steve Naroffb8371e12007-06-09 03:39:29 +000075AttributeList *Parser::ParseAttributes() {
Chris Lattner76c72282007-10-09 17:33:22 +000076 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Steve Naroff0f2fe172007-06-01 17:11:19 +000077
Steve Naroffb8371e12007-06-09 03:39:29 +000078 AttributeList *CurrAttr = 0;
Steve Naroff0f2fe172007-06-01 17:11:19 +000079
Chris Lattner76c72282007-10-09 17:33:22 +000080 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000081 ConsumeToken();
82 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
83 "attribute")) {
84 SkipUntil(tok::r_paren, true); // skip until ) or ;
85 return CurrAttr;
86 }
87 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +000092 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
93 Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000094
Chris Lattner76c72282007-10-09 17:33:22 +000095 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000096 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
97 ConsumeToken();
98 continue;
99 }
100 // we have an identifier or declaration specifier (const, int, etc.)
101 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
102 SourceLocation AttrNameLoc = ConsumeToken();
Steve Naroff0f2fe172007-06-01 17:11:19 +0000103
104 // check if we have a "paramterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000105 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000106 ConsumeParen(); // ignore the left paren loc for now
Steve Naroff0f2fe172007-06-01 17:11:19 +0000107
Chris Lattner76c72282007-10-09 17:33:22 +0000108 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000109 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
110 SourceLocation ParmLoc = ConsumeToken();
111
Chris Lattner76c72282007-10-09 17:33:22 +0000112 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000113 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000114 ConsumeParen(); // ignore the right paren loc for now
115 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
116 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000117 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000118 ConsumeToken();
119 // __attribute__(( format(printf, 1, 2) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000120 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000121 bool ArgExprsOk = true;
122
123 // now parse the non-empty comma separated list of expressions
124 while (1) {
125 ExprResult ArgExpr = ParseAssignmentExpression();
126 if (ArgExpr.isInvalid) {
127 ArgExprsOk = false;
128 SkipUntil(tok::r_paren);
129 break;
130 } else {
131 ArgExprs.push_back(ArgExpr.Val);
132 }
Chris Lattner76c72282007-10-09 17:33:22 +0000133 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000134 break;
135 ConsumeToken(); // Eat the comma, move to the next argument
136 }
Chris Lattner76c72282007-10-09 17:33:22 +0000137 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000138 ConsumeParen(); // ignore the right paren loc for now
139 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
140 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 }
142 }
143 } else { // not an identifier
144 // parse a possibly empty comma separated list of expressions
Chris Lattner76c72282007-10-09 17:33:22 +0000145 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000146 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000147 ConsumeParen(); // ignore the right paren loc for now
148 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
149 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000150 } else {
151 // __attribute__(( aligned(16) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000152 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000153 bool ArgExprsOk = true;
154
155 // now parse the list of expressions
156 while (1) {
157 ExprResult ArgExpr = ParseAssignmentExpression();
158 if (ArgExpr.isInvalid) {
159 ArgExprsOk = false;
160 SkipUntil(tok::r_paren);
161 break;
162 } else {
163 ArgExprs.push_back(ArgExpr.Val);
164 }
Chris Lattner76c72282007-10-09 17:33:22 +0000165 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000166 break;
167 ConsumeToken(); // Eat the comma, move to the next argument
168 }
169 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000170 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000171 ConsumeParen(); // ignore the right paren loc for now
172 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
173 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
174 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000175 }
176 }
177 }
178 } else {
Steve Naroffb8371e12007-06-09 03:39:29 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
180 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000181 }
182 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000183 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
184 SkipUntil(tok::r_paren, false);
185 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
186 SkipUntil(tok::r_paren, false);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000187 }
188 return CurrAttr;
189}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000190
Chris Lattner53361ac2006-08-10 05:19:57 +0000191/// ParseDeclaration - Parse a full 'declaration', which consists of
192/// declaration-specifiers, some number of declarators, and a semicolon.
193/// 'Context' should be a Declarator::TheContext value.
Chris Lattnera5235172007-08-25 06:57:03 +0000194///
195/// declaration: [C99 6.7]
196/// block-declaration ->
197/// simple-declaration
198/// others [FIXME]
199/// [C++] namespace-definition
200/// others... [FIXME]
201///
Chris Lattner302b4be2006-11-19 02:31:38 +0000202Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnera5235172007-08-25 06:57:03 +0000203 switch (Tok.getKind()) {
204 case tok::kw_namespace:
205 return ParseNamespace(Context);
206 default:
207 return ParseSimpleDeclaration(Context);
208 }
209}
210
211/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
212/// declaration-specifiers init-declarator-list[opt] ';'
213///[C90/C++]init-declarator-list ';' [TODO]
214/// [OMP] threadprivate-directive [TODO]
215Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000216 // Parse the common declaration-specifiers piece.
217 DeclSpec DS;
218 ParseDeclarationSpecifiers(DS);
219
Chris Lattner0e894622006-08-13 19:58:17 +0000220 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
221 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000222 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000223 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000224 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000225 }
226
Chris Lattner53361ac2006-08-10 05:19:57 +0000227 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
228 ParseDeclarator(DeclaratorInfo);
229
Chris Lattner302b4be2006-11-19 02:31:38 +0000230 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +0000231}
232
Chris Lattnera5235172007-08-25 06:57:03 +0000233
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000234/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
235/// parsing 'declaration-specifiers declarator'. This method is split out this
236/// way to handle the ambiguity between top-level function-definitions and
237/// declarations.
238///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000239/// init-declarator-list: [C99 6.7]
240/// init-declarator
241/// init-declarator-list ',' init-declarator
242/// init-declarator: [C99 6.7]
243/// declarator
244/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000245/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
246/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000247///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000248Parser::DeclTy *Parser::
249ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
250
251 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
252 // that they can be chained properly if the actions want this.
253 Parser::DeclTy *LastDeclInGroup = 0;
254
Chris Lattner53361ac2006-08-10 05:19:57 +0000255 // At this point, we know that it is not a function definition. Parse the
256 // rest of the init-declarator-list.
257 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000258 // If a simple-asm-expr is present, parse it.
Daniel Dunbar4983df32008-08-05 01:35:17 +0000259 if (Tok.is(tok::kw_asm)) {
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000260 ExprResult AsmLabel = ParseSimpleAsm();
Daniel Dunbar4983df32008-08-05 01:35:17 +0000261 if (AsmLabel.isInvalid) {
262 SkipUntil(tok::semi);
263 return 0;
264 }
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000265
266 D.setAsmLabel(AsmLabel.Val);
Daniel Dunbar4983df32008-08-05 01:35:17 +0000267 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000268
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000269 // If attributes are present, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000270 if (Tok.is(tok::kw___attribute))
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000271 D.AddAttributes(ParseAttributes());
Steve Naroff61091402007-09-12 14:07:44 +0000272
273 // Inform the current actions module that we just parsed this declarator.
274 // FIXME: pass asm & attributes.
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000275 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff61091402007-09-12 14:07:44 +0000276
Chris Lattner53361ac2006-08-10 05:19:57 +0000277 // Parse declarator '=' initializer.
Chris Lattner76c72282007-10-09 17:33:22 +0000278 if (Tok.is(tok::equal)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000279 ConsumeToken();
Daniel Dunbar4983df32008-08-05 01:35:17 +0000280 ExprResult Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000281 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000282 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000283 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000284 }
Steve Naroff61091402007-09-12 14:07:44 +0000285 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Chris Lattner53361ac2006-08-10 05:19:57 +0000286 }
287
Chris Lattner53361ac2006-08-10 05:19:57 +0000288 // If we don't have a comma, it is either the end of the list (a ';') or an
289 // error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000290 if (Tok.isNot(tok::comma))
Chris Lattner53361ac2006-08-10 05:19:57 +0000291 break;
292
293 // Consume the comma.
294 ConsumeToken();
295
296 // Parse the next declarator.
297 D.clear();
298 ParseDeclarator(D);
299 }
300
Chris Lattner76c72282007-10-09 17:33:22 +0000301 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000302 ConsumeToken();
Chris Lattner776fac82007-06-09 00:53:06 +0000303 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000304 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000305 // If this is an ObjC2 for-each loop, this is a successful declarator
306 // parse. The syntax for these looks like:
307 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000308 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000309 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
310 }
Chris Lattner776fac82007-06-09 00:53:06 +0000311 Diag(Tok, diag::err_parse_error);
312 // Skip to end of block or statement
Chris Lattner43ba2512007-08-21 18:36:18 +0000313 SkipUntil(tok::r_brace, true, true);
Chris Lattner76c72282007-10-09 17:33:22 +0000314 if (Tok.is(tok::semi))
Chris Lattner776fac82007-06-09 00:53:06 +0000315 ConsumeToken();
316 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000317}
318
Chris Lattner1890ac82006-08-13 01:16:23 +0000319/// ParseSpecifierQualifierList
320/// specifier-qualifier-list:
321/// type-specifier specifier-qualifier-list[opt]
322/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000323/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000324///
325void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
326 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
327 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000328 ParseDeclarationSpecifiers(DS);
329
330 // Validate declspec for type-name.
331 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffcfdf6162008-06-05 00:02:44 +0000332 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner1890ac82006-08-13 01:16:23 +0000333 Diag(Tok, diag::err_typename_requires_specqual);
334
Chris Lattner1b22eed2006-11-28 05:12:07 +0000335 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000336 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000337 if (DS.getStorageClassSpecLoc().isValid())
338 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
339 else
340 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000341 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000342 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000343
344 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000345 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000346 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000347 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000348 }
349}
Chris Lattner53361ac2006-08-10 05:19:57 +0000350
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000351/// ParseDeclarationSpecifiers
352/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000353/// storage-class-specifier declaration-specifiers[opt]
354/// type-specifier declaration-specifiers[opt]
355/// type-qualifier declaration-specifiers[opt]
356/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000357/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000358///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000359/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000360/// 'typedef'
361/// 'extern'
362/// 'static'
363/// 'auto'
364/// 'register'
365/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000366/// type-specifier: [C99 6.7.2]
367/// 'void'
368/// 'char'
369/// 'short'
370/// 'int'
371/// 'long'
372/// 'float'
373/// 'double'
374/// 'signed'
375/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000376/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000377/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000378/// typedef-name
Bill Wendling4073ed52007-02-13 01:51:42 +0000379/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000380/// [C99] '_Bool'
381/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000382/// [C99] '_Imaginary' // Removed in TC2?
383/// [GNU] '_Decimal32'
384/// [GNU] '_Decimal64'
385/// [GNU] '_Decimal128'
Steve Naroff872da802007-07-31 23:56:32 +0000386/// [GNU] typeof-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000387/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff7e901fd2007-08-22 23:18:22 +0000388/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000389/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000390/// 'const'
391/// 'volatile'
392/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000393/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000394/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000395///
396void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner2e232092008-03-13 06:29:04 +0000397 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000398 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000399 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000400 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000401 SourceLocation Loc = Tok.getLocation();
402
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000403 switch (Tok.getKind()) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000404 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000405 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000406 // If this is not a declaration specifier token, we're done reading decl
407 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000408 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000409 return;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000410
411 // typedef-name
412 case tok::identifier: {
413 // This identifier can only be a typedef name if we haven't already seen
414 // a type-specifier. Without this check we misparse:
415 // typedef int X; struct Y { short X; }; as 'short int'.
416 if (DS.hasTypeSpecifier())
417 goto DoneWithDeclSpec;
418
419 // It has to be available as a typedef too!
Argyrios Kyrtzidis25d05e82008-08-01 10:35:27 +0000420 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000421 if (TypeRep == 0)
422 goto DoneWithDeclSpec;
423
424 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
425 TypeRep);
426 if (isInvalid)
427 break;
428
429 DS.SetRangeEnd(Tok.getLocation());
430 ConsumeToken(); // The identifier
431
432 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
433 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
434 // Objective-C interface. If we don't have Objective-C or a '<', this is
435 // just a normal reference to a typedef name.
436 if (!Tok.is(tok::less) || !getLang().ObjC1)
437 continue;
438
439 SourceLocation EndProtoLoc;
Chris Lattnerbc762972008-07-26 01:53:50 +0000440 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000441 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000442 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000443
444 DS.SetRangeEnd(EndProtoLoc);
445
446 // Do not allow any other declspecs after the protocol qualifier list
447 // "<foo,bar>short" is not allowed.
448 goto DoneWithDeclSpec;
449 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000450 // GNU attributes support.
451 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000452 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000453 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000454
455 // storage-class-specifier
456 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000457 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000458 break;
459 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000460 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000461 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000462 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000463 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000464 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000465 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
466 PrevSpec);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000467 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000468 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000469 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000470 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000471 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000472 break;
473 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000474 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000475 break;
476 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000477 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000478 break;
479 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000480 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000481 break;
482
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000483 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000484 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000485 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000486 break;
487 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000488 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000489 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000490 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000491 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000492 break;
493 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000494 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000495 break;
496 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000497 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000498 break;
499 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000500 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000501 break;
502 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000503 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000504 break;
505 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000507 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000508 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000509 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000510 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000511 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000512 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000513 break;
514 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000515 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000516 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000517 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000518 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000519 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000520 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000521 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000522 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000523 break;
524 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000525 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000526 break;
527 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000528 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000529 break;
530 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000531 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000532 break;
Chris Lattner861a2262008-04-13 18:59:07 +0000533
534 case tok::kw_class:
Chris Lattner1890ac82006-08-13 01:16:23 +0000535 case tok::kw_struct:
536 case tok::kw_union:
Douglas Gregor556877c2008-04-13 21:30:24 +0000537 ParseClassSpecifier(DS);
Chris Lattner1890ac82006-08-13 01:16:23 +0000538 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000539 case tok::kw_enum:
540 ParseEnumSpecifier(DS);
541 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000542
Steve Naroffad373bd2007-07-31 12:34:36 +0000543 // GNU typeof support.
544 case tok::kw_typeof:
545 ParseTypeofSpecifier(DS);
546 continue;
547
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000548 // type-qualifier
549 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000550 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
551 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000552 break;
553 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000554 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
555 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000556 break;
557 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000558 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
559 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000560 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000561
562 // function-specifier
563 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000564 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000565 break;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000566
Steve Naroffcfdf6162008-06-05 00:02:44 +0000567 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +0000568 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +0000569 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
570 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +0000571 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +0000572 goto DoneWithDeclSpec;
573
574 {
575 SourceLocation EndProtoLoc;
Chris Lattnerbc762972008-07-26 01:53:50 +0000576 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000577 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000578 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000579 DS.SetRangeEnd(EndProtoLoc);
580
Chris Lattner0974b232008-07-26 00:20:22 +0000581 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
582 SourceRange(Loc, EndProtoLoc));
Chris Lattner16fac4f2008-07-26 01:18:38 +0000583 // Do not allow any other declspecs after the protocol qualifier list
584 // "<foo,bar>short" is not allowed.
585 goto DoneWithDeclSpec;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000586 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000587 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000588 // If the specifier combination wasn't legal, issue a diagnostic.
589 if (isInvalid) {
590 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000591 if (isInvalid == 1) // Error.
592 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
593 else // extwarn.
594 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000595 }
Chris Lattner2e232092008-03-13 06:29:04 +0000596 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000597 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000598 }
599}
600
Chris Lattnerffbc2712007-01-25 06:05:38 +0000601/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
602/// the first token has already been read and has been turned into an instance
603/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
604/// otherwise it returns false and fills in Decl.
605bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
Steve Naroffb8371e12007-06-09 03:39:29 +0000606 AttributeList *Attr = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000607 // If attributes exist after tag, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000608 if (Tok.is(tok::kw___attribute))
Steve Naroffb8371e12007-06-09 03:39:29 +0000609 Attr = ParseAttributes();
Chris Lattnerffbc2712007-01-25 06:05:38 +0000610
Chris Lattner1890ac82006-08-13 01:16:23 +0000611 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner76c72282007-10-09 17:33:22 +0000612 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000613 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattner02c04392007-07-25 00:24:17 +0000614
615 // Skip the rest of this declarator, up until the comma or semicolon.
616 SkipUntil(tok::comma, true);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000617 return true;
Chris Lattner1890ac82006-08-13 01:16:23 +0000618 }
619
Chris Lattner8c6519a2007-01-22 07:41:36 +0000620 // If an identifier is present, consume and remember it.
621 IdentifierInfo *Name = 0;
622 SourceLocation NameLoc;
Chris Lattner76c72282007-10-09 17:33:22 +0000623 if (Tok.is(tok::identifier)) {
Chris Lattner8c6519a2007-01-22 07:41:36 +0000624 Name = Tok.getIdentifierInfo();
625 NameLoc = ConsumeToken();
626 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000627
Chris Lattner8c6519a2007-01-22 07:41:36 +0000628 // There are three options here. If we have 'struct foo;', then this is a
629 // forward declaration. If we have 'struct foo {...' then this is a
Chris Lattner7b9ace62007-01-23 20:11:08 +0000630 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Chris Lattner8799cf22007-01-23 01:57:16 +0000631 //
632 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
633 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
634 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
635 //
Chris Lattner7b9ace62007-01-23 20:11:08 +0000636 Action::TagKind TK;
Chris Lattner76c72282007-10-09 17:33:22 +0000637 if (Tok.is(tok::l_brace))
Chris Lattner7b9ace62007-01-23 20:11:08 +0000638 TK = Action::TK_Definition;
Chris Lattner76c72282007-10-09 17:33:22 +0000639 else if (Tok.is(tok::semi))
Chris Lattner7b9ace62007-01-23 20:11:08 +0000640 TK = Action::TK_Declaration;
641 else
642 TK = Action::TK_Reference;
Steve Naroff30d242c2007-09-15 18:49:24 +0000643 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000644 return false;
645}
646
Chris Lattner70ae4912007-10-29 04:42:53 +0000647/// ParseStructDeclaration - Parse a struct declaration without the terminating
648/// semicolon.
649///
Chris Lattner90a26b02007-01-23 04:38:16 +0000650/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +0000651/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +0000652/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +0000653/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +0000654/// struct-declarator-list:
655/// struct-declarator
656/// struct-declarator-list ',' struct-declarator
657/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
658/// struct-declarator:
659/// declarator
660/// [GNU] declarator attributes[opt]
661/// declarator[opt] ':' constant-expression
662/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
663///
Chris Lattnera12405b2008-04-10 06:46:29 +0000664void Parser::
665ParseStructDeclaration(DeclSpec &DS,
666 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroff97170802007-08-20 22:28:22 +0000667 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattnera12405b2008-04-10 06:46:29 +0000668 while (Tok.is(tok::kw___extension__))
Steve Naroff97170802007-08-20 22:28:22 +0000669 ConsumeToken();
670
671 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +0000672 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +0000673 ParseSpecifierQualifierList(DS);
674 // TODO: Does specifier-qualifier list correctly check that *something* is
675 // specified?
676
677 // If there are no declarators, issue a warning.
Chris Lattner76c72282007-10-09 17:33:22 +0000678 if (Tok.is(tok::semi)) {
Chris Lattner32295d32008-04-10 06:15:14 +0000679 Diag(DSStart, diag::w_no_declarators);
Steve Naroff97170802007-08-20 22:28:22 +0000680 return;
681 }
682
683 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000684 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000685 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +0000686 FieldDeclarator &DeclaratorInfo = Fields.back();
687
Steve Naroff97170802007-08-20 22:28:22 +0000688 /// struct-declarator: declarator
689 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +0000690 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +0000691 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff97170802007-08-20 22:28:22 +0000692
Chris Lattner76c72282007-10-09 17:33:22 +0000693 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +0000694 ConsumeToken();
695 ExprResult Res = ParseConstantExpression();
Chris Lattner32295d32008-04-10 06:15:14 +0000696 if (Res.isInvalid)
Steve Naroff97170802007-08-20 22:28:22 +0000697 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +0000698 else
Chris Lattnera12405b2008-04-10 06:46:29 +0000699 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff97170802007-08-20 22:28:22 +0000700 }
701
702 // If attributes exist after the declarator, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000703 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000704 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000705
706 // If we don't have a comma, it is either the end of the list (a ';')
707 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000708 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +0000709 return;
Steve Naroff97170802007-08-20 22:28:22 +0000710
711 // Consume the comma.
712 ConsumeToken();
713
714 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000715 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000716
717 // Attributes are only allowed on the second declarator.
Chris Lattner76c72282007-10-09 17:33:22 +0000718 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000719 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000720 }
Steve Naroff97170802007-08-20 22:28:22 +0000721}
722
723/// ParseStructUnionBody
724/// struct-contents:
725/// struct-declaration-list
726/// [EXT] empty
727/// [GNU] "struct-declaration-list" without terminatoring ';'
728/// struct-declaration-list:
729/// struct-declaration
730/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +0000731/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +0000732///
Chris Lattner1300fb92007-01-23 23:42:53 +0000733void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
734 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000735 SourceLocation LBraceLoc = ConsumeBrace();
736
Chris Lattner7b9ace62007-01-23 20:11:08 +0000737 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
738 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +0000739 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner90a26b02007-01-23 04:38:16 +0000740 Diag(Tok, diag::ext_empty_struct_union_enum,
741 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000742
Chris Lattner23b7eb62007-06-15 23:05:46 +0000743 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000744 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
745
Chris Lattner7b9ace62007-01-23 20:11:08 +0000746 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +0000747 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000748 // Each iteration of this loop reads one struct-declaration.
749
Chris Lattner736ed5d2007-06-09 05:59:07 +0000750 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +0000751 if (Tok.is(tok::semi)) {
Chris Lattner36e46a22007-06-09 05:49:55 +0000752 Diag(Tok, diag::ext_extra_struct_semi);
753 ConsumeToken();
754 continue;
755 }
Chris Lattnera12405b2008-04-10 06:46:29 +0000756
757 // Parse all the comma separated declarators.
758 DeclSpec DS;
759 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +0000760 if (!Tok.is(tok::at)) {
761 ParseStructDeclaration(DS, FieldDeclarators);
762
763 // Convert them all to fields.
764 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
765 FieldDeclarator &FD = FieldDeclarators[i];
766 // Install the declarator into the current TagDecl.
767 DeclTy *Field = Actions.ActOnField(CurScope,
768 DS.getSourceRange().getBegin(),
769 FD.D, FD.BitfieldSize);
770 FieldDecls.push_back(Field);
771 }
772 } else { // Handle @defs
773 ConsumeToken();
774 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
775 Diag(Tok, diag::err_unexpected_at);
776 SkipUntil(tok::semi, true, true);
777 continue;
778 }
779 ConsumeToken();
780 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
781 if (!Tok.is(tok::identifier)) {
782 Diag(Tok, diag::err_expected_ident);
783 SkipUntil(tok::semi, true, true);
784 continue;
785 }
786 llvm::SmallVector<DeclTy*, 16> Fields;
787 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
788 Fields);
789 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
790 ConsumeToken();
791 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
792 }
Chris Lattner736ed5d2007-06-09 05:59:07 +0000793
Chris Lattner76c72282007-10-09 17:33:22 +0000794 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000795 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +0000796 } else if (Tok.is(tok::r_brace)) {
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000797 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
798 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000799 } else {
800 Diag(Tok, diag::err_expected_semi_decl_list);
801 // Skip to end of block or statement
802 SkipUntil(tok::r_brace, true, true);
803 }
804 }
805
Steve Naroff33a1e802007-10-29 21:38:07 +0000806 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +0000807
Fariborz Jahanian343f7092007-09-29 00:54:24 +0000808 Actions.ActOnFields(CurScope,
Chris Lattnereb85ab42008-02-25 21:04:36 +0000809 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff33a1e802007-10-29 21:38:07 +0000810 LBraceLoc, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000811
Steve Naroffb8371e12007-06-09 03:39:29 +0000812 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000813 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000814 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000815 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000816}
817
818
Chris Lattner3b561a32006-08-13 00:12:11 +0000819/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000820/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000821/// 'enum' identifier[opt] '{' enumerator-list '}'
822/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000823/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
824/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000825/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000826/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000827void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000828 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000829 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000830
Chris Lattnerffbc2712007-01-25 06:05:38 +0000831 // Parse the tag portion of this.
832 DeclTy *TagDecl;
833 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000834 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000835
Chris Lattner76c72282007-10-09 17:33:22 +0000836 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000837 ParseEnumBody(StartLoc, TagDecl);
838
Chris Lattner3b561a32006-08-13 00:12:11 +0000839 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000840 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000841 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000842 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000843}
844
Chris Lattnerc1915e22007-01-25 07:29:02 +0000845/// ParseEnumBody - Parse a {} enclosed enumerator-list.
846/// enumerator-list:
847/// enumerator
848/// enumerator-list ',' enumerator
849/// enumerator:
850/// enumeration-constant
851/// enumeration-constant '=' constant-expression
852/// enumeration-constant:
853/// identifier
854///
855void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
856 SourceLocation LBraceLoc = ConsumeBrace();
857
Chris Lattner37256fb2007-08-27 17:24:30 +0000858 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +0000859 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000860 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
861
Chris Lattner23b7eb62007-06-15 23:05:46 +0000862 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000863
Chris Lattner4ef40012007-06-11 01:28:17 +0000864 DeclTy *LastEnumConstDecl = 0;
865
Chris Lattnerc1915e22007-01-25 07:29:02 +0000866 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +0000867 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000868 IdentifierInfo *Ident = Tok.getIdentifierInfo();
869 SourceLocation IdentLoc = ConsumeToken();
870
871 SourceLocation EqualLoc;
872 ExprTy *AssignedVal = 0;
Chris Lattner76c72282007-10-09 17:33:22 +0000873 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000874 EqualLoc = ConsumeToken();
875 ExprResult Res = ParseConstantExpression();
876 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000877 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000878 else
879 AssignedVal = Res.Val;
880 }
881
882 // Install the enumerator constant into EnumDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +0000883 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4ef40012007-06-11 01:28:17 +0000884 LastEnumConstDecl,
885 IdentLoc, Ident,
886 EqualLoc, AssignedVal);
887 EnumConstantDecls.push_back(EnumConstDecl);
888 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000889
Chris Lattner76c72282007-10-09 17:33:22 +0000890 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000891 break;
892 SourceLocation CommaLoc = ConsumeToken();
893
Chris Lattner76c72282007-10-09 17:33:22 +0000894 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000895 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
896 }
897
898 // Eat the }.
899 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
900
Steve Naroff30d242c2007-09-15 18:49:24 +0000901 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +0000902 EnumConstantDecls.size());
903
Steve Naroff0f2fe172007-06-01 17:11:19 +0000904 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000905 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000906 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000907 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000908}
Chris Lattner3b561a32006-08-13 00:12:11 +0000909
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000910/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000911/// start of a type-qualifier-list.
912bool Parser::isTypeQualifier() const {
913 switch (Tok.getKind()) {
914 default: return false;
915 // type-qualifier
916 case tok::kw_const:
917 case tok::kw_volatile:
918 case tok::kw_restrict:
919 return true;
920 }
921}
922
923/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000924/// start of a specifier-qualifier-list.
925bool Parser::isTypeSpecifierQualifier() const {
926 switch (Tok.getKind()) {
927 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000928 // GNU attributes support.
929 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +0000930 // GNU typeof support.
931 case tok::kw_typeof:
Steve Naroffcfdf6162008-06-05 00:02:44 +0000932 // GNU bizarre protocol extension. FIXME: make an extension?
933 case tok::less:
Steve Naroffad373bd2007-07-31 12:34:36 +0000934
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000935 // type-specifiers
936 case tok::kw_short:
937 case tok::kw_long:
938 case tok::kw_signed:
939 case tok::kw_unsigned:
940 case tok::kw__Complex:
941 case tok::kw__Imaginary:
942 case tok::kw_void:
943 case tok::kw_char:
944 case tok::kw_int:
945 case tok::kw_float:
946 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000947 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000948 case tok::kw__Bool:
949 case tok::kw__Decimal32:
950 case tok::kw__Decimal64:
951 case tok::kw__Decimal128:
952
Chris Lattner861a2262008-04-13 18:59:07 +0000953 // struct-or-union-specifier (C99) or class-specifier (C++)
954 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000955 case tok::kw_struct:
956 case tok::kw_union:
957 // enum-specifier
958 case tok::kw_enum:
959
960 // type-qualifier
961 case tok::kw_const:
962 case tok::kw_volatile:
963 case tok::kw_restrict:
964 return true;
965
966 // typedef-name
967 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000968 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000969 }
970}
971
Chris Lattneracd58a32006-08-06 17:24:14 +0000972/// isDeclarationSpecifier() - Return true if the current token is part of a
973/// declaration specifier.
974bool Parser::isDeclarationSpecifier() const {
975 switch (Tok.getKind()) {
976 default: return false;
977 // storage-class-specifier
978 case tok::kw_typedef:
979 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +0000980 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +0000981 case tok::kw_static:
982 case tok::kw_auto:
983 case tok::kw_register:
984 case tok::kw___thread:
985
986 // type-specifiers
987 case tok::kw_short:
988 case tok::kw_long:
989 case tok::kw_signed:
990 case tok::kw_unsigned:
991 case tok::kw__Complex:
992 case tok::kw__Imaginary:
993 case tok::kw_void:
994 case tok::kw_char:
995 case tok::kw_int:
996 case tok::kw_float:
997 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000998 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +0000999 case tok::kw__Bool:
1000 case tok::kw__Decimal32:
1001 case tok::kw__Decimal64:
1002 case tok::kw__Decimal128:
1003
Chris Lattner861a2262008-04-13 18:59:07 +00001004 // struct-or-union-specifier (C99) or class-specifier (C++)
1005 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001006 case tok::kw_struct:
1007 case tok::kw_union:
1008 // enum-specifier
1009 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001010
Chris Lattneracd58a32006-08-06 17:24:14 +00001011 // type-qualifier
1012 case tok::kw_const:
1013 case tok::kw_volatile:
1014 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001015
Chris Lattneracd58a32006-08-06 17:24:14 +00001016 // function-specifier
1017 case tok::kw_inline:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001018
Chris Lattner599e47e2007-08-09 17:01:07 +00001019 // GNU typeof support.
1020 case tok::kw_typeof:
1021
1022 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001023 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00001024 return true;
Chris Lattner8b2ec162008-07-26 03:38:44 +00001025
1026 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1027 case tok::less:
1028 return getLang().ObjC1;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001029
Chris Lattneracd58a32006-08-06 17:24:14 +00001030 // typedef-name
1031 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +00001032 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +00001033 }
1034}
1035
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001036
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001037/// ParseTypeQualifierListOpt
1038/// type-qualifier-list: [C99 6.7.5]
1039/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001040/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001041/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001042/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001043///
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001044void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001045 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001046 int isInvalid = false;
1047 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001048 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001049
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001050 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001051 default:
Chris Lattnere37e2332006-08-15 04:50:22 +00001052 // If this is not a type-qualifier token, we're done reading type
1053 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +00001054 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001055 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001056 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +00001057 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1058 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001059 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001060 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +00001061 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1062 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001063 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001064 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001065 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1066 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001067 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001068 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001069 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +00001070 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001071 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001072
1073 // If the specifier combination wasn't legal, issue a diagnostic.
1074 if (isInvalid) {
1075 assert(PrevSpec && "Method did not return previous specifier!");
1076 if (isInvalid == 1) // Error.
1077 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1078 else // extwarn.
1079 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1080 }
1081 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001082 }
1083}
1084
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001085
1086/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1087///
1088void Parser::ParseDeclarator(Declarator &D) {
1089 /// This implements the 'declarator' production in the C grammar, then checks
1090 /// for well-formedness and issues diagnostics.
1091 ParseDeclaratorInternal(D);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001092}
1093
1094/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001095/// declarator: [C99 6.7.5]
1096/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001097/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1098/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001099///
1100/// pointer: [C99 6.7.5]
1101/// '*' type-qualifier-list[opt]
1102/// '*' type-qualifier-list[opt] pointer
1103///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001104void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001105 tok::TokenKind Kind = Tok.getKind();
1106
1107 // Not a pointer or C++ reference.
Chris Lattner788404f2008-02-21 01:32:26 +00001108 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001109 return ParseDirectDeclarator(D);
1110
Bill Wendling3708c182007-05-27 10:15:43 +00001111 // Otherwise, '*' -> pointer or '&' -> reference.
1112 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1113
1114 if (Kind == tok::star) {
Chris Lattner788404f2008-02-21 01:32:26 +00001115 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00001116 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001117
Bill Wendling3708c182007-05-27 10:15:43 +00001118 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001119
Bill Wendling3708c182007-05-27 10:15:43 +00001120 // Recursively parse the declarator.
1121 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +00001122
Bill Wendling3708c182007-05-27 10:15:43 +00001123 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001124 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1125 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001126 } else {
1127 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001128 DeclSpec DS;
1129
1130 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1131 // cv-qualifiers are introduced through the use of a typedef or of a
1132 // template type argument, in which case the cv-qualifiers are ignored.
1133 //
1134 // [GNU] Retricted references are allowed.
1135 // [GNU] Attributes on references are allowed.
1136 ParseTypeQualifierListOpt(DS);
1137
1138 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1139 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1140 Diag(DS.getConstSpecLoc(),
1141 diag::err_invalid_reference_qualifier_application,
1142 "const");
1143 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1144 Diag(DS.getVolatileSpecLoc(),
1145 diag::err_invalid_reference_qualifier_application,
1146 "volatile");
1147 }
Bill Wendling3708c182007-05-27 10:15:43 +00001148
1149 // Recursively parse the declarator.
1150 ParseDeclaratorInternal(D);
1151
1152 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001153 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1154 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001155 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001156}
1157
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001158/// ParseDirectDeclarator
1159/// direct-declarator: [C99 6.7.5]
1160/// identifier
1161/// '(' declarator ')'
1162/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001163/// [C90] direct-declarator '[' constant-expression[opt] ']'
1164/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1165/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1166/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1167/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001168/// direct-declarator '(' parameter-type-list ')'
1169/// direct-declarator '(' identifier-list[opt] ')'
1170/// [GNU] direct-declarator '(' parameter-forward-declarations
1171/// parameter-type-list[opt] ')'
1172///
Chris Lattneracd58a32006-08-06 17:24:14 +00001173void Parser::ParseDirectDeclarator(Declarator &D) {
1174 // Parse the first direct-declarator seen.
Chris Lattner76c72282007-10-09 17:33:22 +00001175 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001176 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1177 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1178 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001179 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001180 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001181 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001182 // Example: 'char (*X)' or 'int (*XX)(void)'
1183 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001184 } else if (D.mayOmitIdentifier()) {
1185 // This could be something simple like "int" (in which case the declarator
1186 // portion is empty), if an abstract-declarator is allowed.
1187 D.SetIdentifier(0, Tok.getLocation());
1188 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001189 // Expected identifier or '('.
1190 Diag(Tok, diag::err_expected_ident_lparen);
1191 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001192 }
1193
1194 assert(D.isPastIdentifier() &&
1195 "Haven't past the location of the identifier yet?");
1196
1197 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001198 if (Tok.is(tok::l_paren)) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001199 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00001200 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001201 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001202 } else {
1203 break;
1204 }
1205 }
1206}
1207
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001208/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1209/// only called before the identifier, so these are most likely just grouping
1210/// parens for precedence. If we find that these are actually function
1211/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1212///
1213/// direct-declarator:
1214/// '(' declarator ')'
1215/// [GNU] '(' attributes declarator ')'
1216///
1217void Parser::ParseParenDeclarator(Declarator &D) {
1218 SourceLocation StartLoc = ConsumeParen();
1219 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1220
1221 // If we haven't past the identifier yet (or where the identifier would be
1222 // stored, if this is an abstract declarator), then this is probably just
1223 // grouping parens. However, if this could be an abstract-declarator, then
1224 // this could also be the start of function arguments (consider 'void()').
1225 bool isGrouping;
1226
1227 if (!D.mayOmitIdentifier()) {
1228 // If this can't be an abstract-declarator, this *must* be a grouping
1229 // paren, because we haven't seen the identifier yet.
1230 isGrouping = true;
1231 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1232 isDeclarationSpecifier()) { // 'int(int)' is a function.
1233 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1234 // considered to be a type, not a K&R identifier-list.
1235 isGrouping = false;
1236 } else {
1237 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1238 isGrouping = true;
1239 }
1240
1241 // If this is a grouping paren, handle:
1242 // direct-declarator: '(' declarator ')'
1243 // direct-declarator: '(' attributes declarator ')'
1244 if (isGrouping) {
1245 if (Tok.is(tok::kw___attribute))
1246 D.AddAttributes(ParseAttributes());
1247
1248 ParseDeclaratorInternal(D);
1249 // Match the ')'.
1250 MatchRHSPunctuation(tok::r_paren, StartLoc);
1251 return;
1252 }
1253
1254 // Okay, if this wasn't a grouping paren, it must be the start of a function
1255 // argument list. Recognize that this declarator will never have an
1256 // identifier (and remember where it would have been), then fall through to
1257 // the handling of argument lists.
1258 D.SetIdentifier(0, Tok.getLocation());
1259
1260 ParseFunctionDeclarator(StartLoc, D);
1261}
1262
1263/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1264/// declarator D up to a paren, which indicates that we are parsing function
1265/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00001266///
1267/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001268/// parameter-type-list: [C99 6.7.5]
1269/// parameter-list
1270/// parameter-list ',' '...'
1271///
1272/// parameter-list: [C99 6.7.5]
1273/// parameter-declaration
1274/// parameter-list ',' parameter-declaration
1275///
1276/// parameter-declaration: [C99 6.7.5]
1277/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001278/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001279/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001280/// declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00001281/// [C++] declaration-specifiers abstract-declarator[opt]
1282/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001283/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001284///
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001285void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1286 // lparen is already consumed!
1287 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001288
Chris Lattneracd58a32006-08-06 17:24:14 +00001289 // Okay, this is the parameter list of a function definition, or it is an
1290 // identifier list of a K&R-style function.
Chris Lattneredc9e392006-12-02 06:21:46 +00001291
Chris Lattner76c72282007-10-09 17:33:22 +00001292 if (Tok.is(tok::r_paren)) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00001293 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00001294 // int() -> no prototype, no '...'.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001295 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1296 /*variadic*/ false,
1297 /*arglist*/ 0, 0, LParenLoc));
1298
1299 ConsumeParen(); // Eat the closing ')'.
1300 return;
Chris Lattner76c72282007-10-09 17:33:22 +00001301 } else if (Tok.is(tok::identifier) &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001302 // K&R identifier lists can't have typedefs as identifiers, per
1303 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001304 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001305 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1306 // normal declarators, not for abstract-declarators.
Chris Lattner6c940e62008-04-06 06:34:08 +00001307 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001308 }
1309
1310 // Finally, a normal, non-empty parameter type list.
1311
1312 // Build up an array of information about the parsed arguments.
1313 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001314
1315 // Enter function-declaration scope, limiting any declarators to the
1316 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001317 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001318
1319 bool IsVariadic = false;
1320 while (1) {
1321 if (Tok.is(tok::ellipsis)) {
1322 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001323
Chris Lattner371ed4e2008-04-06 06:57:35 +00001324 // Check to see if this is "void(...)" which is not allowed.
1325 if (ParamInfo.empty()) {
1326 // Otherwise, parse parameter type list. If it starts with an
1327 // ellipsis, diagnose the malformed function.
1328 Diag(Tok, diag::err_ellipsis_first_arg);
1329 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner969ca152006-12-03 06:29:03 +00001330 }
Chris Lattner7f024fe2008-01-31 06:10:07 +00001331
Chris Lattner371ed4e2008-04-06 06:57:35 +00001332 ConsumeToken(); // Consume the ellipsis.
1333 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00001334 }
1335
Chris Lattner371ed4e2008-04-06 06:57:35 +00001336 SourceLocation DSStart = Tok.getLocation();
Chris Lattner43e956c2006-11-28 04:05:37 +00001337
Chris Lattner371ed4e2008-04-06 06:57:35 +00001338 // Parse the declaration-specifiers.
1339 DeclSpec DS;
1340 ParseDeclarationSpecifiers(DS);
1341
1342 // Parse the declarator. This is "PrototypeContext", because we must
1343 // accept either 'declarator' or 'abstract-declarator' here.
1344 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1345 ParseDeclarator(ParmDecl);
1346
1347 // Parse GNU attributes, if present.
1348 if (Tok.is(tok::kw___attribute))
1349 ParmDecl.AddAttributes(ParseAttributes());
1350
Chris Lattner371ed4e2008-04-06 06:57:35 +00001351 // Remember this parsed parameter in ParamInfo.
1352 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1353
Chris Lattner371ed4e2008-04-06 06:57:35 +00001354 // If no parameter was specified, verify that *something* was specified,
1355 // otherwise we have a missing type and identifier.
1356 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1357 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1358 // Completely missing, emit error.
1359 Diag(DSStart, diag::err_missing_param);
1360 } else {
1361 // Otherwise, we have something. Add it and let semantic analysis try
1362 // to grok it and add the result to the ParamInfo we are building.
1363
1364 // Inform the actions module about the parameter declarator, so it gets
1365 // added to the current scope.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001366 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1367
1368 // Parse the default argument, if any. We parse the default
1369 // arguments in all dialects; the semantic analysis in
1370 // ActOnParamDefaultArgument will reject the default argument in
1371 // C.
1372 if (Tok.is(tok::equal)) {
1373 SourceLocation EqualLoc = Tok.getLocation();
1374
1375 // Consume the '='.
1376 ConsumeToken();
1377
1378 // Parse the default argument
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001379 ExprResult DefArgResult = ParseAssignmentExpression();
1380 if (DefArgResult.isInvalid) {
1381 SkipUntil(tok::comma, tok::r_paren, true, true);
1382 } else {
1383 // Inform the actions module about the default argument
1384 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1385 }
1386 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001387
1388 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001389 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner371ed4e2008-04-06 06:57:35 +00001390 }
1391
1392 // If the next token is a comma, consume it and keep reading arguments.
1393 if (Tok.isNot(tok::comma)) break;
1394
1395 // Consume the comma.
1396 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001397 }
1398
Chris Lattner371ed4e2008-04-06 06:57:35 +00001399 // Leave prototype scope.
1400 ExitScope();
1401
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001402 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001403 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1404 &ParamInfo[0], ParamInfo.size(),
1405 LParenLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001406
Chris Lattner14776b92006-08-06 22:27:40 +00001407 // If we have the closing ')', eat it and we're done.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001408 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001409}
Chris Lattneracd58a32006-08-06 17:24:14 +00001410
Chris Lattner6c940e62008-04-06 06:34:08 +00001411/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1412/// we found a K&R-style identifier list instead of a type argument list. The
1413/// current token is known to be the first identifier in the list.
1414///
1415/// identifier-list: [C99 6.7.5]
1416/// identifier
1417/// identifier-list ',' identifier
1418///
1419void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1420 Declarator &D) {
1421 // Build up an array of information about the parsed arguments.
1422 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1423 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1424
1425 // If there was no identifier specified for the declarator, either we are in
1426 // an abstract-declarator, or we are in a parameter declarator which was found
1427 // to be abstract. In abstract-declarators, identifier lists are not valid:
1428 // diagnose this.
1429 if (!D.getIdentifier())
1430 Diag(Tok, diag::ext_ident_list_in_param);
1431
1432 // Tok is known to be the first identifier in the list. Remember this
1433 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00001434 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00001435 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1436 Tok.getLocation(), 0));
1437
Chris Lattner9186f552008-04-06 06:39:19 +00001438 ConsumeToken(); // eat the first identifier.
Chris Lattner6c940e62008-04-06 06:34:08 +00001439
1440 while (Tok.is(tok::comma)) {
1441 // Eat the comma.
1442 ConsumeToken();
1443
Chris Lattner9186f552008-04-06 06:39:19 +00001444 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00001445 if (Tok.isNot(tok::identifier)) {
1446 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00001447 SkipUntil(tok::r_paren);
1448 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00001449 }
Chris Lattner67b450c2008-04-06 06:47:48 +00001450
Chris Lattner6c940e62008-04-06 06:34:08 +00001451 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00001452
1453 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1454 if (Actions.isTypeName(*ParmII, CurScope))
1455 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner6c940e62008-04-06 06:34:08 +00001456
1457 // Verify that the argument identifier has not already been mentioned.
1458 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner9186f552008-04-06 06:39:19 +00001459 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1460 } else {
1461 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00001462 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1463 Tok.getLocation(), 0));
Chris Lattner9186f552008-04-06 06:39:19 +00001464 }
Chris Lattner6c940e62008-04-06 06:34:08 +00001465
1466 // Eat the identifier.
1467 ConsumeToken();
1468 }
1469
Chris Lattner9186f552008-04-06 06:39:19 +00001470 // Remember that we parsed a function type, and remember the attributes. This
1471 // function type is always a K&R style function type, which is not varargs and
1472 // has no prototype.
1473 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1474 &ParamInfo[0], ParamInfo.size(),
1475 LParenLoc));
Chris Lattner6c940e62008-04-06 06:34:08 +00001476
1477 // If we have the closing ')', eat it and we're done.
Chris Lattner9186f552008-04-06 06:39:19 +00001478 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00001479}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001480
Chris Lattnere8074e62006-08-06 18:30:15 +00001481/// [C90] direct-declarator '[' constant-expression[opt] ']'
1482/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1483/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1484/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1485/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1486void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001487 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001488
1489 // If valid, this location is the position where we read the 'static' keyword.
1490 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00001491 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001492 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001493
1494 // If there is a type-qualifier-list, read it now.
1495 DeclSpec DS;
1496 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001497
1498 // If we haven't already read 'static', check to see if there is one after the
1499 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001500 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001501 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001502
1503 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001504 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001505 ExprResult NumElements(false);
Chris Lattner521ff2b2008-04-06 05:26:30 +00001506
1507 // Handle the case where we have '[*]' as the array size. However, a leading
1508 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1509 // the the token after the star is a ']'. Since stars in arrays are
1510 // infrequent, use of lookahead is not costly here.
1511 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00001512 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00001513
Chris Lattner521ff2b2008-04-06 05:26:30 +00001514 if (StaticLoc.isValid())
1515 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1516 StaticLoc = SourceLocation(); // Drop the static.
1517 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00001518 } else if (Tok.isNot(tok::r_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001519 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001520 NumElements = ParseAssignmentExpression();
1521 }
1522
1523 // If there was an error parsing the assignment-expression, recover.
1524 if (NumElements.isInvalid) {
1525 // If the expression was invalid, skip it.
1526 SkipUntil(tok::r_square);
1527 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001528 }
1529
Chris Lattner04f80192006-08-15 04:55:54 +00001530 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001531
Chris Lattnere8074e62006-08-06 18:30:15 +00001532 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1533 // it was not a constant expression.
1534 if (!getLang().C99) {
1535 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001536 if (isStar || StaticLoc.isValid() ||
1537 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001538 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001539 }
Bill Wendling93efb222007-06-02 23:28:54 +00001540
Chris Lattner6c7416c2006-08-07 00:19:33 +00001541 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001542 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1543 StaticLoc.isValid(), isStar,
1544 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001545}
1546
Steve Naroffad373bd2007-07-31 12:34:36 +00001547/// [GNU] typeof-specifier:
1548/// typeof ( expressions )
1549/// typeof ( type-name )
1550///
1551void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00001552 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00001553 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00001554 SourceLocation StartLoc = ConsumeToken();
1555
Chris Lattner76c72282007-10-09 17:33:22 +00001556 if (Tok.isNot(tok::l_paren)) {
Steve Naroff4bd2f712007-08-02 02:53:48 +00001557 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1558 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001559 }
1560 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1561
1562 if (isTypeSpecifierQualifier()) {
1563 TypeTy *Ty = ParseTypeName();
1564
Steve Naroff872da802007-07-31 23:56:32 +00001565 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1566
Chris Lattner76c72282007-10-09 17:33:22 +00001567 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001568 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001569 return;
1570 }
1571 RParenLoc = ConsumeParen();
1572 const char *PrevSpec = 0;
1573 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1574 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1575 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001576 } else { // we have an expression.
1577 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001578
Chris Lattner76c72282007-10-09 17:33:22 +00001579 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001580 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001581 return;
1582 }
1583 RParenLoc = ConsumeParen();
1584 const char *PrevSpec = 0;
1585 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1586 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1587 Result.Val))
1588 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001589 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001590}
1591
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001592