blob: f20c72e61ce8c78e816569a2bfe1b4ff7f9b924b [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 Lattner2e232092008-03-13 06:29:04 +0000391 DS.SetRangeStart(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.
Chris Lattner2e232092008-03-13 06:29:04 +0000412 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000413 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__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000450 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
451 PrevSpec);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000452 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000453 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000454 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000455 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000456 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000457 break;
458 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000459 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000460 break;
461 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000462 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000463 break;
464 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000465 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000466 break;
467
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000468 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000469 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000470 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000471 break;
472 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000473 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000474 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000475 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000476 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000477 break;
478 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000479 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000480 break;
481 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000482 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000483 break;
484 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000485 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000486 break;
487 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000488 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000489 break;
490 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000491 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000492 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000493 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000495 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000496 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000497 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000498 break;
499 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000500 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000501 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000502 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000503 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000504 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000505 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000506 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000508 break;
509 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000510 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000511 break;
512 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000514 break;
515 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000517 break;
Chris Lattner861a2262008-04-13 18:59:07 +0000518
519 case tok::kw_class:
Chris Lattner1890ac82006-08-13 01:16:23 +0000520 case tok::kw_struct:
521 case tok::kw_union:
Douglas Gregor556877c2008-04-13 21:30:24 +0000522 ParseClassSpecifier(DS);
Chris Lattner1890ac82006-08-13 01:16:23 +0000523 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000524 case tok::kw_enum:
525 ParseEnumSpecifier(DS);
526 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000527
Steve Naroffad373bd2007-07-31 12:34:36 +0000528 // GNU typeof support.
529 case tok::kw_typeof:
530 ParseTypeofSpecifier(DS);
531 continue;
532
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000533 // type-qualifier
534 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000535 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
536 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000537 break;
538 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000539 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
540 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000541 break;
542 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000543 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
544 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000545 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000546
547 // function-specifier
548 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000549 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000550 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000551 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000552 // If the specifier combination wasn't legal, issue a diagnostic.
553 if (isInvalid) {
554 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000555 if (isInvalid == 1) // Error.
556 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
557 else // extwarn.
558 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000559 }
Chris Lattner2e232092008-03-13 06:29:04 +0000560 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000561 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000562 }
563}
564
Chris Lattnerffbc2712007-01-25 06:05:38 +0000565/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
566/// the first token has already been read and has been turned into an instance
567/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
568/// otherwise it returns false and fills in Decl.
569bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
Steve Naroffb8371e12007-06-09 03:39:29 +0000570 AttributeList *Attr = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000571 // If attributes exist after tag, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000572 if (Tok.is(tok::kw___attribute))
Steve Naroffb8371e12007-06-09 03:39:29 +0000573 Attr = ParseAttributes();
Chris Lattnerffbc2712007-01-25 06:05:38 +0000574
Chris Lattner1890ac82006-08-13 01:16:23 +0000575 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner76c72282007-10-09 17:33:22 +0000576 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000577 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattner02c04392007-07-25 00:24:17 +0000578
579 // Skip the rest of this declarator, up until the comma or semicolon.
580 SkipUntil(tok::comma, true);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000581 return true;
Chris Lattner1890ac82006-08-13 01:16:23 +0000582 }
583
Chris Lattner8c6519a2007-01-22 07:41:36 +0000584 // If an identifier is present, consume and remember it.
585 IdentifierInfo *Name = 0;
586 SourceLocation NameLoc;
Chris Lattner76c72282007-10-09 17:33:22 +0000587 if (Tok.is(tok::identifier)) {
Chris Lattner8c6519a2007-01-22 07:41:36 +0000588 Name = Tok.getIdentifierInfo();
589 NameLoc = ConsumeToken();
590 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000591
Chris Lattner8c6519a2007-01-22 07:41:36 +0000592 // There are three options here. If we have 'struct foo;', then this is a
593 // forward declaration. If we have 'struct foo {...' then this is a
Chris Lattner7b9ace62007-01-23 20:11:08 +0000594 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Chris Lattner8799cf22007-01-23 01:57:16 +0000595 //
596 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
597 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
598 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
599 //
Chris Lattner7b9ace62007-01-23 20:11:08 +0000600 Action::TagKind TK;
Chris Lattner76c72282007-10-09 17:33:22 +0000601 if (Tok.is(tok::l_brace))
Chris Lattner7b9ace62007-01-23 20:11:08 +0000602 TK = Action::TK_Definition;
Chris Lattner76c72282007-10-09 17:33:22 +0000603 else if (Tok.is(tok::semi))
Chris Lattner7b9ace62007-01-23 20:11:08 +0000604 TK = Action::TK_Declaration;
605 else
606 TK = Action::TK_Reference;
Steve Naroff30d242c2007-09-15 18:49:24 +0000607 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000608 return false;
609}
610
Chris Lattner70ae4912007-10-29 04:42:53 +0000611/// ParseStructDeclaration - Parse a struct declaration without the terminating
612/// semicolon.
613///
Chris Lattner90a26b02007-01-23 04:38:16 +0000614/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +0000615/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +0000616/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +0000617/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +0000618/// struct-declarator-list:
619/// struct-declarator
620/// struct-declarator-list ',' struct-declarator
621/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
622/// struct-declarator:
623/// declarator
624/// [GNU] declarator attributes[opt]
625/// declarator[opt] ':' constant-expression
626/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
627///
Chris Lattnera12405b2008-04-10 06:46:29 +0000628void Parser::
629ParseStructDeclaration(DeclSpec &DS,
630 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroff97170802007-08-20 22:28:22 +0000631 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattnera12405b2008-04-10 06:46:29 +0000632 while (Tok.is(tok::kw___extension__))
Steve Naroff97170802007-08-20 22:28:22 +0000633 ConsumeToken();
634
635 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +0000636 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +0000637 ParseSpecifierQualifierList(DS);
638 // TODO: Does specifier-qualifier list correctly check that *something* is
639 // specified?
640
641 // If there are no declarators, issue a warning.
Chris Lattner76c72282007-10-09 17:33:22 +0000642 if (Tok.is(tok::semi)) {
Chris Lattner32295d32008-04-10 06:15:14 +0000643 Diag(DSStart, diag::w_no_declarators);
Steve Naroff97170802007-08-20 22:28:22 +0000644 return;
645 }
646
647 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000648 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000649 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +0000650 FieldDeclarator &DeclaratorInfo = Fields.back();
651
Steve Naroff97170802007-08-20 22:28:22 +0000652 /// struct-declarator: declarator
653 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +0000654 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +0000655 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff97170802007-08-20 22:28:22 +0000656
Chris Lattner76c72282007-10-09 17:33:22 +0000657 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +0000658 ConsumeToken();
659 ExprResult Res = ParseConstantExpression();
Chris Lattner32295d32008-04-10 06:15:14 +0000660 if (Res.isInvalid)
Steve Naroff97170802007-08-20 22:28:22 +0000661 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +0000662 else
Chris Lattnera12405b2008-04-10 06:46:29 +0000663 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff97170802007-08-20 22:28:22 +0000664 }
665
666 // If attributes exist after the declarator, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000667 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000668 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000669
670 // If we don't have a comma, it is either the end of the list (a ';')
671 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000672 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +0000673 return;
Steve Naroff97170802007-08-20 22:28:22 +0000674
675 // Consume the comma.
676 ConsumeToken();
677
678 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000679 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000680
681 // Attributes are only allowed on the second declarator.
Chris Lattner76c72282007-10-09 17:33:22 +0000682 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000683 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000684 }
Steve Naroff97170802007-08-20 22:28:22 +0000685}
686
687/// ParseStructUnionBody
688/// struct-contents:
689/// struct-declaration-list
690/// [EXT] empty
691/// [GNU] "struct-declaration-list" without terminatoring ';'
692/// struct-declaration-list:
693/// struct-declaration
694/// struct-declaration-list struct-declaration
695/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
696///
Chris Lattner1300fb92007-01-23 23:42:53 +0000697void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
698 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000699 SourceLocation LBraceLoc = ConsumeBrace();
700
Chris Lattner7b9ace62007-01-23 20:11:08 +0000701 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
702 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +0000703 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner90a26b02007-01-23 04:38:16 +0000704 Diag(Tok, diag::ext_empty_struct_union_enum,
705 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000706
Chris Lattner23b7eb62007-06-15 23:05:46 +0000707 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000708 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
709
Chris Lattner7b9ace62007-01-23 20:11:08 +0000710 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +0000711 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000712 // Each iteration of this loop reads one struct-declaration.
713
Chris Lattner736ed5d2007-06-09 05:59:07 +0000714 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +0000715 if (Tok.is(tok::semi)) {
Chris Lattner36e46a22007-06-09 05:49:55 +0000716 Diag(Tok, diag::ext_extra_struct_semi);
717 ConsumeToken();
718 continue;
719 }
Chris Lattnera12405b2008-04-10 06:46:29 +0000720
721 // Parse all the comma separated declarators.
722 DeclSpec DS;
723 FieldDeclarators.clear();
724 ParseStructDeclaration(DS, FieldDeclarators);
725
726 // Convert them all to fields.
727 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
728 FieldDeclarator &FD = FieldDeclarators[i];
729 // Install the declarator into the current TagDecl.
Fariborz Jahaniande615832008-04-10 23:32:45 +0000730 DeclTy *Field = Actions.ActOnField(CurScope,
Chris Lattnera12405b2008-04-10 06:46:29 +0000731 DS.getSourceRange().getBegin(),
732 FD.D, FD.BitfieldSize);
733 FieldDecls.push_back(Field);
734 }
735
Chris Lattner736ed5d2007-06-09 05:59:07 +0000736
Chris Lattner76c72282007-10-09 17:33:22 +0000737 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000738 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +0000739 } else if (Tok.is(tok::r_brace)) {
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000740 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
741 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000742 } else {
743 Diag(Tok, diag::err_expected_semi_decl_list);
744 // Skip to end of block or statement
745 SkipUntil(tok::r_brace, true, true);
746 }
747 }
748
Steve Naroff33a1e802007-10-29 21:38:07 +0000749 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +0000750
Fariborz Jahanian343f7092007-09-29 00:54:24 +0000751 Actions.ActOnFields(CurScope,
Chris Lattnereb85ab42008-02-25 21:04:36 +0000752 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff33a1e802007-10-29 21:38:07 +0000753 LBraceLoc, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000754
Steve Naroffb8371e12007-06-09 03:39:29 +0000755 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000756 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000757 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000758 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000759}
760
761
Chris Lattner3b561a32006-08-13 00:12:11 +0000762/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000763/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000764/// 'enum' identifier[opt] '{' enumerator-list '}'
765/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000766/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
767/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000768/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000769/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000770void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000771 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000772 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000773
Chris Lattnerffbc2712007-01-25 06:05:38 +0000774 // Parse the tag portion of this.
775 DeclTy *TagDecl;
776 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000777 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000778
Chris Lattner76c72282007-10-09 17:33:22 +0000779 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000780 ParseEnumBody(StartLoc, TagDecl);
781
Chris Lattner3b561a32006-08-13 00:12:11 +0000782 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000783 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000784 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000785 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000786}
787
Chris Lattnerc1915e22007-01-25 07:29:02 +0000788/// ParseEnumBody - Parse a {} enclosed enumerator-list.
789/// enumerator-list:
790/// enumerator
791/// enumerator-list ',' enumerator
792/// enumerator:
793/// enumeration-constant
794/// enumeration-constant '=' constant-expression
795/// enumeration-constant:
796/// identifier
797///
798void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
799 SourceLocation LBraceLoc = ConsumeBrace();
800
Chris Lattner37256fb2007-08-27 17:24:30 +0000801 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +0000802 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000803 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
804
Chris Lattner23b7eb62007-06-15 23:05:46 +0000805 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000806
Chris Lattner4ef40012007-06-11 01:28:17 +0000807 DeclTy *LastEnumConstDecl = 0;
808
Chris Lattnerc1915e22007-01-25 07:29:02 +0000809 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +0000810 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000811 IdentifierInfo *Ident = Tok.getIdentifierInfo();
812 SourceLocation IdentLoc = ConsumeToken();
813
814 SourceLocation EqualLoc;
815 ExprTy *AssignedVal = 0;
Chris Lattner76c72282007-10-09 17:33:22 +0000816 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000817 EqualLoc = ConsumeToken();
818 ExprResult Res = ParseConstantExpression();
819 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000820 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000821 else
822 AssignedVal = Res.Val;
823 }
824
825 // Install the enumerator constant into EnumDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +0000826 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4ef40012007-06-11 01:28:17 +0000827 LastEnumConstDecl,
828 IdentLoc, Ident,
829 EqualLoc, AssignedVal);
830 EnumConstantDecls.push_back(EnumConstDecl);
831 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000832
Chris Lattner76c72282007-10-09 17:33:22 +0000833 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000834 break;
835 SourceLocation CommaLoc = ConsumeToken();
836
Chris Lattner76c72282007-10-09 17:33:22 +0000837 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000838 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
839 }
840
841 // Eat the }.
842 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
843
Steve Naroff30d242c2007-09-15 18:49:24 +0000844 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +0000845 EnumConstantDecls.size());
846
Steve Naroff0f2fe172007-06-01 17:11:19 +0000847 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000848 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000849 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000850 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000851}
Chris Lattner3b561a32006-08-13 00:12:11 +0000852
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000853/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000854/// start of a type-qualifier-list.
855bool Parser::isTypeQualifier() const {
856 switch (Tok.getKind()) {
857 default: return false;
858 // type-qualifier
859 case tok::kw_const:
860 case tok::kw_volatile:
861 case tok::kw_restrict:
862 return true;
863 }
864}
865
866/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000867/// start of a specifier-qualifier-list.
868bool Parser::isTypeSpecifierQualifier() const {
869 switch (Tok.getKind()) {
870 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000871 // GNU attributes support.
872 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +0000873 // GNU typeof support.
874 case tok::kw_typeof:
875
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000876 // type-specifiers
877 case tok::kw_short:
878 case tok::kw_long:
879 case tok::kw_signed:
880 case tok::kw_unsigned:
881 case tok::kw__Complex:
882 case tok::kw__Imaginary:
883 case tok::kw_void:
884 case tok::kw_char:
885 case tok::kw_int:
886 case tok::kw_float:
887 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000888 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000889 case tok::kw__Bool:
890 case tok::kw__Decimal32:
891 case tok::kw__Decimal64:
892 case tok::kw__Decimal128:
893
Chris Lattner861a2262008-04-13 18:59:07 +0000894 // struct-or-union-specifier (C99) or class-specifier (C++)
895 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000896 case tok::kw_struct:
897 case tok::kw_union:
898 // enum-specifier
899 case tok::kw_enum:
900
901 // type-qualifier
902 case tok::kw_const:
903 case tok::kw_volatile:
904 case tok::kw_restrict:
905 return true;
906
907 // typedef-name
908 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000909 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000910 }
911}
912
Chris Lattneracd58a32006-08-06 17:24:14 +0000913/// isDeclarationSpecifier() - Return true if the current token is part of a
914/// declaration specifier.
915bool Parser::isDeclarationSpecifier() const {
916 switch (Tok.getKind()) {
917 default: return false;
918 // storage-class-specifier
919 case tok::kw_typedef:
920 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +0000921 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +0000922 case tok::kw_static:
923 case tok::kw_auto:
924 case tok::kw_register:
925 case tok::kw___thread:
926
927 // type-specifiers
928 case tok::kw_short:
929 case tok::kw_long:
930 case tok::kw_signed:
931 case tok::kw_unsigned:
932 case tok::kw__Complex:
933 case tok::kw__Imaginary:
934 case tok::kw_void:
935 case tok::kw_char:
936 case tok::kw_int:
937 case tok::kw_float:
938 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000939 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +0000940 case tok::kw__Bool:
941 case tok::kw__Decimal32:
942 case tok::kw__Decimal64:
943 case tok::kw__Decimal128:
944
Chris Lattner861a2262008-04-13 18:59:07 +0000945 // struct-or-union-specifier (C99) or class-specifier (C++)
946 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +0000947 case tok::kw_struct:
948 case tok::kw_union:
949 // enum-specifier
950 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000951
Chris Lattneracd58a32006-08-06 17:24:14 +0000952 // type-qualifier
953 case tok::kw_const:
954 case tok::kw_volatile:
955 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +0000956
Chris Lattneracd58a32006-08-06 17:24:14 +0000957 // function-specifier
958 case tok::kw_inline:
Chris Lattner7b20dc72007-08-09 16:40:21 +0000959
Chris Lattner599e47e2007-08-09 17:01:07 +0000960 // GNU typeof support.
961 case tok::kw_typeof:
962
963 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +0000964 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +0000965 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000966
Chris Lattneracd58a32006-08-06 17:24:14 +0000967 // typedef-name
968 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000969 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000970 }
971}
972
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000973
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000974/// ParseTypeQualifierListOpt
975/// type-qualifier-list: [C99 6.7.5]
976/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000977/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000978/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000979/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000980///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000981void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000982 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000983 int isInvalid = false;
984 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +0000985 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000986
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000987 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000988 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000989 // If this is not a type-qualifier token, we're done reading type
990 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000991 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000992 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000993 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000994 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
995 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000996 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000997 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000998 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
999 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001000 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001001 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001002 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1003 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001004 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001005 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001006 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +00001007 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001008 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001009
1010 // If the specifier combination wasn't legal, issue a diagnostic.
1011 if (isInvalid) {
1012 assert(PrevSpec && "Method did not return previous specifier!");
1013 if (isInvalid == 1) // Error.
1014 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1015 else // extwarn.
1016 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1017 }
1018 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001019 }
1020}
1021
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001022
1023/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1024///
1025void Parser::ParseDeclarator(Declarator &D) {
1026 /// This implements the 'declarator' production in the C grammar, then checks
1027 /// for well-formedness and issues diagnostics.
1028 ParseDeclaratorInternal(D);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001029}
1030
1031/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001032/// declarator: [C99 6.7.5]
1033/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001034/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1035/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001036///
1037/// pointer: [C99 6.7.5]
1038/// '*' type-qualifier-list[opt]
1039/// '*' type-qualifier-list[opt] pointer
1040///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001041void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001042 tok::TokenKind Kind = Tok.getKind();
1043
1044 // Not a pointer or C++ reference.
Chris Lattner788404f2008-02-21 01:32:26 +00001045 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001046 return ParseDirectDeclarator(D);
1047
Bill Wendling3708c182007-05-27 10:15:43 +00001048 // Otherwise, '*' -> pointer or '&' -> reference.
1049 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1050
1051 if (Kind == tok::star) {
Chris Lattner788404f2008-02-21 01:32:26 +00001052 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00001053 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001054
Bill Wendling3708c182007-05-27 10:15:43 +00001055 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001056
Bill Wendling3708c182007-05-27 10:15:43 +00001057 // Recursively parse the declarator.
1058 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +00001059
Bill Wendling3708c182007-05-27 10:15:43 +00001060 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001061 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1062 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001063 } else {
1064 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001065 DeclSpec DS;
1066
1067 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1068 // cv-qualifiers are introduced through the use of a typedef or of a
1069 // template type argument, in which case the cv-qualifiers are ignored.
1070 //
1071 // [GNU] Retricted references are allowed.
1072 // [GNU] Attributes on references are allowed.
1073 ParseTypeQualifierListOpt(DS);
1074
1075 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1076 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1077 Diag(DS.getConstSpecLoc(),
1078 diag::err_invalid_reference_qualifier_application,
1079 "const");
1080 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1081 Diag(DS.getVolatileSpecLoc(),
1082 diag::err_invalid_reference_qualifier_application,
1083 "volatile");
1084 }
Bill Wendling3708c182007-05-27 10:15:43 +00001085
1086 // Recursively parse the declarator.
1087 ParseDeclaratorInternal(D);
1088
1089 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001090 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1091 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001092 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001093}
1094
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001095/// ParseDirectDeclarator
1096/// direct-declarator: [C99 6.7.5]
1097/// identifier
1098/// '(' declarator ')'
1099/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001100/// [C90] direct-declarator '[' constant-expression[opt] ']'
1101/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1102/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1103/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1104/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001105/// direct-declarator '(' parameter-type-list ')'
1106/// direct-declarator '(' identifier-list[opt] ')'
1107/// [GNU] direct-declarator '(' parameter-forward-declarations
1108/// parameter-type-list[opt] ')'
1109///
Chris Lattneracd58a32006-08-06 17:24:14 +00001110void Parser::ParseDirectDeclarator(Declarator &D) {
1111 // Parse the first direct-declarator seen.
Chris Lattner76c72282007-10-09 17:33:22 +00001112 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001113 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1114 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1115 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001116 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001117 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001118 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001119 // Example: 'char (*X)' or 'int (*XX)(void)'
1120 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001121 } else if (D.mayOmitIdentifier()) {
1122 // This could be something simple like "int" (in which case the declarator
1123 // portion is empty), if an abstract-declarator is allowed.
1124 D.SetIdentifier(0, Tok.getLocation());
1125 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001126 // Expected identifier or '('.
1127 Diag(Tok, diag::err_expected_ident_lparen);
1128 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001129 }
1130
1131 assert(D.isPastIdentifier() &&
1132 "Haven't past the location of the identifier yet?");
1133
1134 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001135 if (Tok.is(tok::l_paren)) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001136 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00001137 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001138 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001139 } else {
1140 break;
1141 }
1142 }
1143}
1144
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001145/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1146/// only called before the identifier, so these are most likely just grouping
1147/// parens for precedence. If we find that these are actually function
1148/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1149///
1150/// direct-declarator:
1151/// '(' declarator ')'
1152/// [GNU] '(' attributes declarator ')'
1153///
1154void Parser::ParseParenDeclarator(Declarator &D) {
1155 SourceLocation StartLoc = ConsumeParen();
1156 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1157
1158 // If we haven't past the identifier yet (or where the identifier would be
1159 // stored, if this is an abstract declarator), then this is probably just
1160 // grouping parens. However, if this could be an abstract-declarator, then
1161 // this could also be the start of function arguments (consider 'void()').
1162 bool isGrouping;
1163
1164 if (!D.mayOmitIdentifier()) {
1165 // If this can't be an abstract-declarator, this *must* be a grouping
1166 // paren, because we haven't seen the identifier yet.
1167 isGrouping = true;
1168 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1169 isDeclarationSpecifier()) { // 'int(int)' is a function.
1170 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1171 // considered to be a type, not a K&R identifier-list.
1172 isGrouping = false;
1173 } else {
1174 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1175 isGrouping = true;
1176 }
1177
1178 // If this is a grouping paren, handle:
1179 // direct-declarator: '(' declarator ')'
1180 // direct-declarator: '(' attributes declarator ')'
1181 if (isGrouping) {
1182 if (Tok.is(tok::kw___attribute))
1183 D.AddAttributes(ParseAttributes());
1184
1185 ParseDeclaratorInternal(D);
1186 // Match the ')'.
1187 MatchRHSPunctuation(tok::r_paren, StartLoc);
1188 return;
1189 }
1190
1191 // Okay, if this wasn't a grouping paren, it must be the start of a function
1192 // argument list. Recognize that this declarator will never have an
1193 // identifier (and remember where it would have been), then fall through to
1194 // the handling of argument lists.
1195 D.SetIdentifier(0, Tok.getLocation());
1196
1197 ParseFunctionDeclarator(StartLoc, D);
1198}
1199
1200/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1201/// declarator D up to a paren, which indicates that we are parsing function
1202/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00001203///
1204/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001205/// parameter-type-list: [C99 6.7.5]
1206/// parameter-list
1207/// parameter-list ',' '...'
1208///
1209/// parameter-list: [C99 6.7.5]
1210/// parameter-declaration
1211/// parameter-list ',' parameter-declaration
1212///
1213/// parameter-declaration: [C99 6.7.5]
1214/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001215/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001216/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001217/// declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00001218/// [C++] declaration-specifiers abstract-declarator[opt]
1219/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001220/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001221///
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001222void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1223 // lparen is already consumed!
1224 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001225
Chris Lattneracd58a32006-08-06 17:24:14 +00001226 // Okay, this is the parameter list of a function definition, or it is an
1227 // identifier list of a K&R-style function.
Chris Lattneredc9e392006-12-02 06:21:46 +00001228
Chris Lattner76c72282007-10-09 17:33:22 +00001229 if (Tok.is(tok::r_paren)) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00001230 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00001231 // int() -> no prototype, no '...'.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001232 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1233 /*variadic*/ false,
1234 /*arglist*/ 0, 0, LParenLoc));
1235
1236 ConsumeParen(); // Eat the closing ')'.
1237 return;
Chris Lattner76c72282007-10-09 17:33:22 +00001238 } else if (Tok.is(tok::identifier) &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001239 // K&R identifier lists can't have typedefs as identifiers, per
1240 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001241 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001242 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1243 // normal declarators, not for abstract-declarators.
Chris Lattner6c940e62008-04-06 06:34:08 +00001244 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001245 }
1246
1247 // Finally, a normal, non-empty parameter type list.
1248
1249 // Build up an array of information about the parsed arguments.
1250 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001251
1252 // Enter function-declaration scope, limiting any declarators to the
1253 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001254 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001255
1256 bool IsVariadic = false;
1257 while (1) {
1258 if (Tok.is(tok::ellipsis)) {
1259 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001260
Chris Lattner371ed4e2008-04-06 06:57:35 +00001261 // Check to see if this is "void(...)" which is not allowed.
1262 if (ParamInfo.empty()) {
1263 // Otherwise, parse parameter type list. If it starts with an
1264 // ellipsis, diagnose the malformed function.
1265 Diag(Tok, diag::err_ellipsis_first_arg);
1266 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner969ca152006-12-03 06:29:03 +00001267 }
Chris Lattner7f024fe2008-01-31 06:10:07 +00001268
Chris Lattner371ed4e2008-04-06 06:57:35 +00001269 ConsumeToken(); // Consume the ellipsis.
1270 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00001271 }
1272
Chris Lattner371ed4e2008-04-06 06:57:35 +00001273 SourceLocation DSStart = Tok.getLocation();
Chris Lattner43e956c2006-11-28 04:05:37 +00001274
Chris Lattner371ed4e2008-04-06 06:57:35 +00001275 // Parse the declaration-specifiers.
1276 DeclSpec DS;
1277 ParseDeclarationSpecifiers(DS);
1278
1279 // Parse the declarator. This is "PrototypeContext", because we must
1280 // accept either 'declarator' or 'abstract-declarator' here.
1281 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1282 ParseDeclarator(ParmDecl);
1283
1284 // Parse GNU attributes, if present.
1285 if (Tok.is(tok::kw___attribute))
1286 ParmDecl.AddAttributes(ParseAttributes());
1287
Chris Lattner371ed4e2008-04-06 06:57:35 +00001288 // Remember this parsed parameter in ParamInfo.
1289 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1290
Chris Lattner371ed4e2008-04-06 06:57:35 +00001291 // If no parameter was specified, verify that *something* was specified,
1292 // otherwise we have a missing type and identifier.
1293 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1294 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1295 // Completely missing, emit error.
1296 Diag(DSStart, diag::err_missing_param);
1297 } else {
1298 // Otherwise, we have something. Add it and let semantic analysis try
1299 // to grok it and add the result to the ParamInfo we are building.
1300
1301 // Inform the actions module about the parameter declarator, so it gets
1302 // added to the current scope.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001303 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1304
1305 // Parse the default argument, if any. We parse the default
1306 // arguments in all dialects; the semantic analysis in
1307 // ActOnParamDefaultArgument will reject the default argument in
1308 // C.
1309 if (Tok.is(tok::equal)) {
1310 SourceLocation EqualLoc = Tok.getLocation();
1311
1312 // Consume the '='.
1313 ConsumeToken();
1314
1315 // Parse the default argument
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001316 ExprResult DefArgResult = ParseAssignmentExpression();
1317 if (DefArgResult.isInvalid) {
1318 SkipUntil(tok::comma, tok::r_paren, true, true);
1319 } else {
1320 // Inform the actions module about the default argument
1321 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1322 }
1323 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001324
1325 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001326 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner371ed4e2008-04-06 06:57:35 +00001327 }
1328
1329 // If the next token is a comma, consume it and keep reading arguments.
1330 if (Tok.isNot(tok::comma)) break;
1331
1332 // Consume the comma.
1333 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001334 }
1335
Chris Lattner371ed4e2008-04-06 06:57:35 +00001336 // Leave prototype scope.
1337 ExitScope();
1338
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001339 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001340 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1341 &ParamInfo[0], ParamInfo.size(),
1342 LParenLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001343
Chris Lattner14776b92006-08-06 22:27:40 +00001344 // If we have the closing ')', eat it and we're done.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001345 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001346}
Chris Lattneracd58a32006-08-06 17:24:14 +00001347
Chris Lattner6c940e62008-04-06 06:34:08 +00001348/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1349/// we found a K&R-style identifier list instead of a type argument list. The
1350/// current token is known to be the first identifier in the list.
1351///
1352/// identifier-list: [C99 6.7.5]
1353/// identifier
1354/// identifier-list ',' identifier
1355///
1356void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1357 Declarator &D) {
1358 // Build up an array of information about the parsed arguments.
1359 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1360 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1361
1362 // If there was no identifier specified for the declarator, either we are in
1363 // an abstract-declarator, or we are in a parameter declarator which was found
1364 // to be abstract. In abstract-declarators, identifier lists are not valid:
1365 // diagnose this.
1366 if (!D.getIdentifier())
1367 Diag(Tok, diag::ext_ident_list_in_param);
1368
1369 // Tok is known to be the first identifier in the list. Remember this
1370 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00001371 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00001372 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1373 Tok.getLocation(), 0));
1374
Chris Lattner9186f552008-04-06 06:39:19 +00001375 ConsumeToken(); // eat the first identifier.
Chris Lattner6c940e62008-04-06 06:34:08 +00001376
1377 while (Tok.is(tok::comma)) {
1378 // Eat the comma.
1379 ConsumeToken();
1380
Chris Lattner9186f552008-04-06 06:39:19 +00001381 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00001382 if (Tok.isNot(tok::identifier)) {
1383 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00001384 SkipUntil(tok::r_paren);
1385 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00001386 }
Chris Lattner67b450c2008-04-06 06:47:48 +00001387
Chris Lattner6c940e62008-04-06 06:34:08 +00001388 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00001389
1390 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1391 if (Actions.isTypeName(*ParmII, CurScope))
1392 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner6c940e62008-04-06 06:34:08 +00001393
1394 // Verify that the argument identifier has not already been mentioned.
1395 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner9186f552008-04-06 06:39:19 +00001396 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1397 } else {
1398 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00001399 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1400 Tok.getLocation(), 0));
Chris Lattner9186f552008-04-06 06:39:19 +00001401 }
Chris Lattner6c940e62008-04-06 06:34:08 +00001402
1403 // Eat the identifier.
1404 ConsumeToken();
1405 }
1406
Chris Lattner9186f552008-04-06 06:39:19 +00001407 // Remember that we parsed a function type, and remember the attributes. This
1408 // function type is always a K&R style function type, which is not varargs and
1409 // has no prototype.
1410 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1411 &ParamInfo[0], ParamInfo.size(),
1412 LParenLoc));
Chris Lattner6c940e62008-04-06 06:34:08 +00001413
1414 // If we have the closing ')', eat it and we're done.
Chris Lattner9186f552008-04-06 06:39:19 +00001415 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00001416}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001417
Chris Lattnere8074e62006-08-06 18:30:15 +00001418/// [C90] direct-declarator '[' constant-expression[opt] ']'
1419/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1420/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1421/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1422/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1423void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001424 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001425
1426 // If valid, this location is the position where we read the 'static' keyword.
1427 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00001428 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001429 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001430
1431 // If there is a type-qualifier-list, read it now.
1432 DeclSpec DS;
1433 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001434
1435 // If we haven't already read 'static', check to see if there is one after the
1436 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001437 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001438 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001439
1440 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001441 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001442 ExprResult NumElements(false);
Chris Lattner521ff2b2008-04-06 05:26:30 +00001443
1444 // Handle the case where we have '[*]' as the array size. However, a leading
1445 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1446 // the the token after the star is a ']'. Since stars in arrays are
1447 // infrequent, use of lookahead is not costly here.
1448 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00001449 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00001450
Chris Lattner521ff2b2008-04-06 05:26:30 +00001451 if (StaticLoc.isValid())
1452 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1453 StaticLoc = SourceLocation(); // Drop the static.
1454 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00001455 } else if (Tok.isNot(tok::r_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001456 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001457 NumElements = ParseAssignmentExpression();
1458 }
1459
1460 // If there was an error parsing the assignment-expression, recover.
1461 if (NumElements.isInvalid) {
1462 // If the expression was invalid, skip it.
1463 SkipUntil(tok::r_square);
1464 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001465 }
1466
Chris Lattner04f80192006-08-15 04:55:54 +00001467 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001468
Chris Lattnere8074e62006-08-06 18:30:15 +00001469 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1470 // it was not a constant expression.
1471 if (!getLang().C99) {
1472 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001473 if (isStar || StaticLoc.isValid() ||
1474 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001475 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001476 }
Bill Wendling93efb222007-06-02 23:28:54 +00001477
Chris Lattner6c7416c2006-08-07 00:19:33 +00001478 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001479 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1480 StaticLoc.isValid(), isStar,
1481 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001482}
1483
Steve Naroffad373bd2007-07-31 12:34:36 +00001484/// [GNU] typeof-specifier:
1485/// typeof ( expressions )
1486/// typeof ( type-name )
1487///
1488void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00001489 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00001490 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00001491 SourceLocation StartLoc = ConsumeToken();
1492
Chris Lattner76c72282007-10-09 17:33:22 +00001493 if (Tok.isNot(tok::l_paren)) {
Steve Naroff4bd2f712007-08-02 02:53:48 +00001494 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1495 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001496 }
1497 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1498
1499 if (isTypeSpecifierQualifier()) {
1500 TypeTy *Ty = ParseTypeName();
1501
Steve Naroff872da802007-07-31 23:56:32 +00001502 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1503
Chris Lattner76c72282007-10-09 17:33:22 +00001504 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001505 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001506 return;
1507 }
1508 RParenLoc = ConsumeParen();
1509 const char *PrevSpec = 0;
1510 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1511 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1512 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001513 } else { // we have an expression.
1514 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001515
Chris Lattner76c72282007-10-09 17:33:22 +00001516 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001517 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001518 return;
1519 }
1520 RParenLoc = ConsumeParen();
1521 const char *PrevSpec = 0;
1522 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1523 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1524 Result.Val))
1525 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001526 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001527}
1528
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001529