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