Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1 | //===--- ParserDeclarations.cpp - Declaration Parsing ---------------------===// |
| 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 | |
| 23 | /// ParseDeclarationSpecifiers |
| 24 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 25 | /// storage-class-specifier declaration-specifiers [opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 26 | /// type-specifier declaration-specifiers [opt] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 27 | /// type-qualifier declaration-specifiers [opt] |
| 28 | /// [C99] function-specifier declaration-specifiers [opt] |
| 29 | /// [GNU] attributes declaration-specifiers [opt] [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 30 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 31 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 32 | /// 'typedef' |
| 33 | /// 'extern' |
| 34 | /// 'static' |
| 35 | /// 'auto' |
| 36 | /// 'register' |
| 37 | /// [GNU] '__thread' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 38 | /// type-specifier: [C99 6.7.2] |
| 39 | /// 'void' |
| 40 | /// 'char' |
| 41 | /// 'short' |
| 42 | /// 'int' |
| 43 | /// 'long' |
| 44 | /// 'float' |
| 45 | /// 'double' |
| 46 | /// 'signed' |
| 47 | /// 'unsigned' |
Chris Lattner | 8e90ef6 | 2006-08-05 03:30:45 +0000 | [diff] [blame] | 48 | /// struct-or-union-specifier [TODO] |
| 49 | /// enum-specifier [TODO] |
| 50 | /// typedef-name [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 51 | /// [C99] '_Bool' |
| 52 | /// [C99] '_Complex' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 53 | /// [C99] '_Imaginary' // Removed in TC2? |
| 54 | /// [GNU] '_Decimal32' |
| 55 | /// [GNU] '_Decimal64' |
| 56 | /// [GNU] '_Decimal128' |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 57 | /// [GNU] typeof-specifier [TODO] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 58 | /// [OBJC] class-name objc-protocol-refs [opt] [TODO] |
| 59 | /// [OBJC] typedef-name objc-protocol-refs [TODO] |
| 60 | /// [OBJC] objc-protocol-refs [TODO] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 61 | /// type-qualifier: |
| 62 | /// const |
| 63 | /// volatile |
| 64 | /// [C99] restrict |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 65 | /// function-specifier: [C99 6.7.4] |
| 66 | /// [C99] inline |
| 67 | /// |
| 68 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) { |
| 69 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 70 | while (1) { |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 71 | int isInvalid = false; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 72 | const char *PrevSpec = 0; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 73 | switch (Tok.getKind()) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 74 | default: |
| 75 | // If this is not a declaration specifier token, we're done reading decl |
| 76 | // specifiers. First verify that DeclSpec's are consistent. |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 77 | DS.Finish(StartLoc, Diags, getLang()); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 78 | return; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 79 | |
| 80 | // storage-class-specifier |
| 81 | case tok::kw_typedef: |
| 82 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec); |
| 83 | break; |
| 84 | case tok::kw_extern: |
| 85 | if (DS.SCS_thread_specified) |
| 86 | Diag(Tok, diag::ext_thread_before, "extern"); |
| 87 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec); |
| 88 | break; |
| 89 | case tok::kw_static: |
| 90 | if (DS.SCS_thread_specified) |
| 91 | Diag(Tok, diag::ext_thread_before, "static"); |
| 92 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec); |
| 93 | break; |
| 94 | case tok::kw_auto: |
| 95 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec); |
| 96 | break; |
| 97 | case tok::kw_register: |
| 98 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec); |
| 99 | break; |
| 100 | case tok::kw___thread: |
| 101 | if (DS.SCS_thread_specified) |
| 102 | isInvalid = 2, PrevSpec = "__thread"; |
| 103 | else |
| 104 | DS.SCS_thread_specified = true; |
| 105 | break; |
| 106 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 107 | // type-specifiers |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 108 | case tok::kw_short: |
| 109 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec); |
| 110 | break; |
| 111 | case tok::kw_long: |
| 112 | if (DS.TypeSpecWidth != DeclSpec::TSW_long) { |
| 113 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec); |
| 114 | } else { |
| 115 | DS.TypeSpecWidth = DeclSpec::TSW_unspecified; |
| 116 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec); |
| 117 | } |
| 118 | break; |
| 119 | case tok::kw_signed: |
| 120 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec); |
| 121 | break; |
| 122 | case tok::kw_unsigned: |
| 123 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec); |
| 124 | break; |
| 125 | case tok::kw__Complex: |
| 126 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec); |
| 127 | break; |
| 128 | case tok::kw__Imaginary: |
| 129 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec); |
| 130 | break; |
| 131 | case tok::kw_void: |
| 132 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec); |
| 133 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 134 | case tok::kw_char: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 135 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec); |
| 136 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 137 | case tok::kw_int: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 138 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec); |
| 139 | break; |
| 140 | case tok::kw_float: |
| 141 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec); |
| 142 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 143 | case tok::kw_double: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 144 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec); |
| 145 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 146 | case tok::kw__Bool: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 147 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec); |
| 148 | break; |
| 149 | case tok::kw__Decimal32: |
| 150 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec); |
| 151 | break; |
| 152 | case tok::kw__Decimal64: |
| 153 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec); |
| 154 | break; |
| 155 | case tok::kw__Decimal128: |
| 156 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 157 | break; |
| 158 | |
| 159 | //case tok::kw_struct: |
| 160 | //case tok::kw_union: |
| 161 | //case tok::kw_enum: |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 162 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 163 | //case tok::identifier: |
| 164 | // TODO: handle typedef names. |
| 165 | |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 166 | // type-qualifier |
| 167 | case tok::kw_const: |
| 168 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2; |
| 169 | break; |
| 170 | case tok::kw_volatile: |
| 171 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2; |
| 172 | break; |
| 173 | case tok::kw_restrict: |
| 174 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2; |
| 175 | break; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 176 | |
| 177 | // function-specifier |
| 178 | case tok::kw_inline: |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 179 | // 'inline inline' is ok. |
| 180 | DS.FS_inline_specified = true; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 181 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 182 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 183 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 184 | if (isInvalid) { |
| 185 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 186 | if (isInvalid == 1) // Error. |
| 187 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 188 | else // extwarn. |
| 189 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 190 | } |
| 191 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 195 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 196 | /// declaration specifier. |
| 197 | bool Parser::isDeclarationSpecifier() const { |
| 198 | switch (Tok.getKind()) { |
| 199 | default: return false; |
| 200 | // storage-class-specifier |
| 201 | case tok::kw_typedef: |
| 202 | case tok::kw_extern: |
| 203 | case tok::kw_static: |
| 204 | case tok::kw_auto: |
| 205 | case tok::kw_register: |
| 206 | case tok::kw___thread: |
| 207 | |
| 208 | // type-specifiers |
| 209 | case tok::kw_short: |
| 210 | case tok::kw_long: |
| 211 | case tok::kw_signed: |
| 212 | case tok::kw_unsigned: |
| 213 | case tok::kw__Complex: |
| 214 | case tok::kw__Imaginary: |
| 215 | case tok::kw_void: |
| 216 | case tok::kw_char: |
| 217 | case tok::kw_int: |
| 218 | case tok::kw_float: |
| 219 | case tok::kw_double: |
| 220 | case tok::kw__Bool: |
| 221 | case tok::kw__Decimal32: |
| 222 | case tok::kw__Decimal64: |
| 223 | case tok::kw__Decimal128: |
| 224 | |
| 225 | // struct-or-union-specifier |
| 226 | case tok::kw_struct: |
| 227 | case tok::kw_union: |
| 228 | // enum-specifier |
| 229 | case tok::kw_enum: |
| 230 | // type-qualifier |
| 231 | case tok::kw_const: |
| 232 | case tok::kw_volatile: |
| 233 | case tok::kw_restrict: |
| 234 | // function-specifier |
| 235 | case tok::kw_inline: |
| 236 | return true; |
| 237 | // typedef-name |
| 238 | case tok::identifier: |
| 239 | // FIXME: if this is a typedef return true. |
| 240 | return false; |
| 241 | // TODO: Attributes. |
| 242 | } |
| 243 | } |
| 244 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 245 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 246 | /// ParseTypeQualifierListOpt |
| 247 | /// type-qualifier-list: [C99 6.7.5] |
| 248 | /// type-qualifier |
| 249 | /// [GNU] attributes [TODO] |
| 250 | /// type-qualifier-list type-qualifier |
| 251 | /// [GNU] type-qualifier-list attributes [TODO] |
| 252 | /// |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 253 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) { |
| 254 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 255 | while (1) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 256 | int isInvalid = false; |
| 257 | const char *PrevSpec = 0; |
| 258 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 259 | switch (Tok.getKind()) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 260 | default: |
| 261 | // If this is not a declaration specifier token, we're done reading decl |
| 262 | // specifiers. First verify that DeclSpec's are consistent. |
| 263 | DS.Finish(StartLoc, Diags, getLang()); |
| 264 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 265 | // TODO: attributes. |
| 266 | case tok::kw_const: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 267 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2; |
| 268 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 269 | case tok::kw_volatile: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 270 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2; |
| 271 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 272 | case tok::kw_restrict: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 273 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 274 | break; |
| 275 | } |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 276 | |
| 277 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 278 | if (isInvalid) { |
| 279 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 280 | if (isInvalid == 1) // Error. |
| 281 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 282 | else // extwarn. |
| 283 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 284 | } |
| 285 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 289 | |
| 290 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 291 | /// |
| 292 | void Parser::ParseDeclarator(Declarator &D) { |
| 293 | /// This implements the 'declarator' production in the C grammar, then checks |
| 294 | /// for well-formedness and issues diagnostics. |
| 295 | ParseDeclaratorInternal(D); |
| 296 | |
| 297 | // FIXME: validate D. |
| 298 | |
| 299 | |
| 300 | } |
| 301 | |
| 302 | /// ParseDeclaratorInternal |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 303 | /// declarator: [C99 6.7.5] |
| 304 | /// pointer[opt] direct-declarator |
| 305 | /// |
| 306 | /// pointer: [C99 6.7.5] |
| 307 | /// '*' type-qualifier-list[opt] |
| 308 | /// '*' type-qualifier-list[opt] pointer |
| 309 | /// |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 310 | void Parser::ParseDeclaratorInternal(Declarator &D) { |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 311 | if (Tok.getKind() != tok::star) |
| 312 | return ParseDirectDeclarator(D); |
| 313 | |
| 314 | // Otherwise, '*' -> pointer. |
| 315 | SourceLocation Loc = Tok.getLocation(); |
| 316 | ConsumeToken(); // Eat the *. |
| 317 | DeclSpec DS; |
| 318 | ParseTypeQualifierListOpt(DS); |
| 319 | |
| 320 | // Recursively parse the declarator. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 321 | ParseDeclaratorInternal(D); |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 322 | |
| 323 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 324 | D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc)); |
| 325 | } |
| 326 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 327 | |
| 328 | /// ParseDirectDeclarator |
| 329 | /// direct-declarator: [C99 6.7.5] |
| 330 | /// identifier |
| 331 | /// '(' declarator ')' |
| 332 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 333 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 334 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 335 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 336 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 337 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 338 | /// direct-declarator '(' parameter-type-list ')' |
| 339 | /// direct-declarator '(' identifier-list[opt] ')' |
| 340 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 341 | /// parameter-type-list[opt] ')' |
| 342 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 343 | void Parser::ParseDirectDeclarator(Declarator &D) { |
| 344 | // Parse the first direct-declarator seen. |
| 345 | if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) { |
| 346 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 347 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 348 | ConsumeToken(); |
| 349 | } else if (Tok.getKind() == tok::l_paren) { |
| 350 | // direct-declarator: '(' declarator ')' |
| 351 | // direct-declarator: '(' attributes declarator ')' [TODO] |
| 352 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 353 | ParseParenDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 354 | } else if (D.mayOmitIdentifier()) { |
| 355 | // This could be something simple like "int" (in which case the declarator |
| 356 | // portion is empty), if an abstract-declarator is allowed. |
| 357 | D.SetIdentifier(0, Tok.getLocation()); |
| 358 | } else { |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 359 | // Expected identifier or '('. |
| 360 | Diag(Tok, diag::err_expected_ident_lparen); |
| 361 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | assert(D.isPastIdentifier() && |
| 365 | "Haven't past the location of the identifier yet?"); |
| 366 | |
| 367 | while (1) { |
| 368 | if (Tok.getKind() == tok::l_paren) { |
| 369 | ParseParenDeclarator(D); |
| 370 | } else if (Tok.getKind() == tok::l_square) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 371 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 372 | } else { |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This may |
| 379 | /// either be before the identifier (in which case these are just grouping |
| 380 | /// parens for precedence) or it may be after the identifier, in which case |
| 381 | /// these are function arguments. |
| 382 | /// |
| 383 | /// This method also handles this portion of the grammar: |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 384 | /// parameter-type-list: [C99 6.7.5] |
| 385 | /// parameter-list |
| 386 | /// parameter-list ',' '...' |
| 387 | /// |
| 388 | /// parameter-list: [C99 6.7.5] |
| 389 | /// parameter-declaration |
| 390 | /// parameter-list ',' parameter-declaration |
| 391 | /// |
| 392 | /// parameter-declaration: [C99 6.7.5] |
| 393 | /// declaration-specifiers declarator |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 394 | /// [GNU] declaration-specifiers declarator attributes [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 395 | /// declaration-specifiers abstract-declarator[opt] |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 396 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 397 | /// |
| 398 | /// identifier-list: [C99 6.7.5] |
| 399 | /// identifier |
| 400 | /// identifier-list ',' identifier |
| 401 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 402 | void Parser::ParseParenDeclarator(Declarator &D) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 403 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 404 | ConsumeParen(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 405 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 406 | // If we haven't past the identifier yet (or where the identifier would be |
| 407 | // stored, if this is an abstract declarator), then this is probably just |
| 408 | // grouping parens. |
| 409 | if (!D.isPastIdentifier()) { |
| 410 | // Okay, this is probably a grouping paren. However, if this could be an |
| 411 | // abstract-declarator, then this could also be the start of function |
| 412 | // arguments (consider 'void()'). |
| 413 | bool isGrouping; |
| 414 | |
| 415 | if (!D.mayOmitIdentifier()) { |
| 416 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 417 | // paren, because we haven't seen the identifier yet. |
| 418 | isGrouping = true; |
| 419 | } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function. |
| 420 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 421 | |
| 422 | isGrouping = false; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 423 | } else { |
Chris Lattner | a350722 | 2006-08-07 00:33:37 +0000 | [diff] [blame] | 424 | // Otherwise, this is a grouping paren, e.g. 'int (*X)'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 425 | isGrouping = true; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 426 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 427 | |
| 428 | // If this is a grouping paren, handle: |
| 429 | // direct-declarator: '(' declarator ')' |
| 430 | // direct-declarator: '(' attributes declarator ')' [TODO] |
| 431 | if (isGrouping) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 432 | ParseDeclaratorInternal(D); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 433 | if (Tok.getKind() == tok::r_paren) { |
| 434 | ConsumeParen(); |
| 435 | } else { |
| 436 | // expected ')': skip until we find ')'. |
| 437 | Diag(Tok, diag::err_expected_rparen); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 438 | Diag(StartLoc, diag::err_matching); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 439 | SkipUntil(tok::r_paren); |
| 440 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 441 | return; |
| 442 | } |
| 443 | |
| 444 | // 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] | 445 | // argument list. Recognize that this declarator will never have an |
| 446 | // identifier (and remember where it would have been), then fall through to |
| 447 | // the handling of argument lists. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 448 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 451 | // Okay, this is the parameter list of a function definition, or it is an |
| 452 | // identifier list of a K&R-style function. |
| 453 | |
| 454 | // FIXME: enter function-declaration scope, limiting any declarators for |
| 455 | // arguments to the function scope. |
| 456 | // NOTE: better to only create a scope if not '()' |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 457 | bool IsVariadic; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 458 | bool HasPrototype; |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 459 | bool ErrorEmitted = false; |
| 460 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 461 | if (Tok.getKind() == tok::r_paren) { |
| 462 | // int() -> no prototype, no '...'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 463 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 464 | HasPrototype = false; |
| 465 | } else if (Tok.getKind() == tok::identifier && |
| 466 | 0/*TODO: !isatypedefname(Tok.getIdentifierInfo())*/) { |
| 467 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 468 | // normal declarators, not for abstract-declarators. |
| 469 | assert(D.isPastIdentifier() && "Identifier (if present) must be passed!"); |
| 470 | |
| 471 | // If there was no identifier specified, either we are in an |
| 472 | // abstract-declarator, or we are in a parameter declarator which was found |
| 473 | // to be abstract. In abstract-declarators, identifier lists are not valid, |
| 474 | // diagnose this. |
| 475 | if (!D.getIdentifier()) |
| 476 | Diag(Tok, diag::ext_ident_list_in_param); |
| 477 | |
| 478 | // FIXME: Remember token. |
| 479 | ConsumeToken(); |
| 480 | while (Tok.getKind() == tok::comma) { |
| 481 | // Eat the comma. |
| 482 | ConsumeToken(); |
| 483 | |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 484 | if (Tok.getKind() != tok::identifier) { |
| 485 | // If not identifier, diagnose the error. |
| 486 | Diag(Tok, diag::err_expected_ident); |
| 487 | ErrorEmitted = true; |
| 488 | break; |
| 489 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 490 | |
| 491 | // Eat the id. |
| 492 | // FIXME: remember it! |
| 493 | ConsumeToken(); |
| 494 | } |
| 495 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 496 | // K&R 'prototype'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 497 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 498 | HasPrototype = false; |
| 499 | } else { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 500 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 501 | bool ReadArg = false; |
| 502 | // Finally, a normal, non-empty parameter type list. |
| 503 | while (1) { |
| 504 | if (Tok.getKind() == tok::ellipsis) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 505 | IsVariadic = true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 506 | |
| 507 | // Check to see if this is "void(...)" which is not allowed. |
| 508 | if (!ReadArg) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 509 | // Otherwise, parse parameter type list. If it starts with an |
| 510 | // ellipsis, diagnose the malformed function. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 511 | Diag(Tok, diag::err_ellipsis_first_arg); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 512 | IsVariadic = false; // Treat this like 'void()'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | // Consume the ellipsis. |
| 516 | ConsumeToken(); |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | ReadArg = true; |
| 521 | |
| 522 | // Parse the declaration-specifiers. |
| 523 | DeclSpec DS; |
| 524 | ParseDeclarationSpecifiers(DS); |
| 525 | |
| 526 | // Parse the declarator. This is "PrototypeContext", because we must |
| 527 | // accept either 'declarator' or 'abstract-declarator' here. |
| 528 | Declarator DeclaratorInfo(DS, Declarator::PrototypeContext); |
| 529 | ParseDeclarator(DeclaratorInfo); |
| 530 | |
| 531 | // TODO: do something with the declarator, if it is valid. |
| 532 | |
| 533 | // If the next token is a comma, consume it and keep reading arguments. |
| 534 | if (Tok.getKind() != tok::comma) break; |
| 535 | |
| 536 | // Consume the comma. |
| 537 | ConsumeToken(); |
| 538 | } |
| 539 | |
| 540 | HasPrototype = true; |
| 541 | } |
| 542 | |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 543 | // FIXME: pop the scope. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 544 | |
| 545 | // FIXME: capture argument info. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 546 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame^] | 547 | // Remember that we parsed a function type, and remember the attributes. |
| 548 | D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic, |
| 549 | StartLoc)); |
| 550 | |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 551 | |
| 552 | // If we have the closing ')', eat it and we're done. |
| 553 | if (Tok.getKind() == tok::r_paren) { |
| 554 | ConsumeParen(); |
| 555 | } else { |
| 556 | // If an error happened earlier parsing something else in the proto, don't |
| 557 | // issue another error. |
| 558 | if (!ErrorEmitted) |
| 559 | Diag(Tok, diag::err_expected_rparen); |
| 560 | SkipUntil(tok::r_paren); |
| 561 | } |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 562 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 563 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 564 | |
| 565 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 566 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 567 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 568 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 569 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 570 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 571 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 572 | ConsumeBracket(); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 573 | |
| 574 | // If valid, this location is the position where we read the 'static' keyword. |
| 575 | SourceLocation StaticLoc; |
| 576 | if (Tok.getKind() == tok::kw_static) { |
| 577 | StaticLoc = Tok.getLocation(); |
| 578 | ConsumeToken(); |
| 579 | } |
| 580 | |
| 581 | // If there is a type-qualifier-list, read it now. |
| 582 | DeclSpec DS; |
| 583 | ParseTypeQualifierListOpt(DS); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 584 | |
| 585 | // If we haven't already read 'static', check to see if there is one after the |
| 586 | // type-qualifier-list. |
| 587 | if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) { |
| 588 | StaticLoc = Tok.getLocation(); |
| 589 | ConsumeToken(); |
| 590 | } |
| 591 | |
| 592 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 593 | bool isStar = false; |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 594 | if (Tok.getKind() == tok::star) { |
| 595 | // Remember the '*' token, in case we have to un-get it. |
| 596 | LexerToken StarTok = Tok; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 597 | ConsumeToken(); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 598 | |
| 599 | // Check that the ']' token is present to avoid incorrectly parsing |
| 600 | // expressions starting with '*' as [*]. |
| 601 | if (Tok.getKind() == tok::r_square) { |
| 602 | if (StaticLoc.isValid()) |
| 603 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
| 604 | StaticLoc = SourceLocation(); // Drop the static. |
| 605 | isStar = true; |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 606 | } else { |
| 607 | // Otherwise, the * must have been some expression (such as '*ptr') that |
| 608 | // started an assign-expr. We already consumed the token, but now we need |
| 609 | // to reparse it. |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 610 | // FIXME: We must push 'StarTok' and Tok back into the preprocessor as a |
| 611 | // macro expansion context, so they will be read again. It is basically |
| 612 | // impossible to refudge the * in otherwise, due to cases like X[*p + 4]. |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 613 | assert(0 && "FIXME: int X[*p] unimplemented"); |
| 614 | } |
| 615 | } |
| 616 | |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 617 | void *NumElts = 0; |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 618 | if (!isStar && Tok.getKind() != tok::r_square) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 619 | // Parse the assignment-expression now. |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 620 | NumElts = /*FIXME: parse array size expr*/0; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 621 | assert(0 && "expr parsing not impl yet!"); |
| 622 | } |
| 623 | |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 624 | ConsumeBracket(); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 625 | |
| 626 | // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if |
| 627 | // it was not a constant expression. |
| 628 | if (!getLang().C99) { |
| 629 | // TODO: check C90 array constant exprness. |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 630 | if (isStar || StaticLoc.isValid() || 0/*NumElts is constantexpr*/) |
Chris Lattner | 8a39edc | 2006-08-06 18:33:32 +0000 | [diff] [blame] | 631 | Diag(StartLoc, diag::ext_c99_array_usage); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 632 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 633 | |
| 634 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 635 | D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers, |
| 636 | StaticLoc.isValid(), isStar, |
| 637 | NumElts, StartLoc)); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 638 | } |
| 639 | |