blob: 1ac26a309fa2a93db3f7160c9465baa0d4c2c88e [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();
Steve Naroffcfdf6162008-06-05 00:02:44 +0000326 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner1890ac82006-08-13 01:16:23 +0000327 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;
Chris Lattnerd7352d62008-07-21 22:17:28 +0000416 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Steve Naroffc5484042007-10-30 02:23:23 +0000417 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Chris Lattnerd7352d62008-07-21 22:17:28 +0000418
419 // FIXME: New'ing this here seems wrong, why not have the action do
420 // it?
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000421 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
422 new llvm::SmallVector<DeclTy *, 8>;
423 DS.setProtocolQualifiers(ProtocolDecl);
424 Actions.FindProtocolDeclaration(Loc,
425 &ProtocolRefs[0], ProtocolRefs.size(),
426 *ProtocolDecl);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000427 }
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000428 continue;
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000429 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000430 }
431 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000432 default:
433 // If this is not a declaration specifier token, we're done reading decl
434 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000435 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000436 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000437
438 // GNU attributes support.
439 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000440 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000441 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000442
443 // storage-class-specifier
444 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000445 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000446 break;
447 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000448 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000449 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000450 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000451 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000452 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000453 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
454 PrevSpec);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000455 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000456 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000457 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000458 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000459 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000460 break;
461 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000462 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000463 break;
464 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000465 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000466 break;
467 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000468 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000469 break;
470
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000471 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000472 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000473 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000474 break;
475 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000476 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000477 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000478 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000479 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000480 break;
481 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000482 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000483 break;
484 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000485 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000486 break;
487 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000488 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000489 break;
490 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000491 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000492 break;
493 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000495 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000496 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000497 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000498 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000499 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000500 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000501 break;
502 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000503 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000504 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000505 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000507 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000508 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000509 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000510 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000511 break;
512 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000514 break;
515 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000517 break;
518 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000519 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000520 break;
Chris Lattner861a2262008-04-13 18:59:07 +0000521
522 case tok::kw_class:
Chris Lattner1890ac82006-08-13 01:16:23 +0000523 case tok::kw_struct:
524 case tok::kw_union:
Douglas Gregor556877c2008-04-13 21:30:24 +0000525 ParseClassSpecifier(DS);
Chris Lattner1890ac82006-08-13 01:16:23 +0000526 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000527 case tok::kw_enum:
528 ParseEnumSpecifier(DS);
529 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000530
Steve Naroffad373bd2007-07-31 12:34:36 +0000531 // GNU typeof support.
532 case tok::kw_typeof:
533 ParseTypeofSpecifier(DS);
534 continue;
535
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000536 // type-qualifier
537 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000538 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
539 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000540 break;
541 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000542 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
543 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000544 break;
545 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000546 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
547 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000548 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000549
550 // function-specifier
551 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000552 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000553 break;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000554
555 // Gross GCC-ism that we are forced support. FIXME: make an extension?
556 case tok::less:
557 if (!DS.hasTypeSpecifier()) {
558 SourceLocation endProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +0000559 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000560 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
561 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
562 new llvm::SmallVector<DeclTy *, 8>;
563 DS.setProtocolQualifiers(ProtocolDecl);
564 Actions.FindProtocolDeclaration(Loc,
565 &ProtocolRefs[0], ProtocolRefs.size(),
566 *ProtocolDecl);
567 }
568 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000569 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000570 // If the specifier combination wasn't legal, issue a diagnostic.
571 if (isInvalid) {
572 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000573 if (isInvalid == 1) // Error.
574 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
575 else // extwarn.
576 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000577 }
Chris Lattner2e232092008-03-13 06:29:04 +0000578 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000579 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000580 }
581}
582
Chris Lattnerffbc2712007-01-25 06:05:38 +0000583/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
584/// the first token has already been read and has been turned into an instance
585/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
586/// otherwise it returns false and fills in Decl.
587bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
Steve Naroffb8371e12007-06-09 03:39:29 +0000588 AttributeList *Attr = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000589 // If attributes exist after tag, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000590 if (Tok.is(tok::kw___attribute))
Steve Naroffb8371e12007-06-09 03:39:29 +0000591 Attr = ParseAttributes();
Chris Lattnerffbc2712007-01-25 06:05:38 +0000592
Chris Lattner1890ac82006-08-13 01:16:23 +0000593 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner76c72282007-10-09 17:33:22 +0000594 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000595 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattner02c04392007-07-25 00:24:17 +0000596
597 // Skip the rest of this declarator, up until the comma or semicolon.
598 SkipUntil(tok::comma, true);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000599 return true;
Chris Lattner1890ac82006-08-13 01:16:23 +0000600 }
601
Chris Lattner8c6519a2007-01-22 07:41:36 +0000602 // If an identifier is present, consume and remember it.
603 IdentifierInfo *Name = 0;
604 SourceLocation NameLoc;
Chris Lattner76c72282007-10-09 17:33:22 +0000605 if (Tok.is(tok::identifier)) {
Chris Lattner8c6519a2007-01-22 07:41:36 +0000606 Name = Tok.getIdentifierInfo();
607 NameLoc = ConsumeToken();
608 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000609
Chris Lattner8c6519a2007-01-22 07:41:36 +0000610 // There are three options here. If we have 'struct foo;', then this is a
611 // forward declaration. If we have 'struct foo {...' then this is a
Chris Lattner7b9ace62007-01-23 20:11:08 +0000612 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Chris Lattner8799cf22007-01-23 01:57:16 +0000613 //
614 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
615 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
616 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
617 //
Chris Lattner7b9ace62007-01-23 20:11:08 +0000618 Action::TagKind TK;
Chris Lattner76c72282007-10-09 17:33:22 +0000619 if (Tok.is(tok::l_brace))
Chris Lattner7b9ace62007-01-23 20:11:08 +0000620 TK = Action::TK_Definition;
Chris Lattner76c72282007-10-09 17:33:22 +0000621 else if (Tok.is(tok::semi))
Chris Lattner7b9ace62007-01-23 20:11:08 +0000622 TK = Action::TK_Declaration;
623 else
624 TK = Action::TK_Reference;
Steve Naroff30d242c2007-09-15 18:49:24 +0000625 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000626 return false;
627}
628
Chris Lattner70ae4912007-10-29 04:42:53 +0000629/// ParseStructDeclaration - Parse a struct declaration without the terminating
630/// semicolon.
631///
Chris Lattner90a26b02007-01-23 04:38:16 +0000632/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +0000633/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +0000634/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +0000635/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +0000636/// struct-declarator-list:
637/// struct-declarator
638/// struct-declarator-list ',' struct-declarator
639/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
640/// struct-declarator:
641/// declarator
642/// [GNU] declarator attributes[opt]
643/// declarator[opt] ':' constant-expression
644/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
645///
Chris Lattnera12405b2008-04-10 06:46:29 +0000646void Parser::
647ParseStructDeclaration(DeclSpec &DS,
648 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroff97170802007-08-20 22:28:22 +0000649 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattnera12405b2008-04-10 06:46:29 +0000650 while (Tok.is(tok::kw___extension__))
Steve Naroff97170802007-08-20 22:28:22 +0000651 ConsumeToken();
652
653 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +0000654 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +0000655 ParseSpecifierQualifierList(DS);
656 // TODO: Does specifier-qualifier list correctly check that *something* is
657 // specified?
658
659 // If there are no declarators, issue a warning.
Chris Lattner76c72282007-10-09 17:33:22 +0000660 if (Tok.is(tok::semi)) {
Chris Lattner32295d32008-04-10 06:15:14 +0000661 Diag(DSStart, diag::w_no_declarators);
Steve Naroff97170802007-08-20 22:28:22 +0000662 return;
663 }
664
665 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000666 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000667 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +0000668 FieldDeclarator &DeclaratorInfo = Fields.back();
669
Steve Naroff97170802007-08-20 22:28:22 +0000670 /// struct-declarator: declarator
671 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +0000672 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +0000673 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff97170802007-08-20 22:28:22 +0000674
Chris Lattner76c72282007-10-09 17:33:22 +0000675 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +0000676 ConsumeToken();
677 ExprResult Res = ParseConstantExpression();
Chris Lattner32295d32008-04-10 06:15:14 +0000678 if (Res.isInvalid)
Steve Naroff97170802007-08-20 22:28:22 +0000679 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +0000680 else
Chris Lattnera12405b2008-04-10 06:46:29 +0000681 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff97170802007-08-20 22:28:22 +0000682 }
683
684 // If attributes exist after the declarator, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000685 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000686 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000687
688 // If we don't have a comma, it is either the end of the list (a ';')
689 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000690 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +0000691 return;
Steve Naroff97170802007-08-20 22:28:22 +0000692
693 // Consume the comma.
694 ConsumeToken();
695
696 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000697 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000698
699 // Attributes are only allowed on the second declarator.
Chris Lattner76c72282007-10-09 17:33:22 +0000700 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000701 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000702 }
Steve Naroff97170802007-08-20 22:28:22 +0000703}
704
705/// ParseStructUnionBody
706/// struct-contents:
707/// struct-declaration-list
708/// [EXT] empty
709/// [GNU] "struct-declaration-list" without terminatoring ';'
710/// struct-declaration-list:
711/// struct-declaration
712/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +0000713/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +0000714///
Chris Lattner1300fb92007-01-23 23:42:53 +0000715void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
716 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000717 SourceLocation LBraceLoc = ConsumeBrace();
718
Chris Lattner7b9ace62007-01-23 20:11:08 +0000719 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
720 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +0000721 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner90a26b02007-01-23 04:38:16 +0000722 Diag(Tok, diag::ext_empty_struct_union_enum,
723 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000724
Chris Lattner23b7eb62007-06-15 23:05:46 +0000725 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000726 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
727
Chris Lattner7b9ace62007-01-23 20:11:08 +0000728 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +0000729 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000730 // Each iteration of this loop reads one struct-declaration.
731
Chris Lattner736ed5d2007-06-09 05:59:07 +0000732 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +0000733 if (Tok.is(tok::semi)) {
Chris Lattner36e46a22007-06-09 05:49:55 +0000734 Diag(Tok, diag::ext_extra_struct_semi);
735 ConsumeToken();
736 continue;
737 }
Chris Lattnera12405b2008-04-10 06:46:29 +0000738
739 // Parse all the comma separated declarators.
740 DeclSpec DS;
741 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +0000742 if (!Tok.is(tok::at)) {
743 ParseStructDeclaration(DS, FieldDeclarators);
744
745 // Convert them all to fields.
746 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
747 FieldDeclarator &FD = FieldDeclarators[i];
748 // Install the declarator into the current TagDecl.
749 DeclTy *Field = Actions.ActOnField(CurScope,
750 DS.getSourceRange().getBegin(),
751 FD.D, FD.BitfieldSize);
752 FieldDecls.push_back(Field);
753 }
754 } else { // Handle @defs
755 ConsumeToken();
756 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
757 Diag(Tok, diag::err_unexpected_at);
758 SkipUntil(tok::semi, true, true);
759 continue;
760 }
761 ConsumeToken();
762 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
763 if (!Tok.is(tok::identifier)) {
764 Diag(Tok, diag::err_expected_ident);
765 SkipUntil(tok::semi, true, true);
766 continue;
767 }
768 llvm::SmallVector<DeclTy*, 16> Fields;
769 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
770 Fields);
771 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
772 ConsumeToken();
773 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
774 }
Chris Lattner736ed5d2007-06-09 05:59:07 +0000775
Chris Lattner76c72282007-10-09 17:33:22 +0000776 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000777 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +0000778 } else if (Tok.is(tok::r_brace)) {
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000779 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
780 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000781 } else {
782 Diag(Tok, diag::err_expected_semi_decl_list);
783 // Skip to end of block or statement
784 SkipUntil(tok::r_brace, true, true);
785 }
786 }
787
Steve Naroff33a1e802007-10-29 21:38:07 +0000788 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +0000789
Fariborz Jahanian343f7092007-09-29 00:54:24 +0000790 Actions.ActOnFields(CurScope,
Chris Lattnereb85ab42008-02-25 21:04:36 +0000791 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff33a1e802007-10-29 21:38:07 +0000792 LBraceLoc, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000793
Steve Naroffb8371e12007-06-09 03:39:29 +0000794 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000795 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000796 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000797 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000798}
799
800
Chris Lattner3b561a32006-08-13 00:12:11 +0000801/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000802/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000803/// 'enum' identifier[opt] '{' enumerator-list '}'
804/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000805/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
806/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000807/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000808/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000809void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000810 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000811 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000812
Chris Lattnerffbc2712007-01-25 06:05:38 +0000813 // Parse the tag portion of this.
814 DeclTy *TagDecl;
815 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000816 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000817
Chris Lattner76c72282007-10-09 17:33:22 +0000818 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000819 ParseEnumBody(StartLoc, TagDecl);
820
Chris Lattner3b561a32006-08-13 00:12:11 +0000821 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000822 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000823 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000824 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000825}
826
Chris Lattnerc1915e22007-01-25 07:29:02 +0000827/// ParseEnumBody - Parse a {} enclosed enumerator-list.
828/// enumerator-list:
829/// enumerator
830/// enumerator-list ',' enumerator
831/// enumerator:
832/// enumeration-constant
833/// enumeration-constant '=' constant-expression
834/// enumeration-constant:
835/// identifier
836///
837void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
838 SourceLocation LBraceLoc = ConsumeBrace();
839
Chris Lattner37256fb2007-08-27 17:24:30 +0000840 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +0000841 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000842 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
843
Chris Lattner23b7eb62007-06-15 23:05:46 +0000844 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000845
Chris Lattner4ef40012007-06-11 01:28:17 +0000846 DeclTy *LastEnumConstDecl = 0;
847
Chris Lattnerc1915e22007-01-25 07:29:02 +0000848 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +0000849 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000850 IdentifierInfo *Ident = Tok.getIdentifierInfo();
851 SourceLocation IdentLoc = ConsumeToken();
852
853 SourceLocation EqualLoc;
854 ExprTy *AssignedVal = 0;
Chris Lattner76c72282007-10-09 17:33:22 +0000855 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000856 EqualLoc = ConsumeToken();
857 ExprResult Res = ParseConstantExpression();
858 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000859 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000860 else
861 AssignedVal = Res.Val;
862 }
863
864 // Install the enumerator constant into EnumDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +0000865 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4ef40012007-06-11 01:28:17 +0000866 LastEnumConstDecl,
867 IdentLoc, Ident,
868 EqualLoc, AssignedVal);
869 EnumConstantDecls.push_back(EnumConstDecl);
870 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000871
Chris Lattner76c72282007-10-09 17:33:22 +0000872 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000873 break;
874 SourceLocation CommaLoc = ConsumeToken();
875
Chris Lattner76c72282007-10-09 17:33:22 +0000876 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000877 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
878 }
879
880 // Eat the }.
881 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
882
Steve Naroff30d242c2007-09-15 18:49:24 +0000883 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +0000884 EnumConstantDecls.size());
885
Steve Naroff0f2fe172007-06-01 17:11:19 +0000886 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000887 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000888 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000889 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000890}
Chris Lattner3b561a32006-08-13 00:12:11 +0000891
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000892/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000893/// start of a type-qualifier-list.
894bool Parser::isTypeQualifier() const {
895 switch (Tok.getKind()) {
896 default: return false;
897 // type-qualifier
898 case tok::kw_const:
899 case tok::kw_volatile:
900 case tok::kw_restrict:
901 return true;
902 }
903}
904
905/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000906/// start of a specifier-qualifier-list.
907bool Parser::isTypeSpecifierQualifier() const {
908 switch (Tok.getKind()) {
909 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000910 // GNU attributes support.
911 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +0000912 // GNU typeof support.
913 case tok::kw_typeof:
Steve Naroffcfdf6162008-06-05 00:02:44 +0000914 // GNU bizarre protocol extension. FIXME: make an extension?
915 case tok::less:
Steve Naroffad373bd2007-07-31 12:34:36 +0000916
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000917 // type-specifiers
918 case tok::kw_short:
919 case tok::kw_long:
920 case tok::kw_signed:
921 case tok::kw_unsigned:
922 case tok::kw__Complex:
923 case tok::kw__Imaginary:
924 case tok::kw_void:
925 case tok::kw_char:
926 case tok::kw_int:
927 case tok::kw_float:
928 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000929 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000930 case tok::kw__Bool:
931 case tok::kw__Decimal32:
932 case tok::kw__Decimal64:
933 case tok::kw__Decimal128:
934
Chris Lattner861a2262008-04-13 18:59:07 +0000935 // struct-or-union-specifier (C99) or class-specifier (C++)
936 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000937 case tok::kw_struct:
938 case tok::kw_union:
939 // enum-specifier
940 case tok::kw_enum:
941
942 // type-qualifier
943 case tok::kw_const:
944 case tok::kw_volatile:
945 case tok::kw_restrict:
946 return true;
947
948 // typedef-name
949 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000950 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000951 }
952}
953
Chris Lattneracd58a32006-08-06 17:24:14 +0000954/// isDeclarationSpecifier() - Return true if the current token is part of a
955/// declaration specifier.
956bool Parser::isDeclarationSpecifier() const {
957 switch (Tok.getKind()) {
958 default: return false;
959 // storage-class-specifier
960 case tok::kw_typedef:
961 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +0000962 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +0000963 case tok::kw_static:
964 case tok::kw_auto:
965 case tok::kw_register:
966 case tok::kw___thread:
967
968 // type-specifiers
969 case tok::kw_short:
970 case tok::kw_long:
971 case tok::kw_signed:
972 case tok::kw_unsigned:
973 case tok::kw__Complex:
974 case tok::kw__Imaginary:
975 case tok::kw_void:
976 case tok::kw_char:
977 case tok::kw_int:
978 case tok::kw_float:
979 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000980 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +0000981 case tok::kw__Bool:
982 case tok::kw__Decimal32:
983 case tok::kw__Decimal64:
984 case tok::kw__Decimal128:
985
Chris Lattner861a2262008-04-13 18:59:07 +0000986 // struct-or-union-specifier (C99) or class-specifier (C++)
987 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +0000988 case tok::kw_struct:
989 case tok::kw_union:
990 // enum-specifier
991 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000992
Chris Lattneracd58a32006-08-06 17:24:14 +0000993 // type-qualifier
994 case tok::kw_const:
995 case tok::kw_volatile:
996 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +0000997
Chris Lattneracd58a32006-08-06 17:24:14 +0000998 // function-specifier
999 case tok::kw_inline:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001000
Chris Lattner599e47e2007-08-09 17:01:07 +00001001 // GNU typeof support.
1002 case tok::kw_typeof:
1003
1004 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001005 case tok::kw___attribute:
Steve Naroffcfdf6162008-06-05 00:02:44 +00001006
1007 // GNU bizarre protocol extension. FIXME: make an extension?
1008 case tok::less:
Chris Lattneracd58a32006-08-06 17:24:14 +00001009 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001010
Chris Lattneracd58a32006-08-06 17:24:14 +00001011 // typedef-name
1012 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +00001013 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +00001014 }
1015}
1016
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001017
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001018/// ParseTypeQualifierListOpt
1019/// type-qualifier-list: [C99 6.7.5]
1020/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001021/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001022/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001023/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001024///
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001025void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001026 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001027 int isInvalid = false;
1028 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001029 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001030
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001031 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001032 default:
Chris Lattnere37e2332006-08-15 04:50:22 +00001033 // If this is not a type-qualifier token, we're done reading type
1034 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +00001035 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001036 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001037 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +00001038 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1039 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001040 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001041 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +00001042 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1043 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001044 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001045 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001046 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1047 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001048 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001049 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001050 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +00001051 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001052 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001053
1054 // If the specifier combination wasn't legal, issue a diagnostic.
1055 if (isInvalid) {
1056 assert(PrevSpec && "Method did not return previous specifier!");
1057 if (isInvalid == 1) // Error.
1058 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1059 else // extwarn.
1060 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1061 }
1062 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001063 }
1064}
1065
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001066
1067/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1068///
1069void Parser::ParseDeclarator(Declarator &D) {
1070 /// This implements the 'declarator' production in the C grammar, then checks
1071 /// for well-formedness and issues diagnostics.
1072 ParseDeclaratorInternal(D);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001073}
1074
1075/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001076/// declarator: [C99 6.7.5]
1077/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001078/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1079/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001080///
1081/// pointer: [C99 6.7.5]
1082/// '*' type-qualifier-list[opt]
1083/// '*' type-qualifier-list[opt] pointer
1084///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001085void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001086 tok::TokenKind Kind = Tok.getKind();
1087
1088 // Not a pointer or C++ reference.
Chris Lattner788404f2008-02-21 01:32:26 +00001089 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001090 return ParseDirectDeclarator(D);
1091
Bill Wendling3708c182007-05-27 10:15:43 +00001092 // Otherwise, '*' -> pointer or '&' -> reference.
1093 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1094
1095 if (Kind == tok::star) {
Chris Lattner788404f2008-02-21 01:32:26 +00001096 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00001097 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001098
Bill Wendling3708c182007-05-27 10:15:43 +00001099 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001100
Bill Wendling3708c182007-05-27 10:15:43 +00001101 // Recursively parse the declarator.
1102 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +00001103
Bill Wendling3708c182007-05-27 10:15:43 +00001104 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001105 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1106 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001107 } else {
1108 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001109 DeclSpec DS;
1110
1111 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1112 // cv-qualifiers are introduced through the use of a typedef or of a
1113 // template type argument, in which case the cv-qualifiers are ignored.
1114 //
1115 // [GNU] Retricted references are allowed.
1116 // [GNU] Attributes on references are allowed.
1117 ParseTypeQualifierListOpt(DS);
1118
1119 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1120 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1121 Diag(DS.getConstSpecLoc(),
1122 diag::err_invalid_reference_qualifier_application,
1123 "const");
1124 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1125 Diag(DS.getVolatileSpecLoc(),
1126 diag::err_invalid_reference_qualifier_application,
1127 "volatile");
1128 }
Bill Wendling3708c182007-05-27 10:15:43 +00001129
1130 // Recursively parse the declarator.
1131 ParseDeclaratorInternal(D);
1132
1133 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001134 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1135 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001136 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001137}
1138
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001139/// ParseDirectDeclarator
1140/// direct-declarator: [C99 6.7.5]
1141/// identifier
1142/// '(' declarator ')'
1143/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001144/// [C90] direct-declarator '[' constant-expression[opt] ']'
1145/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1146/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1147/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1148/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001149/// direct-declarator '(' parameter-type-list ')'
1150/// direct-declarator '(' identifier-list[opt] ')'
1151/// [GNU] direct-declarator '(' parameter-forward-declarations
1152/// parameter-type-list[opt] ')'
1153///
Chris Lattneracd58a32006-08-06 17:24:14 +00001154void Parser::ParseDirectDeclarator(Declarator &D) {
1155 // Parse the first direct-declarator seen.
Chris Lattner76c72282007-10-09 17:33:22 +00001156 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001157 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1158 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1159 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001160 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001161 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001162 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001163 // Example: 'char (*X)' or 'int (*XX)(void)'
1164 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001165 } else if (D.mayOmitIdentifier()) {
1166 // This could be something simple like "int" (in which case the declarator
1167 // portion is empty), if an abstract-declarator is allowed.
1168 D.SetIdentifier(0, Tok.getLocation());
1169 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001170 // Expected identifier or '('.
1171 Diag(Tok, diag::err_expected_ident_lparen);
1172 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001173 }
1174
1175 assert(D.isPastIdentifier() &&
1176 "Haven't past the location of the identifier yet?");
1177
1178 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001179 if (Tok.is(tok::l_paren)) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001180 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00001181 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001182 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001183 } else {
1184 break;
1185 }
1186 }
1187}
1188
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001189/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1190/// only called before the identifier, so these are most likely just grouping
1191/// parens for precedence. If we find that these are actually function
1192/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1193///
1194/// direct-declarator:
1195/// '(' declarator ')'
1196/// [GNU] '(' attributes declarator ')'
1197///
1198void Parser::ParseParenDeclarator(Declarator &D) {
1199 SourceLocation StartLoc = ConsumeParen();
1200 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1201
1202 // If we haven't past the identifier yet (or where the identifier would be
1203 // stored, if this is an abstract declarator), then this is probably just
1204 // grouping parens. However, if this could be an abstract-declarator, then
1205 // this could also be the start of function arguments (consider 'void()').
1206 bool isGrouping;
1207
1208 if (!D.mayOmitIdentifier()) {
1209 // If this can't be an abstract-declarator, this *must* be a grouping
1210 // paren, because we haven't seen the identifier yet.
1211 isGrouping = true;
1212 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1213 isDeclarationSpecifier()) { // 'int(int)' is a function.
1214 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1215 // considered to be a type, not a K&R identifier-list.
1216 isGrouping = false;
1217 } else {
1218 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1219 isGrouping = true;
1220 }
1221
1222 // If this is a grouping paren, handle:
1223 // direct-declarator: '(' declarator ')'
1224 // direct-declarator: '(' attributes declarator ')'
1225 if (isGrouping) {
1226 if (Tok.is(tok::kw___attribute))
1227 D.AddAttributes(ParseAttributes());
1228
1229 ParseDeclaratorInternal(D);
1230 // Match the ')'.
1231 MatchRHSPunctuation(tok::r_paren, StartLoc);
1232 return;
1233 }
1234
1235 // Okay, if this wasn't a grouping paren, it must be the start of a function
1236 // argument list. Recognize that this declarator will never have an
1237 // identifier (and remember where it would have been), then fall through to
1238 // the handling of argument lists.
1239 D.SetIdentifier(0, Tok.getLocation());
1240
1241 ParseFunctionDeclarator(StartLoc, D);
1242}
1243
1244/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1245/// declarator D up to a paren, which indicates that we are parsing function
1246/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00001247///
1248/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001249/// parameter-type-list: [C99 6.7.5]
1250/// parameter-list
1251/// parameter-list ',' '...'
1252///
1253/// parameter-list: [C99 6.7.5]
1254/// parameter-declaration
1255/// parameter-list ',' parameter-declaration
1256///
1257/// parameter-declaration: [C99 6.7.5]
1258/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001259/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001260/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001261/// declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00001262/// [C++] declaration-specifiers abstract-declarator[opt]
1263/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001264/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001265///
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001266void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1267 // lparen is already consumed!
1268 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001269
Chris Lattneracd58a32006-08-06 17:24:14 +00001270 // Okay, this is the parameter list of a function definition, or it is an
1271 // identifier list of a K&R-style function.
Chris Lattneredc9e392006-12-02 06:21:46 +00001272
Chris Lattner76c72282007-10-09 17:33:22 +00001273 if (Tok.is(tok::r_paren)) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00001274 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00001275 // int() -> no prototype, no '...'.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001276 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1277 /*variadic*/ false,
1278 /*arglist*/ 0, 0, LParenLoc));
1279
1280 ConsumeParen(); // Eat the closing ')'.
1281 return;
Chris Lattner76c72282007-10-09 17:33:22 +00001282 } else if (Tok.is(tok::identifier) &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001283 // K&R identifier lists can't have typedefs as identifiers, per
1284 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001285 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001286 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1287 // normal declarators, not for abstract-declarators.
Chris Lattner6c940e62008-04-06 06:34:08 +00001288 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001289 }
1290
1291 // Finally, a normal, non-empty parameter type list.
1292
1293 // Build up an array of information about the parsed arguments.
1294 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001295
1296 // Enter function-declaration scope, limiting any declarators to the
1297 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001298 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001299
1300 bool IsVariadic = false;
1301 while (1) {
1302 if (Tok.is(tok::ellipsis)) {
1303 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001304
Chris Lattner371ed4e2008-04-06 06:57:35 +00001305 // Check to see if this is "void(...)" which is not allowed.
1306 if (ParamInfo.empty()) {
1307 // Otherwise, parse parameter type list. If it starts with an
1308 // ellipsis, diagnose the malformed function.
1309 Diag(Tok, diag::err_ellipsis_first_arg);
1310 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner969ca152006-12-03 06:29:03 +00001311 }
Chris Lattner7f024fe2008-01-31 06:10:07 +00001312
Chris Lattner371ed4e2008-04-06 06:57:35 +00001313 ConsumeToken(); // Consume the ellipsis.
1314 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00001315 }
1316
Chris Lattner371ed4e2008-04-06 06:57:35 +00001317 SourceLocation DSStart = Tok.getLocation();
Chris Lattner43e956c2006-11-28 04:05:37 +00001318
Chris Lattner371ed4e2008-04-06 06:57:35 +00001319 // Parse the declaration-specifiers.
1320 DeclSpec DS;
1321 ParseDeclarationSpecifiers(DS);
1322
1323 // Parse the declarator. This is "PrototypeContext", because we must
1324 // accept either 'declarator' or 'abstract-declarator' here.
1325 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1326 ParseDeclarator(ParmDecl);
1327
1328 // Parse GNU attributes, if present.
1329 if (Tok.is(tok::kw___attribute))
1330 ParmDecl.AddAttributes(ParseAttributes());
1331
Chris Lattner371ed4e2008-04-06 06:57:35 +00001332 // Remember this parsed parameter in ParamInfo.
1333 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1334
Chris Lattner371ed4e2008-04-06 06:57:35 +00001335 // If no parameter was specified, verify that *something* was specified,
1336 // otherwise we have a missing type and identifier.
1337 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1338 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1339 // Completely missing, emit error.
1340 Diag(DSStart, diag::err_missing_param);
1341 } else {
1342 // Otherwise, we have something. Add it and let semantic analysis try
1343 // to grok it and add the result to the ParamInfo we are building.
1344
1345 // Inform the actions module about the parameter declarator, so it gets
1346 // added to the current scope.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001347 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1348
1349 // Parse the default argument, if any. We parse the default
1350 // arguments in all dialects; the semantic analysis in
1351 // ActOnParamDefaultArgument will reject the default argument in
1352 // C.
1353 if (Tok.is(tok::equal)) {
1354 SourceLocation EqualLoc = Tok.getLocation();
1355
1356 // Consume the '='.
1357 ConsumeToken();
1358
1359 // Parse the default argument
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001360 ExprResult DefArgResult = ParseAssignmentExpression();
1361 if (DefArgResult.isInvalid) {
1362 SkipUntil(tok::comma, tok::r_paren, true, true);
1363 } else {
1364 // Inform the actions module about the default argument
1365 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1366 }
1367 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001368
1369 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001370 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner371ed4e2008-04-06 06:57:35 +00001371 }
1372
1373 // If the next token is a comma, consume it and keep reading arguments.
1374 if (Tok.isNot(tok::comma)) break;
1375
1376 // Consume the comma.
1377 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001378 }
1379
Chris Lattner371ed4e2008-04-06 06:57:35 +00001380 // Leave prototype scope.
1381 ExitScope();
1382
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001383 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001384 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1385 &ParamInfo[0], ParamInfo.size(),
1386 LParenLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001387
Chris Lattner14776b92006-08-06 22:27:40 +00001388 // If we have the closing ')', eat it and we're done.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001389 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001390}
Chris Lattneracd58a32006-08-06 17:24:14 +00001391
Chris Lattner6c940e62008-04-06 06:34:08 +00001392/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1393/// we found a K&R-style identifier list instead of a type argument list. The
1394/// current token is known to be the first identifier in the list.
1395///
1396/// identifier-list: [C99 6.7.5]
1397/// identifier
1398/// identifier-list ',' identifier
1399///
1400void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1401 Declarator &D) {
1402 // Build up an array of information about the parsed arguments.
1403 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1404 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1405
1406 // If there was no identifier specified for the declarator, either we are in
1407 // an abstract-declarator, or we are in a parameter declarator which was found
1408 // to be abstract. In abstract-declarators, identifier lists are not valid:
1409 // diagnose this.
1410 if (!D.getIdentifier())
1411 Diag(Tok, diag::ext_ident_list_in_param);
1412
1413 // Tok is known to be the first identifier in the list. Remember this
1414 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00001415 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00001416 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1417 Tok.getLocation(), 0));
1418
Chris Lattner9186f552008-04-06 06:39:19 +00001419 ConsumeToken(); // eat the first identifier.
Chris Lattner6c940e62008-04-06 06:34:08 +00001420
1421 while (Tok.is(tok::comma)) {
1422 // Eat the comma.
1423 ConsumeToken();
1424
Chris Lattner9186f552008-04-06 06:39:19 +00001425 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00001426 if (Tok.isNot(tok::identifier)) {
1427 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00001428 SkipUntil(tok::r_paren);
1429 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00001430 }
Chris Lattner67b450c2008-04-06 06:47:48 +00001431
Chris Lattner6c940e62008-04-06 06:34:08 +00001432 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00001433
1434 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1435 if (Actions.isTypeName(*ParmII, CurScope))
1436 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner6c940e62008-04-06 06:34:08 +00001437
1438 // Verify that the argument identifier has not already been mentioned.
1439 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner9186f552008-04-06 06:39:19 +00001440 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1441 } else {
1442 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00001443 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1444 Tok.getLocation(), 0));
Chris Lattner9186f552008-04-06 06:39:19 +00001445 }
Chris Lattner6c940e62008-04-06 06:34:08 +00001446
1447 // Eat the identifier.
1448 ConsumeToken();
1449 }
1450
Chris Lattner9186f552008-04-06 06:39:19 +00001451 // Remember that we parsed a function type, and remember the attributes. This
1452 // function type is always a K&R style function type, which is not varargs and
1453 // has no prototype.
1454 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1455 &ParamInfo[0], ParamInfo.size(),
1456 LParenLoc));
Chris Lattner6c940e62008-04-06 06:34:08 +00001457
1458 // If we have the closing ')', eat it and we're done.
Chris Lattner9186f552008-04-06 06:39:19 +00001459 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00001460}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001461
Chris Lattnere8074e62006-08-06 18:30:15 +00001462/// [C90] direct-declarator '[' constant-expression[opt] ']'
1463/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1464/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1465/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1466/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1467void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001468 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001469
1470 // If valid, this location is the position where we read the 'static' keyword.
1471 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00001472 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001473 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001474
1475 // If there is a type-qualifier-list, read it now.
1476 DeclSpec DS;
1477 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001478
1479 // If we haven't already read 'static', check to see if there is one after the
1480 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001481 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001482 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001483
1484 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001485 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001486 ExprResult NumElements(false);
Chris Lattner521ff2b2008-04-06 05:26:30 +00001487
1488 // Handle the case where we have '[*]' as the array size. However, a leading
1489 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1490 // the the token after the star is a ']'. Since stars in arrays are
1491 // infrequent, use of lookahead is not costly here.
1492 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00001493 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00001494
Chris Lattner521ff2b2008-04-06 05:26:30 +00001495 if (StaticLoc.isValid())
1496 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1497 StaticLoc = SourceLocation(); // Drop the static.
1498 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00001499 } else if (Tok.isNot(tok::r_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001500 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001501 NumElements = ParseAssignmentExpression();
1502 }
1503
1504 // If there was an error parsing the assignment-expression, recover.
1505 if (NumElements.isInvalid) {
1506 // If the expression was invalid, skip it.
1507 SkipUntil(tok::r_square);
1508 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001509 }
1510
Chris Lattner04f80192006-08-15 04:55:54 +00001511 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001512
Chris Lattnere8074e62006-08-06 18:30:15 +00001513 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1514 // it was not a constant expression.
1515 if (!getLang().C99) {
1516 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001517 if (isStar || StaticLoc.isValid() ||
1518 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001519 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001520 }
Bill Wendling93efb222007-06-02 23:28:54 +00001521
Chris Lattner6c7416c2006-08-07 00:19:33 +00001522 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001523 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1524 StaticLoc.isValid(), isStar,
1525 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001526}
1527
Steve Naroffad373bd2007-07-31 12:34:36 +00001528/// [GNU] typeof-specifier:
1529/// typeof ( expressions )
1530/// typeof ( type-name )
1531///
1532void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00001533 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00001534 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00001535 SourceLocation StartLoc = ConsumeToken();
1536
Chris Lattner76c72282007-10-09 17:33:22 +00001537 if (Tok.isNot(tok::l_paren)) {
Steve Naroff4bd2f712007-08-02 02:53:48 +00001538 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1539 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001540 }
1541 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1542
1543 if (isTypeSpecifierQualifier()) {
1544 TypeTy *Ty = ParseTypeName();
1545
Steve Naroff872da802007-07-31 23:56:32 +00001546 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1547
Chris Lattner76c72282007-10-09 17:33:22 +00001548 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001549 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001550 return;
1551 }
1552 RParenLoc = ConsumeParen();
1553 const char *PrevSpec = 0;
1554 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1555 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1556 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001557 } else { // we have an expression.
1558 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001559
Chris Lattner76c72282007-10-09 17:33:22 +00001560 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001561 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001562 return;
1563 }
1564 RParenLoc = ConsumeParen();
1565 const char *PrevSpec = 0;
1566 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1567 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1568 Result.Val))
1569 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001570 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001571}
1572
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001573