blob: 3b6e04d84f471bccf802b19c8e4c00d89aa189fe [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.
Chris Lattner76c72282007-10-09 17:33:22 +0000259 if (Tok.is(tok::kw_asm))
Chris Lattner6d7e6342006-08-15 03:41:14 +0000260 ParseSimpleAsm();
261
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000262 // If attributes are present, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000263 if (Tok.is(tok::kw___attribute))
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000264 D.AddAttributes(ParseAttributes());
Steve Naroff61091402007-09-12 14:07:44 +0000265
266 // Inform the current actions module that we just parsed this declarator.
267 // FIXME: pass asm & attributes.
Steve Naroff30d242c2007-09-15 18:49:24 +0000268 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff61091402007-09-12 14:07:44 +0000269
Chris Lattner53361ac2006-08-10 05:19:57 +0000270 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000271 ExprResult Init;
Chris Lattner76c72282007-10-09 17:33:22 +0000272 if (Tok.is(tok::equal)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000273 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000274 Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000275 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000276 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000277 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000278 }
Steve Naroff61091402007-09-12 14:07:44 +0000279 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Chris Lattner53361ac2006-08-10 05:19:57 +0000280 }
281
Chris Lattner53361ac2006-08-10 05:19:57 +0000282 // If we don't have a comma, it is either the end of the list (a ';') or an
283 // error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000284 if (Tok.isNot(tok::comma))
Chris Lattner53361ac2006-08-10 05:19:57 +0000285 break;
286
287 // Consume the comma.
288 ConsumeToken();
289
290 // Parse the next declarator.
291 D.clear();
292 ParseDeclarator(D);
293 }
294
Chris Lattner76c72282007-10-09 17:33:22 +0000295 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000296 ConsumeToken();
Chris Lattner776fac82007-06-09 00:53:06 +0000297 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000298 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000299 // If this is an ObjC2 for-each loop, this is a successful declarator
300 // parse. The syntax for these looks like:
301 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000302 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000303 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
304 }
Chris Lattner776fac82007-06-09 00:53:06 +0000305 Diag(Tok, diag::err_parse_error);
306 // Skip to end of block or statement
Chris Lattner43ba2512007-08-21 18:36:18 +0000307 SkipUntil(tok::r_brace, true, true);
Chris Lattner76c72282007-10-09 17:33:22 +0000308 if (Tok.is(tok::semi))
Chris Lattner776fac82007-06-09 00:53:06 +0000309 ConsumeToken();
310 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000311}
312
Chris Lattner1890ac82006-08-13 01:16:23 +0000313/// ParseSpecifierQualifierList
314/// specifier-qualifier-list:
315/// type-specifier specifier-qualifier-list[opt]
316/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000317/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000318///
319void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
320 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
321 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000322 ParseDeclarationSpecifiers(DS);
323
324 // Validate declspec for type-name.
325 unsigned Specs = DS.getParsedSpecifiers();
326 if (Specs == DeclSpec::PQ_None)
327 Diag(Tok, diag::err_typename_requires_specqual);
328
Chris Lattner1b22eed2006-11-28 05:12:07 +0000329 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000330 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000331 if (DS.getStorageClassSpecLoc().isValid())
332 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
333 else
334 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000335 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000336 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000337
338 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000339 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000340 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000341 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000342 }
343}
Chris Lattner53361ac2006-08-10 05:19:57 +0000344
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000345/// ParseDeclarationSpecifiers
346/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000347/// storage-class-specifier declaration-specifiers[opt]
348/// type-specifier declaration-specifiers[opt]
349/// type-qualifier declaration-specifiers[opt]
350/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000351/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000352///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000353/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000354/// 'typedef'
355/// 'extern'
356/// 'static'
357/// 'auto'
358/// 'register'
359/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000360/// type-specifier: [C99 6.7.2]
361/// 'void'
362/// 'char'
363/// 'short'
364/// 'int'
365/// 'long'
366/// 'float'
367/// 'double'
368/// 'signed'
369/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000370/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000371/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000372/// typedef-name
Bill Wendling4073ed52007-02-13 01:51:42 +0000373/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000374/// [C99] '_Bool'
375/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000376/// [C99] '_Imaginary' // Removed in TC2?
377/// [GNU] '_Decimal32'
378/// [GNU] '_Decimal64'
379/// [GNU] '_Decimal128'
Steve Naroff872da802007-07-31 23:56:32 +0000380/// [GNU] typeof-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000381/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff7e901fd2007-08-22 23:18:22 +0000382/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000383/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000384/// 'const'
385/// 'volatile'
386/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000387/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000388/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000389///
390void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner02c04392007-07-25 00:24:17 +0000391 DS.Range.setBegin(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000392 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000393 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000394 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000395 SourceLocation Loc = Tok.getLocation();
396
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000397 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000398 // typedef-name
399 case tok::identifier:
400 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000401 // a type-specifier. Without this check we misparse:
402 // typedef int X; struct Y { short X; }; as 'short int'.
Chris Lattnerf055d432006-11-28 04:28:12 +0000403 if (!DS.hasTypeSpecifier()) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000404 // It has to be available as a typedef too!
405 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
406 CurScope)) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000407 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000408 TypeRep);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000409 if (isInvalid)
410 break;
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000411 // FIXME: restrict this to "id" and ObjC classnames.
412 DS.Range.setEnd(Tok.getLocation());
413 ConsumeToken(); // The identifier
414 if (Tok.is(tok::less)) {
Steve Naroffc5484042007-10-30 02:23:23 +0000415 SourceLocation endProtoLoc;
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000416 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffc5484042007-10-30 02:23:23 +0000417 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000418 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
419 new llvm::SmallVector<DeclTy *, 8>;
420 DS.setProtocolQualifiers(ProtocolDecl);
421 Actions.FindProtocolDeclaration(Loc,
422 &ProtocolRefs[0], ProtocolRefs.size(),
423 *ProtocolDecl);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000424 }
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000425 continue;
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000426 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000427 }
428 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000429 default:
430 // If this is not a declaration specifier token, we're done reading decl
431 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000432 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000433 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000434
435 // GNU attributes support.
436 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000437 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000438 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000439
440 // storage-class-specifier
441 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000442 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000443 break;
444 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000445 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000446 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000447 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000448 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000449 case tok::kw___private_extern__:
Steve Narofffda82092008-01-25 22:14:40 +0000450 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, PrevSpec);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000451 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000452 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000453 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000454 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000455 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000456 break;
457 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000458 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000459 break;
460 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000461 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000462 break;
463 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000464 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000465 break;
466
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000467 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000468 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000469 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000470 break;
471 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000472 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000473 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000474 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000475 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000476 break;
477 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000478 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000479 break;
480 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000481 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000482 break;
483 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000484 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000485 break;
486 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000487 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000488 break;
489 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000490 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000491 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000492 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000493 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000494 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000495 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000496 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000497 break;
498 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000499 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000500 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000501 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000502 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000503 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000504 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000505 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000507 break;
508 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000509 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000510 break;
511 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000512 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000513 break;
514 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000515 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000516 break;
517
Chris Lattner1890ac82006-08-13 01:16:23 +0000518 case tok::kw_struct:
519 case tok::kw_union:
520 ParseStructUnionSpecifier(DS);
521 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000522 case tok::kw_enum:
523 ParseEnumSpecifier(DS);
524 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000525
Steve Naroffad373bd2007-07-31 12:34:36 +0000526 // GNU typeof support.
527 case tok::kw_typeof:
528 ParseTypeofSpecifier(DS);
529 continue;
530
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000531 // type-qualifier
532 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000533 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
534 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000535 break;
536 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000537 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
538 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000539 break;
540 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000541 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
542 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000543 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000544
545 // function-specifier
546 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000547 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000548 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000549 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000550 // If the specifier combination wasn't legal, issue a diagnostic.
551 if (isInvalid) {
552 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000553 if (isInvalid == 1) // Error.
554 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
555 else // extwarn.
556 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000557 }
Chris Lattner02c04392007-07-25 00:24:17 +0000558 DS.Range.setEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000559 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000560 }
561}
562
Chris Lattnerffbc2712007-01-25 06:05:38 +0000563/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
564/// the first token has already been read and has been turned into an instance
565/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
566/// otherwise it returns false and fills in Decl.
567bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
Steve Naroffb8371e12007-06-09 03:39:29 +0000568 AttributeList *Attr = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000569 // If attributes exist after tag, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000570 if (Tok.is(tok::kw___attribute))
Steve Naroffb8371e12007-06-09 03:39:29 +0000571 Attr = ParseAttributes();
Chris Lattnerffbc2712007-01-25 06:05:38 +0000572
Chris Lattner1890ac82006-08-13 01:16:23 +0000573 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner76c72282007-10-09 17:33:22 +0000574 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000575 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattner02c04392007-07-25 00:24:17 +0000576
577 // Skip the rest of this declarator, up until the comma or semicolon.
578 SkipUntil(tok::comma, true);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000579 return true;
Chris Lattner1890ac82006-08-13 01:16:23 +0000580 }
581
Chris Lattner8c6519a2007-01-22 07:41:36 +0000582 // If an identifier is present, consume and remember it.
583 IdentifierInfo *Name = 0;
584 SourceLocation NameLoc;
Chris Lattner76c72282007-10-09 17:33:22 +0000585 if (Tok.is(tok::identifier)) {
Chris Lattner8c6519a2007-01-22 07:41:36 +0000586 Name = Tok.getIdentifierInfo();
587 NameLoc = ConsumeToken();
588 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000589
Chris Lattner8c6519a2007-01-22 07:41:36 +0000590 // There are three options here. If we have 'struct foo;', then this is a
591 // forward declaration. If we have 'struct foo {...' then this is a
Chris Lattner7b9ace62007-01-23 20:11:08 +0000592 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Chris Lattner8799cf22007-01-23 01:57:16 +0000593 //
594 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
595 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
596 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
597 //
Chris Lattner7b9ace62007-01-23 20:11:08 +0000598 Action::TagKind TK;
Chris Lattner76c72282007-10-09 17:33:22 +0000599 if (Tok.is(tok::l_brace))
Chris Lattner7b9ace62007-01-23 20:11:08 +0000600 TK = Action::TK_Definition;
Chris Lattner76c72282007-10-09 17:33:22 +0000601 else if (Tok.is(tok::semi))
Chris Lattner7b9ace62007-01-23 20:11:08 +0000602 TK = Action::TK_Declaration;
603 else
604 TK = Action::TK_Reference;
Steve Naroff30d242c2007-09-15 18:49:24 +0000605 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000606 return false;
607}
608
609
610/// ParseStructUnionSpecifier
611/// struct-or-union-specifier: [C99 6.7.2.1]
612/// struct-or-union identifier[opt] '{' struct-contents '}'
613/// struct-or-union identifier
614/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
615/// '}' attributes[opt]
616/// [GNU] struct-or-union attributes[opt] identifier
617/// struct-or-union:
618/// 'struct'
619/// 'union'
620///
621void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000622 assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) &&
623 "Not a struct/union specifier");
Chris Lattnerffbc2712007-01-25 06:05:38 +0000624 DeclSpec::TST TagType =
Chris Lattner76c72282007-10-09 17:33:22 +0000625 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000626 SourceLocation StartLoc = ConsumeToken();
627
628 // Parse the tag portion of this.
629 DeclTy *TagDecl;
630 if (ParseTag(TagDecl, TagType, StartLoc))
631 return;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000632
Chris Lattner90a26b02007-01-23 04:38:16 +0000633 // If there is a body, parse it and inform the actions module.
Chris Lattner76c72282007-10-09 17:33:22 +0000634 if (Tok.is(tok::l_brace))
Chris Lattner1300fb92007-01-23 23:42:53 +0000635 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Chris Lattnerda72c822006-08-13 22:16:42 +0000636
637 const char *PrevSpec = 0;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000638 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000639 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000640}
641
Chris Lattner70ae4912007-10-29 04:42:53 +0000642/// ParseStructDeclaration - Parse a struct declaration without the terminating
643/// semicolon.
644///
Chris Lattner90a26b02007-01-23 04:38:16 +0000645/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +0000646/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +0000647/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +0000648/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +0000649/// struct-declarator-list:
650/// struct-declarator
651/// struct-declarator-list ',' struct-declarator
652/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
653/// struct-declarator:
654/// declarator
655/// [GNU] declarator attributes[opt]
656/// declarator[opt] ':' constant-expression
657/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
658///
Steve Naroff97170802007-08-20 22:28:22 +0000659void Parser::ParseStructDeclaration(DeclTy *TagDecl,
Steve Naroff4ca5d5a2007-08-28 16:31:47 +0000660 llvm::SmallVectorImpl<DeclTy*> &FieldDecls) {
Steve Naroff97170802007-08-20 22:28:22 +0000661 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner76c72282007-10-09 17:33:22 +0000662 if (Tok.is(tok::kw___extension__))
Steve Naroff97170802007-08-20 22:28:22 +0000663 ConsumeToken();
664
665 // Parse the common specifier-qualifiers-list piece.
666 DeclSpec DS;
667 SourceLocation SpecQualLoc = Tok.getLocation();
668 ParseSpecifierQualifierList(DS);
669 // TODO: Does specifier-qualifier list correctly check that *something* is
670 // specified?
671
672 // If there are no declarators, issue a warning.
Chris Lattner76c72282007-10-09 17:33:22 +0000673 if (Tok.is(tok::semi)) {
Steve Naroffbff73852008-02-11 22:40:08 +0000674 Diag(SpecQualLoc, diag::w_no_declarators);
Steve Naroff97170802007-08-20 22:28:22 +0000675 return;
676 }
677
678 // Read struct-declarators until we find the semicolon.
679 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
680
681 while (1) {
682 /// struct-declarator: declarator
683 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +0000684 if (Tok.isNot(tok::colon))
Steve Naroff97170802007-08-20 22:28:22 +0000685 ParseDeclarator(DeclaratorInfo);
686
687 ExprTy *BitfieldSize = 0;
Chris Lattner76c72282007-10-09 17:33:22 +0000688 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +0000689 ConsumeToken();
690 ExprResult Res = ParseConstantExpression();
691 if (Res.isInvalid) {
692 SkipUntil(tok::semi, true, true);
693 } else {
694 BitfieldSize = Res.Val;
695 }
696 }
697
698 // If attributes exist after the declarator, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000699 if (Tok.is(tok::kw___attribute))
Steve Naroff97170802007-08-20 22:28:22 +0000700 DeclaratorInfo.AddAttributes(ParseAttributes());
701
702 // Install the declarator into the current TagDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +0000703 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, SpecQualLoc,
Steve Naroff97170802007-08-20 22:28:22 +0000704 DeclaratorInfo, BitfieldSize);
705 FieldDecls.push_back(Field);
706
707 // If we don't have a comma, it is either the end of the list (a ';')
708 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000709 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +0000710 return;
Steve Naroff97170802007-08-20 22:28:22 +0000711
712 // Consume the comma.
713 ConsumeToken();
714
715 // Parse the next declarator.
716 DeclaratorInfo.clear();
717
718 // Attributes are only allowed on the second declarator.
Chris Lattner76c72282007-10-09 17:33:22 +0000719 if (Tok.is(tok::kw___attribute))
Steve Naroff97170802007-08-20 22:28:22 +0000720 DeclaratorInfo.AddAttributes(ParseAttributes());
721 }
Steve Naroff97170802007-08-20 22:28:22 +0000722}
723
724/// ParseStructUnionBody
725/// struct-contents:
726/// struct-declaration-list
727/// [EXT] empty
728/// [GNU] "struct-declaration-list" without terminatoring ';'
729/// struct-declaration-list:
730/// struct-declaration
731/// struct-declaration-list struct-declaration
732/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
733///
Chris Lattner1300fb92007-01-23 23:42:53 +0000734void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
735 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000736 SourceLocation LBraceLoc = ConsumeBrace();
737
Chris Lattner7b9ace62007-01-23 20:11:08 +0000738 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
739 // C++.
Chris Lattner76c72282007-10-09 17:33:22 +0000740 if (Tok.is(tok::r_brace))
Chris Lattner90a26b02007-01-23 04:38:16 +0000741 Diag(Tok, diag::ext_empty_struct_union_enum,
742 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000743
Chris Lattner23b7eb62007-06-15 23:05:46 +0000744 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner1300fb92007-01-23 23:42:53 +0000745
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 }
Steve Naroff97170802007-08-20 22:28:22 +0000756 ParseStructDeclaration(TagDecl, FieldDecls);
Chris Lattner736ed5d2007-06-09 05:59:07 +0000757
Chris Lattner76c72282007-10-09 17:33:22 +0000758 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000759 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +0000760 } else if (Tok.is(tok::r_brace)) {
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000761 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
762 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000763 } else {
764 Diag(Tok, diag::err_expected_semi_decl_list);
765 // Skip to end of block or statement
766 SkipUntil(tok::r_brace, true, true);
767 }
768 }
769
Steve Naroff33a1e802007-10-29 21:38:07 +0000770 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +0000771
Fariborz Jahanian343f7092007-09-29 00:54:24 +0000772 Actions.ActOnFields(CurScope,
Steve Naroff33a1e802007-10-29 21:38:07 +0000773 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
774 LBraceLoc, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000775
Steve Naroffb8371e12007-06-09 03:39:29 +0000776 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000777 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000778 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000779 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000780}
781
782
Chris Lattner3b561a32006-08-13 00:12:11 +0000783/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000784/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000785/// 'enum' identifier[opt] '{' enumerator-list '}'
786/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000787/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
788/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000789/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000790/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000791void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000792 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000793 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000794
Chris Lattnerffbc2712007-01-25 06:05:38 +0000795 // Parse the tag portion of this.
796 DeclTy *TagDecl;
797 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000798 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000799
Chris Lattner76c72282007-10-09 17:33:22 +0000800 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000801 ParseEnumBody(StartLoc, TagDecl);
802
Chris Lattner3b561a32006-08-13 00:12:11 +0000803 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000804 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000805 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000806 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000807}
808
Chris Lattnerc1915e22007-01-25 07:29:02 +0000809/// ParseEnumBody - Parse a {} enclosed enumerator-list.
810/// enumerator-list:
811/// enumerator
812/// enumerator-list ',' enumerator
813/// enumerator:
814/// enumeration-constant
815/// enumeration-constant '=' constant-expression
816/// enumeration-constant:
817/// identifier
818///
819void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
820 SourceLocation LBraceLoc = ConsumeBrace();
821
Chris Lattner37256fb2007-08-27 17:24:30 +0000822 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +0000823 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000824 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
825
Chris Lattner23b7eb62007-06-15 23:05:46 +0000826 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000827
Chris Lattner4ef40012007-06-11 01:28:17 +0000828 DeclTy *LastEnumConstDecl = 0;
829
Chris Lattnerc1915e22007-01-25 07:29:02 +0000830 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +0000831 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000832 IdentifierInfo *Ident = Tok.getIdentifierInfo();
833 SourceLocation IdentLoc = ConsumeToken();
834
835 SourceLocation EqualLoc;
836 ExprTy *AssignedVal = 0;
Chris Lattner76c72282007-10-09 17:33:22 +0000837 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000838 EqualLoc = ConsumeToken();
839 ExprResult Res = ParseConstantExpression();
840 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000841 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000842 else
843 AssignedVal = Res.Val;
844 }
845
846 // Install the enumerator constant into EnumDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +0000847 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4ef40012007-06-11 01:28:17 +0000848 LastEnumConstDecl,
849 IdentLoc, Ident,
850 EqualLoc, AssignedVal);
851 EnumConstantDecls.push_back(EnumConstDecl);
852 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000853
Chris Lattner76c72282007-10-09 17:33:22 +0000854 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000855 break;
856 SourceLocation CommaLoc = ConsumeToken();
857
Chris Lattner76c72282007-10-09 17:33:22 +0000858 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000859 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
860 }
861
862 // Eat the }.
863 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
864
Steve Naroff30d242c2007-09-15 18:49:24 +0000865 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +0000866 EnumConstantDecls.size());
867
Steve Naroff0f2fe172007-06-01 17:11:19 +0000868 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000869 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000870 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000871 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000872}
Chris Lattner3b561a32006-08-13 00:12:11 +0000873
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000874/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000875/// start of a type-qualifier-list.
876bool Parser::isTypeQualifier() const {
877 switch (Tok.getKind()) {
878 default: return false;
879 // type-qualifier
880 case tok::kw_const:
881 case tok::kw_volatile:
882 case tok::kw_restrict:
883 return true;
884 }
885}
886
887/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000888/// start of a specifier-qualifier-list.
889bool Parser::isTypeSpecifierQualifier() const {
890 switch (Tok.getKind()) {
891 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000892 // GNU attributes support.
893 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +0000894 // GNU typeof support.
895 case tok::kw_typeof:
896
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000897 // type-specifiers
898 case tok::kw_short:
899 case tok::kw_long:
900 case tok::kw_signed:
901 case tok::kw_unsigned:
902 case tok::kw__Complex:
903 case tok::kw__Imaginary:
904 case tok::kw_void:
905 case tok::kw_char:
906 case tok::kw_int:
907 case tok::kw_float:
908 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000909 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000910 case tok::kw__Bool:
911 case tok::kw__Decimal32:
912 case tok::kw__Decimal64:
913 case tok::kw__Decimal128:
914
915 // struct-or-union-specifier
916 case tok::kw_struct:
917 case tok::kw_union:
918 // enum-specifier
919 case tok::kw_enum:
920
921 // type-qualifier
922 case tok::kw_const:
923 case tok::kw_volatile:
924 case tok::kw_restrict:
925 return true;
926
927 // typedef-name
928 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000929 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000930 }
931}
932
Chris Lattneracd58a32006-08-06 17:24:14 +0000933/// isDeclarationSpecifier() - Return true if the current token is part of a
934/// declaration specifier.
935bool Parser::isDeclarationSpecifier() const {
936 switch (Tok.getKind()) {
937 default: return false;
938 // storage-class-specifier
939 case tok::kw_typedef:
940 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +0000941 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +0000942 case tok::kw_static:
943 case tok::kw_auto:
944 case tok::kw_register:
945 case tok::kw___thread:
946
947 // type-specifiers
948 case tok::kw_short:
949 case tok::kw_long:
950 case tok::kw_signed:
951 case tok::kw_unsigned:
952 case tok::kw__Complex:
953 case tok::kw__Imaginary:
954 case tok::kw_void:
955 case tok::kw_char:
956 case tok::kw_int:
957 case tok::kw_float:
958 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000959 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +0000960 case tok::kw__Bool:
961 case tok::kw__Decimal32:
962 case tok::kw__Decimal64:
963 case tok::kw__Decimal128:
964
965 // struct-or-union-specifier
966 case tok::kw_struct:
967 case tok::kw_union:
968 // enum-specifier
969 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000970
Chris Lattneracd58a32006-08-06 17:24:14 +0000971 // type-qualifier
972 case tok::kw_const:
973 case tok::kw_volatile:
974 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +0000975
Chris Lattneracd58a32006-08-06 17:24:14 +0000976 // function-specifier
977 case tok::kw_inline:
Chris Lattner7b20dc72007-08-09 16:40:21 +0000978
Chris Lattner599e47e2007-08-09 17:01:07 +0000979 // GNU typeof support.
980 case tok::kw_typeof:
981
982 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +0000983 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +0000984 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000985
Chris Lattneracd58a32006-08-06 17:24:14 +0000986 // typedef-name
987 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000988 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000989 }
990}
991
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000992
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000993/// ParseTypeQualifierListOpt
994/// type-qualifier-list: [C99 6.7.5]
995/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000996/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000997/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000998/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000999///
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001000void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001001 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001002 int isInvalid = false;
1003 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001004 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001005
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001006 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001007 default:
Chris Lattnere37e2332006-08-15 04:50:22 +00001008 // If this is not a type-qualifier token, we're done reading type
1009 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +00001010 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001011 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001012 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +00001013 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1014 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001015 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001016 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +00001017 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1018 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001019 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001020 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001021 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1022 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001023 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001024 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001025 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +00001026 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001027 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001028
1029 // If the specifier combination wasn't legal, issue a diagnostic.
1030 if (isInvalid) {
1031 assert(PrevSpec && "Method did not return previous specifier!");
1032 if (isInvalid == 1) // Error.
1033 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1034 else // extwarn.
1035 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1036 }
1037 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001038 }
1039}
1040
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001041
1042/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1043///
1044void Parser::ParseDeclarator(Declarator &D) {
1045 /// This implements the 'declarator' production in the C grammar, then checks
1046 /// for well-formedness and issues diagnostics.
1047 ParseDeclaratorInternal(D);
1048
Chris Lattner9fab3b92006-08-12 18:25:42 +00001049 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +00001050
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001051}
1052
1053/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001054/// declarator: [C99 6.7.5]
1055/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001056/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1057/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001058///
1059/// pointer: [C99 6.7.5]
1060/// '*' type-qualifier-list[opt]
1061/// '*' type-qualifier-list[opt] pointer
1062///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001063void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001064 tok::TokenKind Kind = Tok.getKind();
1065
1066 // Not a pointer or C++ reference.
1067 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001068 return ParseDirectDeclarator(D);
1069
Bill Wendling3708c182007-05-27 10:15:43 +00001070 // Otherwise, '*' -> pointer or '&' -> reference.
1071 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1072
1073 if (Kind == tok::star) {
1074 // Is a pointer
1075 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001076
Bill Wendling3708c182007-05-27 10:15:43 +00001077 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001078
Bill Wendling3708c182007-05-27 10:15:43 +00001079 // Recursively parse the declarator.
1080 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +00001081
Bill Wendling3708c182007-05-27 10:15:43 +00001082 // Remember that we parsed a pointer type, and remember the type-quals.
1083 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1084 } else {
1085 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001086 DeclSpec DS;
1087
1088 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1089 // cv-qualifiers are introduced through the use of a typedef or of a
1090 // template type argument, in which case the cv-qualifiers are ignored.
1091 //
1092 // [GNU] Retricted references are allowed.
1093 // [GNU] Attributes on references are allowed.
1094 ParseTypeQualifierListOpt(DS);
1095
1096 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1097 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1098 Diag(DS.getConstSpecLoc(),
1099 diag::err_invalid_reference_qualifier_application,
1100 "const");
1101 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1102 Diag(DS.getVolatileSpecLoc(),
1103 diag::err_invalid_reference_qualifier_application,
1104 "volatile");
1105 }
Bill Wendling3708c182007-05-27 10:15:43 +00001106
1107 // Recursively parse the declarator.
1108 ParseDeclaratorInternal(D);
1109
1110 // Remember that we parsed a reference type. It doesn't have type-quals.
Bill Wendling93efb222007-06-02 23:28:54 +00001111 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001112 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001113}
1114
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001115/// ParseDirectDeclarator
1116/// direct-declarator: [C99 6.7.5]
1117/// identifier
1118/// '(' declarator ')'
1119/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001120/// [C90] direct-declarator '[' constant-expression[opt] ']'
1121/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1122/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1123/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1124/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001125/// direct-declarator '(' parameter-type-list ')'
1126/// direct-declarator '(' identifier-list[opt] ')'
1127/// [GNU] direct-declarator '(' parameter-forward-declarations
1128/// parameter-type-list[opt] ')'
1129///
Chris Lattneracd58a32006-08-06 17:24:14 +00001130void Parser::ParseDirectDeclarator(Declarator &D) {
1131 // Parse the first direct-declarator seen.
Chris Lattner76c72282007-10-09 17:33:22 +00001132 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001133 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1134 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1135 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001136 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001137 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001138 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001139 // Example: 'char (*X)' or 'int (*XX)(void)'
1140 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001141 } else if (D.mayOmitIdentifier()) {
1142 // This could be something simple like "int" (in which case the declarator
1143 // portion is empty), if an abstract-declarator is allowed.
1144 D.SetIdentifier(0, Tok.getLocation());
1145 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001146 // Expected identifier or '('.
1147 Diag(Tok, diag::err_expected_ident_lparen);
1148 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001149 }
1150
1151 assert(D.isPastIdentifier() &&
1152 "Haven't past the location of the identifier yet?");
1153
1154 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001155 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001156 ParseParenDeclarator(D);
Chris Lattner76c72282007-10-09 17:33:22 +00001157 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001158 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001159 } else {
1160 break;
1161 }
1162 }
1163}
1164
1165/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1166/// either be before the identifier (in which case these are just grouping
1167/// parens for precedence) or it may be after the identifier, in which case
1168/// these are function arguments.
1169///
1170/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001171/// parameter-type-list: [C99 6.7.5]
1172/// parameter-list
1173/// parameter-list ',' '...'
1174///
1175/// parameter-list: [C99 6.7.5]
1176/// parameter-declaration
1177/// parameter-list ',' parameter-declaration
1178///
1179/// parameter-declaration: [C99 6.7.5]
1180/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +00001181/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001182/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001183/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001184///
1185/// identifier-list: [C99 6.7.5]
1186/// identifier
1187/// identifier-list ',' identifier
1188///
Chris Lattneracd58a32006-08-06 17:24:14 +00001189void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001190 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001191
Chris Lattneracd58a32006-08-06 17:24:14 +00001192 // If we haven't past the identifier yet (or where the identifier would be
1193 // stored, if this is an abstract declarator), then this is probably just
1194 // grouping parens.
1195 if (!D.isPastIdentifier()) {
1196 // Okay, this is probably a grouping paren. However, if this could be an
1197 // abstract-declarator, then this could also be the start of function
1198 // arguments (consider 'void()').
1199 bool isGrouping;
1200
1201 if (!D.mayOmitIdentifier()) {
1202 // If this can't be an abstract-declarator, this *must* be a grouping
1203 // paren, because we haven't seen the identifier yet.
1204 isGrouping = true;
Chris Lattner76c72282007-10-09 17:33:22 +00001205 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001206 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001207 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1208 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +00001209 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001210 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001211 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001212 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001213 }
Chris Lattneracd58a32006-08-06 17:24:14 +00001214
1215 // If this is a grouping paren, handle:
1216 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001217 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001218 if (isGrouping) {
Chris Lattner76c72282007-10-09 17:33:22 +00001219 if (Tok.is(tok::kw___attribute))
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001220 D.AddAttributes(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001221
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001222 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +00001223 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +00001224 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +00001225 return;
1226 }
1227
1228 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +00001229 // argument list. Recognize that this declarator will never have an
1230 // identifier (and remember where it would have been), then fall through to
1231 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +00001232 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001233 }
1234
Chris Lattneracd58a32006-08-06 17:24:14 +00001235 // Okay, this is the parameter list of a function definition, or it is an
1236 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001237 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +00001238 bool HasPrototype;
Chris Lattner14776b92006-08-06 22:27:40 +00001239 bool ErrorEmitted = false;
1240
Chris Lattneredc9e392006-12-02 06:21:46 +00001241 // Build up an array of information about the parsed arguments.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001242 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1243 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Chris Lattneredc9e392006-12-02 06:21:46 +00001244
Chris Lattner76c72282007-10-09 17:33:22 +00001245 if (Tok.is(tok::r_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001246 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001247 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001248 HasPrototype = false;
Chris Lattner76c72282007-10-09 17:33:22 +00001249 } else if (Tok.is(tok::identifier) &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001250 // K&R identifier lists can't have typedefs as identifiers, per
1251 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001252 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001253 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1254 // normal declarators, not for abstract-declarators.
1255 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1256
1257 // If there was no identifier specified, either we are in an
1258 // abstract-declarator, or we are in a parameter declarator which was found
1259 // to be abstract. In abstract-declarators, identifier lists are not valid,
1260 // diagnose this.
1261 if (!D.getIdentifier())
1262 Diag(Tok, diag::ext_ident_list_in_param);
Chris Lattneredc9e392006-12-02 06:21:46 +00001263
Chris Lattnercbc426d2006-12-02 06:43:02 +00001264 // Remember this identifier in ParamInfo.
1265 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1266 Tok.getLocation(), 0));
1267
Chris Lattneracd58a32006-08-06 17:24:14 +00001268 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001269 while (Tok.is(tok::comma)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001270 // Eat the comma.
1271 ConsumeToken();
1272
Chris Lattner76c72282007-10-09 17:33:22 +00001273 if (Tok.isNot(tok::identifier)) {
Chris Lattnercbc426d2006-12-02 06:43:02 +00001274 Diag(Tok, diag::err_expected_ident);
Chris Lattner14776b92006-08-06 22:27:40 +00001275 ErrorEmitted = true;
1276 break;
1277 }
Chris Lattnercbc426d2006-12-02 06:43:02 +00001278
Chris Lattner969ca152006-12-03 06:29:03 +00001279 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1280
1281 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001282 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001283 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1284 ParmII = 0;
1285 }
Chris Lattner969ca152006-12-03 06:29:03 +00001286
Chris Lattnercbc426d2006-12-02 06:43:02 +00001287 // Remember this identifier in ParamInfo.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001288 if (ParmII)
1289 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1290 Tok.getLocation(), 0));
Chris Lattnercbc426d2006-12-02 06:43:02 +00001291
1292 // Eat the identifier.
1293 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001294 }
1295
Chris Lattneracd58a32006-08-06 17:24:14 +00001296 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001297 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001298 HasPrototype = false;
1299 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +00001300 // Finally, a normal, non-empty parameter type list.
1301
Chris Lattnercbc426d2006-12-02 06:43:02 +00001302 // Enter function-declaration scope, limiting any declarators for struct
1303 // tags to the function prototype scope.
1304 // FIXME: is this needed?
Chris Lattner1a76a3c2007-08-26 06:24:45 +00001305 EnterScope(Scope::DeclScope);
Chris Lattner43e956c2006-11-28 04:05:37 +00001306
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001307 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001308 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001309 if (Tok.is(tok::ellipsis)) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001310 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001311
1312 // Check to see if this is "void(...)" which is not allowed.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001313 if (ParamInfo.empty()) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001314 // Otherwise, parse parameter type list. If it starts with an
1315 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001316 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001317 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001318 }
1319
1320 // Consume the ellipsis.
1321 ConsumeToken();
1322 break;
1323 }
1324
Chris Lattner7b8134f2008-02-10 23:08:00 +00001325 SourceLocation DSStart = Tok.getLocation();
1326
Chris Lattneracd58a32006-08-06 17:24:14 +00001327 // Parse the declaration-specifiers.
1328 DeclSpec DS;
1329 ParseDeclarationSpecifiers(DS);
1330
1331 // Parse the declarator. This is "PrototypeContext", because we must
1332 // accept either 'declarator' or 'abstract-declarator' here.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001333 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1334 ParseDeclarator(ParmDecl);
Chris Lattneracd58a32006-08-06 17:24:14 +00001335
Chris Lattnere37e2332006-08-15 04:50:22 +00001336 // Parse GNU attributes, if present.
Chris Lattner76c72282007-10-09 17:33:22 +00001337 if (Tok.is(tok::kw___attribute))
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001338 ParmDecl.AddAttributes(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001339
Chris Lattner43e956c2006-11-28 04:05:37 +00001340 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001341 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1342 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1343 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
Chris Lattner4d8f8732006-11-28 05:05:08 +00001344 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001345 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001346 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001347 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001348 if (DS.isThreadSpecified()) {
1349 Diag(DS.getThreadSpecLoc(),
1350 diag::err_invalid_storage_class_in_func_decl);
1351 DS.ClearStorageClassSpecs();
1352 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001353
1354 // Inform the actions module about the parameter declarator, so it gets
1355 // added to the current scope.
Chris Lattner216d8652006-12-02 06:47:41 +00001356 Action::TypeResult ParamTy =
Steve Naroff30d242c2007-09-15 18:49:24 +00001357 Actions.ActOnParamDeclaratorType(CurScope, ParmDecl);
Chris Lattnercbc426d2006-12-02 06:43:02 +00001358
1359 // Remember this parsed parameter in ParamInfo.
Chris Lattner969ca152006-12-03 06:29:03 +00001360 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1361
1362 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001363 if (ParmII && !ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001364 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1365 ParmII->getName());
1366 ParmII = 0;
Chris Lattner969ca152006-12-03 06:29:03 +00001367 }
Chris Lattner7f024fe2008-01-31 06:10:07 +00001368
1369 // If no parameter was specified, verify that *something* was specified,
1370 // otherwise we have a missing type and identifier.
Chris Lattner7b8134f2008-02-10 23:08:00 +00001371 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1372 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1373 Diag(DSStart, diag::err_missing_param);
1374 } else if (!DS.hasTypeSpecifier() &&
1375 (getLang().C99 || getLang().CPlusPlus)) {
1376 // Otherwise, if something was specified but a type specifier wasn't,
1377 // (e.g. "x" or "restrict x" or "restrict"), this is a use of implicit
1378 // int. This is valid in C90, but not in C99 or C++.
Chris Lattner7f024fe2008-01-31 06:10:07 +00001379 if (ParmII)
1380 Diag(ParmDecl.getIdentifierLoc(),
Chris Lattner7b8134f2008-02-10 23:08:00 +00001381 diag::ext_param_requires_type_specifier, ParmII->getName());
Chris Lattner7f024fe2008-01-31 06:10:07 +00001382 else
Chris Lattner7b8134f2008-02-10 23:08:00 +00001383 Diag(DSStart, diag::ext_anon_param_requires_type_specifier);
Chris Lattner7f024fe2008-01-31 06:10:07 +00001384 }
Chris Lattner969ca152006-12-03 06:29:03 +00001385
Steve Naroff7e6f7c22007-08-28 03:03:08 +00001386 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Nate Begemana0f78972007-11-13 22:14:47 +00001387 ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType(),
1388 ParmDecl.getDeclSpec().getAttributes()));
1389
1390 // Ownership of DeclSpec has been handed off to ParamInfo.
1391 DS.clearAttributes();
Chris Lattneracd58a32006-08-06 17:24:14 +00001392
1393 // If the next token is a comma, consume it and keep reading arguments.
Chris Lattner76c72282007-10-09 17:33:22 +00001394 if (Tok.isNot(tok::comma)) break;
Chris Lattneracd58a32006-08-06 17:24:14 +00001395
1396 // Consume the comma.
1397 ConsumeToken();
1398 }
1399
1400 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001401
1402 // Leave prototype scope.
1403 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001404 }
1405
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001406 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerd2e97c12006-12-03 02:03:33 +00001407 if (!ErrorEmitted)
1408 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1409 &ParamInfo[0], ParamInfo.size(),
1410 StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001411
Chris Lattner14776b92006-08-06 22:27:40 +00001412 // If we have the closing ')', eat it and we're done.
Chris Lattner76c72282007-10-09 17:33:22 +00001413 if (Tok.is(tok::r_paren)) {
Chris Lattner14776b92006-08-06 22:27:40 +00001414 ConsumeParen();
1415 } else {
1416 // If an error happened earlier parsing something else in the proto, don't
1417 // issue another error.
1418 if (!ErrorEmitted)
1419 Diag(Tok, diag::err_expected_rparen);
1420 SkipUntil(tok::r_paren);
1421 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001422}
Chris Lattneracd58a32006-08-06 17:24:14 +00001423
Chris Lattnere8074e62006-08-06 18:30:15 +00001424
1425/// [C90] direct-declarator '[' constant-expression[opt] ']'
1426/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1427/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1428/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1429/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1430void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001431 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001432
1433 // If valid, this location is the position where we read the 'static' keyword.
1434 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00001435 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001436 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001437
1438 // If there is a type-qualifier-list, read it now.
1439 DeclSpec DS;
1440 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001441
1442 // If we haven't already read 'static', check to see if there is one after the
1443 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001444 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001445 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001446
1447 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001448 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001449 ExprResult NumElements(false);
Chris Lattner76c72282007-10-09 17:33:22 +00001450 if (Tok.is(tok::star)) {
Chris Lattner1906f802006-08-06 19:14:46 +00001451 // Remember the '*' token, in case we have to un-get it.
Chris Lattner146762e2007-07-20 16:59:19 +00001452 Token StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001453 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001454
1455 // Check that the ']' token is present to avoid incorrectly parsing
1456 // expressions starting with '*' as [*].
Chris Lattner76c72282007-10-09 17:33:22 +00001457 if (Tok.is(tok::r_square)) {
Chris Lattner1906f802006-08-06 19:14:46 +00001458 if (StaticLoc.isValid())
1459 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1460 StaticLoc = SourceLocation(); // Drop the static.
1461 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001462 } else {
1463 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001464 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001465 // need to reparse it. This handles cases like 'X[*p + 4]'
1466 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001467 }
Chris Lattner76c72282007-10-09 17:33:22 +00001468 } else if (Tok.isNot(tok::r_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001469 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001470 NumElements = ParseAssignmentExpression();
1471 }
1472
1473 // If there was an error parsing the assignment-expression, recover.
1474 if (NumElements.isInvalid) {
1475 // If the expression was invalid, skip it.
1476 SkipUntil(tok::r_square);
1477 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001478 }
1479
Chris Lattner04f80192006-08-15 04:55:54 +00001480 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001481
Chris Lattnere8074e62006-08-06 18:30:15 +00001482 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1483 // it was not a constant expression.
1484 if (!getLang().C99) {
1485 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001486 if (isStar || StaticLoc.isValid() ||
1487 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001488 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001489 }
Bill Wendling93efb222007-06-02 23:28:54 +00001490
Chris Lattner6c7416c2006-08-07 00:19:33 +00001491 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001492 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1493 StaticLoc.isValid(), isStar,
1494 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001495}
1496
Steve Naroffad373bd2007-07-31 12:34:36 +00001497/// [GNU] typeof-specifier:
1498/// typeof ( expressions )
1499/// typeof ( type-name )
1500///
1501void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00001502 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00001503 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00001504 SourceLocation StartLoc = ConsumeToken();
1505
Chris Lattner76c72282007-10-09 17:33:22 +00001506 if (Tok.isNot(tok::l_paren)) {
Steve Naroff4bd2f712007-08-02 02:53:48 +00001507 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1508 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001509 }
1510 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1511
1512 if (isTypeSpecifierQualifier()) {
1513 TypeTy *Ty = ParseTypeName();
1514
Steve Naroff872da802007-07-31 23:56:32 +00001515 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1516
Chris Lattner76c72282007-10-09 17:33:22 +00001517 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001518 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001519 return;
1520 }
1521 RParenLoc = ConsumeParen();
1522 const char *PrevSpec = 0;
1523 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1524 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1525 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001526 } else { // we have an expression.
1527 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001528
Chris Lattner76c72282007-10-09 17:33:22 +00001529 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001530 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001531 return;
1532 }
1533 RParenLoc = ConsumeParen();
1534 const char *PrevSpec = 0;
1535 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1536 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1537 Result.Val))
1538 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001539 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001540}
1541