Chris Lattner | eb8a28f | 2006-08-10 18:43:39 +0000 | [diff] [blame] | 1 | //===--- Declaration.cpp - Declaration Parsing ----------------------------===// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Declarations.h" |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | using namespace clang; |
| 18 | |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | // C99 6.7: Declarations. |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 23 | /// ParseTypeName |
| 24 | /// type-name: [C99 6.7.6] |
| 25 | /// specifier-qualifier-list abstract-declarator[opt] |
| 26 | void Parser::ParseTypeName() { |
| 27 | // Parse the common declaration-specifiers piece. |
| 28 | DeclSpec DS; |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 29 | ParseSpecifierQualifierList(DS); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 30 | |
| 31 | // Parse the abstract-declarator, if present. |
| 32 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 33 | ParseDeclarator(DeclaratorInfo); |
| 34 | } |
| 35 | |
| 36 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 37 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 38 | /// declaration-specifiers, some number of declarators, and a semicolon. |
| 39 | /// 'Context' should be a Declarator::TheContext value. |
| 40 | void Parser::ParseDeclaration(unsigned Context) { |
| 41 | // Parse the common declaration-specifiers piece. |
| 42 | DeclSpec DS; |
| 43 | ParseDeclarationSpecifiers(DS); |
| 44 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 45 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 46 | // declaration-specifiers init-declarator-list[opt] ';' |
| 47 | if (Tok.getKind() == tok::semi) { |
| 48 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 49 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 50 | |
| 51 | ConsumeToken(); |
| 52 | return; |
| 53 | } |
| 54 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 55 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 56 | ParseDeclarator(DeclaratorInfo); |
| 57 | |
| 58 | ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
| 59 | } |
| 60 | |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame^] | 61 | /// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after |
| 62 | /// parsing 'declaration-specifiers declarator'. This method is split out this |
| 63 | /// way to handle the ambiguity between top-level function-definitions and |
| 64 | /// declarations. |
| 65 | /// |
| 66 | /// declaration: [C99 6.7] |
| 67 | /// declaration-specifiers init-declarator-list[opt] ';' [TODO] |
| 68 | /// [!C99] init-declarator-list ';' [TODO] |
| 69 | /// [OMP] threadprivate-directive [TODO] |
| 70 | /// |
| 71 | /// init-declarator-list: [C99 6.7] |
| 72 | /// init-declarator |
| 73 | /// init-declarator-list ',' init-declarator |
| 74 | /// init-declarator: [C99 6.7] |
| 75 | /// declarator |
| 76 | /// declarator '=' initializer |
| 77 | /// |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 78 | void Parser::ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { |
| 79 | // At this point, we know that it is not a function definition. Parse the |
| 80 | // rest of the init-declarator-list. |
| 81 | while (1) { |
| 82 | // must be: decl-spec[opt] declarator init-declarator-list |
| 83 | // Parse declarator '=' initializer. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame^] | 84 | ExprResult Init; |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 85 | if (Tok.getKind() == tok::equal) { |
| 86 | ConsumeToken(); |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame^] | 87 | Init = ParseInitializer(); |
| 88 | if (!Init.isInvalid) { |
| 89 | SkipUntil(tok::semi); |
| 90 | return; |
| 91 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame^] | 94 | // Inform the current actions module that we just parsed a declarator. |
| 95 | Actions.ParseDeclarator(Tok.getLocation(), CurScope, D, Init.Val); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 96 | |
| 97 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 98 | // error, bail out. |
| 99 | if (Tok.getKind() != tok::comma) |
| 100 | break; |
| 101 | |
| 102 | // Consume the comma. |
| 103 | ConsumeToken(); |
| 104 | |
| 105 | // Parse the next declarator. |
| 106 | D.clear(); |
| 107 | ParseDeclarator(D); |
| 108 | } |
| 109 | |
| 110 | if (Tok.getKind() == tok::semi) { |
| 111 | ConsumeToken(); |
| 112 | } else { |
| 113 | Diag(Tok, diag::err_parse_error); |
| 114 | // Skip to end of block or statement |
| 115 | SkipUntil(tok::r_brace, true); |
| 116 | if (Tok.getKind() == tok::semi) |
| 117 | ConsumeToken(); |
| 118 | } |
| 119 | } |
| 120 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 121 | /// ParseSpecifierQualifierList |
| 122 | /// specifier-qualifier-list: |
| 123 | /// type-specifier specifier-qualifier-list[opt] |
| 124 | /// type-qualifier specifier-qualifier-list[opt] |
| 125 | /// |
| 126 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 127 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 128 | /// parse declaration-specifiers and complain about extra stuff. |
| 129 | SourceLocation Loc = Tok.getLocation(); |
| 130 | ParseDeclarationSpecifiers(DS); |
| 131 | |
| 132 | // Validate declspec for type-name. |
| 133 | unsigned Specs = DS.getParsedSpecifiers(); |
| 134 | if (Specs == DeclSpec::PQ_None) |
| 135 | Diag(Tok, diag::err_typename_requires_specqual); |
| 136 | |
| 137 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 138 | Diag(Loc, diag::err_typename_invalid_storageclass); |
| 139 | // Remove storage class. |
| 140 | DS.StorageClassSpec = DeclSpec::SCS_unspecified; |
| 141 | DS.SCS_thread_specified = false; |
| 142 | } |
| 143 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
| 144 | Diag(Loc, diag::err_typename_invalid_functionspec); |
| 145 | DS.FS_inline_specified = false; |
| 146 | } |
| 147 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 148 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 149 | /// ParseDeclarationSpecifiers |
| 150 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 151 | /// storage-class-specifier declaration-specifiers[opt] |
| 152 | /// type-specifier declaration-specifiers[opt] |
| 153 | /// type-qualifier declaration-specifiers[opt] |
| 154 | /// [C99] function-specifier declaration-specifiers[opt] |
| 155 | /// [GNU] attributes declaration-specifiers[opt] [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 156 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 157 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 158 | /// 'typedef' |
| 159 | /// 'extern' |
| 160 | /// 'static' |
| 161 | /// 'auto' |
| 162 | /// 'register' |
| 163 | /// [GNU] '__thread' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 164 | /// type-specifier: [C99 6.7.2] |
| 165 | /// 'void' |
| 166 | /// 'char' |
| 167 | /// 'short' |
| 168 | /// 'int' |
| 169 | /// 'long' |
| 170 | /// 'float' |
| 171 | /// 'double' |
| 172 | /// 'signed' |
| 173 | /// 'unsigned' |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 174 | /// struct-or-union-specifier |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 175 | /// enum-specifier |
Chris Lattner | 8e90ef6 | 2006-08-05 03:30:45 +0000 | [diff] [blame] | 176 | /// typedef-name [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 177 | /// [C99] '_Bool' |
| 178 | /// [C99] '_Complex' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 179 | /// [C99] '_Imaginary' // Removed in TC2? |
| 180 | /// [GNU] '_Decimal32' |
| 181 | /// [GNU] '_Decimal64' |
| 182 | /// [GNU] '_Decimal128' |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 183 | /// [GNU] typeof-specifier [TODO] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 184 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 185 | /// [OBJC] typedef-name objc-protocol-refs [TODO] |
| 186 | /// [OBJC] objc-protocol-refs [TODO] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 187 | /// type-qualifier: |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 188 | /// 'const' |
| 189 | /// 'volatile' |
| 190 | /// [C99] 'restrict' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 191 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 192 | /// [C99] 'inline' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 193 | /// |
| 194 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) { |
| 195 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 196 | while (1) { |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 197 | int isInvalid = false; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 198 | const char *PrevSpec = 0; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 199 | switch (Tok.getKind()) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 200 | default: |
| 201 | // If this is not a declaration specifier token, we're done reading decl |
| 202 | // specifiers. First verify that DeclSpec's are consistent. |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 203 | DS.Finish(StartLoc, Diags, getLang()); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 204 | return; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 205 | |
| 206 | // storage-class-specifier |
| 207 | case tok::kw_typedef: |
| 208 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec); |
| 209 | break; |
| 210 | case tok::kw_extern: |
| 211 | if (DS.SCS_thread_specified) |
| 212 | Diag(Tok, diag::ext_thread_before, "extern"); |
| 213 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec); |
| 214 | break; |
| 215 | case tok::kw_static: |
| 216 | if (DS.SCS_thread_specified) |
| 217 | Diag(Tok, diag::ext_thread_before, "static"); |
| 218 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec); |
| 219 | break; |
| 220 | case tok::kw_auto: |
| 221 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec); |
| 222 | break; |
| 223 | case tok::kw_register: |
| 224 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec); |
| 225 | break; |
| 226 | case tok::kw___thread: |
| 227 | if (DS.SCS_thread_specified) |
| 228 | isInvalid = 2, PrevSpec = "__thread"; |
| 229 | else |
| 230 | DS.SCS_thread_specified = true; |
| 231 | break; |
| 232 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 233 | // type-specifiers |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 234 | case tok::kw_short: |
| 235 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec); |
| 236 | break; |
| 237 | case tok::kw_long: |
| 238 | if (DS.TypeSpecWidth != DeclSpec::TSW_long) { |
| 239 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec); |
| 240 | } else { |
| 241 | DS.TypeSpecWidth = DeclSpec::TSW_unspecified; |
| 242 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec); |
| 243 | } |
| 244 | break; |
| 245 | case tok::kw_signed: |
| 246 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec); |
| 247 | break; |
| 248 | case tok::kw_unsigned: |
| 249 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec); |
| 250 | break; |
| 251 | case tok::kw__Complex: |
| 252 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec); |
| 253 | break; |
| 254 | case tok::kw__Imaginary: |
| 255 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec); |
| 256 | break; |
| 257 | case tok::kw_void: |
| 258 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec); |
| 259 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 260 | case tok::kw_char: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 261 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec); |
| 262 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 263 | case tok::kw_int: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 264 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec); |
| 265 | break; |
| 266 | case tok::kw_float: |
| 267 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec); |
| 268 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 269 | case tok::kw_double: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 270 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec); |
| 271 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 272 | case tok::kw__Bool: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 273 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec); |
| 274 | break; |
| 275 | case tok::kw__Decimal32: |
| 276 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec); |
| 277 | break; |
| 278 | case tok::kw__Decimal64: |
| 279 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec); |
| 280 | break; |
| 281 | case tok::kw__Decimal128: |
| 282 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 283 | break; |
| 284 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 285 | case tok::kw_struct: |
| 286 | case tok::kw_union: |
| 287 | ParseStructUnionSpecifier(DS); |
| 288 | continue; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 289 | case tok::kw_enum: |
| 290 | ParseEnumSpecifier(DS); |
| 291 | continue; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 292 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 293 | //case tok::identifier: |
| 294 | // TODO: handle typedef names. |
| 295 | |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 296 | // type-qualifier |
| 297 | case tok::kw_const: |
| 298 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2; |
| 299 | break; |
| 300 | case tok::kw_volatile: |
| 301 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2; |
| 302 | break; |
| 303 | case tok::kw_restrict: |
| 304 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2; |
| 305 | break; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 306 | |
| 307 | // function-specifier |
| 308 | case tok::kw_inline: |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 309 | // 'inline inline' is ok. |
| 310 | DS.FS_inline_specified = true; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 311 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 312 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 313 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 314 | if (isInvalid) { |
| 315 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 316 | if (isInvalid == 1) // Error. |
| 317 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 318 | else // extwarn. |
| 319 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 320 | } |
| 321 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 325 | |
| 326 | /// ParseStructUnionSpecifier |
| 327 | /// struct-or-union-specifier: [C99 6.7.2.1] |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 328 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 329 | /// struct-or-union identifier |
| 330 | /// struct-or-union: |
| 331 | /// 'struct' |
| 332 | /// 'union' |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 333 | /// struct-contents: |
| 334 | /// struct-declaration-list |
| 335 | /// [EXT] empty |
| 336 | /// [GNU] "struct-declaration-list" without terminatoring ';' [TODO] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 337 | /// struct-declaration-list: |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 338 | /// struct-declaration |
| 339 | /// struct-declaration-list struct-declaration |
| 340 | /// [OBC] '@' 'defs' '(' class-name ')' [TODO] |
| 341 | /// struct-declaration: |
| 342 | /// specifier-qualifier-list struct-declarator-list ';' |
| 343 | /// [GNU] __extension__ struct-declaration [TODO] |
| 344 | /// [GNU] specifier-qualifier-list ';' [TODO] |
| 345 | /// struct-declarator-list: |
| 346 | /// struct-declarator |
| 347 | /// struct-declarator-list ',' struct-declarator |
| 348 | /// struct-declarator: |
| 349 | /// declarator |
| 350 | /// declarator[opt] ':' constant-expression |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 351 | /// |
| 352 | void Parser::ParseStructUnionSpecifier(DeclSpec &DS) { |
| 353 | assert((Tok.getKind() == tok::kw_struct || |
| 354 | Tok.getKind() == tok::kw_union) && "Not a struct/union specifier"); |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 355 | SourceLocation Start = Tok.getLocation(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 356 | bool isUnion = Tok.getKind() == tok::kw_union; |
| 357 | ConsumeToken(); |
| 358 | |
| 359 | // Must have either 'struct name' or 'struct {...}'. |
| 360 | if (Tok.getKind() != tok::identifier && |
| 361 | Tok.getKind() != tok::l_brace) { |
| 362 | Diag(Tok, diag::err_expected_ident_lbrace); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | if (Tok.getKind() == tok::identifier) |
| 367 | ConsumeToken(); |
| 368 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 369 | if (Tok.getKind() == tok::l_brace) { |
| 370 | SourceLocation LBraceLoc = Tok.getLocation(); |
| 371 | ConsumeBrace(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 372 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 373 | if (Tok.getKind() == tok::r_brace) |
| 374 | Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct"); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 375 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 376 | while (Tok.getKind() != tok::r_brace && |
| 377 | Tok.getKind() != tok::eof) { |
| 378 | // Each iteration of this loop reads one struct-declaration. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 379 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 380 | // Parse the common specifier-qualifiers-list piece. |
| 381 | DeclSpec DS; |
| 382 | SourceLocation SpecQualLoc = Tok.getLocation(); |
| 383 | ParseSpecifierQualifierList(DS); |
| 384 | // TODO: Does specifier-qualifier list correctly check that *something* is |
| 385 | // specified? |
| 386 | |
| 387 | Declarator DeclaratorInfo(DS, Declarator::MemberContext); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 388 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 389 | // If there are no declarators, issue a warning. |
| 390 | if (Tok.getKind() == tok::semi) { |
| 391 | Diag(SpecQualLoc, diag::w_no_declarators); |
| 392 | } else { |
| 393 | // Read struct-declarators until we find the semicolon. |
| 394 | while (1) { |
| 395 | /// struct-declarator: declarator |
| 396 | /// struct-declarator: declarator[opt] ':' constant-expression |
| 397 | if (Tok.getKind() != tok::colon) |
| 398 | ParseDeclarator(DeclaratorInfo); |
| 399 | |
| 400 | if (Tok.getKind() == tok::colon) { |
| 401 | ConsumeToken(); |
| 402 | ExprResult Res = ParseConstantExpression(); |
| 403 | if (Res.isInvalid) { |
| 404 | SkipUntil(tok::semi, true, true); |
| 405 | } else { |
| 406 | // Process it. |
| 407 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 408 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 409 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 410 | // TODO: install declarator. |
| 411 | |
| 412 | // If we don't have a comma, it is either the end of the list (a ';') |
| 413 | // or an error, bail out. |
| 414 | if (Tok.getKind() != tok::comma) |
| 415 | break; |
| 416 | |
| 417 | // Consume the comma. |
| 418 | ConsumeToken(); |
| 419 | |
| 420 | // Parse the next declarator. |
| 421 | DeclaratorInfo.clear(); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | if (Tok.getKind() == tok::semi) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 426 | ConsumeToken(); |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 427 | } else { |
| 428 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 429 | // Skip to end of block or statement |
| 430 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 433 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 434 | MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",diag::err_expected_rbrace); |
| 435 | } |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 436 | |
| 437 | const char *PrevSpec = 0; |
| 438 | if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct, |
| 439 | PrevSpec)) |
| 440 | Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 444 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 445 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 446 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
| 447 | /// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 448 | /// [GNU] 'enum' identifier[opt] '{' enumerator-list '}' attributes [TODO] |
| 449 | /// [GNU] 'enum' identifier[opt] '{' enumerator-list ',' '}' attributes [TODO] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 450 | /// 'enum' identifier |
| 451 | /// enumerator-list: |
| 452 | /// enumerator |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 453 | /// enumerator-list ',' enumerator |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 454 | /// enumerator: |
| 455 | /// enumeration-constant |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 456 | /// enumeration-constant '=' constant-expression |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 457 | /// enumeration-constant: |
| 458 | /// identifier |
| 459 | /// |
| 460 | void Parser::ParseEnumSpecifier(DeclSpec &DS) { |
| 461 | assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier"); |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 462 | SourceLocation Start = Tok.getLocation(); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 463 | ConsumeToken(); |
| 464 | |
| 465 | // Must have either 'enum name' or 'enum {...}'. |
| 466 | if (Tok.getKind() != tok::identifier && |
| 467 | Tok.getKind() != tok::l_brace) { |
| 468 | Diag(Tok, diag::err_expected_ident_lbrace); |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | if (Tok.getKind() == tok::identifier) |
| 473 | ConsumeToken(); |
| 474 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 475 | if (Tok.getKind() != tok::l_brace) |
| 476 | return; |
| 477 | |
| 478 | SourceLocation LBraceLoc = Tok.getLocation(); |
| 479 | ConsumeBrace(); |
| 480 | |
| 481 | if (Tok.getKind() == tok::r_brace) |
| 482 | Diag(Tok, diag::ext_empty_struct_union_enum, "enum"); |
| 483 | |
| 484 | // Parse the enumerator-list. |
| 485 | while (Tok.getKind() == tok::identifier) { |
| 486 | ConsumeToken(); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 488 | if (Tok.getKind() == tok::equal) { |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 489 | ConsumeToken(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 490 | ExprResult Res = ParseConstantExpression(); |
| 491 | if (Res.isInvalid) SkipUntil(tok::comma, true, false); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 494 | if (Tok.getKind() != tok::comma) |
| 495 | break; |
| 496 | SourceLocation CommaLoc = Tok.getLocation(); |
| 497 | ConsumeToken(); |
| 498 | |
| 499 | if (Tok.getKind() != tok::identifier && !getLang().C99) |
| 500 | Diag(CommaLoc, diag::ext_c99_enumerator_list_comma); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 501 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 502 | |
| 503 | // Eat the }. |
| 504 | MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{", |
| 505 | diag::err_expected_rbrace); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 506 | // TODO: semantic analysis on the declspec for enums. |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 507 | |
| 508 | |
| 509 | const char *PrevSpec = 0; |
| 510 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec)) |
| 511 | Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 515 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
| 516 | /// start of a specifier-qualifier-list. |
| 517 | bool Parser::isTypeSpecifierQualifier() const { |
| 518 | switch (Tok.getKind()) { |
| 519 | default: return false; |
| 520 | // type-specifiers |
| 521 | case tok::kw_short: |
| 522 | case tok::kw_long: |
| 523 | case tok::kw_signed: |
| 524 | case tok::kw_unsigned: |
| 525 | case tok::kw__Complex: |
| 526 | case tok::kw__Imaginary: |
| 527 | case tok::kw_void: |
| 528 | case tok::kw_char: |
| 529 | case tok::kw_int: |
| 530 | case tok::kw_float: |
| 531 | case tok::kw_double: |
| 532 | case tok::kw__Bool: |
| 533 | case tok::kw__Decimal32: |
| 534 | case tok::kw__Decimal64: |
| 535 | case tok::kw__Decimal128: |
| 536 | |
| 537 | // struct-or-union-specifier |
| 538 | case tok::kw_struct: |
| 539 | case tok::kw_union: |
| 540 | // enum-specifier |
| 541 | case tok::kw_enum: |
| 542 | |
| 543 | // type-qualifier |
| 544 | case tok::kw_const: |
| 545 | case tok::kw_volatile: |
| 546 | case tok::kw_restrict: |
| 547 | return true; |
| 548 | |
| 549 | // typedef-name |
| 550 | case tok::identifier: |
| 551 | // FIXME: if this is a typedef return true. |
| 552 | return false; |
| 553 | |
| 554 | // TODO: Attributes. |
| 555 | } |
| 556 | } |
| 557 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 558 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 559 | /// declaration specifier. |
| 560 | bool Parser::isDeclarationSpecifier() const { |
| 561 | switch (Tok.getKind()) { |
| 562 | default: return false; |
| 563 | // storage-class-specifier |
| 564 | case tok::kw_typedef: |
| 565 | case tok::kw_extern: |
| 566 | case tok::kw_static: |
| 567 | case tok::kw_auto: |
| 568 | case tok::kw_register: |
| 569 | case tok::kw___thread: |
| 570 | |
| 571 | // type-specifiers |
| 572 | case tok::kw_short: |
| 573 | case tok::kw_long: |
| 574 | case tok::kw_signed: |
| 575 | case tok::kw_unsigned: |
| 576 | case tok::kw__Complex: |
| 577 | case tok::kw__Imaginary: |
| 578 | case tok::kw_void: |
| 579 | case tok::kw_char: |
| 580 | case tok::kw_int: |
| 581 | case tok::kw_float: |
| 582 | case tok::kw_double: |
| 583 | case tok::kw__Bool: |
| 584 | case tok::kw__Decimal32: |
| 585 | case tok::kw__Decimal64: |
| 586 | case tok::kw__Decimal128: |
| 587 | |
| 588 | // struct-or-union-specifier |
| 589 | case tok::kw_struct: |
| 590 | case tok::kw_union: |
| 591 | // enum-specifier |
| 592 | case tok::kw_enum: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 593 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 594 | // type-qualifier |
| 595 | case tok::kw_const: |
| 596 | case tok::kw_volatile: |
| 597 | case tok::kw_restrict: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 598 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 599 | // function-specifier |
| 600 | case tok::kw_inline: |
| 601 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 602 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 603 | // typedef-name |
| 604 | case tok::identifier: |
| 605 | // FIXME: if this is a typedef return true. |
| 606 | return false; |
| 607 | // TODO: Attributes. |
| 608 | } |
| 609 | } |
| 610 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 611 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 612 | /// ParseTypeQualifierListOpt |
| 613 | /// type-qualifier-list: [C99 6.7.5] |
| 614 | /// type-qualifier |
| 615 | /// [GNU] attributes [TODO] |
| 616 | /// type-qualifier-list type-qualifier |
| 617 | /// [GNU] type-qualifier-list attributes [TODO] |
| 618 | /// |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 619 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) { |
| 620 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 621 | while (1) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 622 | int isInvalid = false; |
| 623 | const char *PrevSpec = 0; |
| 624 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 625 | switch (Tok.getKind()) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 626 | default: |
| 627 | // If this is not a declaration specifier token, we're done reading decl |
| 628 | // specifiers. First verify that DeclSpec's are consistent. |
| 629 | DS.Finish(StartLoc, Diags, getLang()); |
| 630 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 631 | // TODO: attributes. |
| 632 | case tok::kw_const: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 633 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2; |
| 634 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 635 | case tok::kw_volatile: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 636 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2; |
| 637 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 638 | case tok::kw_restrict: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 639 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 640 | break; |
| 641 | } |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 642 | |
| 643 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 644 | if (isInvalid) { |
| 645 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 646 | if (isInvalid == 1) // Error. |
| 647 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 648 | else // extwarn. |
| 649 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 650 | } |
| 651 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 655 | |
| 656 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 657 | /// |
| 658 | void Parser::ParseDeclarator(Declarator &D) { |
| 659 | /// This implements the 'declarator' production in the C grammar, then checks |
| 660 | /// for well-formedness and issues diagnostics. |
| 661 | ParseDeclaratorInternal(D); |
| 662 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 663 | // TODO: validate D. |
Chris Lattner | bf320c8 | 2006-08-07 05:05:30 +0000 | [diff] [blame] | 664 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /// ParseDeclaratorInternal |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 668 | /// declarator: [C99 6.7.5] |
| 669 | /// pointer[opt] direct-declarator |
| 670 | /// |
| 671 | /// pointer: [C99 6.7.5] |
| 672 | /// '*' type-qualifier-list[opt] |
| 673 | /// '*' type-qualifier-list[opt] pointer |
| 674 | /// |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 675 | void Parser::ParseDeclaratorInternal(Declarator &D) { |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 676 | if (Tok.getKind() != tok::star) |
| 677 | return ParseDirectDeclarator(D); |
| 678 | |
| 679 | // Otherwise, '*' -> pointer. |
| 680 | SourceLocation Loc = Tok.getLocation(); |
| 681 | ConsumeToken(); // Eat the *. |
| 682 | DeclSpec DS; |
| 683 | ParseTypeQualifierListOpt(DS); |
| 684 | |
| 685 | // Recursively parse the declarator. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 686 | ParseDeclaratorInternal(D); |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 687 | |
| 688 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 689 | D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc)); |
| 690 | } |
| 691 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 692 | |
| 693 | /// ParseDirectDeclarator |
| 694 | /// direct-declarator: [C99 6.7.5] |
| 695 | /// identifier |
| 696 | /// '(' declarator ')' |
| 697 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 698 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 699 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 700 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 701 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 702 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 703 | /// direct-declarator '(' parameter-type-list ')' |
| 704 | /// direct-declarator '(' identifier-list[opt] ')' |
| 705 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 706 | /// parameter-type-list[opt] ')' |
| 707 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 708 | void Parser::ParseDirectDeclarator(Declarator &D) { |
| 709 | // Parse the first direct-declarator seen. |
| 710 | if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) { |
| 711 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 712 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 713 | ConsumeToken(); |
| 714 | } else if (Tok.getKind() == tok::l_paren) { |
| 715 | // direct-declarator: '(' declarator ')' |
| 716 | // direct-declarator: '(' attributes declarator ')' [TODO] |
| 717 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 718 | ParseParenDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 719 | } else if (D.mayOmitIdentifier()) { |
| 720 | // This could be something simple like "int" (in which case the declarator |
| 721 | // portion is empty), if an abstract-declarator is allowed. |
| 722 | D.SetIdentifier(0, Tok.getLocation()); |
| 723 | } else { |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 724 | // Expected identifier or '('. |
| 725 | Diag(Tok, diag::err_expected_ident_lparen); |
| 726 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | assert(D.isPastIdentifier() && |
| 730 | "Haven't past the location of the identifier yet?"); |
| 731 | |
| 732 | while (1) { |
| 733 | if (Tok.getKind() == tok::l_paren) { |
| 734 | ParseParenDeclarator(D); |
| 735 | } else if (Tok.getKind() == tok::l_square) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 736 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 737 | } else { |
| 738 | break; |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This may |
| 744 | /// either be before the identifier (in which case these are just grouping |
| 745 | /// parens for precedence) or it may be after the identifier, in which case |
| 746 | /// these are function arguments. |
| 747 | /// |
| 748 | /// This method also handles this portion of the grammar: |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 749 | /// parameter-type-list: [C99 6.7.5] |
| 750 | /// parameter-list |
| 751 | /// parameter-list ',' '...' |
| 752 | /// |
| 753 | /// parameter-list: [C99 6.7.5] |
| 754 | /// parameter-declaration |
| 755 | /// parameter-list ',' parameter-declaration |
| 756 | /// |
| 757 | /// parameter-declaration: [C99 6.7.5] |
| 758 | /// declaration-specifiers declarator |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 759 | /// [GNU] declaration-specifiers declarator attributes [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 760 | /// declaration-specifiers abstract-declarator[opt] |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 761 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 762 | /// |
| 763 | /// identifier-list: [C99 6.7.5] |
| 764 | /// identifier |
| 765 | /// identifier-list ',' identifier |
| 766 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 767 | void Parser::ParseParenDeclarator(Declarator &D) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 768 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 769 | ConsumeParen(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 770 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 771 | // If we haven't past the identifier yet (or where the identifier would be |
| 772 | // stored, if this is an abstract declarator), then this is probably just |
| 773 | // grouping parens. |
| 774 | if (!D.isPastIdentifier()) { |
| 775 | // Okay, this is probably a grouping paren. However, if this could be an |
| 776 | // abstract-declarator, then this could also be the start of function |
| 777 | // arguments (consider 'void()'). |
| 778 | bool isGrouping; |
| 779 | |
| 780 | if (!D.mayOmitIdentifier()) { |
| 781 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 782 | // paren, because we haven't seen the identifier yet. |
| 783 | isGrouping = true; |
| 784 | } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function. |
| 785 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 786 | |
| 787 | isGrouping = false; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 788 | } else { |
Chris Lattner | a350722 | 2006-08-07 00:33:37 +0000 | [diff] [blame] | 789 | // Otherwise, this is a grouping paren, e.g. 'int (*X)'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 790 | isGrouping = true; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 791 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 792 | |
| 793 | // If this is a grouping paren, handle: |
| 794 | // direct-declarator: '(' declarator ')' |
| 795 | // direct-declarator: '(' attributes declarator ')' [TODO] |
| 796 | if (isGrouping) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 797 | ParseDeclaratorInternal(D); |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 798 | // Match the ')'. |
| 799 | MatchRHSPunctuation(tok::r_paren, StartLoc, "(", |
| 800 | diag::err_expected_rparen); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 801 | return; |
| 802 | } |
| 803 | |
| 804 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
Chris Lattner | a350722 | 2006-08-07 00:33:37 +0000 | [diff] [blame] | 805 | // argument list. Recognize that this declarator will never have an |
| 806 | // identifier (and remember where it would have been), then fall through to |
| 807 | // the handling of argument lists. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 808 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 811 | // Okay, this is the parameter list of a function definition, or it is an |
| 812 | // identifier list of a K&R-style function. |
| 813 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 814 | // TODO: enter function-declaration scope, limiting any declarators for |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 815 | // arguments to the function scope. |
| 816 | // NOTE: better to only create a scope if not '()' |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 817 | bool IsVariadic; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 818 | bool HasPrototype; |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 819 | bool IsEmpty = false; |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 820 | bool ErrorEmitted = false; |
| 821 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 822 | if (Tok.getKind() == tok::r_paren) { |
| 823 | // int() -> no prototype, no '...'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 824 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 825 | HasPrototype = false; |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 826 | IsEmpty = true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 827 | } else if (Tok.getKind() == tok::identifier && |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 828 | 1/*TODO: !isatypedefname(Tok.getIdentifierInfo())*/) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 829 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 830 | // normal declarators, not for abstract-declarators. |
| 831 | assert(D.isPastIdentifier() && "Identifier (if present) must be passed!"); |
| 832 | |
| 833 | // If there was no identifier specified, either we are in an |
| 834 | // abstract-declarator, or we are in a parameter declarator which was found |
| 835 | // to be abstract. In abstract-declarators, identifier lists are not valid, |
| 836 | // diagnose this. |
| 837 | if (!D.getIdentifier()) |
| 838 | Diag(Tok, diag::ext_ident_list_in_param); |
| 839 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 840 | // TODO: Remember token. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 841 | ConsumeToken(); |
| 842 | while (Tok.getKind() == tok::comma) { |
| 843 | // Eat the comma. |
| 844 | ConsumeToken(); |
| 845 | |
Chris Lattner | 0be454e | 2006-08-12 19:30:51 +0000 | [diff] [blame] | 846 | if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) { |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 847 | ErrorEmitted = true; |
| 848 | break; |
| 849 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 852 | // K&R 'prototype'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 853 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 854 | HasPrototype = false; |
| 855 | } else { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 856 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 857 | bool ReadArg = false; |
| 858 | // Finally, a normal, non-empty parameter type list. |
| 859 | while (1) { |
| 860 | if (Tok.getKind() == tok::ellipsis) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 861 | IsVariadic = true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 862 | |
| 863 | // Check to see if this is "void(...)" which is not allowed. |
| 864 | if (!ReadArg) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 865 | // Otherwise, parse parameter type list. If it starts with an |
| 866 | // ellipsis, diagnose the malformed function. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 867 | Diag(Tok, diag::err_ellipsis_first_arg); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 868 | IsVariadic = false; // Treat this like 'void()'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | // Consume the ellipsis. |
| 872 | ConsumeToken(); |
| 873 | break; |
| 874 | } |
| 875 | |
| 876 | ReadArg = true; |
| 877 | |
| 878 | // Parse the declaration-specifiers. |
| 879 | DeclSpec DS; |
| 880 | ParseDeclarationSpecifiers(DS); |
| 881 | |
| 882 | // Parse the declarator. This is "PrototypeContext", because we must |
| 883 | // accept either 'declarator' or 'abstract-declarator' here. |
| 884 | Declarator DeclaratorInfo(DS, Declarator::PrototypeContext); |
| 885 | ParseDeclarator(DeclaratorInfo); |
| 886 | |
| 887 | // TODO: do something with the declarator, if it is valid. |
| 888 | |
| 889 | // If the next token is a comma, consume it and keep reading arguments. |
| 890 | if (Tok.getKind() != tok::comma) break; |
| 891 | |
| 892 | // Consume the comma. |
| 893 | ConsumeToken(); |
| 894 | } |
| 895 | |
| 896 | HasPrototype = true; |
| 897 | } |
| 898 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 899 | // TODO: pop the scope. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 900 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 901 | // TODO: capture argument info. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 902 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 903 | // Remember that we parsed a function type, and remember the attributes. |
| 904 | D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic, |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 905 | IsEmpty, StartLoc)); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 906 | |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 907 | |
| 908 | // If we have the closing ')', eat it and we're done. |
| 909 | if (Tok.getKind() == tok::r_paren) { |
| 910 | ConsumeParen(); |
| 911 | } else { |
| 912 | // If an error happened earlier parsing something else in the proto, don't |
| 913 | // issue another error. |
| 914 | if (!ErrorEmitted) |
| 915 | Diag(Tok, diag::err_expected_rparen); |
| 916 | SkipUntil(tok::r_paren); |
| 917 | } |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 918 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 919 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 920 | |
| 921 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 922 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 923 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 924 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 925 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 926 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 927 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 928 | ConsumeBracket(); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 929 | |
| 930 | // If valid, this location is the position where we read the 'static' keyword. |
| 931 | SourceLocation StaticLoc; |
| 932 | if (Tok.getKind() == tok::kw_static) { |
| 933 | StaticLoc = Tok.getLocation(); |
| 934 | ConsumeToken(); |
| 935 | } |
| 936 | |
| 937 | // If there is a type-qualifier-list, read it now. |
| 938 | DeclSpec DS; |
| 939 | ParseTypeQualifierListOpt(DS); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 940 | |
| 941 | // If we haven't already read 'static', check to see if there is one after the |
| 942 | // type-qualifier-list. |
| 943 | if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) { |
| 944 | StaticLoc = Tok.getLocation(); |
| 945 | ConsumeToken(); |
| 946 | } |
| 947 | |
| 948 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 949 | bool isStar = false; |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 950 | ExprResult NumElements(false); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 951 | if (Tok.getKind() == tok::star) { |
| 952 | // Remember the '*' token, in case we have to un-get it. |
| 953 | LexerToken StarTok = Tok; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 954 | ConsumeToken(); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 955 | |
| 956 | // Check that the ']' token is present to avoid incorrectly parsing |
| 957 | // expressions starting with '*' as [*]. |
| 958 | if (Tok.getKind() == tok::r_square) { |
| 959 | if (StaticLoc.isValid()) |
| 960 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
| 961 | StaticLoc = SourceLocation(); // Drop the static. |
| 962 | isStar = true; |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 963 | } else { |
| 964 | // Otherwise, the * must have been some expression (such as '*ptr') that |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 965 | // started an assignment-expr. We already consumed the token, but now we |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 966 | // need to reparse it. This handles cases like 'X[*p + 4]' |
| 967 | NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 968 | } |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 969 | } else if (Tok.getKind() != tok::r_square) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 970 | // Parse the assignment-expression now. |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 971 | NumElements = ParseAssignmentExpression(); |
| 972 | } |
| 973 | |
| 974 | // If there was an error parsing the assignment-expression, recover. |
| 975 | if (NumElements.isInvalid) { |
| 976 | // If the expression was invalid, skip it. |
| 977 | SkipUntil(tok::r_square); |
| 978 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 981 | MatchRHSPunctuation(tok::r_square, StartLoc, "[", diag::err_expected_rsquare); |
| 982 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 983 | // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if |
| 984 | // it was not a constant expression. |
| 985 | if (!getLang().C99) { |
| 986 | // TODO: check C90 array constant exprness. |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 987 | if (isStar || StaticLoc.isValid() || |
| 988 | 0/*TODO: NumElts is not a C90 constantexpr */) |
Chris Lattner | 8a39edc | 2006-08-06 18:33:32 +0000 | [diff] [blame] | 989 | Diag(StartLoc, diag::ext_c99_array_usage); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 990 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 991 | |
| 992 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 993 | D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers, |
| 994 | StaticLoc.isValid(), isStar, |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 995 | NumElements.Val, StartLoc)); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 996 | } |
| 997 | |