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