Chris Lattner | 7ad0fbe | 2006-11-05 07:46:30 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.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 | 288e86ff1 | 2006-11-11 23:03:42 +0000 | [diff] [blame] | 15 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | ad9ac94 | 2007-01-23 01:14:52 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 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] |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 26 | Parser::TypeTy *Parser::ParseTypeName() { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 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); |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 558cb29 | 2006-11-19 01:31:06 +0000 | [diff] [blame] | 35 | return Actions.ParseTypeName(CurScope, DeclaratorInfo).Val; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 38 | /// ParseAttributes - Parse a non-empty attributes list. |
| 39 | /// |
| 40 | /// [GNU] attributes: |
| 41 | /// attribute |
| 42 | /// attributes attribute |
| 43 | /// |
| 44 | /// [GNU] attribute: |
| 45 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 46 | /// |
| 47 | /// [GNU] attribute-list: |
| 48 | /// attrib |
| 49 | /// attribute_list ',' attrib |
| 50 | /// |
| 51 | /// [GNU] attrib: |
| 52 | /// empty |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 53 | /// attrib-name |
| 54 | /// attrib-name '(' identifier ')' |
| 55 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 56 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 57 | /// |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 58 | /// [GNU] attrib-name: |
| 59 | /// identifier |
| 60 | /// typespec |
| 61 | /// typequal |
| 62 | /// storageclass |
| 63 | /// |
| 64 | /// FIXME: The GCC grammar/code for this construct implies we need two |
| 65 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 66 | /// which is followed by a comma or close parenthesis, then the arguments |
| 67 | /// start with that identifier; otherwise they are an expression list." |
| 68 | /// |
| 69 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 70 | /// any attributes that don't work (based on my limited testing). Most |
| 71 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 72 | /// a pressing need to implement the 2 token lookahead. |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 73 | |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 74 | AttributeList *Parser::ParseAttributes() { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 75 | assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!"); |
| 76 | |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 77 | AttributeList *CurrAttr = 0; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 78 | |
| 79 | while (Tok.getKind() == tok::kw___attribute) { |
| 80 | ConsumeToken(); |
| 81 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 82 | "attribute")) { |
| 83 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 84 | return CurrAttr; |
| 85 | } |
| 86 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 87 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 88 | return CurrAttr; |
| 89 | } |
| 90 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
| 91 | while (Tok.getKind() == tok::identifier || isDeclarationSpecifier() || |
| 92 | Tok.getKind() == tok::comma) { |
| 93 | |
| 94 | if (Tok.getKind() == tok::comma) { |
| 95 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 96 | ConsumeToken(); |
| 97 | continue; |
| 98 | } |
| 99 | // we have an identifier or declaration specifier (const, int, etc.) |
| 100 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 101 | SourceLocation AttrNameLoc = ConsumeToken(); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 102 | |
| 103 | // check if we have a "paramterized" attribute |
| 104 | if (Tok.getKind() == tok::l_paren) { |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 105 | ConsumeParen(); // ignore the left paren loc for now |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 106 | |
| 107 | if (Tok.getKind() == tok::identifier) { |
| 108 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 109 | SourceLocation ParmLoc = ConsumeToken(); |
| 110 | |
| 111 | if (Tok.getKind() == tok::r_paren) { |
| 112 | // __attribute__(( mode(byte) )) |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 113 | ConsumeParen(); // ignore the right paren loc for now |
| 114 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 115 | ParmName, ParmLoc, 0, 0, CurrAttr); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 116 | } else if (Tok.getKind() == tok::comma) { |
| 117 | ConsumeToken(); |
| 118 | // __attribute__(( format(printf, 1, 2) )) |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 119 | llvm::SmallVector<ExprTy*, 8> ArgExprs; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 120 | bool ArgExprsOk = true; |
| 121 | |
| 122 | // now parse the non-empty comma separated list of expressions |
| 123 | while (1) { |
| 124 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 125 | if (ArgExpr.isInvalid) { |
| 126 | ArgExprsOk = false; |
| 127 | SkipUntil(tok::r_paren); |
| 128 | break; |
| 129 | } else { |
| 130 | ArgExprs.push_back(ArgExpr.Val); |
| 131 | } |
| 132 | if (Tok.getKind() != tok::comma) |
| 133 | break; |
| 134 | ConsumeToken(); // Eat the comma, move to the next argument |
| 135 | } |
| 136 | if (ArgExprsOk && Tok.getKind() == tok::r_paren) { |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 137 | ConsumeParen(); // ignore the right paren loc for now |
| 138 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName, |
| 139 | ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } else { // not an identifier |
| 143 | // parse a possibly empty comma separated list of expressions |
| 144 | if (Tok.getKind() == tok::r_paren) { |
| 145 | // __attribute__(( nonnull() )) |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 146 | ConsumeParen(); // ignore the right paren loc for now |
| 147 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 148 | 0, SourceLocation(), 0, 0, CurrAttr); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 149 | } else { |
| 150 | // __attribute__(( aligned(16) )) |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 151 | llvm::SmallVector<ExprTy*, 8> ArgExprs; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 152 | bool ArgExprsOk = true; |
| 153 | |
| 154 | // now parse the list of expressions |
| 155 | while (1) { |
| 156 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 157 | if (ArgExpr.isInvalid) { |
| 158 | ArgExprsOk = false; |
| 159 | SkipUntil(tok::r_paren); |
| 160 | break; |
| 161 | } else { |
| 162 | ArgExprs.push_back(ArgExpr.Val); |
| 163 | } |
| 164 | if (Tok.getKind() != tok::comma) |
| 165 | break; |
| 166 | ConsumeToken(); // Eat the comma, move to the next argument |
| 167 | } |
| 168 | // Match the ')'. |
| 169 | if (ArgExprsOk && Tok.getKind() == tok::r_paren) { |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 170 | ConsumeParen(); // ignore the right paren loc for now |
| 171 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
| 172 | SourceLocation(), &ArgExprs[0], ArgExprs.size(), |
| 173 | CurrAttr); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | } else { |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 178 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 179 | 0, SourceLocation(), 0, 0, CurrAttr); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
Steve Naroff | 98d153c | 2007-06-06 23:19:11 +0000 | [diff] [blame] | 182 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 183 | SkipUntil(tok::r_paren, false); |
| 184 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 185 | SkipUntil(tok::r_paren, false); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 186 | } |
| 187 | return CurrAttr; |
| 188 | } |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 190 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 191 | /// declaration-specifiers, some number of declarators, and a semicolon. |
| 192 | /// 'Context' should be a Declarator::TheContext value. |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 193 | Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 194 | // Parse the common declaration-specifiers piece. |
| 195 | DeclSpec DS; |
| 196 | ParseDeclarationSpecifiers(DS); |
| 197 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 198 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 199 | // declaration-specifiers init-declarator-list[opt] ';' |
| 200 | if (Tok.getKind() == tok::semi) { |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 201 | ConsumeToken(); |
Chris Lattner | 200bdc3 | 2006-11-19 02:43:37 +0000 | [diff] [blame] | 202 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 205 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 206 | ParseDeclarator(DeclaratorInfo); |
| 207 | |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 208 | return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 211 | /// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after |
| 212 | /// parsing 'declaration-specifiers declarator'. This method is split out this |
| 213 | /// way to handle the ambiguity between top-level function-definitions and |
| 214 | /// declarations. |
| 215 | /// |
| 216 | /// declaration: [C99 6.7] |
| 217 | /// declaration-specifiers init-declarator-list[opt] ';' [TODO] |
| 218 | /// [!C99] init-declarator-list ';' [TODO] |
| 219 | /// [OMP] threadprivate-directive [TODO] |
| 220 | /// |
| 221 | /// init-declarator-list: [C99 6.7] |
| 222 | /// init-declarator |
| 223 | /// init-declarator-list ',' init-declarator |
| 224 | /// init-declarator: [C99 6.7] |
| 225 | /// declarator |
| 226 | /// declarator '=' initializer |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 227 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 228 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 229 | /// |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 230 | Parser::DeclTy *Parser:: |
| 231 | ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { |
| 232 | |
| 233 | // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so |
| 234 | // that they can be chained properly if the actions want this. |
| 235 | Parser::DeclTy *LastDeclInGroup = 0; |
| 236 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 237 | // At this point, we know that it is not a function definition. Parse the |
| 238 | // rest of the init-declarator-list. |
| 239 | while (1) { |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 240 | // If a simple-asm-expr is present, parse it. |
| 241 | if (Tok.getKind() == tok::kw_asm) |
| 242 | ParseSimpleAsm(); |
| 243 | |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 244 | // If attributes are present, parse them. |
| 245 | if (Tok.getKind() == tok::kw___attribute) |
Steve Naroff | 0f05a7a | 2007-06-09 23:38:17 +0000 | [diff] [blame] | 246 | D.AddAttributes(ParseAttributes()); |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 248 | // Parse declarator '=' initializer. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 249 | ExprResult Init; |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 250 | if (Tok.getKind() == tok::equal) { |
| 251 | ConsumeToken(); |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 252 | Init = ParseInitializer(); |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 253 | if (Init.isInvalid) { |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 254 | SkipUntil(tok::semi); |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 255 | return 0; |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 256 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame] | 259 | // Inform the current actions module that we just parsed this declarator. |
Chris Lattner | 289ab7b | 2006-11-08 06:54:53 +0000 | [diff] [blame] | 260 | // FIXME: pass asm & attributes. |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 261 | LastDeclInGroup = Actions.ParseDeclarator(CurScope, D, Init.Val, |
| 262 | LastDeclInGroup); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 263 | |
| 264 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 265 | // error, bail out. |
| 266 | if (Tok.getKind() != tok::comma) |
| 267 | break; |
| 268 | |
| 269 | // Consume the comma. |
| 270 | ConsumeToken(); |
| 271 | |
| 272 | // Parse the next declarator. |
| 273 | D.clear(); |
| 274 | ParseDeclarator(D); |
| 275 | } |
| 276 | |
| 277 | if (Tok.getKind() == tok::semi) { |
| 278 | ConsumeToken(); |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 279 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 280 | } |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 281 | |
| 282 | Diag(Tok, diag::err_parse_error); |
| 283 | // Skip to end of block or statement |
Chris Lattner | 43ba251 | 2007-08-21 18:36:18 +0000 | [diff] [blame] | 284 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 285 | if (Tok.getKind() == tok::semi) |
| 286 | ConsumeToken(); |
| 287 | return 0; |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 290 | /// ParseSpecifierQualifierList |
| 291 | /// specifier-qualifier-list: |
| 292 | /// type-specifier specifier-qualifier-list[opt] |
| 293 | /// type-qualifier specifier-qualifier-list[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 294 | /// [GNU] attributes specifier-qualifier-list[opt] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 295 | /// |
| 296 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 297 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 298 | /// parse declaration-specifiers and complain about extra stuff. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 299 | ParseDeclarationSpecifiers(DS); |
| 300 | |
| 301 | // Validate declspec for type-name. |
| 302 | unsigned Specs = DS.getParsedSpecifiers(); |
| 303 | if (Specs == DeclSpec::PQ_None) |
| 304 | Diag(Tok, diag::err_typename_requires_specqual); |
| 305 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 306 | // Issue diagnostic and remove storage class if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 307 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 308 | if (DS.getStorageClassSpecLoc().isValid()) |
| 309 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 310 | else |
| 311 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 312 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 314 | |
| 315 | // Issue diagnostic and remove function specfier if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 316 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 317 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 318 | DS.ClearFunctionSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 321 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 322 | /// ParseDeclarationSpecifiers |
| 323 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 324 | /// storage-class-specifier declaration-specifiers[opt] |
| 325 | /// type-specifier declaration-specifiers[opt] |
| 326 | /// type-qualifier declaration-specifiers[opt] |
| 327 | /// [C99] function-specifier declaration-specifiers[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 328 | /// [GNU] attributes declaration-specifiers[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 329 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 330 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 331 | /// 'typedef' |
| 332 | /// 'extern' |
| 333 | /// 'static' |
| 334 | /// 'auto' |
| 335 | /// 'register' |
| 336 | /// [GNU] '__thread' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 337 | /// type-specifier: [C99 6.7.2] |
| 338 | /// 'void' |
| 339 | /// 'char' |
| 340 | /// 'short' |
| 341 | /// 'int' |
| 342 | /// 'long' |
| 343 | /// 'float' |
| 344 | /// 'double' |
| 345 | /// 'signed' |
| 346 | /// 'unsigned' |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 347 | /// struct-or-union-specifier |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 348 | /// enum-specifier |
Chris Lattner | 3b4fdda3 | 2006-08-14 00:45:39 +0000 | [diff] [blame] | 349 | /// typedef-name |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 350 | /// [C++] 'bool' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 351 | /// [C99] '_Bool' |
| 352 | /// [C99] '_Complex' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 353 | /// [C99] '_Imaginary' // Removed in TC2? |
| 354 | /// [GNU] '_Decimal32' |
| 355 | /// [GNU] '_Decimal64' |
| 356 | /// [GNU] '_Decimal128' |
Steve Naroff | 872da80 | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 357 | /// [GNU] typeof-specifier |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 358 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
Steve Naroff | 7e901fd | 2007-08-22 23:18:22 +0000 | [diff] [blame^] | 359 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 360 | /// type-qualifier: |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 361 | /// 'const' |
| 362 | /// 'volatile' |
| 363 | /// [C99] 'restrict' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 364 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 365 | /// [C99] 'inline' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 366 | /// |
| 367 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) { |
Chris Lattner | 02c0439 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 368 | DS.Range.setBegin(Tok.getLocation()); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 369 | while (1) { |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 370 | int isInvalid = false; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 371 | const char *PrevSpec = 0; |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 372 | SourceLocation Loc = Tok.getLocation(); |
| 373 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 374 | switch (Tok.getKind()) { |
Chris Lattner | 3b4fdda3 | 2006-08-14 00:45:39 +0000 | [diff] [blame] | 375 | // typedef-name |
| 376 | case tok::identifier: |
| 377 | // This identifier can only be a typedef name if we haven't already seen |
Chris Lattner | 5646b3e | 2006-08-15 05:12:01 +0000 | [diff] [blame] | 378 | // a type-specifier. Without this check we misparse: |
| 379 | // typedef int X; struct Y { short X; }; as 'short int'. |
Chris Lattner | f055d43 | 2006-11-28 04:28:12 +0000 | [diff] [blame] | 380 | if (!DS.hasTypeSpecifier()) { |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 381 | // It has to be available as a typedef too! |
| 382 | if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), |
| 383 | CurScope)) { |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 384 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 385 | TypeRep); |
Steve Naroff | 7e901fd | 2007-08-22 23:18:22 +0000 | [diff] [blame^] | 386 | if (isInvalid) |
| 387 | break; |
| 388 | else { // FIXME: restrict this to "id" and ObjC classnames. |
| 389 | DS.Range.setEnd(Tok.getLocation()); |
| 390 | ConsumeToken(); // The identifier |
| 391 | if (Tok.getKind() == tok::less) |
| 392 | ParseObjCProtocolReferences(); |
| 393 | continue; |
| 394 | } |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 395 | } |
Chris Lattner | 3b4fdda3 | 2006-08-14 00:45:39 +0000 | [diff] [blame] | 396 | } |
| 397 | // FALL THROUGH. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 398 | default: |
| 399 | // If this is not a declaration specifier token, we're done reading decl |
| 400 | // specifiers. First verify that DeclSpec's are consistent. |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 401 | DS.Finish(Diags, getLang()); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 402 | return; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 403 | |
| 404 | // GNU attributes support. |
| 405 | case tok::kw___attribute: |
Steve Naroff | 0f05a7a | 2007-06-09 23:38:17 +0000 | [diff] [blame] | 406 | DS.AddAttributes(ParseAttributes()); |
Chris Lattner | b95cca0 | 2006-10-17 03:01:08 +0000 | [diff] [blame] | 407 | continue; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 408 | |
| 409 | // storage-class-specifier |
| 410 | case tok::kw_typedef: |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 411 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 412 | break; |
| 413 | case tok::kw_extern: |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 414 | if (DS.isThreadSpecified()) |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 415 | Diag(Tok, diag::ext_thread_before, "extern"); |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 416 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 417 | break; |
| 418 | case tok::kw_static: |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 419 | if (DS.isThreadSpecified()) |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 420 | Diag(Tok, diag::ext_thread_before, "static"); |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 421 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 422 | break; |
| 423 | case tok::kw_auto: |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 424 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 425 | break; |
| 426 | case tok::kw_register: |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 427 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 428 | break; |
| 429 | case tok::kw___thread: |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 430 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 431 | break; |
| 432 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 433 | // type-specifiers |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 434 | case tok::kw_short: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 435 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 436 | break; |
| 437 | case tok::kw_long: |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 438 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 439 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 440 | else |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 441 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 442 | break; |
| 443 | case tok::kw_signed: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 444 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 445 | break; |
| 446 | case tok::kw_unsigned: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 447 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 448 | break; |
| 449 | case tok::kw__Complex: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 450 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 451 | break; |
| 452 | case tok::kw__Imaginary: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 453 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 454 | break; |
| 455 | case tok::kw_void: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 456 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 457 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 458 | case tok::kw_char: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 459 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 460 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 461 | case tok::kw_int: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 462 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 463 | break; |
| 464 | case tok::kw_float: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 465 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 466 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 467 | case tok::kw_double: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 468 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 469 | break; |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 470 | case tok::kw_bool: // [C++ 2.11p1] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 471 | case tok::kw__Bool: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 472 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 473 | break; |
| 474 | case tok::kw__Decimal32: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 475 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 476 | break; |
| 477 | case tok::kw__Decimal64: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 478 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 479 | break; |
| 480 | case tok::kw__Decimal128: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 481 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 482 | break; |
| 483 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 484 | case tok::kw_struct: |
| 485 | case tok::kw_union: |
| 486 | ParseStructUnionSpecifier(DS); |
| 487 | continue; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 488 | case tok::kw_enum: |
| 489 | ParseEnumSpecifier(DS); |
| 490 | continue; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 491 | |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 492 | // GNU typeof support. |
| 493 | case tok::kw_typeof: |
| 494 | ParseTypeofSpecifier(DS); |
| 495 | continue; |
| 496 | |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 497 | // type-qualifier |
| 498 | case tok::kw_const: |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 499 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 500 | getLang())*2; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 501 | break; |
| 502 | case tok::kw_volatile: |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 503 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 504 | getLang())*2; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 505 | break; |
| 506 | case tok::kw_restrict: |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 507 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 508 | getLang())*2; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 509 | break; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 510 | |
| 511 | // function-specifier |
| 512 | case tok::kw_inline: |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 513 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 514 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 515 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 516 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 517 | if (isInvalid) { |
| 518 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 519 | if (isInvalid == 1) // Error. |
| 520 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 521 | else // extwarn. |
| 522 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 523 | } |
Chris Lattner | 02c0439 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 524 | DS.Range.setEnd(Tok.getLocation()); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 525 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 529 | /// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where |
| 530 | /// the first token has already been read and has been turned into an instance |
| 531 | /// of DeclSpec::TST (TagType). This returns true if there is an error parsing, |
| 532 | /// otherwise it returns false and fills in Decl. |
| 533 | bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){ |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 534 | AttributeList *Attr = 0; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 535 | // If attributes exist after tag, parse them. |
| 536 | if (Tok.getKind() == tok::kw___attribute) |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 537 | Attr = ParseAttributes(); |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 539 | // Must have either 'struct name' or 'struct {...}'. |
| 540 | if (Tok.getKind() != tok::identifier && |
| 541 | Tok.getKind() != tok::l_brace) { |
| 542 | Diag(Tok, diag::err_expected_ident_lbrace); |
Chris Lattner | 02c0439 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 543 | |
| 544 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 545 | SkipUntil(tok::comma, true); |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 546 | return true; |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Chris Lattner | 8c6519a | 2007-01-22 07:41:36 +0000 | [diff] [blame] | 549 | // If an identifier is present, consume and remember it. |
| 550 | IdentifierInfo *Name = 0; |
| 551 | SourceLocation NameLoc; |
| 552 | if (Tok.getKind() == tok::identifier) { |
| 553 | Name = Tok.getIdentifierInfo(); |
| 554 | NameLoc = ConsumeToken(); |
| 555 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 556 | |
Chris Lattner | 8c6519a | 2007-01-22 07:41:36 +0000 | [diff] [blame] | 557 | // There are three options here. If we have 'struct foo;', then this is a |
| 558 | // forward declaration. If we have 'struct foo {...' then this is a |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 559 | // definition. Otherwise we have something like 'struct foo xyz', a reference. |
Chris Lattner | 8799cf2 | 2007-01-23 01:57:16 +0000 | [diff] [blame] | 560 | // |
| 561 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 562 | // struct foo {..}; void bar() { struct foo; } <- new foo in bar. |
| 563 | // struct foo {..}; void bar() { struct foo x; } <- use of old foo. |
| 564 | // |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 565 | Action::TagKind TK; |
| 566 | if (Tok.getKind() == tok::l_brace) |
| 567 | TK = Action::TK_Definition; |
| 568 | else if (Tok.getKind() == tok::semi) |
| 569 | TK = Action::TK_Declaration; |
| 570 | else |
| 571 | TK = Action::TK_Reference; |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 572 | Decl = Actions.ParseTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr); |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 573 | return false; |
| 574 | } |
| 575 | |
| 576 | |
| 577 | /// ParseStructUnionSpecifier |
| 578 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 579 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 580 | /// struct-or-union identifier |
| 581 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 582 | /// '}' attributes[opt] |
| 583 | /// [GNU] struct-or-union attributes[opt] identifier |
| 584 | /// struct-or-union: |
| 585 | /// 'struct' |
| 586 | /// 'union' |
| 587 | /// |
| 588 | void Parser::ParseStructUnionSpecifier(DeclSpec &DS) { |
| 589 | assert((Tok.getKind() == tok::kw_struct || |
| 590 | Tok.getKind() == tok::kw_union) && "Not a struct/union specifier"); |
| 591 | DeclSpec::TST TagType = |
| 592 | Tok.getKind() == tok::kw_union ? DeclSpec::TST_union : DeclSpec::TST_struct; |
| 593 | SourceLocation StartLoc = ConsumeToken(); |
| 594 | |
| 595 | // Parse the tag portion of this. |
| 596 | DeclTy *TagDecl; |
| 597 | if (ParseTag(TagDecl, TagType, StartLoc)) |
| 598 | return; |
Chris Lattner | bf0b798 | 2007-01-23 04:27:41 +0000 | [diff] [blame] | 599 | |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 600 | // If there is a body, parse it and inform the actions module. |
| 601 | if (Tok.getKind() == tok::l_brace) |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 602 | ParseStructUnionBody(StartLoc, TagType, TagDecl); |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 603 | |
| 604 | const char *PrevSpec = 0; |
Chris Lattner | b9d572a | 2007-01-23 04:58:34 +0000 | [diff] [blame] | 605 | if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl)) |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 606 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 609 | /// ParseStructDeclaration |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 610 | /// struct-declaration: |
| 611 | /// specifier-qualifier-list struct-declarator-list ';' |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 612 | /// [GNU] __extension__ struct-declaration |
| 613 | /// [GNU] specifier-qualifier-list ';' |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 614 | /// struct-declarator-list: |
| 615 | /// struct-declarator |
| 616 | /// struct-declarator-list ',' struct-declarator |
| 617 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 618 | /// struct-declarator: |
| 619 | /// declarator |
| 620 | /// [GNU] declarator attributes[opt] |
| 621 | /// declarator[opt] ':' constant-expression |
| 622 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 623 | /// |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 624 | void Parser::ParseStructDeclaration(DeclTy *TagDecl, |
| 625 | llvm::SmallVector<DeclTy*, 32> &FieldDecls) { |
| 626 | // FIXME: When __extension__ is specified, disable extension diagnostics. |
| 627 | if (Tok.getKind() == tok::kw___extension__) |
| 628 | ConsumeToken(); |
| 629 | |
| 630 | // Parse the common specifier-qualifiers-list piece. |
| 631 | DeclSpec DS; |
| 632 | SourceLocation SpecQualLoc = Tok.getLocation(); |
| 633 | ParseSpecifierQualifierList(DS); |
| 634 | // TODO: Does specifier-qualifier list correctly check that *something* is |
| 635 | // specified? |
| 636 | |
| 637 | // If there are no declarators, issue a warning. |
| 638 | if (Tok.getKind() == tok::semi) { |
| 639 | Diag(SpecQualLoc, diag::w_no_declarators); |
| 640 | ConsumeToken(); |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | // Read struct-declarators until we find the semicolon. |
| 645 | Declarator DeclaratorInfo(DS, Declarator::MemberContext); |
| 646 | |
| 647 | while (1) { |
| 648 | /// struct-declarator: declarator |
| 649 | /// struct-declarator: declarator[opt] ':' constant-expression |
| 650 | if (Tok.getKind() != tok::colon) |
| 651 | ParseDeclarator(DeclaratorInfo); |
| 652 | |
| 653 | ExprTy *BitfieldSize = 0; |
| 654 | if (Tok.getKind() == tok::colon) { |
| 655 | ConsumeToken(); |
| 656 | ExprResult Res = ParseConstantExpression(); |
| 657 | if (Res.isInvalid) { |
| 658 | SkipUntil(tok::semi, true, true); |
| 659 | } else { |
| 660 | BitfieldSize = Res.Val; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // If attributes exist after the declarator, parse them. |
| 665 | if (Tok.getKind() == tok::kw___attribute) |
| 666 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 667 | |
| 668 | // Install the declarator into the current TagDecl. |
| 669 | DeclTy *Field = Actions.ParseField(CurScope, TagDecl, SpecQualLoc, |
| 670 | DeclaratorInfo, BitfieldSize); |
| 671 | FieldDecls.push_back(Field); |
| 672 | |
| 673 | // If we don't have a comma, it is either the end of the list (a ';') |
| 674 | // or an error, bail out. |
| 675 | if (Tok.getKind() != tok::comma) |
| 676 | break; |
| 677 | |
| 678 | // Consume the comma. |
| 679 | ConsumeToken(); |
| 680 | |
| 681 | // Parse the next declarator. |
| 682 | DeclaratorInfo.clear(); |
| 683 | |
| 684 | // Attributes are only allowed on the second declarator. |
| 685 | if (Tok.getKind() == tok::kw___attribute) |
| 686 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 687 | } |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | /// ParseStructUnionBody |
| 692 | /// struct-contents: |
| 693 | /// struct-declaration-list |
| 694 | /// [EXT] empty |
| 695 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 696 | /// struct-declaration-list: |
| 697 | /// struct-declaration |
| 698 | /// struct-declaration-list struct-declaration |
| 699 | /// [OBC] '@' 'defs' '(' class-name ')' [TODO] |
| 700 | /// |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 701 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
| 702 | unsigned TagType, DeclTy *TagDecl) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 703 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 704 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 705 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 706 | // C++. |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 707 | if (Tok.getKind() == tok::r_brace) |
| 708 | Diag(Tok, diag::ext_empty_struct_union_enum, |
| 709 | DeclSpec::getSpecifierName((DeclSpec::TST)TagType)); |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 711 | llvm::SmallVector<DeclTy*, 32> FieldDecls; |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 712 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 713 | // While we still have something to read, read the declarations in the struct. |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 714 | while (Tok.getKind() != tok::r_brace && |
| 715 | Tok.getKind() != tok::eof) { |
| 716 | // Each iteration of this loop reads one struct-declaration. |
| 717 | |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 718 | // Check for extraneous top-level semicolon. |
Chris Lattner | 36e46a2 | 2007-06-09 05:49:55 +0000 | [diff] [blame] | 719 | if (Tok.getKind() == tok::semi) { |
| 720 | Diag(Tok, diag::ext_extra_struct_semi); |
| 721 | ConsumeToken(); |
| 722 | continue; |
| 723 | } |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 724 | ParseStructDeclaration(TagDecl, FieldDecls); |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 725 | |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 726 | if (Tok.getKind() == tok::semi) { |
| 727 | ConsumeToken(); |
Chris Lattner | 0c7e82d | 2007-06-09 05:54:40 +0000 | [diff] [blame] | 728 | } else if (Tok.getKind() == tok::r_brace) { |
| 729 | Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list); |
| 730 | break; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 731 | } else { |
| 732 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 733 | // Skip to end of block or statement |
| 734 | SkipUntil(tok::r_brace, true, true); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 739 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 740 | Actions.ParseRecordBody(RecordLoc, TagDecl, &FieldDecls[0],FieldDecls.size()); |
| 741 | |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 742 | AttributeList *AttrList = 0; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 743 | // If attributes exist after struct contents, parse them. |
| 744 | if (Tok.getKind() == tok::kw___attribute) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 745 | AttrList = ParseAttributes(); // FIXME: where should I put them? |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 749 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 750 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 751 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
| 752 | /// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 753 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 754 | /// '}' attributes[opt] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 755 | /// 'enum' identifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 756 | /// [GNU] 'enum' attributes[opt] identifier |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 757 | void Parser::ParseEnumSpecifier(DeclSpec &DS) { |
| 758 | assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier"); |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 759 | SourceLocation StartLoc = ConsumeToken(); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 760 | |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 761 | // Parse the tag portion of this. |
| 762 | DeclTy *TagDecl; |
| 763 | if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc)) |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 764 | return; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 765 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 766 | if (Tok.getKind() == tok::l_brace) |
| 767 | ParseEnumBody(StartLoc, TagDecl); |
| 768 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 769 | // TODO: semantic analysis on the declspec for enums. |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 770 | const char *PrevSpec = 0; |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 771 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl)) |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 772 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 775 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 776 | /// enumerator-list: |
| 777 | /// enumerator |
| 778 | /// enumerator-list ',' enumerator |
| 779 | /// enumerator: |
| 780 | /// enumeration-constant |
| 781 | /// enumeration-constant '=' constant-expression |
| 782 | /// enumeration-constant: |
| 783 | /// identifier |
| 784 | /// |
| 785 | void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) { |
| 786 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 787 | |
| 788 | if (Tok.getKind() == tok::r_brace) |
| 789 | Diag(Tok, diag::ext_empty_struct_union_enum, "enum"); |
| 790 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 791 | llvm::SmallVector<DeclTy*, 32> EnumConstantDecls; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 792 | |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 793 | DeclTy *LastEnumConstDecl = 0; |
| 794 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 795 | // Parse the enumerator-list. |
| 796 | while (Tok.getKind() == tok::identifier) { |
| 797 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 798 | SourceLocation IdentLoc = ConsumeToken(); |
| 799 | |
| 800 | SourceLocation EqualLoc; |
| 801 | ExprTy *AssignedVal = 0; |
| 802 | if (Tok.getKind() == tok::equal) { |
| 803 | EqualLoc = ConsumeToken(); |
| 804 | ExprResult Res = ParseConstantExpression(); |
| 805 | if (Res.isInvalid) |
Chris Lattner | da6c2ce | 2007-04-27 19:13:15 +0000 | [diff] [blame] | 806 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 807 | else |
| 808 | AssignedVal = Res.Val; |
| 809 | } |
| 810 | |
| 811 | // Install the enumerator constant into EnumDecl. |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 812 | DeclTy *EnumConstDecl = Actions.ParseEnumConstant(CurScope, EnumDecl, |
| 813 | LastEnumConstDecl, |
| 814 | IdentLoc, Ident, |
| 815 | EqualLoc, AssignedVal); |
| 816 | EnumConstantDecls.push_back(EnumConstDecl); |
| 817 | LastEnumConstDecl = EnumConstDecl; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 818 | |
| 819 | if (Tok.getKind() != tok::comma) |
| 820 | break; |
| 821 | SourceLocation CommaLoc = ConsumeToken(); |
| 822 | |
| 823 | if (Tok.getKind() != tok::identifier && !getLang().C99) |
| 824 | Diag(CommaLoc, diag::ext_c99_enumerator_list_comma); |
| 825 | } |
| 826 | |
| 827 | // Eat the }. |
| 828 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 829 | |
| 830 | Actions.ParseEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0], |
| 831 | EnumConstantDecls.size()); |
| 832 | |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 833 | DeclTy *AttrList = 0; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 834 | // If attributes exist after the identifier list, parse them. |
| 835 | if (Tok.getKind() == tok::kw___attribute) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 836 | AttrList = ParseAttributes(); // FIXME: where do they do? |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 837 | } |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 838 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 839 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
| 840 | /// start of a specifier-qualifier-list. |
| 841 | bool Parser::isTypeSpecifierQualifier() const { |
| 842 | switch (Tok.getKind()) { |
| 843 | default: return false; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 844 | // GNU attributes support. |
| 845 | case tok::kw___attribute: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 846 | // GNU typeof support. |
| 847 | case tok::kw_typeof: |
| 848 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 849 | // type-specifiers |
| 850 | case tok::kw_short: |
| 851 | case tok::kw_long: |
| 852 | case tok::kw_signed: |
| 853 | case tok::kw_unsigned: |
| 854 | case tok::kw__Complex: |
| 855 | case tok::kw__Imaginary: |
| 856 | case tok::kw_void: |
| 857 | case tok::kw_char: |
| 858 | case tok::kw_int: |
| 859 | case tok::kw_float: |
| 860 | case tok::kw_double: |
| 861 | case tok::kw__Bool: |
| 862 | case tok::kw__Decimal32: |
| 863 | case tok::kw__Decimal64: |
| 864 | case tok::kw__Decimal128: |
| 865 | |
| 866 | // struct-or-union-specifier |
| 867 | case tok::kw_struct: |
| 868 | case tok::kw_union: |
| 869 | // enum-specifier |
| 870 | case tok::kw_enum: |
| 871 | |
| 872 | // type-qualifier |
| 873 | case tok::kw_const: |
| 874 | case tok::kw_volatile: |
| 875 | case tok::kw_restrict: |
| 876 | return true; |
| 877 | |
| 878 | // typedef-name |
| 879 | case tok::identifier: |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 880 | return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 884 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 885 | /// declaration specifier. |
| 886 | bool Parser::isDeclarationSpecifier() const { |
| 887 | switch (Tok.getKind()) { |
| 888 | default: return false; |
| 889 | // storage-class-specifier |
| 890 | case tok::kw_typedef: |
| 891 | case tok::kw_extern: |
| 892 | case tok::kw_static: |
| 893 | case tok::kw_auto: |
| 894 | case tok::kw_register: |
| 895 | case tok::kw___thread: |
| 896 | |
| 897 | // type-specifiers |
| 898 | case tok::kw_short: |
| 899 | case tok::kw_long: |
| 900 | case tok::kw_signed: |
| 901 | case tok::kw_unsigned: |
| 902 | case tok::kw__Complex: |
| 903 | case tok::kw__Imaginary: |
| 904 | case tok::kw_void: |
| 905 | case tok::kw_char: |
| 906 | case tok::kw_int: |
| 907 | case tok::kw_float: |
| 908 | case tok::kw_double: |
| 909 | case tok::kw__Bool: |
| 910 | case tok::kw__Decimal32: |
| 911 | case tok::kw__Decimal64: |
| 912 | case tok::kw__Decimal128: |
| 913 | |
| 914 | // struct-or-union-specifier |
| 915 | case tok::kw_struct: |
| 916 | case tok::kw_union: |
| 917 | // enum-specifier |
| 918 | case tok::kw_enum: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 919 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 920 | // type-qualifier |
| 921 | case tok::kw_const: |
| 922 | case tok::kw_volatile: |
| 923 | case tok::kw_restrict: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 924 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 925 | // function-specifier |
| 926 | case tok::kw_inline: |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 927 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 928 | // GNU typeof support. |
| 929 | case tok::kw_typeof: |
| 930 | |
| 931 | // GNU attributes. |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 932 | case tok::kw___attribute: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 933 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 934 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 935 | // typedef-name |
| 936 | case tok::identifier: |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 937 | return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 941 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 942 | /// ParseTypeQualifierListOpt |
| 943 | /// type-qualifier-list: [C99 6.7.5] |
| 944 | /// type-qualifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 945 | /// [GNU] attributes |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 946 | /// type-qualifier-list type-qualifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 947 | /// [GNU] type-qualifier-list attributes |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 948 | /// |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 949 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) { |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 950 | while (1) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 951 | int isInvalid = false; |
| 952 | const char *PrevSpec = 0; |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 953 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 954 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 955 | switch (Tok.getKind()) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 956 | default: |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 957 | // If this is not a type-qualifier token, we're done reading type |
| 958 | // qualifiers. First verify that DeclSpec's are consistent. |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 959 | DS.Finish(Diags, getLang()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 960 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 961 | case tok::kw_const: |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 962 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 963 | getLang())*2; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 964 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 965 | case tok::kw_volatile: |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 966 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 967 | getLang())*2; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 968 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 969 | case tok::kw_restrict: |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 970 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 971 | getLang())*2; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 972 | break; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 973 | case tok::kw___attribute: |
Steve Naroff | 0f05a7a | 2007-06-09 23:38:17 +0000 | [diff] [blame] | 974 | DS.AddAttributes(ParseAttributes()); |
Steve Naroff | 98d153c | 2007-06-06 23:19:11 +0000 | [diff] [blame] | 975 | continue; // do *not* consume the next token! |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 976 | } |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 977 | |
| 978 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 979 | if (isInvalid) { |
| 980 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 981 | if (isInvalid == 1) // Error. |
| 982 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 983 | else // extwarn. |
| 984 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 985 | } |
| 986 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 990 | |
| 991 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 992 | /// |
| 993 | void Parser::ParseDeclarator(Declarator &D) { |
| 994 | /// This implements the 'declarator' production in the C grammar, then checks |
| 995 | /// for well-formedness and issues diagnostics. |
| 996 | ParseDeclaratorInternal(D); |
| 997 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 998 | // TODO: validate D. |
Chris Lattner | bf320c8 | 2006-08-07 05:05:30 +0000 | [diff] [blame] | 999 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /// ParseDeclaratorInternal |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1003 | /// declarator: [C99 6.7.5] |
| 1004 | /// pointer[opt] direct-declarator |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 1005 | /// [C++] '&' declarator [C++ 8p4, dcl.decl] |
| 1006 | /// [GNU] '&' restrict[opt] attributes[opt] declarator |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1007 | /// |
| 1008 | /// pointer: [C99 6.7.5] |
| 1009 | /// '*' type-qualifier-list[opt] |
| 1010 | /// '*' type-qualifier-list[opt] pointer |
| 1011 | /// |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1012 | void Parser::ParseDeclaratorInternal(Declarator &D) { |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1013 | tok::TokenKind Kind = Tok.getKind(); |
| 1014 | |
| 1015 | // Not a pointer or C++ reference. |
| 1016 | if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus)) |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1017 | return ParseDirectDeclarator(D); |
| 1018 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1019 | // Otherwise, '*' -> pointer or '&' -> reference. |
| 1020 | SourceLocation Loc = ConsumeToken(); // Eat the * or &. |
| 1021 | |
| 1022 | if (Kind == tok::star) { |
| 1023 | // Is a pointer |
| 1024 | DeclSpec DS; |
Steve Naroff | 98d153c | 2007-06-06 23:19:11 +0000 | [diff] [blame] | 1025 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1026 | ParseTypeQualifierListOpt(DS); |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1027 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1028 | // Recursively parse the declarator. |
| 1029 | ParseDeclaratorInternal(D); |
Chris Lattner | 9dfdb3c | 2006-11-13 07:38:09 +0000 | [diff] [blame] | 1030 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1031 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1032 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc)); |
| 1033 | } else { |
| 1034 | // Is a reference |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 1035 | DeclSpec DS; |
| 1036 | |
| 1037 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 1038 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 1039 | // template type argument, in which case the cv-qualifiers are ignored. |
| 1040 | // |
| 1041 | // [GNU] Retricted references are allowed. |
| 1042 | // [GNU] Attributes on references are allowed. |
| 1043 | ParseTypeQualifierListOpt(DS); |
| 1044 | |
| 1045 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 1046 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 1047 | Diag(DS.getConstSpecLoc(), |
| 1048 | diag::err_invalid_reference_qualifier_application, |
| 1049 | "const"); |
| 1050 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 1051 | Diag(DS.getVolatileSpecLoc(), |
| 1052 | diag::err_invalid_reference_qualifier_application, |
| 1053 | "volatile"); |
| 1054 | } |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1055 | |
| 1056 | // Recursively parse the declarator. |
| 1057 | ParseDeclaratorInternal(D); |
| 1058 | |
| 1059 | // Remember that we parsed a reference type. It doesn't have type-quals. |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 1060 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc)); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1061 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1064 | /// ParseDirectDeclarator |
| 1065 | /// direct-declarator: [C99 6.7.5] |
| 1066 | /// identifier |
| 1067 | /// '(' declarator ')' |
| 1068 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1069 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1070 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1071 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1072 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1073 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1074 | /// direct-declarator '(' parameter-type-list ')' |
| 1075 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1076 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1077 | /// parameter-type-list[opt] ')' |
| 1078 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1079 | void Parser::ParseDirectDeclarator(Declarator &D) { |
| 1080 | // Parse the first direct-declarator seen. |
| 1081 | if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) { |
| 1082 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 1083 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1084 | ConsumeToken(); |
| 1085 | } else if (Tok.getKind() == tok::l_paren) { |
| 1086 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1087 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1088 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 1089 | ParseParenDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1090 | } else if (D.mayOmitIdentifier()) { |
| 1091 | // This could be something simple like "int" (in which case the declarator |
| 1092 | // portion is empty), if an abstract-declarator is allowed. |
| 1093 | D.SetIdentifier(0, Tok.getLocation()); |
| 1094 | } else { |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 1095 | // Expected identifier or '('. |
| 1096 | Diag(Tok, diag::err_expected_ident_lparen); |
| 1097 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | assert(D.isPastIdentifier() && |
| 1101 | "Haven't past the location of the identifier yet?"); |
| 1102 | |
| 1103 | while (1) { |
| 1104 | if (Tok.getKind() == tok::l_paren) { |
| 1105 | ParseParenDeclarator(D); |
| 1106 | } else if (Tok.getKind() == tok::l_square) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1107 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1108 | } else { |
| 1109 | break; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This may |
| 1115 | /// either be before the identifier (in which case these are just grouping |
| 1116 | /// parens for precedence) or it may be after the identifier, in which case |
| 1117 | /// these are function arguments. |
| 1118 | /// |
| 1119 | /// This method also handles this portion of the grammar: |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1120 | /// parameter-type-list: [C99 6.7.5] |
| 1121 | /// parameter-list |
| 1122 | /// parameter-list ',' '...' |
| 1123 | /// |
| 1124 | /// parameter-list: [C99 6.7.5] |
| 1125 | /// parameter-declaration |
| 1126 | /// parameter-list ',' parameter-declaration |
| 1127 | /// |
| 1128 | /// parameter-declaration: [C99 6.7.5] |
| 1129 | /// declaration-specifiers declarator |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1130 | /// [GNU] declaration-specifiers declarator attributes |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1131 | /// declaration-specifiers abstract-declarator[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1132 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1133 | /// |
| 1134 | /// identifier-list: [C99 6.7.5] |
| 1135 | /// identifier |
| 1136 | /// identifier-list ',' identifier |
| 1137 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1138 | void Parser::ParseParenDeclarator(Declarator &D) { |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 1139 | SourceLocation StartLoc = ConsumeParen(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1140 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1141 | // If we haven't past the identifier yet (or where the identifier would be |
| 1142 | // stored, if this is an abstract declarator), then this is probably just |
| 1143 | // grouping parens. |
| 1144 | if (!D.isPastIdentifier()) { |
| 1145 | // Okay, this is probably a grouping paren. However, if this could be an |
| 1146 | // abstract-declarator, then this could also be the start of function |
| 1147 | // arguments (consider 'void()'). |
| 1148 | bool isGrouping; |
| 1149 | |
| 1150 | if (!D.mayOmitIdentifier()) { |
| 1151 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 1152 | // paren, because we haven't seen the identifier yet. |
| 1153 | isGrouping = true; |
| 1154 | } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function. |
| 1155 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
Chris Lattner | bb233fe | 2006-11-21 23:13:27 +0000 | [diff] [blame] | 1156 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 1157 | // considered to be a type, not a K&R identifier-list. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1158 | isGrouping = false; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1159 | } else { |
Chris Lattner | bb233fe | 2006-11-21 23:13:27 +0000 | [diff] [blame] | 1160 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1161 | isGrouping = true; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1162 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1163 | |
| 1164 | // If this is a grouping paren, handle: |
| 1165 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1166 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1167 | if (isGrouping) { |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1168 | if (Tok.getKind() == tok::kw___attribute) |
Steve Naroff | 0f05a7a | 2007-06-09 23:38:17 +0000 | [diff] [blame] | 1169 | D.AddAttributes(ParseAttributes()); |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1170 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1171 | ParseDeclaratorInternal(D); |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 1172 | // Match the ')'. |
Chris Lattner | 04f8019 | 2006-08-15 04:55:54 +0000 | [diff] [blame] | 1173 | MatchRHSPunctuation(tok::r_paren, StartLoc); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1174 | return; |
| 1175 | } |
| 1176 | |
| 1177 | // 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] | 1178 | // argument list. Recognize that this declarator will never have an |
| 1179 | // identifier (and remember where it would have been), then fall through to |
| 1180 | // the handling of argument lists. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1181 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1184 | // Okay, this is the parameter list of a function definition, or it is an |
| 1185 | // identifier list of a K&R-style function. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1186 | bool IsVariadic; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1187 | bool HasPrototype; |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 1188 | bool ErrorEmitted = false; |
| 1189 | |
Chris Lattner | edc9e39 | 2006-12-02 06:21:46 +0000 | [diff] [blame] | 1190 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1191 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 1192 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
Chris Lattner | edc9e39 | 2006-12-02 06:21:46 +0000 | [diff] [blame] | 1193 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1194 | if (Tok.getKind() == tok::r_paren) { |
| 1195 | // int() -> no prototype, no '...'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1196 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1197 | HasPrototype = false; |
| 1198 | } else if (Tok.getKind() == tok::identifier && |
Chris Lattner | bb233fe | 2006-11-21 23:13:27 +0000 | [diff] [blame] | 1199 | // K&R identifier lists can't have typedefs as identifiers, per |
| 1200 | // C99 6.7.5.3p11. |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 1201 | !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1202 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 1203 | // normal declarators, not for abstract-declarators. |
| 1204 | assert(D.isPastIdentifier() && "Identifier (if present) must be passed!"); |
| 1205 | |
| 1206 | // If there was no identifier specified, either we are in an |
| 1207 | // abstract-declarator, or we are in a parameter declarator which was found |
| 1208 | // to be abstract. In abstract-declarators, identifier lists are not valid, |
| 1209 | // diagnose this. |
| 1210 | if (!D.getIdentifier()) |
| 1211 | Diag(Tok, diag::ext_ident_list_in_param); |
Chris Lattner | edc9e39 | 2006-12-02 06:21:46 +0000 | [diff] [blame] | 1212 | |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1213 | // Remember this identifier in ParamInfo. |
| 1214 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(), |
| 1215 | Tok.getLocation(), 0)); |
| 1216 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1217 | ConsumeToken(); |
| 1218 | while (Tok.getKind() == tok::comma) { |
| 1219 | // Eat the comma. |
| 1220 | ConsumeToken(); |
| 1221 | |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1222 | if (Tok.getKind() != tok::identifier) { |
| 1223 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 1224 | ErrorEmitted = true; |
| 1225 | break; |
| 1226 | } |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1227 | |
Chris Lattner | 969ca15 | 2006-12-03 06:29:03 +0000 | [diff] [blame] | 1228 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 1229 | |
| 1230 | // Verify that the argument identifier has not already been mentioned. |
Chris Lattner | baf3366 | 2007-01-27 02:14:08 +0000 | [diff] [blame] | 1231 | if (!ParamsSoFar.insert(ParmII)) { |
Chris Lattner | ad9ac94 | 2007-01-23 01:14:52 +0000 | [diff] [blame] | 1232 | Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName()); |
| 1233 | ParmII = 0; |
| 1234 | } |
Chris Lattner | 969ca15 | 2006-12-03 06:29:03 +0000 | [diff] [blame] | 1235 | |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1236 | // Remember this identifier in ParamInfo. |
Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 1237 | if (ParmII) |
| 1238 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 1239 | Tok.getLocation(), 0)); |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1240 | |
| 1241 | // Eat the identifier. |
| 1242 | ConsumeToken(); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1245 | // K&R 'prototype'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1246 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1247 | HasPrototype = false; |
| 1248 | } else { |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1249 | // Finally, a normal, non-empty parameter type list. |
| 1250 | |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1251 | // Enter function-declaration scope, limiting any declarators for struct |
| 1252 | // tags to the function prototype scope. |
| 1253 | // FIXME: is this needed? |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1254 | EnterScope(0); |
| 1255 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1256 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1257 | while (1) { |
| 1258 | if (Tok.getKind() == tok::ellipsis) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1259 | IsVariadic = true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1260 | |
| 1261 | // Check to see if this is "void(...)" which is not allowed. |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1262 | if (ParamInfo.empty()) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1263 | // Otherwise, parse parameter type list. If it starts with an |
| 1264 | // ellipsis, diagnose the malformed function. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1265 | Diag(Tok, diag::err_ellipsis_first_arg); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1266 | IsVariadic = false; // Treat this like 'void()'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | // Consume the ellipsis. |
| 1270 | ConsumeToken(); |
| 1271 | break; |
| 1272 | } |
| 1273 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1274 | // Parse the declaration-specifiers. |
| 1275 | DeclSpec DS; |
| 1276 | ParseDeclarationSpecifiers(DS); |
| 1277 | |
| 1278 | // Parse the declarator. This is "PrototypeContext", because we must |
| 1279 | // accept either 'declarator' or 'abstract-declarator' here. |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1280 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 1281 | ParseDeclarator(ParmDecl); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1282 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1283 | // Parse GNU attributes, if present. |
| 1284 | if (Tok.getKind() == tok::kw___attribute) |
Steve Naroff | 0f05a7a | 2007-06-09 23:38:17 +0000 | [diff] [blame] | 1285 | ParmDecl.AddAttributes(ParseAttributes()); |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1286 | |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1287 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 1288 | // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted. |
| 1289 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && |
| 1290 | DS.getStorageClassSpec() != DeclSpec::SCS_register) { |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 1291 | Diag(DS.getStorageClassSpecLoc(), |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1292 | diag::err_invalid_storage_class_in_func_decl); |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 1293 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1294 | } |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 1295 | if (DS.isThreadSpecified()) { |
| 1296 | Diag(DS.getThreadSpecLoc(), |
| 1297 | diag::err_invalid_storage_class_in_func_decl); |
| 1298 | DS.ClearStorageClassSpecs(); |
| 1299 | } |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1300 | |
| 1301 | // Inform the actions module about the parameter declarator, so it gets |
| 1302 | // added to the current scope. |
Chris Lattner | 216d865 | 2006-12-02 06:47:41 +0000 | [diff] [blame] | 1303 | Action::TypeResult ParamTy = |
| 1304 | Actions.ParseParamDeclaratorType(CurScope, ParmDecl); |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1305 | |
| 1306 | // Remember this parsed parameter in ParamInfo. |
Chris Lattner | 969ca15 | 2006-12-03 06:29:03 +0000 | [diff] [blame] | 1307 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
| 1308 | |
| 1309 | // Verify that the argument identifier has not already been mentioned. |
Chris Lattner | baf3366 | 2007-01-27 02:14:08 +0000 | [diff] [blame] | 1310 | if (ParmII && !ParamsSoFar.insert(ParmII)) { |
Chris Lattner | ad9ac94 | 2007-01-23 01:14:52 +0000 | [diff] [blame] | 1311 | Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition, |
| 1312 | ParmII->getName()); |
| 1313 | ParmII = 0; |
Chris Lattner | 969ca15 | 2006-12-03 06:29:03 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1317 | ParmDecl.getIdentifierLoc(), |
| 1318 | ParamTy.Val)); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1319 | |
| 1320 | // If the next token is a comma, consume it and keep reading arguments. |
| 1321 | if (Tok.getKind() != tok::comma) break; |
| 1322 | |
| 1323 | // Consume the comma. |
| 1324 | ConsumeToken(); |
| 1325 | } |
| 1326 | |
| 1327 | HasPrototype = true; |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1328 | |
| 1329 | // Leave prototype scope. |
| 1330 | ExitScope(); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1333 | // Remember that we parsed a function type, and remember the attributes. |
Chris Lattner | d2e97c1 | 2006-12-03 02:03:33 +0000 | [diff] [blame] | 1334 | if (!ErrorEmitted) |
| 1335 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic, |
| 1336 | &ParamInfo[0], ParamInfo.size(), |
| 1337 | StartLoc)); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1338 | |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 1339 | // If we have the closing ')', eat it and we're done. |
| 1340 | if (Tok.getKind() == tok::r_paren) { |
| 1341 | ConsumeParen(); |
| 1342 | } else { |
| 1343 | // If an error happened earlier parsing something else in the proto, don't |
| 1344 | // issue another error. |
| 1345 | if (!ErrorEmitted) |
| 1346 | Diag(Tok, diag::err_expected_rparen); |
| 1347 | SkipUntil(tok::r_paren); |
| 1348 | } |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1349 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1350 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1351 | |
| 1352 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1353 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1354 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1355 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1356 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1357 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 1358 | SourceLocation StartLoc = ConsumeBracket(); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1359 | |
| 1360 | // If valid, this location is the position where we read the 'static' keyword. |
| 1361 | SourceLocation StaticLoc; |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1362 | if (Tok.getKind() == tok::kw_static) |
| 1363 | StaticLoc = ConsumeToken(); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1364 | |
| 1365 | // If there is a type-qualifier-list, read it now. |
| 1366 | DeclSpec DS; |
| 1367 | ParseTypeQualifierListOpt(DS); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1368 | |
| 1369 | // If we haven't already read 'static', check to see if there is one after the |
| 1370 | // type-qualifier-list. |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1371 | if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) |
| 1372 | StaticLoc = ConsumeToken(); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1373 | |
| 1374 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1375 | bool isStar = false; |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 1376 | ExprResult NumElements(false); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 1377 | if (Tok.getKind() == tok::star) { |
| 1378 | // Remember the '*' token, in case we have to un-get it. |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1379 | Token StarTok = Tok; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1380 | ConsumeToken(); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 1381 | |
| 1382 | // Check that the ']' token is present to avoid incorrectly parsing |
| 1383 | // expressions starting with '*' as [*]. |
| 1384 | if (Tok.getKind() == tok::r_square) { |
| 1385 | if (StaticLoc.isValid()) |
| 1386 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
| 1387 | StaticLoc = SourceLocation(); // Drop the static. |
| 1388 | isStar = true; |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 1389 | } else { |
| 1390 | // Otherwise, the * must have been some expression (such as '*ptr') that |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 1391 | // started an assignment-expr. We already consumed the token, but now we |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 1392 | // need to reparse it. This handles cases like 'X[*p + 4]' |
| 1393 | NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 1394 | } |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 1395 | } else if (Tok.getKind() != tok::r_square) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1396 | // Parse the assignment-expression now. |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 1397 | NumElements = ParseAssignmentExpression(); |
| 1398 | } |
| 1399 | |
| 1400 | // If there was an error parsing the assignment-expression, recover. |
| 1401 | if (NumElements.isInvalid) { |
| 1402 | // If the expression was invalid, skip it. |
| 1403 | SkipUntil(tok::r_square); |
| 1404 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Chris Lattner | 04f8019 | 2006-08-15 04:55:54 +0000 | [diff] [blame] | 1407 | MatchRHSPunctuation(tok::r_square, StartLoc); |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 1408 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1409 | // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if |
| 1410 | // it was not a constant expression. |
| 1411 | if (!getLang().C99) { |
| 1412 | // TODO: check C90 array constant exprness. |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1413 | if (isStar || StaticLoc.isValid() || |
| 1414 | 0/*TODO: NumElts is not a C90 constantexpr */) |
Chris Lattner | 8a39edc | 2006-08-06 18:33:32 +0000 | [diff] [blame] | 1415 | Diag(StartLoc, diag::ext_c99_array_usage); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1416 | } |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 1417 | |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1418 | // Remember that we parsed a pointer type, and remember the type-quals. |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1419 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
| 1420 | StaticLoc.isValid(), isStar, |
| 1421 | NumElements.Val, StartLoc)); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1424 | /// [GNU] typeof-specifier: |
| 1425 | /// typeof ( expressions ) |
| 1426 | /// typeof ( type-name ) |
| 1427 | /// |
| 1428 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
| 1429 | assert(Tok.getKind() == tok::kw_typeof && "Not a typeof specifier"); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1430 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1431 | SourceLocation StartLoc = ConsumeToken(); |
| 1432 | |
| 1433 | if (Tok.getKind() != tok::l_paren) { |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1434 | Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName()); |
| 1435 | return; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1436 | } |
| 1437 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
| 1438 | |
| 1439 | if (isTypeSpecifierQualifier()) { |
| 1440 | TypeTy *Ty = ParseTypeName(); |
| 1441 | |
Steve Naroff | 872da80 | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1442 | assert(Ty && "Parser::ParseTypeofSpecifier(): missing type"); |
| 1443 | |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1444 | if (Tok.getKind() != tok::r_paren) { |
Steve Naroff | 872da80 | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1445 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1446 | return; |
| 1447 | } |
| 1448 | RParenLoc = ConsumeParen(); |
| 1449 | const char *PrevSpec = 0; |
| 1450 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 1451 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty)) |
| 1452 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1453 | } else { // we have an expression. |
| 1454 | ExprResult Result = ParseExpression(); |
Steve Naroff | 872da80 | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1455 | |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1456 | if (Result.isInvalid || Tok.getKind() != tok::r_paren) { |
Steve Naroff | 872da80 | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1457 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1458 | return; |
| 1459 | } |
| 1460 | RParenLoc = ConsumeParen(); |
| 1461 | const char *PrevSpec = 0; |
| 1462 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 1463 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
| 1464 | Result.Val)) |
| 1465 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1466 | } |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |