blob: 3c2eb2a84057f6ad636c88c26f1711c302321f50 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 }
Chris Lattner776fac82007-06-09 00:53:06 +0000299
300 Diag(Tok, diag::err_parse_error);
301 // Skip to end of block or statement
Chris Lattner43ba2512007-08-21 18:36:18 +0000302 SkipUntil(tok::r_brace, true, true);
Chris Lattner76c72282007-10-09 17:33:22 +0000303 if (Tok.is(tok::semi))
Chris Lattner776fac82007-06-09 00:53:06 +0000304 ConsumeToken();
305 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000306}
307
Chris Lattner1890ac82006-08-13 01:16:23 +0000308/// ParseSpecifierQualifierList
309/// specifier-qualifier-list:
310/// type-specifier specifier-qualifier-list[opt]
311/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000312/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000313///
314void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
315 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
316 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000317 ParseDeclarationSpecifiers(DS);
318
319 // Validate declspec for type-name.
320 unsigned Specs = DS.getParsedSpecifiers();
321 if (Specs == DeclSpec::PQ_None)
322 Diag(Tok, diag::err_typename_requires_specqual);
323
Chris Lattner1b22eed2006-11-28 05:12:07 +0000324 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000325 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000326 if (DS.getStorageClassSpecLoc().isValid())
327 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
328 else
329 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000330 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000331 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000332
333 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000334 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000335 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000336 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000337 }
338}
Chris Lattner53361ac2006-08-10 05:19:57 +0000339
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000340/// ParseDeclarationSpecifiers
341/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000342/// storage-class-specifier declaration-specifiers[opt]
343/// type-specifier declaration-specifiers[opt]
344/// type-qualifier declaration-specifiers[opt]
345/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000346/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000347///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000348/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000349/// 'typedef'
350/// 'extern'
351/// 'static'
352/// 'auto'
353/// 'register'
354/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000355/// type-specifier: [C99 6.7.2]
356/// 'void'
357/// 'char'
358/// 'short'
359/// 'int'
360/// 'long'
361/// 'float'
362/// 'double'
363/// 'signed'
364/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000365/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000366/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000367/// typedef-name
Bill Wendling4073ed52007-02-13 01:51:42 +0000368/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000369/// [C99] '_Bool'
370/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000371/// [C99] '_Imaginary' // Removed in TC2?
372/// [GNU] '_Decimal32'
373/// [GNU] '_Decimal64'
374/// [GNU] '_Decimal128'
Steve Naroff872da802007-07-31 23:56:32 +0000375/// [GNU] typeof-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000376/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff7e901fd2007-08-22 23:18:22 +0000377/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000378/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000379/// 'const'
380/// 'volatile'
381/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000382/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000383/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000384///
385void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner02c04392007-07-25 00:24:17 +0000386 DS.Range.setBegin(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000387 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000388 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000389 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000390 SourceLocation Loc = Tok.getLocation();
391
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000392 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000393 // typedef-name
394 case tok::identifier:
395 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000396 // a type-specifier. Without this check we misparse:
397 // typedef int X; struct Y { short X; }; as 'short int'.
Chris Lattnerf055d432006-11-28 04:28:12 +0000398 if (!DS.hasTypeSpecifier()) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000399 // It has to be available as a typedef too!
400 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
401 CurScope)) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000402 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000403 TypeRep);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000404 if (isInvalid)
405 break;
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000406 // FIXME: restrict this to "id" and ObjC classnames.
407 DS.Range.setEnd(Tok.getLocation());
408 ConsumeToken(); // The identifier
409 if (Tok.is(tok::less)) {
Steve Naroffc5484042007-10-30 02:23:23 +0000410 SourceLocation endProtoLoc;
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000411 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffc5484042007-10-30 02:23:23 +0000412 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000413 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
414 new llvm::SmallVector<DeclTy *, 8>;
415 DS.setProtocolQualifiers(ProtocolDecl);
416 Actions.FindProtocolDeclaration(Loc,
417 &ProtocolRefs[0], ProtocolRefs.size(),
418 *ProtocolDecl);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000419 }
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000420 continue;
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000421 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000422 }
423 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000424 default:
425 // If this is not a declaration specifier token, we're done reading decl
426 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000427 DS.Finish(Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000428 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000429
430 // GNU attributes support.
431 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000432 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000433 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000434
435 // storage-class-specifier
436 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000437 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000438 break;
439 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000440 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000441 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000442 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000443 break;
444 case tok::kw_static:
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, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000447 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000448 break;
449 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000450 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000451 break;
452 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000453 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000454 break;
455 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000456 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000457 break;
458
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000459 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000460 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000461 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000462 break;
463 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000464 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000465 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000466 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000467 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000468 break;
469 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000470 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000471 break;
472 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000473 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000474 break;
475 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000476 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000477 break;
478 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000479 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000480 break;
481 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000482 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000483 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000484 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000485 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000486 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000487 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000488 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000489 break;
490 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000491 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000492 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000493 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000495 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000496 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000497 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000498 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000499 break;
500 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000501 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000502 break;
503 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000504 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000505 break;
506 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000508 break;
509
Chris Lattner1890ac82006-08-13 01:16:23 +0000510 case tok::kw_struct:
511 case tok::kw_union:
512 ParseStructUnionSpecifier(DS);
513 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000514 case tok::kw_enum:
515 ParseEnumSpecifier(DS);
516 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000517
Steve Naroffad373bd2007-07-31 12:34:36 +0000518 // GNU typeof support.
519 case tok::kw_typeof:
520 ParseTypeofSpecifier(DS);
521 continue;
522
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000523 // type-qualifier
524 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000525 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
526 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000527 break;
528 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000529 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
530 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000531 break;
532 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000533 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
534 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000535 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000536
537 // function-specifier
538 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000539 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000540 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000541 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000542 // If the specifier combination wasn't legal, issue a diagnostic.
543 if (isInvalid) {
544 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000545 if (isInvalid == 1) // Error.
546 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
547 else // extwarn.
548 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000549 }
Chris Lattner02c04392007-07-25 00:24:17 +0000550 DS.Range.setEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000551 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000552 }
553}
554
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000555/// ParseObjcTypeQualifierList - This routine parses the objective-c's type
556/// qualifier list and builds their bitmask representation in the input
557/// argument.
558void Parser::ParseObjcTypeQualifierList(ObjcDeclSpec &DS) {
559 bool found = true;
560 while (found) {
561 found = false;
562 if (Tok.is(tok::identifier)) {
563 const IdentifierInfo *II = Tok.getIdentifierInfo();
564 unsigned i;
565 for (i = 0; i < objc_NumQuals; ++i) {
566 if (II == ObjcTypeQuals[i]) {
567 switch (i) {
568 case objc_in:
569 DS.setObjcDeclQualifier(ObjcDeclSpec::DQ_In);
570 ConsumeToken();
571 found = true;
572 break;
573 case objc_out:
574 DS.setObjcDeclQualifier(ObjcDeclSpec::DQ_Out);
575 ConsumeToken();
576 found = true;
577 break;
578 case objc_inout:
579 DS.setObjcDeclQualifier(ObjcDeclSpec::DQ_Inout);
580 ConsumeToken();
581 found = true;
582 break;
583 case objc_oneway:
584 DS.setObjcDeclQualifier(ObjcDeclSpec::DQ_Oneway);
585 ConsumeToken();
586 found = true;
587 break;
588 case objc_bycopy:
589 DS.setObjcDeclQualifier(ObjcDeclSpec::DQ_Bycopy);
590 ConsumeToken();
591 found = true;
592 break;
593 case objc_byref:
594 DS.setObjcDeclQualifier(ObjcDeclSpec::DQ_Byref);
595 ConsumeToken();
596 found = true;
597 break;
598 }
599 if (found)
600 break;
601 }
602 }
603 }
604 }
605}
606
Chris Lattnerffbc2712007-01-25 06:05:38 +0000607/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
608/// the first token has already been read and has been turned into an instance
609/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
610/// otherwise it returns false and fills in Decl.
611bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
Steve Naroffb8371e12007-06-09 03:39:29 +0000612 AttributeList *Attr = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000613 // If attributes exist after tag, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000614 if (Tok.is(tok::kw___attribute))
Steve Naroffb8371e12007-06-09 03:39:29 +0000615 Attr = ParseAttributes();
Chris Lattnerffbc2712007-01-25 06:05:38 +0000616
Chris Lattner1890ac82006-08-13 01:16:23 +0000617 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner76c72282007-10-09 17:33:22 +0000618 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000619 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattner02c04392007-07-25 00:24:17 +0000620
621 // Skip the rest of this declarator, up until the comma or semicolon.
622 SkipUntil(tok::comma, true);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000623 return true;
Chris Lattner1890ac82006-08-13 01:16:23 +0000624 }
625
Chris Lattner8c6519a2007-01-22 07:41:36 +0000626 // If an identifier is present, consume and remember it.
627 IdentifierInfo *Name = 0;
628 SourceLocation NameLoc;
Chris Lattner76c72282007-10-09 17:33:22 +0000629 if (Tok.is(tok::identifier)) {
Chris Lattner8c6519a2007-01-22 07:41:36 +0000630 Name = Tok.getIdentifierInfo();
631 NameLoc = ConsumeToken();
632 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000633
Chris Lattner8c6519a2007-01-22 07:41:36 +0000634 // There are three options here. If we have 'struct foo;', then this is a
635 // forward declaration. If we have 'struct foo {...' then this is a
Chris Lattner7b9ace62007-01-23 20:11:08 +0000636 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Chris Lattner8799cf22007-01-23 01:57:16 +0000637 //
638 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
639 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
640 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
641 //
Chris Lattner7b9ace62007-01-23 20:11:08 +0000642 Action::TagKind TK;
Chris Lattner76c72282007-10-09 17:33:22 +0000643 if (Tok.is(tok::l_brace))
Chris Lattner7b9ace62007-01-23 20:11:08 +0000644 TK = Action::TK_Definition;
Chris Lattner76c72282007-10-09 17:33:22 +0000645 else if (Tok.is(tok::semi))
Chris Lattner7b9ace62007-01-23 20:11:08 +0000646 TK = Action::TK_Declaration;
647 else
648 TK = Action::TK_Reference;
Steve Naroff30d242c2007-09-15 18:49:24 +0000649 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000650 return false;
651}
652
653
654/// ParseStructUnionSpecifier
655/// struct-or-union-specifier: [C99 6.7.2.1]
656/// struct-or-union identifier[opt] '{' struct-contents '}'
657/// struct-or-union identifier
658/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
659/// '}' attributes[opt]
660/// [GNU] struct-or-union attributes[opt] identifier
661/// struct-or-union:
662/// 'struct'
663/// 'union'
664///
665void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000666 assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) &&
667 "Not a struct/union specifier");
Chris Lattnerffbc2712007-01-25 06:05:38 +0000668 DeclSpec::TST TagType =
Chris Lattner76c72282007-10-09 17:33:22 +0000669 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000670 SourceLocation StartLoc = ConsumeToken();
671
672 // Parse the tag portion of this.
673 DeclTy *TagDecl;
674 if (ParseTag(TagDecl, TagType, StartLoc))
675 return;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000676
Chris Lattner90a26b02007-01-23 04:38:16 +0000677 // If there is a body, parse it and inform the actions module.
Chris Lattner76c72282007-10-09 17:33:22 +0000678 if (Tok.is(tok::l_brace))
Chris Lattner1300fb92007-01-23 23:42:53 +0000679 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Chris Lattnerda72c822006-08-13 22:16:42 +0000680
681 const char *PrevSpec = 0;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000682 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000683 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000684}
685
Chris Lattner70ae4912007-10-29 04:42:53 +0000686/// ParseStructDeclaration - Parse a struct declaration without the terminating
687/// semicolon.
688///
Chris Lattner90a26b02007-01-23 04:38:16 +0000689/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +0000690/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +0000691/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +0000692/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +0000693/// struct-declarator-list:
694/// struct-declarator
695/// struct-declarator-list ',' struct-declarator
696/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
697/// struct-declarator:
698/// declarator
699/// [GNU] declarator attributes[opt]
700/// declarator[opt] ':' constant-expression
701/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
702///
Steve Naroff97170802007-08-20 22:28:22 +0000703void Parser::ParseStructDeclaration(DeclTy *TagDecl,
Steve Naroff4ca5d5a2007-08-28 16:31:47 +0000704 llvm::SmallVectorImpl<DeclTy*> &FieldDecls) {
Steve Naroff97170802007-08-20 22:28:22 +0000705 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner76c72282007-10-09 17:33:22 +0000706 if (Tok.is(tok::kw___extension__))
Steve Naroff97170802007-08-20 22:28:22 +0000707 ConsumeToken();
708
709 // Parse the common specifier-qualifiers-list piece.
710 DeclSpec DS;
711 SourceLocation SpecQualLoc = Tok.getLocation();
712 ParseSpecifierQualifierList(DS);
713 // TODO: Does specifier-qualifier list correctly check that *something* is
714 // specified?
715
716 // If there are no declarators, issue a warning.
Chris Lattner76c72282007-10-09 17:33:22 +0000717 if (Tok.is(tok::semi)) {
Steve Naroff97170802007-08-20 22:28:22 +0000718 Diag(SpecQualLoc, diag::w_no_declarators);
Steve Naroff97170802007-08-20 22:28:22 +0000719 return;
720 }
721
722 // Read struct-declarators until we find the semicolon.
723 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
724
725 while (1) {
726 /// struct-declarator: declarator
727 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +0000728 if (Tok.isNot(tok::colon))
Steve Naroff97170802007-08-20 22:28:22 +0000729 ParseDeclarator(DeclaratorInfo);
730
731 ExprTy *BitfieldSize = 0;
Chris Lattner76c72282007-10-09 17:33:22 +0000732 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +0000733 ConsumeToken();
734 ExprResult Res = ParseConstantExpression();
735 if (Res.isInvalid) {
736 SkipUntil(tok::semi, true, true);
737 } else {
738 BitfieldSize = Res.Val;
739 }
740 }
741
742 // If attributes exist after the declarator, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000743 if (Tok.is(tok::kw___attribute))
Steve Naroff97170802007-08-20 22:28:22 +0000744 DeclaratorInfo.AddAttributes(ParseAttributes());
745
746 // Install the declarator into the current TagDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +0000747 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, SpecQualLoc,
Steve Naroff97170802007-08-20 22:28:22 +0000748 DeclaratorInfo, BitfieldSize);
749 FieldDecls.push_back(Field);
750
751 // If we don't have a comma, it is either the end of the list (a ';')
752 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000753 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +0000754 return;
Steve Naroff97170802007-08-20 22:28:22 +0000755
756 // Consume the comma.
757 ConsumeToken();
758
759 // Parse the next declarator.
760 DeclaratorInfo.clear();
761
762 // Attributes are only allowed on the second declarator.
Chris Lattner76c72282007-10-09 17:33:22 +0000763 if (Tok.is(tok::kw___attribute))
Steve Naroff97170802007-08-20 22:28:22 +0000764 DeclaratorInfo.AddAttributes(ParseAttributes());
765 }
Steve Naroff97170802007-08-20 22:28:22 +0000766}
767
768/// ParseStructUnionBody
769/// struct-contents:
770/// struct-declaration-list
771/// [EXT] empty
772/// [GNU] "struct-declaration-list" without terminatoring ';'
773/// struct-declaration-list:
774/// struct-declaration
775/// struct-declaration-list struct-declaration
776/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
777///
Chris Lattner1300fb92007-01-23 23:42:53 +0000778void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
779 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000780 SourceLocation LBraceLoc = ConsumeBrace();
781
Chris Lattner7b9ace62007-01-23 20:11:08 +0000782 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
783 // C++.
Chris Lattner76c72282007-10-09 17:33:22 +0000784 if (Tok.is(tok::r_brace))
Chris Lattner90a26b02007-01-23 04:38:16 +0000785 Diag(Tok, diag::ext_empty_struct_union_enum,
786 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000787
Chris Lattner23b7eb62007-06-15 23:05:46 +0000788 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner1300fb92007-01-23 23:42:53 +0000789
Chris Lattner7b9ace62007-01-23 20:11:08 +0000790 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +0000791 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000792 // Each iteration of this loop reads one struct-declaration.
793
Chris Lattner736ed5d2007-06-09 05:59:07 +0000794 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +0000795 if (Tok.is(tok::semi)) {
Chris Lattner36e46a22007-06-09 05:49:55 +0000796 Diag(Tok, diag::ext_extra_struct_semi);
797 ConsumeToken();
798 continue;
799 }
Steve Naroff97170802007-08-20 22:28:22 +0000800 ParseStructDeclaration(TagDecl, FieldDecls);
Chris Lattner736ed5d2007-06-09 05:59:07 +0000801
Chris Lattner76c72282007-10-09 17:33:22 +0000802 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000803 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +0000804 } else if (Tok.is(tok::r_brace)) {
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000805 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
806 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000807 } else {
808 Diag(Tok, diag::err_expected_semi_decl_list);
809 // Skip to end of block or statement
810 SkipUntil(tok::r_brace, true, true);
811 }
812 }
813
Steve Naroff33a1e802007-10-29 21:38:07 +0000814 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +0000815
Fariborz Jahanian343f7092007-09-29 00:54:24 +0000816 Actions.ActOnFields(CurScope,
Steve Naroff33a1e802007-10-29 21:38:07 +0000817 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
818 LBraceLoc, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000819
Steve Naroffb8371e12007-06-09 03:39:29 +0000820 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000821 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000822 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000823 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000824}
825
826
Chris Lattner3b561a32006-08-13 00:12:11 +0000827/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000828/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000829/// 'enum' identifier[opt] '{' enumerator-list '}'
830/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000831/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
832/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000833/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000834/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000835void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000836 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000837 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000838
Chris Lattnerffbc2712007-01-25 06:05:38 +0000839 // Parse the tag portion of this.
840 DeclTy *TagDecl;
841 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000842 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000843
Chris Lattner76c72282007-10-09 17:33:22 +0000844 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000845 ParseEnumBody(StartLoc, TagDecl);
846
Chris Lattner3b561a32006-08-13 00:12:11 +0000847 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000848 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000849 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000850 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000851}
852
Chris Lattnerc1915e22007-01-25 07:29:02 +0000853/// ParseEnumBody - Parse a {} enclosed enumerator-list.
854/// enumerator-list:
855/// enumerator
856/// enumerator-list ',' enumerator
857/// enumerator:
858/// enumeration-constant
859/// enumeration-constant '=' constant-expression
860/// enumeration-constant:
861/// identifier
862///
863void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
864 SourceLocation LBraceLoc = ConsumeBrace();
865
Chris Lattner37256fb2007-08-27 17:24:30 +0000866 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +0000867 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000868 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
869
Chris Lattner23b7eb62007-06-15 23:05:46 +0000870 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000871
Chris Lattner4ef40012007-06-11 01:28:17 +0000872 DeclTy *LastEnumConstDecl = 0;
873
Chris Lattnerc1915e22007-01-25 07:29:02 +0000874 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +0000875 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000876 IdentifierInfo *Ident = Tok.getIdentifierInfo();
877 SourceLocation IdentLoc = ConsumeToken();
878
879 SourceLocation EqualLoc;
880 ExprTy *AssignedVal = 0;
Chris Lattner76c72282007-10-09 17:33:22 +0000881 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000882 EqualLoc = ConsumeToken();
883 ExprResult Res = ParseConstantExpression();
884 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000885 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000886 else
887 AssignedVal = Res.Val;
888 }
889
890 // Install the enumerator constant into EnumDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +0000891 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4ef40012007-06-11 01:28:17 +0000892 LastEnumConstDecl,
893 IdentLoc, Ident,
894 EqualLoc, AssignedVal);
895 EnumConstantDecls.push_back(EnumConstDecl);
896 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000897
Chris Lattner76c72282007-10-09 17:33:22 +0000898 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000899 break;
900 SourceLocation CommaLoc = ConsumeToken();
901
Chris Lattner76c72282007-10-09 17:33:22 +0000902 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000903 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
904 }
905
906 // Eat the }.
907 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
908
Steve Naroff30d242c2007-09-15 18:49:24 +0000909 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +0000910 EnumConstantDecls.size());
911
Steve Naroff0f2fe172007-06-01 17:11:19 +0000912 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000913 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000914 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000915 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000916}
Chris Lattner3b561a32006-08-13 00:12:11 +0000917
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000918/// isTypeSpecifierQualifier - Return true if the current token could be the
919/// start of a specifier-qualifier-list.
920bool Parser::isTypeSpecifierQualifier() const {
921 switch (Tok.getKind()) {
922 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000923 // GNU attributes support.
924 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +0000925 // GNU typeof support.
926 case tok::kw_typeof:
927
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000928 // type-specifiers
929 case tok::kw_short:
930 case tok::kw_long:
931 case tok::kw_signed:
932 case tok::kw_unsigned:
933 case tok::kw__Complex:
934 case tok::kw__Imaginary:
935 case tok::kw_void:
936 case tok::kw_char:
937 case tok::kw_int:
938 case tok::kw_float:
939 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000940 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000941 case tok::kw__Bool:
942 case tok::kw__Decimal32:
943 case tok::kw__Decimal64:
944 case tok::kw__Decimal128:
945
946 // struct-or-union-specifier
947 case tok::kw_struct:
948 case tok::kw_union:
949 // enum-specifier
950 case tok::kw_enum:
951
952 // type-qualifier
953 case tok::kw_const:
954 case tok::kw_volatile:
955 case tok::kw_restrict:
956 return true;
957
958 // typedef-name
959 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000960 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000961 }
962}
963
Chris Lattneracd58a32006-08-06 17:24:14 +0000964/// isDeclarationSpecifier() - Return true if the current token is part of a
965/// declaration specifier.
966bool Parser::isDeclarationSpecifier() const {
967 switch (Tok.getKind()) {
968 default: return false;
969 // storage-class-specifier
970 case tok::kw_typedef:
971 case tok::kw_extern:
972 case tok::kw_static:
973 case tok::kw_auto:
974 case tok::kw_register:
975 case tok::kw___thread:
976
977 // type-specifiers
978 case tok::kw_short:
979 case tok::kw_long:
980 case tok::kw_signed:
981 case tok::kw_unsigned:
982 case tok::kw__Complex:
983 case tok::kw__Imaginary:
984 case tok::kw_void:
985 case tok::kw_char:
986 case tok::kw_int:
987 case tok::kw_float:
988 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000989 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +0000990 case tok::kw__Bool:
991 case tok::kw__Decimal32:
992 case tok::kw__Decimal64:
993 case tok::kw__Decimal128:
994
995 // struct-or-union-specifier
996 case tok::kw_struct:
997 case tok::kw_union:
998 // enum-specifier
999 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001000
Chris Lattneracd58a32006-08-06 17:24:14 +00001001 // type-qualifier
1002 case tok::kw_const:
1003 case tok::kw_volatile:
1004 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001005
Chris Lattneracd58a32006-08-06 17:24:14 +00001006 // function-specifier
1007 case tok::kw_inline:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001008
Chris Lattner599e47e2007-08-09 17:01:07 +00001009 // GNU typeof support.
1010 case tok::kw_typeof:
1011
1012 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001013 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00001014 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001015
Chris Lattneracd58a32006-08-06 17:24:14 +00001016 // typedef-name
1017 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +00001018 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +00001019 }
1020}
1021
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001022
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001023/// ParseTypeQualifierListOpt
1024/// type-qualifier-list: [C99 6.7.5]
1025/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001026/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001027/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001028/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001029///
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001030void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001031 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001032 int isInvalid = false;
1033 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001034 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001035
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001036 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001037 default:
Chris Lattnere37e2332006-08-15 04:50:22 +00001038 // If this is not a type-qualifier token, we're done reading type
1039 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +00001040 DS.Finish(Diags, getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001041 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001042 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +00001043 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1044 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001045 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001046 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +00001047 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1048 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001049 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001050 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001051 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1052 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001053 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001054 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001055 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +00001056 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001057 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001058
1059 // If the specifier combination wasn't legal, issue a diagnostic.
1060 if (isInvalid) {
1061 assert(PrevSpec && "Method did not return previous specifier!");
1062 if (isInvalid == 1) // Error.
1063 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1064 else // extwarn.
1065 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1066 }
1067 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001068 }
1069}
1070
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001071
1072/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1073///
1074void Parser::ParseDeclarator(Declarator &D) {
1075 /// This implements the 'declarator' production in the C grammar, then checks
1076 /// for well-formedness and issues diagnostics.
1077 ParseDeclaratorInternal(D);
1078
Chris Lattner9fab3b92006-08-12 18:25:42 +00001079 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +00001080
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001081}
1082
1083/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001084/// declarator: [C99 6.7.5]
1085/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001086/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1087/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001088///
1089/// pointer: [C99 6.7.5]
1090/// '*' type-qualifier-list[opt]
1091/// '*' type-qualifier-list[opt] pointer
1092///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001093void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001094 tok::TokenKind Kind = Tok.getKind();
1095
1096 // Not a pointer or C++ reference.
1097 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001098 return ParseDirectDeclarator(D);
1099
Bill Wendling3708c182007-05-27 10:15:43 +00001100 // Otherwise, '*' -> pointer or '&' -> reference.
1101 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1102
1103 if (Kind == tok::star) {
1104 // Is a pointer
1105 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001106
Bill Wendling3708c182007-05-27 10:15:43 +00001107 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001108
Bill Wendling3708c182007-05-27 10:15:43 +00001109 // Recursively parse the declarator.
1110 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +00001111
Bill Wendling3708c182007-05-27 10:15:43 +00001112 // Remember that we parsed a pointer type, and remember the type-quals.
1113 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1114 } else {
1115 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001116 DeclSpec DS;
1117
1118 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1119 // cv-qualifiers are introduced through the use of a typedef or of a
1120 // template type argument, in which case the cv-qualifiers are ignored.
1121 //
1122 // [GNU] Retricted references are allowed.
1123 // [GNU] Attributes on references are allowed.
1124 ParseTypeQualifierListOpt(DS);
1125
1126 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1127 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1128 Diag(DS.getConstSpecLoc(),
1129 diag::err_invalid_reference_qualifier_application,
1130 "const");
1131 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1132 Diag(DS.getVolatileSpecLoc(),
1133 diag::err_invalid_reference_qualifier_application,
1134 "volatile");
1135 }
Bill Wendling3708c182007-05-27 10:15:43 +00001136
1137 // Recursively parse the declarator.
1138 ParseDeclaratorInternal(D);
1139
1140 // Remember that we parsed a reference type. It doesn't have type-quals.
Bill Wendling93efb222007-06-02 23:28:54 +00001141 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001142 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001143}
1144
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001145/// ParseDirectDeclarator
1146/// direct-declarator: [C99 6.7.5]
1147/// identifier
1148/// '(' declarator ')'
1149/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001150/// [C90] direct-declarator '[' constant-expression[opt] ']'
1151/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1152/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1153/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1154/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001155/// direct-declarator '(' parameter-type-list ')'
1156/// direct-declarator '(' identifier-list[opt] ')'
1157/// [GNU] direct-declarator '(' parameter-forward-declarations
1158/// parameter-type-list[opt] ')'
1159///
Chris Lattneracd58a32006-08-06 17:24:14 +00001160void Parser::ParseDirectDeclarator(Declarator &D) {
1161 // Parse the first direct-declarator seen.
Chris Lattner76c72282007-10-09 17:33:22 +00001162 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001163 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1164 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1165 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001166 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001167 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001168 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001169 // Example: 'char (*X)' or 'int (*XX)(void)'
1170 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001171 } else if (D.mayOmitIdentifier()) {
1172 // This could be something simple like "int" (in which case the declarator
1173 // portion is empty), if an abstract-declarator is allowed.
1174 D.SetIdentifier(0, Tok.getLocation());
1175 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001176 // Expected identifier or '('.
1177 Diag(Tok, diag::err_expected_ident_lparen);
1178 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001179 }
1180
1181 assert(D.isPastIdentifier() &&
1182 "Haven't past the location of the identifier yet?");
1183
1184 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001185 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001186 ParseParenDeclarator(D);
Chris Lattner76c72282007-10-09 17:33:22 +00001187 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001188 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001189 } else {
1190 break;
1191 }
1192 }
1193}
1194
1195/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1196/// either be before the identifier (in which case these are just grouping
1197/// parens for precedence) or it may be after the identifier, in which case
1198/// these are function arguments.
1199///
1200/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001201/// parameter-type-list: [C99 6.7.5]
1202/// parameter-list
1203/// parameter-list ',' '...'
1204///
1205/// parameter-list: [C99 6.7.5]
1206/// parameter-declaration
1207/// parameter-list ',' parameter-declaration
1208///
1209/// parameter-declaration: [C99 6.7.5]
1210/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +00001211/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001212/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001213/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001214///
1215/// identifier-list: [C99 6.7.5]
1216/// identifier
1217/// identifier-list ',' identifier
1218///
Chris Lattneracd58a32006-08-06 17:24:14 +00001219void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001220 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001221
Chris Lattneracd58a32006-08-06 17:24:14 +00001222 // If we haven't past the identifier yet (or where the identifier would be
1223 // stored, if this is an abstract declarator), then this is probably just
1224 // grouping parens.
1225 if (!D.isPastIdentifier()) {
1226 // Okay, this is probably a grouping paren. However, if this could be an
1227 // abstract-declarator, then this could also be the start of function
1228 // arguments (consider 'void()').
1229 bool isGrouping;
1230
1231 if (!D.mayOmitIdentifier()) {
1232 // If this can't be an abstract-declarator, this *must* be a grouping
1233 // paren, because we haven't seen the identifier yet.
1234 isGrouping = true;
Chris Lattner76c72282007-10-09 17:33:22 +00001235 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001236 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001237 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1238 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +00001239 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001240 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001241 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001242 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001243 }
Chris Lattneracd58a32006-08-06 17:24:14 +00001244
1245 // If this is a grouping paren, handle:
1246 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001247 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001248 if (isGrouping) {
Chris Lattner76c72282007-10-09 17:33:22 +00001249 if (Tok.is(tok::kw___attribute))
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001250 D.AddAttributes(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001251
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001252 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +00001253 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +00001254 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +00001255 return;
1256 }
1257
1258 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +00001259 // argument list. Recognize that this declarator will never have an
1260 // identifier (and remember where it would have been), then fall through to
1261 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +00001262 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001263 }
1264
Chris Lattneracd58a32006-08-06 17:24:14 +00001265 // Okay, this is the parameter list of a function definition, or it is an
1266 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001267 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +00001268 bool HasPrototype;
Chris Lattner14776b92006-08-06 22:27:40 +00001269 bool ErrorEmitted = false;
1270
Chris Lattneredc9e392006-12-02 06:21:46 +00001271 // Build up an array of information about the parsed arguments.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001272 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1273 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Chris Lattneredc9e392006-12-02 06:21:46 +00001274
Chris Lattner76c72282007-10-09 17:33:22 +00001275 if (Tok.is(tok::r_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001276 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001277 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001278 HasPrototype = false;
Chris Lattner76c72282007-10-09 17:33:22 +00001279 } else if (Tok.is(tok::identifier) &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001280 // K&R identifier lists can't have typedefs as identifiers, per
1281 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001282 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001283 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1284 // normal declarators, not for abstract-declarators.
1285 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1286
1287 // If there was no identifier specified, either we are in an
1288 // abstract-declarator, or we are in a parameter declarator which was found
1289 // to be abstract. In abstract-declarators, identifier lists are not valid,
1290 // diagnose this.
1291 if (!D.getIdentifier())
1292 Diag(Tok, diag::ext_ident_list_in_param);
Chris Lattneredc9e392006-12-02 06:21:46 +00001293
Chris Lattnercbc426d2006-12-02 06:43:02 +00001294 // Remember this identifier in ParamInfo.
1295 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1296 Tok.getLocation(), 0));
1297
Chris Lattneracd58a32006-08-06 17:24:14 +00001298 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001299 while (Tok.is(tok::comma)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001300 // Eat the comma.
1301 ConsumeToken();
1302
Chris Lattner76c72282007-10-09 17:33:22 +00001303 if (Tok.isNot(tok::identifier)) {
Chris Lattnercbc426d2006-12-02 06:43:02 +00001304 Diag(Tok, diag::err_expected_ident);
Chris Lattner14776b92006-08-06 22:27:40 +00001305 ErrorEmitted = true;
1306 break;
1307 }
Chris Lattnercbc426d2006-12-02 06:43:02 +00001308
Chris Lattner969ca152006-12-03 06:29:03 +00001309 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1310
1311 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001312 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001313 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1314 ParmII = 0;
1315 }
Chris Lattner969ca152006-12-03 06:29:03 +00001316
Chris Lattnercbc426d2006-12-02 06:43:02 +00001317 // Remember this identifier in ParamInfo.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001318 if (ParmII)
1319 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1320 Tok.getLocation(), 0));
Chris Lattnercbc426d2006-12-02 06:43:02 +00001321
1322 // Eat the identifier.
1323 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001324 }
1325
Chris Lattneracd58a32006-08-06 17:24:14 +00001326 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001327 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001328 HasPrototype = false;
1329 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +00001330 // Finally, a normal, non-empty parameter type list.
1331
Chris Lattnercbc426d2006-12-02 06:43:02 +00001332 // Enter function-declaration scope, limiting any declarators for struct
1333 // tags to the function prototype scope.
1334 // FIXME: is this needed?
Chris Lattner1a76a3c2007-08-26 06:24:45 +00001335 EnterScope(Scope::DeclScope);
Chris Lattner43e956c2006-11-28 04:05:37 +00001336
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001337 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001338 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001339 if (Tok.is(tok::ellipsis)) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001340 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001341
1342 // Check to see if this is "void(...)" which is not allowed.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001343 if (ParamInfo.empty()) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001344 // Otherwise, parse parameter type list. If it starts with an
1345 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001346 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001347 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001348 }
1349
1350 // Consume the ellipsis.
1351 ConsumeToken();
1352 break;
1353 }
1354
Chris Lattneracd58a32006-08-06 17:24:14 +00001355 // Parse the declaration-specifiers.
1356 DeclSpec DS;
1357 ParseDeclarationSpecifiers(DS);
1358
1359 // Parse the declarator. This is "PrototypeContext", because we must
1360 // accept either 'declarator' or 'abstract-declarator' here.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001361 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1362 ParseDeclarator(ParmDecl);
Chris Lattneracd58a32006-08-06 17:24:14 +00001363
Chris Lattnere37e2332006-08-15 04:50:22 +00001364 // Parse GNU attributes, if present.
Chris Lattner76c72282007-10-09 17:33:22 +00001365 if (Tok.is(tok::kw___attribute))
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001366 ParmDecl.AddAttributes(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001367
Chris Lattner43e956c2006-11-28 04:05:37 +00001368 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001369 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1370 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1371 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
Chris Lattner4d8f8732006-11-28 05:05:08 +00001372 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001373 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001374 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001375 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001376 if (DS.isThreadSpecified()) {
1377 Diag(DS.getThreadSpecLoc(),
1378 diag::err_invalid_storage_class_in_func_decl);
1379 DS.ClearStorageClassSpecs();
1380 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001381
1382 // Inform the actions module about the parameter declarator, so it gets
1383 // added to the current scope.
Chris Lattner216d8652006-12-02 06:47:41 +00001384 Action::TypeResult ParamTy =
Steve Naroff30d242c2007-09-15 18:49:24 +00001385 Actions.ActOnParamDeclaratorType(CurScope, ParmDecl);
Chris Lattnercbc426d2006-12-02 06:43:02 +00001386
1387 // Remember this parsed parameter in ParamInfo.
Chris Lattner969ca152006-12-03 06:29:03 +00001388 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1389
1390 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001391 if (ParmII && !ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001392 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1393 ParmII->getName());
1394 ParmII = 0;
Chris Lattner969ca152006-12-03 06:29:03 +00001395 }
1396
Steve Naroff7e6f7c22007-08-28 03:03:08 +00001397 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Nate Begemana0f78972007-11-13 22:14:47 +00001398 ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType(),
1399 ParmDecl.getDeclSpec().getAttributes()));
1400
1401 // Ownership of DeclSpec has been handed off to ParamInfo.
1402 DS.clearAttributes();
Chris Lattneracd58a32006-08-06 17:24:14 +00001403
1404 // If the next token is a comma, consume it and keep reading arguments.
Chris Lattner76c72282007-10-09 17:33:22 +00001405 if (Tok.isNot(tok::comma)) break;
Chris Lattneracd58a32006-08-06 17:24:14 +00001406
1407 // Consume the comma.
1408 ConsumeToken();
1409 }
1410
1411 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001412
1413 // Leave prototype scope.
1414 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001415 }
1416
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001417 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerd2e97c12006-12-03 02:03:33 +00001418 if (!ErrorEmitted)
1419 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1420 &ParamInfo[0], ParamInfo.size(),
1421 StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001422
Chris Lattner14776b92006-08-06 22:27:40 +00001423 // If we have the closing ')', eat it and we're done.
Chris Lattner76c72282007-10-09 17:33:22 +00001424 if (Tok.is(tok::r_paren)) {
Chris Lattner14776b92006-08-06 22:27:40 +00001425 ConsumeParen();
1426 } else {
1427 // If an error happened earlier parsing something else in the proto, don't
1428 // issue another error.
1429 if (!ErrorEmitted)
1430 Diag(Tok, diag::err_expected_rparen);
1431 SkipUntil(tok::r_paren);
1432 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001433}
Chris Lattneracd58a32006-08-06 17:24:14 +00001434
Chris Lattnere8074e62006-08-06 18:30:15 +00001435
1436/// [C90] direct-declarator '[' constant-expression[opt] ']'
1437/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1438/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1439/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1440/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1441void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001442 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001443
1444 // If valid, this location is the position where we read the 'static' keyword.
1445 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00001446 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001447 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001448
1449 // If there is a type-qualifier-list, read it now.
1450 DeclSpec DS;
1451 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001452
1453 // If we haven't already read 'static', check to see if there is one after the
1454 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001455 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001456 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001457
1458 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001459 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001460 ExprResult NumElements(false);
Chris Lattner76c72282007-10-09 17:33:22 +00001461 if (Tok.is(tok::star)) {
Chris Lattner1906f802006-08-06 19:14:46 +00001462 // Remember the '*' token, in case we have to un-get it.
Chris Lattner146762e2007-07-20 16:59:19 +00001463 Token StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001464 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001465
1466 // Check that the ']' token is present to avoid incorrectly parsing
1467 // expressions starting with '*' as [*].
Chris Lattner76c72282007-10-09 17:33:22 +00001468 if (Tok.is(tok::r_square)) {
Chris Lattner1906f802006-08-06 19:14:46 +00001469 if (StaticLoc.isValid())
1470 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1471 StaticLoc = SourceLocation(); // Drop the static.
1472 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001473 } else {
1474 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001475 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001476 // need to reparse it. This handles cases like 'X[*p + 4]'
1477 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001478 }
Chris Lattner76c72282007-10-09 17:33:22 +00001479 } else if (Tok.isNot(tok::r_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001480 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001481 NumElements = ParseAssignmentExpression();
1482 }
1483
1484 // If there was an error parsing the assignment-expression, recover.
1485 if (NumElements.isInvalid) {
1486 // If the expression was invalid, skip it.
1487 SkipUntil(tok::r_square);
1488 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001489 }
1490
Chris Lattner04f80192006-08-15 04:55:54 +00001491 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001492
Chris Lattnere8074e62006-08-06 18:30:15 +00001493 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1494 // it was not a constant expression.
1495 if (!getLang().C99) {
1496 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001497 if (isStar || StaticLoc.isValid() ||
1498 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001499 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001500 }
Bill Wendling93efb222007-06-02 23:28:54 +00001501
Chris Lattner6c7416c2006-08-07 00:19:33 +00001502 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001503 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1504 StaticLoc.isValid(), isStar,
1505 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001506}
1507
Steve Naroffad373bd2007-07-31 12:34:36 +00001508/// [GNU] typeof-specifier:
1509/// typeof ( expressions )
1510/// typeof ( type-name )
1511///
1512void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00001513 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00001514 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00001515 SourceLocation StartLoc = ConsumeToken();
1516
Chris Lattner76c72282007-10-09 17:33:22 +00001517 if (Tok.isNot(tok::l_paren)) {
Steve Naroff4bd2f712007-08-02 02:53:48 +00001518 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1519 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001520 }
1521 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1522
1523 if (isTypeSpecifierQualifier()) {
1524 TypeTy *Ty = ParseTypeName();
1525
Steve Naroff872da802007-07-31 23:56:32 +00001526 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1527
Chris Lattner76c72282007-10-09 17:33:22 +00001528 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001529 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001530 return;
1531 }
1532 RParenLoc = ConsumeParen();
1533 const char *PrevSpec = 0;
1534 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1535 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1536 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001537 } else { // we have an expression.
1538 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001539
Chris Lattner76c72282007-10-09 17:33:22 +00001540 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001541 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001542 return;
1543 }
1544 RParenLoc = ConsumeParen();
1545 const char *PrevSpec = 0;
1546 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1547 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1548 Result.Val))
1549 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001550 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001551}
1552