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