blob: c243f522b631f557a23455647c65c07ce1b120ca [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 Lattnerad9ac942007-01-23 01:14:52 +000016#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000017using namespace llvm;
18using 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
Chris Lattner558cb292006-11-19 01:31:06 +000036 return Actions.ParseTypeName(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
54/// any-word
55/// any-word '(' identifier ')'
56/// any-word '(' identifier ',' nonempty-expr-list ')'
57/// any-word '(' expr-list ')'
58///
59void Parser::ParseAttributes() {
60 assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!");
61 ConsumeToken();
62
63 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
64 "attribute"))
65 return;
66
67 // TODO: Parse the attributes.
68 SkipUntil(tok::r_paren, false);
69}
70
Chris Lattnerf5fbd792006-08-10 23:56:11 +000071
Chris Lattner53361ac2006-08-10 05:19:57 +000072/// ParseDeclaration - Parse a full 'declaration', which consists of
73/// declaration-specifiers, some number of declarators, and a semicolon.
74/// 'Context' should be a Declarator::TheContext value.
Chris Lattner302b4be2006-11-19 02:31:38 +000075Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +000076 // Parse the common declaration-specifiers piece.
77 DeclSpec DS;
78 ParseDeclarationSpecifiers(DS);
79
Chris Lattner0e894622006-08-13 19:58:17 +000080 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
81 // declaration-specifiers init-declarator-list[opt] ';'
82 if (Tok.getKind() == tok::semi) {
Chris Lattner0e894622006-08-13 19:58:17 +000083 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +000084 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +000085 }
86
Chris Lattner53361ac2006-08-10 05:19:57 +000087 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
88 ParseDeclarator(DeclaratorInfo);
89
Chris Lattner302b4be2006-11-19 02:31:38 +000090 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +000091}
92
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000093/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
94/// parsing 'declaration-specifiers declarator'. This method is split out this
95/// way to handle the ambiguity between top-level function-definitions and
96/// declarations.
97///
98/// declaration: [C99 6.7]
99/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
100/// [!C99] init-declarator-list ';' [TODO]
101/// [OMP] threadprivate-directive [TODO]
102///
103/// init-declarator-list: [C99 6.7]
104/// init-declarator
105/// init-declarator-list ',' init-declarator
106/// init-declarator: [C99 6.7]
107/// declarator
108/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000109/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
110/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000111///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000112Parser::DeclTy *Parser::
113ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
114
115 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
116 // that they can be chained properly if the actions want this.
117 Parser::DeclTy *LastDeclInGroup = 0;
118
Chris Lattner53361ac2006-08-10 05:19:57 +0000119 // At this point, we know that it is not a function definition. Parse the
120 // rest of the init-declarator-list.
121 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000122 // If a simple-asm-expr is present, parse it.
123 if (Tok.getKind() == tok::kw_asm)
124 ParseSimpleAsm();
125
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000126 // If attributes are present, parse them.
127 if (Tok.getKind() == tok::kw___attribute)
128 ParseAttributes();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000129
Chris Lattner53361ac2006-08-10 05:19:57 +0000130 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000131 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000132 if (Tok.getKind() == tok::equal) {
133 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000134 Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000135 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000136 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000137 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000138 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000139 }
140
Chris Lattner697e5d62006-11-09 06:32:27 +0000141 // Inform the current actions module that we just parsed this declarator.
Chris Lattner289ab7b2006-11-08 06:54:53 +0000142 // FIXME: pass asm & attributes.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000143 LastDeclInGroup = Actions.ParseDeclarator(CurScope, D, Init.Val,
144 LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000145
146 // If we don't have a comma, it is either the end of the list (a ';') or an
147 // error, bail out.
148 if (Tok.getKind() != tok::comma)
149 break;
150
151 // Consume the comma.
152 ConsumeToken();
153
154 // Parse the next declarator.
155 D.clear();
156 ParseDeclarator(D);
157 }
158
159 if (Tok.getKind() == tok::semi) {
160 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000161 return LastDeclInGroup;
Chris Lattner53361ac2006-08-10 05:19:57 +0000162 } else {
163 Diag(Tok, diag::err_parse_error);
164 // Skip to end of block or statement
165 SkipUntil(tok::r_brace, true);
166 if (Tok.getKind() == tok::semi)
167 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000168 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000169 }
170}
171
Chris Lattner1890ac82006-08-13 01:16:23 +0000172/// ParseSpecifierQualifierList
173/// specifier-qualifier-list:
174/// type-specifier specifier-qualifier-list[opt]
175/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000176/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000177///
178void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
179 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
180 /// parse declaration-specifiers and complain about extra stuff.
181 SourceLocation Loc = Tok.getLocation();
182 ParseDeclarationSpecifiers(DS);
183
184 // Validate declspec for type-name.
185 unsigned Specs = DS.getParsedSpecifiers();
186 if (Specs == DeclSpec::PQ_None)
187 Diag(Tok, diag::err_typename_requires_specqual);
188
Chris Lattner1b22eed2006-11-28 05:12:07 +0000189 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000190 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000191 if (DS.getStorageClassSpecLoc().isValid())
192 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
193 else
194 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000195 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000196 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000197
198 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000199 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000200 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000201 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000202 }
203}
Chris Lattner53361ac2006-08-10 05:19:57 +0000204
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000205/// ParseDeclarationSpecifiers
206/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000207/// storage-class-specifier declaration-specifiers[opt]
208/// type-specifier declaration-specifiers[opt]
209/// type-qualifier declaration-specifiers[opt]
210/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000211/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000212///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000213/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000214/// 'typedef'
215/// 'extern'
216/// 'static'
217/// 'auto'
218/// 'register'
219/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000220/// type-specifier: [C99 6.7.2]
221/// 'void'
222/// 'char'
223/// 'short'
224/// 'int'
225/// 'long'
226/// 'float'
227/// 'double'
228/// 'signed'
229/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000230/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000231/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000232/// typedef-name
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000233/// [C99] '_Bool'
234/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000235/// [C99] '_Imaginary' // Removed in TC2?
236/// [GNU] '_Decimal32'
237/// [GNU] '_Decimal64'
238/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000239/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000240/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000241/// [OBJC] typedef-name objc-protocol-refs [TODO]
242/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000243/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000244/// 'const'
245/// 'volatile'
246/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000247/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000248/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000249///
250void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000251 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000252 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000253 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000254 SourceLocation Loc = Tok.getLocation();
255
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000256 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000257 // typedef-name
258 case tok::identifier:
259 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000260 // a type-specifier. Without this check we misparse:
261 // typedef int X; struct Y { short X; }; as 'short int'.
Chris Lattnerf055d432006-11-28 04:28:12 +0000262 if (!DS.hasTypeSpecifier()) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000263 // It has to be available as a typedef too!
264 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
265 CurScope)) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000267 TypeRep);
Chris Lattneredc9e392006-12-02 06:21:46 +0000268 break;
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000269 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000270 }
271 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000272 default:
273 // If this is not a declaration specifier token, we're done reading decl
274 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000275 DS.Finish(Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000276 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000277
278 // GNU attributes support.
279 case tok::kw___attribute:
280 ParseAttributes();
Chris Lattnerb95cca02006-10-17 03:01:08 +0000281 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000282
283 // storage-class-specifier
284 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000285 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000286 break;
287 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000288 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000289 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000290 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000291 break;
292 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000293 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000294 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000295 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000296 break;
297 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000298 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000299 break;
300 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000301 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000302 break;
303 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000304 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000305 break;
306
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000307 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000308 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000309 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000310 break;
311 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000312 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000313 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000314 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000315 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000316 break;
317 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000318 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000319 break;
320 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000321 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000322 break;
323 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000324 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000325 break;
326 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000327 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000328 break;
329 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000330 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000331 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000332 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000333 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000334 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000335 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000336 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000337 break;
338 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000339 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000340 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000341 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000342 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000343 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000344 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000346 break;
347 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000348 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000349 break;
350 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000351 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000352 break;
353 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000354 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000355 break;
356
Chris Lattner1890ac82006-08-13 01:16:23 +0000357 case tok::kw_struct:
358 case tok::kw_union:
359 ParseStructUnionSpecifier(DS);
360 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000361 case tok::kw_enum:
362 ParseEnumSpecifier(DS);
363 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000364
365 // type-qualifier
366 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000367 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
368 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000369 break;
370 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000371 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
372 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000373 break;
374 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000375 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
376 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000377 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000378
379 // function-specifier
380 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000381 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000382 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000383 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000384 // If the specifier combination wasn't legal, issue a diagnostic.
385 if (isInvalid) {
386 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000387 if (isInvalid == 1) // Error.
388 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
389 else // extwarn.
390 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000391 }
392 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000393 }
394}
395
Chris Lattner1890ac82006-08-13 01:16:23 +0000396
397/// ParseStructUnionSpecifier
398/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000399/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000400/// struct-or-union identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000401/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
402/// '}' attributes[opt]
403/// [GNU] struct-or-union attributes[opt] identifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000404/// struct-or-union:
405/// 'struct'
406/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000407/// struct-contents:
408/// struct-declaration-list
409/// [EXT] empty
410/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000411/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000412/// struct-declaration
413/// struct-declaration-list struct-declaration
414/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
415/// struct-declaration:
416/// specifier-qualifier-list struct-declarator-list ';'
417/// [GNU] __extension__ struct-declaration [TODO]
418/// [GNU] specifier-qualifier-list ';' [TODO]
419/// struct-declarator-list:
420/// struct-declarator
421/// struct-declarator-list ',' struct-declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000422/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
Chris Lattner476c3ad2006-08-13 22:09:58 +0000423/// struct-declarator:
424/// declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000425/// [GNU] declarator attributes[opt]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000426/// declarator[opt] ':' constant-expression
Chris Lattnere37e2332006-08-15 04:50:22 +0000427/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000428///
429void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
430 assert((Tok.getKind() == tok::kw_struct ||
431 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
432 bool isUnion = Tok.getKind() == tok::kw_union;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000433 SourceLocation StartLoc = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000434
435 // If attributes exist after tag, parse them.
436 if (Tok.getKind() == tok::kw___attribute)
437 ParseAttributes();
438
Chris Lattner1890ac82006-08-13 01:16:23 +0000439 // Must have either 'struct name' or 'struct {...}'.
440 if (Tok.getKind() != tok::identifier &&
441 Tok.getKind() != tok::l_brace) {
442 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattner8c6519a2007-01-22 07:41:36 +0000443 // TODO: better error recovery here.
Chris Lattner1890ac82006-08-13 01:16:23 +0000444 return;
445 }
446
Chris Lattner8c6519a2007-01-22 07:41:36 +0000447 // If an identifier is present, consume and remember it.
448 IdentifierInfo *Name = 0;
449 SourceLocation NameLoc;
450 if (Tok.getKind() == tok::identifier) {
451 Name = Tok.getIdentifierInfo();
452 NameLoc = ConsumeToken();
453 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000454
Chris Lattner8c6519a2007-01-22 07:41:36 +0000455 // There are three options here. If we have 'struct foo;', then this is a
456 // forward declaration. If we have 'struct foo {...' then this is a
Chris Lattner8799cf22007-01-23 01:57:16 +0000457 // definition. Otherwise we have something like 'struct foo xyz', a use. Tell
458 // the actions module whether this is a definition (forward or not) of the
459 // type insted of a use.
460 //
461 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
462 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
463 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
464 //
465 bool isUse = Tok.getKind() != tok::l_brace && Tok.getKind() != tok::semi;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000466 DeclTy *TagDecl = Actions.ParseTag(CurScope,
467 isUnion ? Action::TAG_UNION :
468 Action::TAG_STRUCT, isUse,
469 StartLoc, Name, NameLoc);
Chris Lattner8c6519a2007-01-22 07:41:36 +0000470 // TODO: more with the tag decl.
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000471 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000472 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000473
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000474 if (Tok.getKind() == tok::r_brace)
475 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000476
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000477 while (Tok.getKind() != tok::r_brace &&
478 Tok.getKind() != tok::eof) {
479 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000480
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000481 // Parse the common specifier-qualifiers-list piece.
482 DeclSpec DS;
483 SourceLocation SpecQualLoc = Tok.getLocation();
484 ParseSpecifierQualifierList(DS);
485 // TODO: Does specifier-qualifier list correctly check that *something* is
486 // specified?
487
488 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000489
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000490 // If there are no declarators, issue a warning.
491 if (Tok.getKind() == tok::semi) {
492 Diag(SpecQualLoc, diag::w_no_declarators);
493 } else {
494 // Read struct-declarators until we find the semicolon.
495 while (1) {
496 /// struct-declarator: declarator
497 /// struct-declarator: declarator[opt] ':' constant-expression
498 if (Tok.getKind() != tok::colon)
499 ParseDeclarator(DeclaratorInfo);
500
501 if (Tok.getKind() == tok::colon) {
502 ConsumeToken();
503 ExprResult Res = ParseConstantExpression();
504 if (Res.isInvalid) {
505 SkipUntil(tok::semi, true, true);
506 } else {
507 // Process it.
508 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000509 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000510
511 // If attributes exist after the declarator, parse them.
512 if (Tok.getKind() == tok::kw___attribute)
513 ParseAttributes();
Chris Lattner1890ac82006-08-13 01:16:23 +0000514
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000515 // TODO: install declarator.
516
517 // If we don't have a comma, it is either the end of the list (a ';')
518 // or an error, bail out.
519 if (Tok.getKind() != tok::comma)
520 break;
521
522 // Consume the comma.
523 ConsumeToken();
524
525 // Parse the next declarator.
526 DeclaratorInfo.clear();
Chris Lattnere37e2332006-08-15 04:50:22 +0000527
528 // Attributes are only allowed on the second declarator.
529 if (Tok.getKind() == tok::kw___attribute)
530 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000531 }
532 }
533
534 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000535 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000536 } else {
537 Diag(Tok, diag::err_expected_semi_decl_list);
538 // Skip to end of block or statement
539 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000540 }
541 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000542
Chris Lattner04f80192006-08-15 04:55:54 +0000543 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000544
545 // If attributes exist after struct contents, parse them.
546 if (Tok.getKind() == tok::kw___attribute)
547 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000548 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000549
550 const char *PrevSpec = 0;
551 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
Chris Lattnerb20e8942006-11-28 05:30:29 +0000552 StartLoc, PrevSpec))
553 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000554}
555
556
Chris Lattner3b561a32006-08-13 00:12:11 +0000557/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000558/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000559/// 'enum' identifier[opt] '{' enumerator-list '}'
560/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000561/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
562/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000563/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000564/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000565/// enumerator-list:
566/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000567/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000568/// enumerator:
569/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000570/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000571/// enumeration-constant:
572/// identifier
573///
574void Parser::ParseEnumSpecifier(DeclSpec &DS) {
575 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000576 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000577
Chris Lattnere37e2332006-08-15 04:50:22 +0000578 if (Tok.getKind() == tok::kw___attribute)
579 ParseAttributes();
580
Chris Lattner3b561a32006-08-13 00:12:11 +0000581 // Must have either 'enum name' or 'enum {...}'.
582 if (Tok.getKind() != tok::identifier &&
583 Tok.getKind() != tok::l_brace) {
584 Diag(Tok, diag::err_expected_ident_lbrace);
585 return;
586 }
587
588 if (Tok.getKind() == tok::identifier)
589 ConsumeToken();
590
Chris Lattner0fb8b362006-08-14 01:30:12 +0000591 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000592 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner3b561a32006-08-13 00:12:11 +0000593
Chris Lattner0fb8b362006-08-14 01:30:12 +0000594 if (Tok.getKind() == tok::r_brace)
595 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
596
597 // Parse the enumerator-list.
598 while (Tok.getKind() == tok::identifier) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000599 ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000600
601 if (Tok.getKind() == tok::equal) {
602 ConsumeToken();
603 ExprResult Res = ParseConstantExpression();
604 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
605 }
606
607 if (Tok.getKind() != tok::comma)
608 break;
Chris Lattneraf635312006-10-16 06:06:51 +0000609 SourceLocation CommaLoc = ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000610
611 if (Tok.getKind() != tok::identifier && !getLang().C99)
612 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000613 }
614
Chris Lattner0fb8b362006-08-14 01:30:12 +0000615 // Eat the }.
Chris Lattner04f80192006-08-15 04:55:54 +0000616 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000617
618 // If attributes exist after the identifier list, parse them.
619 if (Tok.getKind() == tok::kw___attribute)
620 ParseAttributes();
Chris Lattner3b561a32006-08-13 00:12:11 +0000621 }
622 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000623
624
625 const char *PrevSpec = 0;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000626 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec))
627 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000628}
629
630
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000631/// isTypeSpecifierQualifier - Return true if the current token could be the
632/// start of a specifier-qualifier-list.
633bool Parser::isTypeSpecifierQualifier() const {
634 switch (Tok.getKind()) {
635 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000636 // GNU attributes support.
637 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000638 // type-specifiers
639 case tok::kw_short:
640 case tok::kw_long:
641 case tok::kw_signed:
642 case tok::kw_unsigned:
643 case tok::kw__Complex:
644 case tok::kw__Imaginary:
645 case tok::kw_void:
646 case tok::kw_char:
647 case tok::kw_int:
648 case tok::kw_float:
649 case tok::kw_double:
650 case tok::kw__Bool:
651 case tok::kw__Decimal32:
652 case tok::kw__Decimal64:
653 case tok::kw__Decimal128:
654
655 // struct-or-union-specifier
656 case tok::kw_struct:
657 case tok::kw_union:
658 // enum-specifier
659 case tok::kw_enum:
660
661 // type-qualifier
662 case tok::kw_const:
663 case tok::kw_volatile:
664 case tok::kw_restrict:
665 return true;
666
667 // typedef-name
668 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000669 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000670
671 // TODO: Attributes.
672 }
673}
674
Chris Lattneracd58a32006-08-06 17:24:14 +0000675/// isDeclarationSpecifier() - Return true if the current token is part of a
676/// declaration specifier.
677bool Parser::isDeclarationSpecifier() const {
678 switch (Tok.getKind()) {
679 default: return false;
680 // storage-class-specifier
681 case tok::kw_typedef:
682 case tok::kw_extern:
683 case tok::kw_static:
684 case tok::kw_auto:
685 case tok::kw_register:
686 case tok::kw___thread:
687
688 // type-specifiers
689 case tok::kw_short:
690 case tok::kw_long:
691 case tok::kw_signed:
692 case tok::kw_unsigned:
693 case tok::kw__Complex:
694 case tok::kw__Imaginary:
695 case tok::kw_void:
696 case tok::kw_char:
697 case tok::kw_int:
698 case tok::kw_float:
699 case tok::kw_double:
700 case tok::kw__Bool:
701 case tok::kw__Decimal32:
702 case tok::kw__Decimal64:
703 case tok::kw__Decimal128:
704
705 // struct-or-union-specifier
706 case tok::kw_struct:
707 case tok::kw_union:
708 // enum-specifier
709 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000710
Chris Lattneracd58a32006-08-06 17:24:14 +0000711 // type-qualifier
712 case tok::kw_const:
713 case tok::kw_volatile:
714 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000715
Chris Lattneracd58a32006-08-06 17:24:14 +0000716 // function-specifier
717 case tok::kw_inline:
718 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000719
Chris Lattneracd58a32006-08-06 17:24:14 +0000720 // typedef-name
721 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000722 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000723 // TODO: Attributes.
724 }
725}
726
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000727
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000728/// ParseTypeQualifierListOpt
729/// type-qualifier-list: [C99 6.7.5]
730/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000731/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000732/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000733/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000734///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000735void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000736 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000737 int isInvalid = false;
738 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +0000739 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000740
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000741 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000742 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000743 // If this is not a type-qualifier token, we're done reading type
744 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000745 DS.Finish(Diags, getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000746 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000747 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000748 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
749 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000750 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000751 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000752 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
753 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000754 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000755 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000756 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
757 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000758 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000759
760 case tok::kw___attribute:
761 ParseAttributes();
762 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000763 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000764
765 // If the specifier combination wasn't legal, issue a diagnostic.
766 if (isInvalid) {
767 assert(PrevSpec && "Method did not return previous specifier!");
768 if (isInvalid == 1) // Error.
769 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
770 else // extwarn.
771 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
772 }
773 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000774 }
775}
776
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000777
778/// ParseDeclarator - Parse and verify a newly-initialized declarator.
779///
780void Parser::ParseDeclarator(Declarator &D) {
781 /// This implements the 'declarator' production in the C grammar, then checks
782 /// for well-formedness and issues diagnostics.
783 ParseDeclaratorInternal(D);
784
Chris Lattner9fab3b92006-08-12 18:25:42 +0000785 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000786
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000787}
788
789/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000790/// declarator: [C99 6.7.5]
791/// pointer[opt] direct-declarator
792///
793/// pointer: [C99 6.7.5]
794/// '*' type-qualifier-list[opt]
795/// '*' type-qualifier-list[opt] pointer
796///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000797void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000798 if (Tok.getKind() != tok::star)
799 return ParseDirectDeclarator(D);
800
801 // Otherwise, '*' -> pointer.
Chris Lattneraf635312006-10-16 06:06:51 +0000802 SourceLocation Loc = ConsumeToken(); // Eat the *.
Chris Lattner6c7416c2006-08-07 00:19:33 +0000803 DeclSpec DS;
804 ParseTypeQualifierListOpt(DS);
805
806 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000807 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +0000808
Chris Lattner6c7416c2006-08-07 00:19:33 +0000809 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +0000810 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
Chris Lattner6c7416c2006-08-07 00:19:33 +0000811}
812
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000813
814/// ParseDirectDeclarator
815/// direct-declarator: [C99 6.7.5]
816/// identifier
817/// '(' declarator ')'
818/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000819/// [C90] direct-declarator '[' constant-expression[opt] ']'
820/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
821/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
822/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
823/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000824/// direct-declarator '(' parameter-type-list ')'
825/// direct-declarator '(' identifier-list[opt] ')'
826/// [GNU] direct-declarator '(' parameter-forward-declarations
827/// parameter-type-list[opt] ')'
828///
Chris Lattneracd58a32006-08-06 17:24:14 +0000829void Parser::ParseDirectDeclarator(Declarator &D) {
830 // Parse the first direct-declarator seen.
831 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
832 assert(Tok.getIdentifierInfo() && "Not an identifier?");
833 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
834 ConsumeToken();
835 } else if (Tok.getKind() == tok::l_paren) {
836 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000837 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000838 // Example: 'char (*X)' or 'int (*XX)(void)'
839 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000840 } else if (D.mayOmitIdentifier()) {
841 // This could be something simple like "int" (in which case the declarator
842 // portion is empty), if an abstract-declarator is allowed.
843 D.SetIdentifier(0, Tok.getLocation());
844 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000845 // Expected identifier or '('.
846 Diag(Tok, diag::err_expected_ident_lparen);
847 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000848 }
849
850 assert(D.isPastIdentifier() &&
851 "Haven't past the location of the identifier yet?");
852
853 while (1) {
854 if (Tok.getKind() == tok::l_paren) {
855 ParseParenDeclarator(D);
856 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000857 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000858 } else {
859 break;
860 }
861 }
862}
863
864/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
865/// either be before the identifier (in which case these are just grouping
866/// parens for precedence) or it may be after the identifier, in which case
867/// these are function arguments.
868///
869/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000870/// parameter-type-list: [C99 6.7.5]
871/// parameter-list
872/// parameter-list ',' '...'
873///
874/// parameter-list: [C99 6.7.5]
875/// parameter-declaration
876/// parameter-list ',' parameter-declaration
877///
878/// parameter-declaration: [C99 6.7.5]
879/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000880/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000881/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000882/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000883///
884/// identifier-list: [C99 6.7.5]
885/// identifier
886/// identifier-list ',' identifier
887///
Chris Lattneracd58a32006-08-06 17:24:14 +0000888void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +0000889 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000890
Chris Lattneracd58a32006-08-06 17:24:14 +0000891 // If we haven't past the identifier yet (or where the identifier would be
892 // stored, if this is an abstract declarator), then this is probably just
893 // grouping parens.
894 if (!D.isPastIdentifier()) {
895 // Okay, this is probably a grouping paren. However, if this could be an
896 // abstract-declarator, then this could also be the start of function
897 // arguments (consider 'void()').
898 bool isGrouping;
899
900 if (!D.mayOmitIdentifier()) {
901 // If this can't be an abstract-declarator, this *must* be a grouping
902 // paren, because we haven't seen the identifier yet.
903 isGrouping = true;
904 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
905 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000906 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
907 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +0000908 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000909 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000910 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000911 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000912 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000913
914 // If this is a grouping paren, handle:
915 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000916 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000917 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +0000918 if (Tok.getKind() == tok::kw___attribute)
919 ParseAttributes();
920
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000921 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000922 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000923 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +0000924 return;
925 }
926
927 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000928 // argument list. Recognize that this declarator will never have an
929 // identifier (and remember where it would have been), then fall through to
930 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000931 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000932 }
933
Chris Lattneracd58a32006-08-06 17:24:14 +0000934 // Okay, this is the parameter list of a function definition, or it is an
935 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000936 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000937 bool HasPrototype;
Chris Lattner14776b92006-08-06 22:27:40 +0000938 bool ErrorEmitted = false;
939
Chris Lattneredc9e392006-12-02 06:21:46 +0000940 // Build up an array of information about the parsed arguments.
Chris Lattnercbc426d2006-12-02 06:43:02 +0000941 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattnerad9ac942007-01-23 01:14:52 +0000942 SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Chris Lattneredc9e392006-12-02 06:21:46 +0000943
Chris Lattneracd58a32006-08-06 17:24:14 +0000944 if (Tok.getKind() == tok::r_paren) {
945 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000946 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000947 HasPrototype = false;
948 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000949 // K&R identifier lists can't have typedefs as identifiers, per
950 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +0000951 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000952 // Identifier list. Note that '(' identifier-list ')' is only allowed for
953 // normal declarators, not for abstract-declarators.
954 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
955
956 // If there was no identifier specified, either we are in an
957 // abstract-declarator, or we are in a parameter declarator which was found
958 // to be abstract. In abstract-declarators, identifier lists are not valid,
959 // diagnose this.
960 if (!D.getIdentifier())
961 Diag(Tok, diag::ext_ident_list_in_param);
Chris Lattneredc9e392006-12-02 06:21:46 +0000962
Chris Lattnercbc426d2006-12-02 06:43:02 +0000963 // Remember this identifier in ParamInfo.
964 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
965 Tok.getLocation(), 0));
966
Chris Lattneracd58a32006-08-06 17:24:14 +0000967 ConsumeToken();
968 while (Tok.getKind() == tok::comma) {
969 // Eat the comma.
970 ConsumeToken();
971
Chris Lattnercbc426d2006-12-02 06:43:02 +0000972 if (Tok.getKind() != tok::identifier) {
973 Diag(Tok, diag::err_expected_ident);
Chris Lattner14776b92006-08-06 22:27:40 +0000974 ErrorEmitted = true;
975 break;
976 }
Chris Lattnercbc426d2006-12-02 06:43:02 +0000977
Chris Lattner969ca152006-12-03 06:29:03 +0000978 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
979
980 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerad9ac942007-01-23 01:14:52 +0000981 if (!ParamsSoFar.insert(ParmII).second) {
982 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
983 ParmII = 0;
984 }
Chris Lattner969ca152006-12-03 06:29:03 +0000985
Chris Lattnercbc426d2006-12-02 06:43:02 +0000986 // Remember this identifier in ParamInfo.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000987 if (ParmII)
988 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
989 Tok.getLocation(), 0));
Chris Lattnercbc426d2006-12-02 06:43:02 +0000990
991 // Eat the identifier.
992 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +0000993 }
994
Chris Lattneracd58a32006-08-06 17:24:14 +0000995 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000996 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000997 HasPrototype = false;
998 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +0000999 // Finally, a normal, non-empty parameter type list.
1000
Chris Lattnercbc426d2006-12-02 06:43:02 +00001001 // Enter function-declaration scope, limiting any declarators for struct
1002 // tags to the function prototype scope.
1003 // FIXME: is this needed?
Chris Lattner43e956c2006-11-28 04:05:37 +00001004 EnterScope(0);
1005
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001006 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001007 while (1) {
1008 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001009 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001010
1011 // Check to see if this is "void(...)" which is not allowed.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001012 if (ParamInfo.empty()) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001013 // Otherwise, parse parameter type list. If it starts with an
1014 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001015 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001016 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001017 }
1018
1019 // Consume the ellipsis.
1020 ConsumeToken();
1021 break;
1022 }
1023
Chris Lattneracd58a32006-08-06 17:24:14 +00001024 // Parse the declaration-specifiers.
1025 DeclSpec DS;
1026 ParseDeclarationSpecifiers(DS);
1027
1028 // Parse the declarator. This is "PrototypeContext", because we must
1029 // accept either 'declarator' or 'abstract-declarator' here.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001030 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1031 ParseDeclarator(ParmDecl);
Chris Lattneracd58a32006-08-06 17:24:14 +00001032
Chris Lattnere37e2332006-08-15 04:50:22 +00001033 // Parse GNU attributes, if present.
1034 if (Tok.getKind() == tok::kw___attribute)
1035 ParseAttributes();
1036
Chris Lattner43e956c2006-11-28 04:05:37 +00001037 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001038 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1039 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1040 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
Chris Lattner4d8f8732006-11-28 05:05:08 +00001041 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001042 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001043 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001044 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001045 if (DS.isThreadSpecified()) {
1046 Diag(DS.getThreadSpecLoc(),
1047 diag::err_invalid_storage_class_in_func_decl);
1048 DS.ClearStorageClassSpecs();
1049 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001050
1051 // Inform the actions module about the parameter declarator, so it gets
1052 // added to the current scope.
Chris Lattner216d8652006-12-02 06:47:41 +00001053 Action::TypeResult ParamTy =
1054 Actions.ParseParamDeclaratorType(CurScope, ParmDecl);
Chris Lattnercbc426d2006-12-02 06:43:02 +00001055
1056 // Remember this parsed parameter in ParamInfo.
Chris Lattner969ca152006-12-03 06:29:03 +00001057 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1058
1059 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerad9ac942007-01-23 01:14:52 +00001060 if (ParmII && !ParamsSoFar.insert(ParmII).second) {
1061 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1062 ParmII->getName());
1063 ParmII = 0;
Chris Lattner969ca152006-12-03 06:29:03 +00001064 }
1065
1066 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnercbc426d2006-12-02 06:43:02 +00001067 ParmDecl.getIdentifierLoc(),
1068 ParamTy.Val));
Chris Lattneracd58a32006-08-06 17:24:14 +00001069
1070 // If the next token is a comma, consume it and keep reading arguments.
1071 if (Tok.getKind() != tok::comma) break;
1072
1073 // Consume the comma.
1074 ConsumeToken();
1075 }
1076
1077 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001078
1079 // Leave prototype scope.
1080 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001081 }
1082
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001083 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerd2e97c12006-12-03 02:03:33 +00001084 if (!ErrorEmitted)
1085 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1086 &ParamInfo[0], ParamInfo.size(),
1087 StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001088
Chris Lattner14776b92006-08-06 22:27:40 +00001089 // If we have the closing ')', eat it and we're done.
1090 if (Tok.getKind() == tok::r_paren) {
1091 ConsumeParen();
1092 } else {
1093 // If an error happened earlier parsing something else in the proto, don't
1094 // issue another error.
1095 if (!ErrorEmitted)
1096 Diag(Tok, diag::err_expected_rparen);
1097 SkipUntil(tok::r_paren);
1098 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001099}
Chris Lattneracd58a32006-08-06 17:24:14 +00001100
Chris Lattnere8074e62006-08-06 18:30:15 +00001101
1102/// [C90] direct-declarator '[' constant-expression[opt] ']'
1103/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1104/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1105/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1106/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1107void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001108 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001109
1110 // If valid, this location is the position where we read the 'static' keyword.
1111 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001112 if (Tok.getKind() == tok::kw_static)
1113 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001114
1115 // If there is a type-qualifier-list, read it now.
1116 DeclSpec DS;
1117 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001118
1119 // If we haven't already read 'static', check to see if there is one after the
1120 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001121 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1122 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001123
1124 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001125 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001126 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001127 if (Tok.getKind() == tok::star) {
1128 // Remember the '*' token, in case we have to un-get it.
1129 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001130 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001131
1132 // Check that the ']' token is present to avoid incorrectly parsing
1133 // expressions starting with '*' as [*].
1134 if (Tok.getKind() == tok::r_square) {
1135 if (StaticLoc.isValid())
1136 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1137 StaticLoc = SourceLocation(); // Drop the static.
1138 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001139 } else {
1140 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001141 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001142 // need to reparse it. This handles cases like 'X[*p + 4]'
1143 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001144 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001145 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001146 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001147 NumElements = ParseAssignmentExpression();
1148 }
1149
1150 // If there was an error parsing the assignment-expression, recover.
1151 if (NumElements.isInvalid) {
1152 // If the expression was invalid, skip it.
1153 SkipUntil(tok::r_square);
1154 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001155 }
1156
Chris Lattner04f80192006-08-15 04:55:54 +00001157 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001158
Chris Lattnere8074e62006-08-06 18:30:15 +00001159 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1160 // it was not a constant expression.
1161 if (!getLang().C99) {
1162 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001163 if (isStar || StaticLoc.isValid() ||
1164 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001165 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001166 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001167
1168 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001169 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1170 StaticLoc.isValid(), isStar,
1171 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001172}
1173