Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Scope.h" |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 17 | #include "ExtensionRAIIObject.h" |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 18 | #include "AstGuard.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
| 20 | using namespace clang; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // C99 6.7: Declarations. |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | /// ParseTypeName |
| 27 | /// type-name: [C99 6.7.6] |
| 28 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 29 | /// |
| 30 | /// Called type-id in C++. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 31 | Parser::TypeTy *Parser::ParseTypeName() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | // Parse the common declaration-specifiers piece. |
| 33 | DeclSpec DS; |
| 34 | ParseSpecifierQualifierList(DS); |
| 35 | |
| 36 | // Parse the abstract-declarator, if present. |
| 37 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 38 | ParseDeclarator(DeclaratorInfo); |
| 39 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 40 | return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /// ParseAttributes - Parse a non-empty attributes list. |
| 44 | /// |
| 45 | /// [GNU] attributes: |
| 46 | /// attribute |
| 47 | /// attributes attribute |
| 48 | /// |
| 49 | /// [GNU] attribute: |
| 50 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 51 | /// |
| 52 | /// [GNU] attribute-list: |
| 53 | /// attrib |
| 54 | /// attribute_list ',' attrib |
| 55 | /// |
| 56 | /// [GNU] attrib: |
| 57 | /// empty |
| 58 | /// attrib-name |
| 59 | /// attrib-name '(' identifier ')' |
| 60 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 61 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 62 | /// |
| 63 | /// [GNU] attrib-name: |
| 64 | /// identifier |
| 65 | /// typespec |
| 66 | /// typequal |
| 67 | /// storageclass |
| 68 | /// |
| 69 | /// FIXME: The GCC grammar/code for this construct implies we need two |
| 70 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 71 | /// which is followed by a comma or close parenthesis, then the arguments |
| 72 | /// start with that identifier; otherwise they are an expression list." |
| 73 | /// |
| 74 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 75 | /// any attributes that don't work (based on my limited testing). Most |
| 76 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 77 | /// a pressing need to implement the 2 token lookahead. |
| 78 | |
| 79 | AttributeList *Parser::ParseAttributes() { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 80 | assert(Tok.is(tok::kw___attribute) && "Not an attribute list!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | |
| 82 | AttributeList *CurrAttr = 0; |
| 83 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 84 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | ConsumeToken(); |
| 86 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 87 | "attribute")) { |
| 88 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 89 | return CurrAttr; |
| 90 | } |
| 91 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 92 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 93 | return CurrAttr; |
| 94 | } |
| 95 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 96 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 97 | Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 99 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 101 | ConsumeToken(); |
| 102 | continue; |
| 103 | } |
| 104 | // we have an identifier or declaration specifier (const, int, etc.) |
| 105 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 106 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 107 | |
| 108 | // check if we have a "paramterized" attribute |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 109 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | ConsumeParen(); // ignore the left paren loc for now |
| 111 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 112 | if (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 114 | SourceLocation ParmLoc = ConsumeToken(); |
| 115 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 116 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | // __attribute__(( mode(byte) )) |
| 118 | ConsumeParen(); // ignore the right paren loc for now |
| 119 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 120 | ParmName, ParmLoc, 0, 0, CurrAttr); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 121 | } else if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | ConsumeToken(); |
| 123 | // __attribute__(( format(printf, 1, 2) )) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 124 | ExprVector ArgExprs(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | bool ArgExprsOk = true; |
| 126 | |
| 127 | // now parse the non-empty comma separated list of expressions |
| 128 | while (1) { |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 129 | ExprOwner ArgExpr(Actions, ParseAssignmentExpression()); |
| 130 | if (ArgExpr.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | ArgExprsOk = false; |
| 132 | SkipUntil(tok::r_paren); |
| 133 | break; |
| 134 | } else { |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 135 | ArgExprs.push_back(ArgExpr.move()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 137 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | break; |
| 139 | ConsumeToken(); // Eat the comma, move to the next argument |
| 140 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 141 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 142 | ConsumeParen(); // ignore the right paren loc for now |
| 143 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName, |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 144 | ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } else { // not an identifier |
| 148 | // parse a possibly empty comma separated list of expressions |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 149 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | // __attribute__(( nonnull() )) |
| 151 | ConsumeParen(); // ignore the right paren loc for now |
| 152 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 153 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 154 | } else { |
| 155 | // __attribute__(( aligned(16) )) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 156 | ExprVector ArgExprs(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | bool ArgExprsOk = true; |
| 158 | |
| 159 | // now parse the list of expressions |
| 160 | while (1) { |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 161 | ExprOwner ArgExpr(Actions, ParseAssignmentExpression()); |
| 162 | if (ArgExpr.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | ArgExprsOk = false; |
| 164 | SkipUntil(tok::r_paren); |
| 165 | break; |
| 166 | } else { |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 167 | ArgExprs.push_back(ArgExpr.move()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 169 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | break; |
| 171 | ConsumeToken(); // Eat the comma, move to the next argument |
| 172 | } |
| 173 | // Match the ')'. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 174 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | ConsumeParen(); // ignore the right paren loc for now |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 176 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
| 177 | SourceLocation(), ArgExprs.take(), ArgExprs.size(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | CurrAttr); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } else { |
| 183 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 184 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 185 | } |
| 186 | } |
| 187 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 188 | SkipUntil(tok::r_paren, false); |
| 189 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 190 | SkipUntil(tok::r_paren, false); |
| 191 | } |
| 192 | return CurrAttr; |
| 193 | } |
| 194 | |
| 195 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 196 | /// declaration-specifiers, some number of declarators, and a semicolon. |
| 197 | /// 'Context' should be a Declarator::TheContext value. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 198 | /// |
| 199 | /// declaration: [C99 6.7] |
| 200 | /// block-declaration -> |
| 201 | /// simple-declaration |
| 202 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 203 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 204 | /// [C++] namespace-definition |
| 205 | /// others... [FIXME] |
| 206 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 207 | Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 208 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 209 | case tok::kw_export: |
| 210 | case tok::kw_template: |
| 211 | return ParseTemplateDeclaration(Context); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 212 | case tok::kw_namespace: |
| 213 | return ParseNamespace(Context); |
| 214 | default: |
| 215 | return ParseSimpleDeclaration(Context); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 220 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 221 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 222 | /// [OMP] threadprivate-directive [TODO] |
| 223 | Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | // Parse the common declaration-specifiers piece. |
| 225 | DeclSpec DS; |
| 226 | ParseDeclarationSpecifiers(DS); |
| 227 | |
| 228 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 229 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 230 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 231 | ConsumeToken(); |
| 232 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 233 | } |
| 234 | |
| 235 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 236 | ParseDeclarator(DeclaratorInfo); |
| 237 | |
| 238 | return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
| 239 | } |
| 240 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 241 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 242 | /// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after |
| 243 | /// parsing 'declaration-specifiers declarator'. This method is split out this |
| 244 | /// way to handle the ambiguity between top-level function-definitions and |
| 245 | /// declarations. |
| 246 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | /// init-declarator-list: [C99 6.7] |
| 248 | /// init-declarator |
| 249 | /// init-declarator-list ',' init-declarator |
| 250 | /// init-declarator: [C99 6.7] |
| 251 | /// declarator |
| 252 | /// declarator '=' initializer |
| 253 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 254 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 255 | /// [C++] declarator initializer[opt] |
| 256 | /// |
| 257 | /// [C++] initializer: |
| 258 | /// [C++] '=' initializer-clause |
| 259 | /// [C++] '(' expression-list ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | /// |
| 261 | Parser::DeclTy *Parser:: |
| 262 | ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { |
| 263 | |
| 264 | // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so |
| 265 | // that they can be chained properly if the actions want this. |
| 266 | Parser::DeclTy *LastDeclInGroup = 0; |
| 267 | |
| 268 | // At this point, we know that it is not a function definition. Parse the |
| 269 | // rest of the init-declarator-list. |
| 270 | while (1) { |
| 271 | // If a simple-asm-expr is present, parse it. |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 272 | if (Tok.is(tok::kw_asm)) { |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 273 | ExprOwner AsmLabel(Actions, ParseSimpleAsm()); |
| 274 | if (AsmLabel.isInvalid()) { |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 275 | SkipUntil(tok::semi); |
| 276 | return 0; |
| 277 | } |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 278 | |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 279 | D.setAsmLabel(AsmLabel.move()); |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 280 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | |
| 282 | // If attributes are present, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 283 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | D.AddAttributes(ParseAttributes()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 285 | |
| 286 | // Inform the current actions module that we just parsed this declarator. |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 287 | LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 288 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 289 | // Parse declarator '=' initializer. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 290 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 292 | ExprOwner Init(Actions, ParseInitializer()); |
| 293 | if (Init.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | SkipUntil(tok::semi); |
| 295 | return 0; |
| 296 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 297 | Actions.AddInitializerToDecl(LastDeclInGroup, Init.move()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 298 | } else if (Tok.is(tok::l_paren)) { |
| 299 | // Parse C++ direct initializer: '(' expression-list ')' |
| 300 | SourceLocation LParenLoc = ConsumeParen(); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 301 | ExprVector Exprs(Actions); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 302 | CommaLocsTy CommaLocs; |
| 303 | |
| 304 | bool InvalidExpr = false; |
| 305 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 306 | SkipUntil(tok::r_paren); |
| 307 | InvalidExpr = true; |
| 308 | } |
| 309 | // Match the ')'. |
| 310 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 311 | |
| 312 | if (!InvalidExpr) { |
| 313 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 314 | "Unexpected number of commas!"); |
| 315 | Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc, |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 316 | Exprs.take(), Exprs.size(), |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 317 | &CommaLocs[0], RParenLoc); |
| 318 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 319 | } else { |
| 320 | Actions.ActOnUninitializedDecl(LastDeclInGroup); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 323 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 324 | // error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 325 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 326 | break; |
| 327 | |
| 328 | // Consume the comma. |
| 329 | ConsumeToken(); |
| 330 | |
| 331 | // Parse the next declarator. |
| 332 | D.clear(); |
Chris Lattner | aab740a | 2008-10-20 04:57:38 +0000 | [diff] [blame] | 333 | |
| 334 | // Accept attributes in an init-declarator. In the first declarator in a |
| 335 | // declaration, these would be part of the declspec. In subsequent |
| 336 | // declarators, they become part of the declarator itself, so that they |
| 337 | // don't apply to declarators after *this* one. Examples: |
| 338 | // short __attribute__((common)) var; -> declspec |
| 339 | // short var __attribute__((common)); -> declarator |
| 340 | // short x, __attribute__((common)) var; -> declarator |
| 341 | if (Tok.is(tok::kw___attribute)) |
| 342 | D.AddAttributes(ParseAttributes()); |
| 343 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 344 | ParseDeclarator(D); |
| 345 | } |
| 346 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 347 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 348 | ConsumeToken(); |
| 349 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 350 | } |
Fariborz Jahanian | bdd15f7 | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 351 | // If this is an ObjC2 for-each loop, this is a successful declarator |
| 352 | // parse. The syntax for these looks like: |
| 353 | // 'for' '(' declaration 'in' expr ')' statement |
Fariborz Jahanian | 335a2d4 | 2008-01-04 23:04:08 +0000 | [diff] [blame] | 354 | if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) { |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 355 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 356 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 357 | Diag(Tok, diag::err_parse_error); |
| 358 | // Skip to end of block or statement |
Chris Lattner | ed44238 | 2007-08-21 18:36:18 +0000 | [diff] [blame] | 359 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 360 | if (Tok.is(tok::semi)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | ConsumeToken(); |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | /// ParseSpecifierQualifierList |
| 366 | /// specifier-qualifier-list: |
| 367 | /// type-specifier specifier-qualifier-list[opt] |
| 368 | /// type-qualifier specifier-qualifier-list[opt] |
| 369 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 370 | /// |
| 371 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 372 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 373 | /// parse declaration-specifiers and complain about extra stuff. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 374 | ParseDeclarationSpecifiers(DS); |
| 375 | |
| 376 | // Validate declspec for type-name. |
| 377 | unsigned Specs = DS.getParsedSpecifiers(); |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 378 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 379 | Diag(Tok, diag::err_typename_requires_specqual); |
| 380 | |
| 381 | // Issue diagnostic and remove storage class if present. |
| 382 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 383 | if (DS.getStorageClassSpecLoc().isValid()) |
| 384 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 385 | else |
| 386 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 387 | DS.ClearStorageClassSpecs(); |
| 388 | } |
| 389 | |
| 390 | // Issue diagnostic and remove function specfier if present. |
| 391 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 392 | if (DS.isInlineSpecified()) |
| 393 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 394 | if (DS.isVirtualSpecified()) |
| 395 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 396 | if (DS.isExplicitSpecified()) |
| 397 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 398 | DS.ClearFunctionSpecs(); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /// ParseDeclarationSpecifiers |
| 403 | /// declaration-specifiers: [C99 6.7] |
| 404 | /// storage-class-specifier declaration-specifiers[opt] |
| 405 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 406 | /// [C99] function-specifier declaration-specifiers[opt] |
| 407 | /// [GNU] attributes declaration-specifiers[opt] |
| 408 | /// |
| 409 | /// storage-class-specifier: [C99 6.7.1] |
| 410 | /// 'typedef' |
| 411 | /// 'extern' |
| 412 | /// 'static' |
| 413 | /// 'auto' |
| 414 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 415 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 417 | /// function-specifier: [C99 6.7.4] |
| 418 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 419 | /// [C++] 'virtual' |
| 420 | /// [C++] 'explicit' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 421 | /// |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 422 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) |
| 423 | { |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 424 | DS.SetRangeStart(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 425 | while (1) { |
| 426 | int isInvalid = false; |
| 427 | const char *PrevSpec = 0; |
| 428 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 429 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 430 | // Only annotate C++ scope. Allow class-name as an identifier in case |
| 431 | // it's a constructor. |
Daniel Dunbar | ab4c91c | 2008-11-25 23:05:24 +0000 | [diff] [blame] | 432 | if (getLang().CPlusPlus) |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 433 | TryAnnotateCXXScopeToken(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 434 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 435 | switch (Tok.getKind()) { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 436 | default: |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 437 | // Try to parse a type-specifier; if we found one, continue. If it's not |
| 438 | // a type, this falls through. |
| 439 | if (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec)) { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 440 | continue; |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 441 | } |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 442 | |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 443 | DoneWithDeclSpec: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 444 | // If this is not a declaration specifier token, we're done reading decl |
| 445 | // specifiers. First verify that DeclSpec's are consistent. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 446 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 447 | return; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 448 | |
| 449 | case tok::annot_cxxscope: { |
| 450 | if (DS.hasTypeSpecifier()) |
| 451 | goto DoneWithDeclSpec; |
| 452 | |
| 453 | // We are looking for a qualified typename. |
| 454 | if (NextToken().isNot(tok::identifier)) |
| 455 | goto DoneWithDeclSpec; |
| 456 | |
| 457 | CXXScopeSpec SS; |
| 458 | SS.setScopeRep(Tok.getAnnotationValue()); |
| 459 | SS.setRange(Tok.getAnnotationRange()); |
| 460 | |
| 461 | // If the next token is the name of the class type that the C++ scope |
| 462 | // denotes, followed by a '(', then this is a constructor declaration. |
| 463 | // We're done with the decl-specifiers. |
| 464 | if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(), |
| 465 | CurScope, &SS) && |
| 466 | GetLookAheadToken(2).is(tok::l_paren)) |
| 467 | goto DoneWithDeclSpec; |
| 468 | |
| 469 | TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(), |
| 470 | CurScope, &SS); |
| 471 | if (TypeRep == 0) |
| 472 | goto DoneWithDeclSpec; |
| 473 | |
| 474 | ConsumeToken(); // The C++ scope. |
| 475 | |
| 476 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
| 477 | TypeRep); |
| 478 | if (isInvalid) |
| 479 | break; |
| 480 | |
| 481 | DS.SetRangeEnd(Tok.getLocation()); |
| 482 | ConsumeToken(); // The typename. |
| 483 | |
| 484 | continue; |
| 485 | } |
| 486 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 487 | // typedef-name |
| 488 | case tok::identifier: { |
| 489 | // This identifier can only be a typedef name if we haven't already seen |
| 490 | // a type-specifier. Without this check we misparse: |
| 491 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 492 | if (DS.hasTypeSpecifier()) |
| 493 | goto DoneWithDeclSpec; |
| 494 | |
| 495 | // It has to be available as a typedef too! |
Argyrios Kyrtzidis | 39caa08 | 2008-08-01 10:35:27 +0000 | [diff] [blame] | 496 | TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 497 | if (TypeRep == 0) |
| 498 | goto DoneWithDeclSpec; |
| 499 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 500 | // C++: If the identifier is actually the name of the class type |
| 501 | // being defined and the next token is a '(', then this is a |
| 502 | // constructor declaration. We're done with the decl-specifiers |
| 503 | // and will treat this token as an identifier. |
| 504 | if (getLang().CPlusPlus && |
| 505 | CurScope->isCXXClassScope() && |
| 506 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) && |
| 507 | NextToken().getKind() == tok::l_paren) |
| 508 | goto DoneWithDeclSpec; |
| 509 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 510 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
| 511 | TypeRep); |
| 512 | if (isInvalid) |
| 513 | break; |
| 514 | |
| 515 | DS.SetRangeEnd(Tok.getLocation()); |
| 516 | ConsumeToken(); // The identifier |
| 517 | |
| 518 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 519 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 520 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 521 | // just a normal reference to a typedef name. |
| 522 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 523 | continue; |
| 524 | |
| 525 | SourceLocation EndProtoLoc; |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 526 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 527 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 528 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 529 | |
| 530 | DS.SetRangeEnd(EndProtoLoc); |
| 531 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 532 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 533 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 534 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 535 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 536 | // GNU attributes support. |
| 537 | case tok::kw___attribute: |
| 538 | DS.AddAttributes(ParseAttributes()); |
| 539 | continue; |
| 540 | |
| 541 | // storage-class-specifier |
| 542 | case tok::kw_typedef: |
| 543 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec); |
| 544 | break; |
| 545 | case tok::kw_extern: |
| 546 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 547 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 548 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec); |
| 549 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 550 | case tok::kw___private_extern__: |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 551 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, |
| 552 | PrevSpec); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 553 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 554 | case tok::kw_static: |
| 555 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 556 | Diag(Tok, diag::ext_thread_before) << "static"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 557 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec); |
| 558 | break; |
| 559 | case tok::kw_auto: |
| 560 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec); |
| 561 | break; |
| 562 | case tok::kw_register: |
| 563 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec); |
| 564 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 565 | case tok::kw_mutable: |
| 566 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec); |
| 567 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 568 | case tok::kw___thread: |
| 569 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2; |
| 570 | break; |
| 571 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 572 | continue; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 573 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 574 | // function-specifier |
| 575 | case tok::kw_inline: |
| 576 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec); |
| 577 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 578 | |
| 579 | case tok::kw_virtual: |
| 580 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec); |
| 581 | break; |
| 582 | |
| 583 | case tok::kw_explicit: |
| 584 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec); |
| 585 | break; |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 586 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 587 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 588 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 589 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 590 | // but we support it. |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 591 | if (DS.hasTypeSpecifier() || !getLang().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 592 | goto DoneWithDeclSpec; |
| 593 | |
| 594 | { |
| 595 | SourceLocation EndProtoLoc; |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 596 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 597 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 598 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 599 | DS.SetRangeEnd(EndProtoLoc); |
| 600 | |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 601 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 602 | << SourceRange(Loc, EndProtoLoc); |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 603 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 604 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 605 | continue; |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 606 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 607 | } |
| 608 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 609 | if (isInvalid) { |
| 610 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 611 | // Pick between error or extwarn. |
| 612 | unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination |
| 613 | : diag::ext_duplicate_declspec; |
| 614 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 615 | } |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 616 | DS.SetRangeEnd(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 617 | ConsumeToken(); |
| 618 | } |
| 619 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 620 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 621 | /// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We |
| 622 | /// primarily follow the C++ grammar with additions for C99 and GNU, |
| 623 | /// which together subsume the C grammar. Note that the C++ |
| 624 | /// type-specifier also includes the C type-qualifier (for const, |
| 625 | /// volatile, and C99 restrict). Returns true if a type-specifier was |
| 626 | /// found (and parsed), false otherwise. |
| 627 | /// |
| 628 | /// type-specifier: [C++ 7.1.5] |
| 629 | /// simple-type-specifier |
| 630 | /// class-specifier |
| 631 | /// enum-specifier |
| 632 | /// elaborated-type-specifier [TODO] |
| 633 | /// cv-qualifier |
| 634 | /// |
| 635 | /// cv-qualifier: [C++ 7.1.5.1] |
| 636 | /// 'const' |
| 637 | /// 'volatile' |
| 638 | /// [C99] 'restrict' |
| 639 | /// |
| 640 | /// simple-type-specifier: [ C++ 7.1.5.2] |
| 641 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 642 | /// '::'[opt] nested-name-specifier 'template' template-id [TODO] |
| 643 | /// 'char' |
| 644 | /// 'wchar_t' |
| 645 | /// 'bool' |
| 646 | /// 'short' |
| 647 | /// 'int' |
| 648 | /// 'long' |
| 649 | /// 'signed' |
| 650 | /// 'unsigned' |
| 651 | /// 'float' |
| 652 | /// 'double' |
| 653 | /// 'void' |
| 654 | /// [C99] '_Bool' |
| 655 | /// [C99] '_Complex' |
| 656 | /// [C99] '_Imaginary' // Removed in TC2? |
| 657 | /// [GNU] '_Decimal32' |
| 658 | /// [GNU] '_Decimal64' |
| 659 | /// [GNU] '_Decimal128' |
| 660 | /// [GNU] typeof-specifier |
| 661 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
| 662 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
| 663 | bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid, |
| 664 | const char *&PrevSpec) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 665 | // Annotate typenames and C++ scope specifiers. |
| 666 | TryAnnotateTypeOrScopeToken(); |
| 667 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 668 | SourceLocation Loc = Tok.getLocation(); |
| 669 | |
| 670 | switch (Tok.getKind()) { |
| 671 | // simple-type-specifier: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 672 | case tok::annot_qualtypename: { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 673 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 674 | Tok.getAnnotationValue()); |
| 675 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 676 | ConsumeToken(); // The typename |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 677 | |
| 678 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 679 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 680 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 681 | // just a normal reference to a typedef name. |
| 682 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 683 | return true; |
| 684 | |
| 685 | SourceLocation EndProtoLoc; |
| 686 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
| 687 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
| 688 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
| 689 | |
| 690 | DS.SetRangeEnd(EndProtoLoc); |
| 691 | return true; |
| 692 | } |
| 693 | |
| 694 | case tok::kw_short: |
| 695 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 696 | break; |
| 697 | case tok::kw_long: |
| 698 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
| 699 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 700 | else |
| 701 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
| 702 | break; |
| 703 | case tok::kw_signed: |
| 704 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 705 | break; |
| 706 | case tok::kw_unsigned: |
| 707 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 708 | break; |
| 709 | case tok::kw__Complex: |
| 710 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
| 711 | break; |
| 712 | case tok::kw__Imaginary: |
| 713 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
| 714 | break; |
| 715 | case tok::kw_void: |
| 716 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 717 | break; |
| 718 | case tok::kw_char: |
| 719 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 720 | break; |
| 721 | case tok::kw_int: |
| 722 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 723 | break; |
| 724 | case tok::kw_float: |
| 725 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 726 | break; |
| 727 | case tok::kw_double: |
| 728 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 729 | break; |
| 730 | case tok::kw_wchar_t: |
| 731 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec); |
| 732 | break; |
| 733 | case tok::kw_bool: |
| 734 | case tok::kw__Bool: |
| 735 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 736 | break; |
| 737 | case tok::kw__Decimal32: |
| 738 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
| 739 | break; |
| 740 | case tok::kw__Decimal64: |
| 741 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
| 742 | break; |
| 743 | case tok::kw__Decimal128: |
| 744 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
| 745 | break; |
| 746 | |
| 747 | // class-specifier: |
| 748 | case tok::kw_class: |
| 749 | case tok::kw_struct: |
| 750 | case tok::kw_union: |
| 751 | ParseClassSpecifier(DS); |
| 752 | return true; |
| 753 | |
| 754 | // enum-specifier: |
| 755 | case tok::kw_enum: |
| 756 | ParseEnumSpecifier(DS); |
| 757 | return true; |
| 758 | |
| 759 | // cv-qualifier: |
| 760 | case tok::kw_const: |
| 761 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 762 | getLang())*2; |
| 763 | break; |
| 764 | case tok::kw_volatile: |
| 765 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 766 | getLang())*2; |
| 767 | break; |
| 768 | case tok::kw_restrict: |
| 769 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 770 | getLang())*2; |
| 771 | break; |
| 772 | |
| 773 | // GNU typeof support. |
| 774 | case tok::kw_typeof: |
| 775 | ParseTypeofSpecifier(DS); |
| 776 | return true; |
| 777 | |
| 778 | default: |
| 779 | // Not a type-specifier; do nothing. |
| 780 | return false; |
| 781 | } |
| 782 | |
| 783 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 784 | if (isInvalid) { |
| 785 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 786 | // Pick between error or extwarn. |
| 787 | unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination |
| 788 | : diag::ext_duplicate_declspec; |
| 789 | Diag(Tok, DiagID) << PrevSpec; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 790 | } |
| 791 | DS.SetRangeEnd(Tok.getLocation()); |
| 792 | ConsumeToken(); // whatever we parsed above. |
| 793 | return true; |
| 794 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 795 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 796 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 797 | /// semicolon. |
| 798 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 799 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 800 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 801 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 802 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 803 | /// struct-declarator-list: |
| 804 | /// struct-declarator |
| 805 | /// struct-declarator-list ',' struct-declarator |
| 806 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 807 | /// struct-declarator: |
| 808 | /// declarator |
| 809 | /// [GNU] declarator attributes[opt] |
| 810 | /// declarator[opt] ':' constant-expression |
| 811 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 812 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 813 | void Parser:: |
| 814 | ParseStructDeclaration(DeclSpec &DS, |
| 815 | llvm::SmallVectorImpl<FieldDeclarator> &Fields) { |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 816 | if (Tok.is(tok::kw___extension__)) { |
| 817 | // __extension__ silences extension warnings in the subexpression. |
| 818 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 819 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 820 | return ParseStructDeclaration(DS, Fields); |
| 821 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 822 | |
| 823 | // Parse the common specifier-qualifiers-list piece. |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 824 | SourceLocation DSStart = Tok.getLocation(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 825 | ParseSpecifierQualifierList(DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 826 | |
| 827 | // If there are no declarators, issue a warning. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 828 | if (Tok.is(tok::semi)) { |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 829 | Diag(DSStart, diag::w_no_declarators); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 830 | return; |
| 831 | } |
| 832 | |
| 833 | // Read struct-declarators until we find the semicolon. |
Chris Lattner | ebe457c | 2008-04-10 16:37:40 +0000 | [diff] [blame] | 834 | Fields.push_back(FieldDeclarator(DS)); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 835 | while (1) { |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 836 | FieldDeclarator &DeclaratorInfo = Fields.back(); |
| 837 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 838 | /// struct-declarator: declarator |
| 839 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 840 | if (Tok.isNot(tok::colon)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 841 | ParseDeclarator(DeclaratorInfo.D); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 842 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 843 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 844 | ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 845 | ExprOwner Res(Actions, ParseConstantExpression()); |
| 846 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 847 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 848 | else |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 849 | DeclaratorInfo.BitfieldSize = Res.move(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | // If attributes exist after the declarator, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 853 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 854 | DeclaratorInfo.D.AddAttributes(ParseAttributes()); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 855 | |
| 856 | // If we don't have a comma, it is either the end of the list (a ';') |
| 857 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 858 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 859 | return; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 860 | |
| 861 | // Consume the comma. |
| 862 | ConsumeToken(); |
| 863 | |
| 864 | // Parse the next declarator. |
Chris Lattner | ebe457c | 2008-04-10 16:37:40 +0000 | [diff] [blame] | 865 | Fields.push_back(FieldDeclarator(DS)); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 866 | |
| 867 | // Attributes are only allowed on the second declarator. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 868 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 869 | Fields.back().D.AddAttributes(ParseAttributes()); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 870 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /// ParseStructUnionBody |
| 874 | /// struct-contents: |
| 875 | /// struct-declaration-list |
| 876 | /// [EXT] empty |
| 877 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 878 | /// struct-declaration-list: |
| 879 | /// struct-declaration |
| 880 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 881 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 882 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 883 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
| 884 | unsigned TagType, DeclTy *TagDecl) { |
| 885 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 886 | |
| 887 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 888 | // C++. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 889 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 890 | Diag(Tok, diag::ext_empty_struct_union_enum) |
| 891 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 892 | |
| 893 | llvm::SmallVector<DeclTy*, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 894 | llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators; |
| 895 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 896 | // While we still have something to read, read the declarations in the struct. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 897 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 898 | // Each iteration of this loop reads one struct-declaration. |
| 899 | |
| 900 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 901 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 902 | Diag(Tok, diag::ext_extra_struct_semi); |
| 903 | ConsumeToken(); |
| 904 | continue; |
| 905 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 906 | |
| 907 | // Parse all the comma separated declarators. |
| 908 | DeclSpec DS; |
| 909 | FieldDeclarators.clear(); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 910 | if (!Tok.is(tok::at)) { |
| 911 | ParseStructDeclaration(DS, FieldDeclarators); |
| 912 | |
| 913 | // Convert them all to fields. |
| 914 | for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) { |
| 915 | FieldDeclarator &FD = FieldDeclarators[i]; |
| 916 | // Install the declarator into the current TagDecl. |
| 917 | DeclTy *Field = Actions.ActOnField(CurScope, |
| 918 | DS.getSourceRange().getBegin(), |
| 919 | FD.D, FD.BitfieldSize); |
| 920 | FieldDecls.push_back(Field); |
| 921 | } |
| 922 | } else { // Handle @defs |
| 923 | ConsumeToken(); |
| 924 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 925 | Diag(Tok, diag::err_unexpected_at); |
| 926 | SkipUntil(tok::semi, true, true); |
| 927 | continue; |
| 928 | } |
| 929 | ConsumeToken(); |
| 930 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 931 | if (!Tok.is(tok::identifier)) { |
| 932 | Diag(Tok, diag::err_expected_ident); |
| 933 | SkipUntil(tok::semi, true, true); |
| 934 | continue; |
| 935 | } |
| 936 | llvm::SmallVector<DeclTy*, 16> Fields; |
| 937 | Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(), |
| 938 | Fields); |
| 939 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 940 | ConsumeToken(); |
| 941 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
| 942 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 943 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 944 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 945 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 946 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 947 | Diag(Tok, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 948 | break; |
| 949 | } else { |
| 950 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 951 | // Skip to end of block or statement |
| 952 | SkipUntil(tok::r_brace, true, true); |
| 953 | } |
| 954 | } |
| 955 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 956 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 957 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 958 | AttributeList *AttrList = 0; |
| 959 | // If attributes exist after struct contents, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 960 | if (Tok.is(tok::kw___attribute)) |
Daniel Dunbar | 5e592d8 | 2008-10-03 16:42:10 +0000 | [diff] [blame] | 961 | AttrList = ParseAttributes(); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 962 | |
| 963 | Actions.ActOnFields(CurScope, |
| 964 | RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(), |
| 965 | LBraceLoc, RBraceLoc, |
| 966 | AttrList); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | |
| 970 | /// ParseEnumSpecifier |
| 971 | /// enum-specifier: [C99 6.7.2.2] |
| 972 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 973 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 974 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 975 | /// '}' attributes[opt] |
| 976 | /// 'enum' identifier |
| 977 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 978 | /// |
| 979 | /// [C++] elaborated-type-specifier: |
| 980 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 981 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 982 | void Parser::ParseEnumSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 983 | assert(Tok.is(tok::kw_enum) && "Not an enum specifier"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 984 | SourceLocation StartLoc = ConsumeToken(); |
| 985 | |
| 986 | // Parse the tag portion of this. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 987 | |
| 988 | AttributeList *Attr = 0; |
| 989 | // If attributes exist after tag, parse them. |
| 990 | if (Tok.is(tok::kw___attribute)) |
| 991 | Attr = ParseAttributes(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 992 | |
| 993 | CXXScopeSpec SS; |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 994 | if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 995 | if (Tok.isNot(tok::identifier)) { |
| 996 | Diag(Tok, diag::err_expected_ident); |
| 997 | if (Tok.isNot(tok::l_brace)) { |
| 998 | // Has no name and is not a definition. |
| 999 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 1000 | SkipUntil(tok::comma, true); |
| 1001 | return; |
| 1002 | } |
| 1003 | } |
| 1004 | } |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1005 | |
| 1006 | // Must have either 'enum name' or 'enum {...}'. |
| 1007 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) { |
| 1008 | Diag(Tok, diag::err_expected_ident_lbrace); |
| 1009 | |
| 1010 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 1011 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1012 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | // If an identifier is present, consume and remember it. |
| 1016 | IdentifierInfo *Name = 0; |
| 1017 | SourceLocation NameLoc; |
| 1018 | if (Tok.is(tok::identifier)) { |
| 1019 | Name = Tok.getIdentifierInfo(); |
| 1020 | NameLoc = ConsumeToken(); |
| 1021 | } |
| 1022 | |
| 1023 | // There are three options here. If we have 'enum foo;', then this is a |
| 1024 | // forward declaration. If we have 'enum foo {...' then this is a |
| 1025 | // definition. Otherwise we have something like 'enum foo xyz', a reference. |
| 1026 | // |
| 1027 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 1028 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 1029 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 1030 | // |
| 1031 | Action::TagKind TK; |
| 1032 | if (Tok.is(tok::l_brace)) |
| 1033 | TK = Action::TK_Definition; |
| 1034 | else if (Tok.is(tok::semi)) |
| 1035 | TK = Action::TK_Declaration; |
| 1036 | else |
| 1037 | TK = Action::TK_Reference; |
| 1038 | DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1039 | SS, Name, NameLoc, Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1041 | if (Tok.is(tok::l_brace)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1042 | ParseEnumBody(StartLoc, TagDecl); |
| 1043 | |
| 1044 | // TODO: semantic analysis on the declspec for enums. |
| 1045 | const char *PrevSpec = 0; |
| 1046 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl)) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1047 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 1051 | /// enumerator-list: |
| 1052 | /// enumerator |
| 1053 | /// enumerator-list ',' enumerator |
| 1054 | /// enumerator: |
| 1055 | /// enumeration-constant |
| 1056 | /// enumeration-constant '=' constant-expression |
| 1057 | /// enumeration-constant: |
| 1058 | /// identifier |
| 1059 | /// |
| 1060 | void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) { |
| 1061 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 1062 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 1063 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1064 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1065 | Diag(Tok, diag::ext_empty_struct_union_enum) << "enum"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1066 | |
| 1067 | llvm::SmallVector<DeclTy*, 32> EnumConstantDecls; |
| 1068 | |
| 1069 | DeclTy *LastEnumConstDecl = 0; |
| 1070 | |
| 1071 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1072 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1073 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 1074 | SourceLocation IdentLoc = ConsumeToken(); |
| 1075 | |
| 1076 | SourceLocation EqualLoc; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1077 | ExprOwner AssignedVal(Actions); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1078 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1079 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1080 | AssignedVal = ParseConstantExpression(); |
| 1081 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1082 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | // Install the enumerator constant into EnumDecl. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1086 | DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1087 | LastEnumConstDecl, |
| 1088 | IdentLoc, Ident, |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1089 | EqualLoc, |
| 1090 | AssignedVal.move()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1091 | EnumConstantDecls.push_back(EnumConstDecl); |
| 1092 | LastEnumConstDecl = EnumConstDecl; |
| 1093 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1094 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1095 | break; |
| 1096 | SourceLocation CommaLoc = ConsumeToken(); |
| 1097 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1098 | if (Tok.isNot(tok::identifier) && !getLang().C99) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1099 | Diag(CommaLoc, diag::ext_c99_enumerator_list_comma); |
| 1100 | } |
| 1101 | |
| 1102 | // Eat the }. |
| 1103 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 1104 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1105 | Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0], |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1106 | EnumConstantDecls.size()); |
| 1107 | |
| 1108 | DeclTy *AttrList = 0; |
| 1109 | // If attributes exist after the identifier list, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1110 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1111 | AttrList = ParseAttributes(); // FIXME: where do they do? |
| 1112 | } |
| 1113 | |
| 1114 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1115 | /// start of a type-qualifier-list. |
| 1116 | bool Parser::isTypeQualifier() const { |
| 1117 | switch (Tok.getKind()) { |
| 1118 | default: return false; |
| 1119 | // type-qualifier |
| 1120 | case tok::kw_const: |
| 1121 | case tok::kw_volatile: |
| 1122 | case tok::kw_restrict: |
| 1123 | return true; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1128 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1129 | bool Parser::isTypeSpecifierQualifier() { |
| 1130 | // Annotate typenames and C++ scope specifiers. |
| 1131 | TryAnnotateTypeOrScopeToken(); |
| 1132 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1133 | switch (Tok.getKind()) { |
| 1134 | default: return false; |
| 1135 | // GNU attributes support. |
| 1136 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1137 | // GNU typeof support. |
| 1138 | case tok::kw_typeof: |
| 1139 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1140 | // type-specifiers |
| 1141 | case tok::kw_short: |
| 1142 | case tok::kw_long: |
| 1143 | case tok::kw_signed: |
| 1144 | case tok::kw_unsigned: |
| 1145 | case tok::kw__Complex: |
| 1146 | case tok::kw__Imaginary: |
| 1147 | case tok::kw_void: |
| 1148 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1149 | case tok::kw_wchar_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1150 | case tok::kw_int: |
| 1151 | case tok::kw_float: |
| 1152 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 1153 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1154 | case tok::kw__Bool: |
| 1155 | case tok::kw__Decimal32: |
| 1156 | case tok::kw__Decimal64: |
| 1157 | case tok::kw__Decimal128: |
| 1158 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 1159 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 1160 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1161 | case tok::kw_struct: |
| 1162 | case tok::kw_union: |
| 1163 | // enum-specifier |
| 1164 | case tok::kw_enum: |
| 1165 | |
| 1166 | // type-qualifier |
| 1167 | case tok::kw_const: |
| 1168 | case tok::kw_volatile: |
| 1169 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1170 | |
| 1171 | // typedef-name |
| 1172 | case tok::annot_qualtypename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1173 | return true; |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 1174 | |
| 1175 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 1176 | case tok::less: |
| 1177 | return getLang().ObjC1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 1182 | /// declaration specifier. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1183 | bool Parser::isDeclarationSpecifier() { |
| 1184 | // Annotate typenames and C++ scope specifiers. |
| 1185 | TryAnnotateTypeOrScopeToken(); |
| 1186 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1187 | switch (Tok.getKind()) { |
| 1188 | default: return false; |
| 1189 | // storage-class-specifier |
| 1190 | case tok::kw_typedef: |
| 1191 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1192 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1193 | case tok::kw_static: |
| 1194 | case tok::kw_auto: |
| 1195 | case tok::kw_register: |
| 1196 | case tok::kw___thread: |
| 1197 | |
| 1198 | // type-specifiers |
| 1199 | case tok::kw_short: |
| 1200 | case tok::kw_long: |
| 1201 | case tok::kw_signed: |
| 1202 | case tok::kw_unsigned: |
| 1203 | case tok::kw__Complex: |
| 1204 | case tok::kw__Imaginary: |
| 1205 | case tok::kw_void: |
| 1206 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1207 | case tok::kw_wchar_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1208 | case tok::kw_int: |
| 1209 | case tok::kw_float: |
| 1210 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 1211 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1212 | case tok::kw__Bool: |
| 1213 | case tok::kw__Decimal32: |
| 1214 | case tok::kw__Decimal64: |
| 1215 | case tok::kw__Decimal128: |
| 1216 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 1217 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 1218 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1219 | case tok::kw_struct: |
| 1220 | case tok::kw_union: |
| 1221 | // enum-specifier |
| 1222 | case tok::kw_enum: |
| 1223 | |
| 1224 | // type-qualifier |
| 1225 | case tok::kw_const: |
| 1226 | case tok::kw_volatile: |
| 1227 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1228 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1229 | // function-specifier |
| 1230 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1231 | case tok::kw_virtual: |
| 1232 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 1233 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1234 | // typedef-name |
| 1235 | case tok::annot_qualtypename: |
| 1236 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 1237 | // GNU typeof support. |
| 1238 | case tok::kw_typeof: |
| 1239 | |
| 1240 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 1241 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1242 | return true; |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 1243 | |
| 1244 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 1245 | case tok::less: |
| 1246 | return getLang().ObjC1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | /// ParseTypeQualifierListOpt |
| 1252 | /// type-qualifier-list: [C99 6.7.5] |
| 1253 | /// type-qualifier |
| 1254 | /// [GNU] attributes |
| 1255 | /// type-qualifier-list type-qualifier |
| 1256 | /// [GNU] type-qualifier-list attributes |
| 1257 | /// |
| 1258 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) { |
| 1259 | while (1) { |
| 1260 | int isInvalid = false; |
| 1261 | const char *PrevSpec = 0; |
| 1262 | SourceLocation Loc = Tok.getLocation(); |
| 1263 | |
| 1264 | switch (Tok.getKind()) { |
| 1265 | default: |
| 1266 | // If this is not a type-qualifier token, we're done reading type |
| 1267 | // qualifiers. First verify that DeclSpec's are consistent. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 1268 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1269 | return; |
| 1270 | case tok::kw_const: |
| 1271 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 1272 | getLang())*2; |
| 1273 | break; |
| 1274 | case tok::kw_volatile: |
| 1275 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 1276 | getLang())*2; |
| 1277 | break; |
| 1278 | case tok::kw_restrict: |
| 1279 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 1280 | getLang())*2; |
| 1281 | break; |
| 1282 | case tok::kw___attribute: |
| 1283 | DS.AddAttributes(ParseAttributes()); |
| 1284 | continue; // do *not* consume the next token! |
| 1285 | } |
| 1286 | |
| 1287 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 1288 | if (isInvalid) { |
| 1289 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1290 | // Pick between error or extwarn. |
| 1291 | unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination |
| 1292 | : diag::ext_duplicate_declspec; |
| 1293 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1294 | } |
| 1295 | ConsumeToken(); |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | |
| 1300 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 1301 | /// |
| 1302 | void Parser::ParseDeclarator(Declarator &D) { |
| 1303 | /// This implements the 'declarator' production in the C grammar, then checks |
| 1304 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1305 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1308 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 1309 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 1310 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1311 | /// ptr-operator production. |
| 1312 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1313 | /// declarator: [C99 6.7.5] |
| 1314 | /// pointer[opt] direct-declarator |
| 1315 | /// [C++] '&' declarator [C++ 8p4, dcl.decl] |
| 1316 | /// [GNU] '&' restrict[opt] attributes[opt] declarator |
| 1317 | /// |
| 1318 | /// pointer: [C99 6.7.5] |
| 1319 | /// '*' type-qualifier-list[opt] |
| 1320 | /// '*' type-qualifier-list[opt] pointer |
| 1321 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1322 | /// ptr-operator: |
| 1323 | /// '*' cv-qualifier-seq[opt] |
| 1324 | /// '&' |
| 1325 | /// [GNU] '&' restrict[opt] attributes[opt] |
| 1326 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1327 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 1328 | DirectDeclParseFunction DirectDeclParser) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1329 | tok::TokenKind Kind = Tok.getKind(); |
| 1330 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1331 | // Not a pointer, C++ reference, or block. |
| 1332 | if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) && |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1333 | (Kind != tok::caret || !getLang().Blocks)) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1334 | if (DirectDeclParser) |
| 1335 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1336 | return; |
| 1337 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1338 | |
Steve Naroff | 4ef1c99 | 2008-08-28 10:07:06 +0000 | [diff] [blame] | 1339 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1340 | SourceLocation Loc = ConsumeToken(); // Eat the * or &. |
| 1341 | |
Steve Naroff | 4ef1c99 | 2008-08-28 10:07:06 +0000 | [diff] [blame] | 1342 | if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 1343 | // Is a pointer. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1344 | DeclSpec DS; |
| 1345 | |
| 1346 | ParseTypeQualifierListOpt(DS); |
| 1347 | |
| 1348 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1349 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1350 | if (Kind == tok::star) |
| 1351 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1352 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
| 1353 | DS.TakeAttributes())); |
| 1354 | else |
| 1355 | // Remember that we parsed a Block type, and remember the type-quals. |
| 1356 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
| 1357 | Loc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1358 | } else { |
| 1359 | // Is a reference |
| 1360 | DeclSpec DS; |
| 1361 | |
| 1362 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 1363 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 1364 | // template type argument, in which case the cv-qualifiers are ignored. |
| 1365 | // |
| 1366 | // [GNU] Retricted references are allowed. |
| 1367 | // [GNU] Attributes on references are allowed. |
| 1368 | ParseTypeQualifierListOpt(DS); |
| 1369 | |
| 1370 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 1371 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 1372 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1373 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1374 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 1375 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1376 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1380 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1381 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1382 | if (D.getNumTypeObjects() > 0) { |
| 1383 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 1384 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 1385 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 1386 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 1387 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 1388 | << II; |
| 1389 | else |
| 1390 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 1391 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1392 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1393 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1394 | // can go ahead and build the (technically ill-formed) |
| 1395 | // declarator: reference collapsing will take care of it. |
| 1396 | } |
| 1397 | } |
| 1398 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1399 | // Remember that we parsed a reference type. It doesn't have type-quals. |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 1400 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
| 1401 | DS.TakeAttributes())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | /// ParseDirectDeclarator |
| 1406 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1407 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1408 | /// '(' declarator ')' |
| 1409 | /// [GNU] '(' attributes declarator ')' |
| 1410 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1411 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1412 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1413 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1414 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1415 | /// direct-declarator '(' parameter-type-list ')' |
| 1416 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1417 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1418 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1419 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 1420 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1421 | /// [C++] declarator-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1422 | /// |
| 1423 | /// declarator-id: [C++ 8] |
| 1424 | /// id-expression |
| 1425 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 1426 | /// |
| 1427 | /// id-expression: [C++ 5.1] |
| 1428 | /// unqualified-id |
| 1429 | /// qualified-id [TODO] |
| 1430 | /// |
| 1431 | /// unqualified-id: [C++ 5.1] |
| 1432 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1433 | /// operator-function-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1434 | /// conversion-function-id [TODO] |
| 1435 | /// '~' class-name |
| 1436 | /// template-id [TODO] |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1437 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1438 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1439 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1440 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1441 | if (getLang().CPlusPlus) { |
| 1442 | if (D.mayHaveIdentifier()) { |
| 1443 | bool afterCXXScope = MaybeParseCXXScopeSpecifier(D.getCXXScopeSpec()); |
| 1444 | if (afterCXXScope) { |
| 1445 | // Change the declaration context for name lookup, until this function |
| 1446 | // is exited (and the declarator has been parsed). |
| 1447 | DeclScopeObj.EnterDeclaratorScope(); |
| 1448 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1449 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1450 | if (Tok.is(tok::identifier)) { |
| 1451 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 1452 | // Determine whether this identifier is a C++ constructor name or |
| 1453 | // a normal identifier. |
| 1454 | if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope)) { |
| 1455 | D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(), |
| 1456 | CurScope), |
| 1457 | Tok.getLocation()); |
| 1458 | } else |
| 1459 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1460 | ConsumeToken(); |
| 1461 | goto PastIdentifier; |
| 1462 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1463 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1464 | if (Tok.is(tok::tilde)) { |
| 1465 | // This should be a C++ destructor. |
| 1466 | SourceLocation TildeLoc = ConsumeToken(); |
| 1467 | if (Tok.is(tok::identifier)) { |
| 1468 | if (TypeTy *Type = ParseClassName()) |
| 1469 | D.setDestructor(Type, TildeLoc); |
| 1470 | else |
| 1471 | D.SetIdentifier(0, TildeLoc); |
| 1472 | } else { |
| 1473 | Diag(Tok, diag::err_expected_class_name); |
| 1474 | D.SetIdentifier(0, TildeLoc); |
| 1475 | } |
| 1476 | goto PastIdentifier; |
| 1477 | } |
| 1478 | |
| 1479 | // If we reached this point, token is not identifier and not '~'. |
| 1480 | |
| 1481 | if (afterCXXScope) { |
| 1482 | Diag(Tok, diag::err_expected_unqualified_id); |
| 1483 | D.SetIdentifier(0, Tok.getLocation()); |
| 1484 | D.setInvalidType(true); |
| 1485 | goto PastIdentifier; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1486 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1487 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1488 | |
| 1489 | if (Tok.is(tok::kw_operator)) { |
| 1490 | SourceLocation OperatorLoc = Tok.getLocation(); |
| 1491 | |
| 1492 | // First try the name of an overloaded operator |
| 1493 | if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) { |
| 1494 | D.setOverloadedOperator(Op, OperatorLoc); |
| 1495 | } else { |
| 1496 | // This must be a conversion function (C++ [class.conv.fct]). |
| 1497 | if (TypeTy *ConvType = ParseConversionFunctionId()) |
| 1498 | D.setConversionFunction(ConvType, OperatorLoc); |
| 1499 | else |
| 1500 | D.SetIdentifier(0, Tok.getLocation()); |
| 1501 | } |
| 1502 | goto PastIdentifier; |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | // If we reached this point, we are either in C/ObjC or the token didn't |
| 1507 | // satisfy any of the C++-specific checks. |
| 1508 | |
| 1509 | if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
| 1510 | assert(!getLang().CPlusPlus && |
| 1511 | "There's a C++-specific check for tok::identifier above"); |
| 1512 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 1513 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1514 | ConsumeToken(); |
| 1515 | } else if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1516 | // direct-declarator: '(' declarator ')' |
| 1517 | // direct-declarator: '(' attributes declarator ')' |
| 1518 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 1519 | ParseParenDeclarator(D); |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1520 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1521 | // This could be something simple like "int" (in which case the declarator |
| 1522 | // portion is empty), if an abstract-declarator is allowed. |
| 1523 | D.SetIdentifier(0, Tok.getLocation()); |
| 1524 | } else { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1525 | if (getLang().CPlusPlus) |
| 1526 | Diag(Tok, diag::err_expected_unqualified_id); |
| 1527 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1528 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1529 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 1530 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1533 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1534 | assert(D.isPastIdentifier() && |
| 1535 | "Haven't past the location of the identifier yet?"); |
| 1536 | |
| 1537 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1538 | if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1539 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 1540 | // In such a case, check if we actually have a function declarator; if it |
| 1541 | // is not, the declarator has been fully parsed. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1542 | if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 1543 | // When not in file scope, warn for ambiguous function declarators, just |
| 1544 | // in case the author intended it as a variable definition. |
| 1545 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 1546 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 1547 | break; |
| 1548 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1549 | ParseFunctionDeclarator(ConsumeParen(), D); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1550 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1551 | ParseBracketDeclarator(D); |
| 1552 | } else { |
| 1553 | break; |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1558 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 1559 | /// only called before the identifier, so these are most likely just grouping |
| 1560 | /// parens for precedence. If we find that these are actually function |
| 1561 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 1562 | /// |
| 1563 | /// direct-declarator: |
| 1564 | /// '(' declarator ')' |
| 1565 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1566 | /// direct-declarator '(' parameter-type-list ')' |
| 1567 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1568 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1569 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1570 | /// |
| 1571 | void Parser::ParseParenDeclarator(Declarator &D) { |
| 1572 | SourceLocation StartLoc = ConsumeParen(); |
| 1573 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
| 1574 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1575 | // Eat any attributes before we look at whether this is a grouping or function |
| 1576 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 1577 | // the type being built up, for example: |
| 1578 | // int (__attribute__(()) *x)(long y) |
| 1579 | // If this ends up not being a grouping paren, the attribute applies to the |
| 1580 | // first argument, for example: |
| 1581 | // int (__attribute__(()) int x) |
| 1582 | // In either case, we need to eat any attributes to be able to determine what |
| 1583 | // sort of paren this is. |
| 1584 | // |
| 1585 | AttributeList *AttrList = 0; |
| 1586 | bool RequiresArg = false; |
| 1587 | if (Tok.is(tok::kw___attribute)) { |
| 1588 | AttrList = ParseAttributes(); |
| 1589 | |
| 1590 | // We require that the argument list (if this is a non-grouping paren) be |
| 1591 | // present even if the attribute list was empty. |
| 1592 | RequiresArg = true; |
| 1593 | } |
| 1594 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1595 | // If we haven't past the identifier yet (or where the identifier would be |
| 1596 | // stored, if this is an abstract declarator), then this is probably just |
| 1597 | // grouping parens. However, if this could be an abstract-declarator, then |
| 1598 | // this could also be the start of function arguments (consider 'void()'). |
| 1599 | bool isGrouping; |
| 1600 | |
| 1601 | if (!D.mayOmitIdentifier()) { |
| 1602 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 1603 | // paren, because we haven't seen the identifier yet. |
| 1604 | isGrouping = true; |
| 1605 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Argyrios Kyrtzidis | e25d270 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 1606 | (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1607 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 1608 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 1609 | // considered to be a type, not a K&R identifier-list. |
| 1610 | isGrouping = false; |
| 1611 | } else { |
| 1612 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 1613 | isGrouping = true; |
| 1614 | } |
| 1615 | |
| 1616 | // If this is a grouping paren, handle: |
| 1617 | // direct-declarator: '(' declarator ')' |
| 1618 | // direct-declarator: '(' attributes declarator ')' |
| 1619 | if (isGrouping) { |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 1620 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1621 | D.setGroupingParens(true); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1622 | if (AttrList) |
| 1623 | D.AddAttributes(AttrList); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1624 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1625 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1626 | // Match the ')'. |
| 1627 | MatchRHSPunctuation(tok::r_paren, StartLoc); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 1628 | |
| 1629 | D.setGroupingParens(hadGroupingParens); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1630 | return; |
| 1631 | } |
| 1632 | |
| 1633 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 1634 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1635 | // identifier (and remember where it would have been), then call into |
| 1636 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1637 | D.SetIdentifier(0, Tok.getLocation()); |
| 1638 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1639 | ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 1643 | /// declarator D up to a paren, which indicates that we are parsing function |
| 1644 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1645 | /// |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1646 | /// If AttrList is non-null, then the caller parsed those arguments immediately |
| 1647 | /// after the open paren - they should be considered to be the first argument of |
| 1648 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 1649 | /// function is required to be present and required to not be an identifier |
| 1650 | /// list. |
| 1651 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1652 | /// This method also handles this portion of the grammar: |
| 1653 | /// parameter-type-list: [C99 6.7.5] |
| 1654 | /// parameter-list |
| 1655 | /// parameter-list ',' '...' |
| 1656 | /// |
| 1657 | /// parameter-list: [C99 6.7.5] |
| 1658 | /// parameter-declaration |
| 1659 | /// parameter-list ',' parameter-declaration |
| 1660 | /// |
| 1661 | /// parameter-declaration: [C99 6.7.5] |
| 1662 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1663 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1664 | /// [GNU] declaration-specifiers declarator attributes |
| 1665 | /// declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1666 | /// [C++] declaration-specifiers abstract-declarator[opt] |
| 1667 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1668 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 1669 | /// |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1670 | /// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]" |
| 1671 | /// and "exception-specification[opt]"(TODO). |
| 1672 | /// |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1673 | void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, |
| 1674 | AttributeList *AttrList, |
| 1675 | bool RequiresArg) { |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1676 | // lparen is already consumed! |
| 1677 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1678 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1679 | // This parameter list may be empty. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1680 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1681 | if (RequiresArg) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1682 | Diag(Tok, diag::err_argument_required_after_attribute); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1683 | delete AttrList; |
| 1684 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1685 | |
| 1686 | ConsumeParen(); // Eat the closing ')'. |
| 1687 | |
| 1688 | // cv-qualifier-seq[opt]. |
| 1689 | DeclSpec DS; |
| 1690 | if (getLang().CPlusPlus) { |
| 1691 | ParseTypeQualifierListOpt(DS); |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1692 | |
| 1693 | // Parse exception-specification[opt]. |
| 1694 | if (Tok.is(tok::kw_throw)) |
| 1695 | ParseExceptionSpecification(); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1698 | // Remember that we parsed a function type, and remember the attributes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1699 | // int() -> no prototype, no '...'. |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1700 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus, |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1701 | /*variadic*/ false, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1702 | /*arglist*/ 0, 0, |
| 1703 | DS.getTypeQualifiers(), |
| 1704 | LParenLoc)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1705 | return; |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | // Alternatively, this parameter list may be an identifier list form for a |
| 1709 | // K&R-style function: void foo(a,b,c) |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1710 | if (!getLang().CPlusPlus && Tok.is(tok::identifier) && |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1711 | // K&R identifier lists can't have typedefs as identifiers, per |
| 1712 | // C99 6.7.5.3p11. |
| 1713 | !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) { |
| 1714 | if (RequiresArg) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1715 | Diag(Tok, diag::err_argument_required_after_attribute); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1716 | delete AttrList; |
| 1717 | } |
| 1718 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1719 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 1720 | // normal declarators, not for abstract-declarators. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1721 | return ParseFunctionDeclaratorIdentifierList(LParenLoc, D); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | // Finally, a normal, non-empty parameter type list. |
| 1725 | |
| 1726 | // Build up an array of information about the parsed arguments. |
| 1727 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1728 | |
| 1729 | // Enter function-declaration scope, limiting any declarators to the |
| 1730 | // function prototype scope, including parameter declarators. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1731 | EnterScope(Scope::FnScope|Scope::DeclScope); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1732 | |
| 1733 | bool IsVariadic = false; |
| 1734 | while (1) { |
| 1735 | if (Tok.is(tok::ellipsis)) { |
| 1736 | IsVariadic = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1737 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1738 | // Check to see if this is "void(...)" which is not allowed. |
Argyrios Kyrtzidis | e25d270 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 1739 | if (!getLang().CPlusPlus && ParamInfo.empty()) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1740 | // Otherwise, parse parameter type list. If it starts with an |
| 1741 | // ellipsis, diagnose the malformed function. |
| 1742 | Diag(Tok, diag::err_ellipsis_first_arg); |
| 1743 | IsVariadic = false; // Treat this like 'void()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1744 | } |
Chris Lattner | e0e713b | 2008-01-31 06:10:07 +0000 | [diff] [blame] | 1745 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1746 | ConsumeToken(); // Consume the ellipsis. |
| 1747 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1750 | SourceLocation DSStart = Tok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1751 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1752 | // Parse the declaration-specifiers. |
| 1753 | DeclSpec DS; |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1754 | |
| 1755 | // If the caller parsed attributes for the first argument, add them now. |
| 1756 | if (AttrList) { |
| 1757 | DS.AddAttributes(AttrList); |
| 1758 | AttrList = 0; // Only apply the attributes to the first parameter. |
| 1759 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1760 | ParseDeclarationSpecifiers(DS); |
| 1761 | |
| 1762 | // Parse the declarator. This is "PrototypeContext", because we must |
| 1763 | // accept either 'declarator' or 'abstract-declarator' here. |
| 1764 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 1765 | ParseDeclarator(ParmDecl); |
| 1766 | |
| 1767 | // Parse GNU attributes, if present. |
| 1768 | if (Tok.is(tok::kw___attribute)) |
| 1769 | ParmDecl.AddAttributes(ParseAttributes()); |
| 1770 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1771 | // Remember this parsed parameter in ParamInfo. |
| 1772 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
| 1773 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1774 | // If no parameter was specified, verify that *something* was specified, |
| 1775 | // otherwise we have a missing type and identifier. |
| 1776 | if (DS.getParsedSpecifiers() == DeclSpec::PQ_None && |
| 1777 | ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) { |
| 1778 | // Completely missing, emit error. |
| 1779 | Diag(DSStart, diag::err_missing_param); |
| 1780 | } else { |
| 1781 | // Otherwise, we have something. Add it and let semantic analysis try |
| 1782 | // to grok it and add the result to the ParamInfo we are building. |
| 1783 | |
| 1784 | // Inform the actions module about the parameter declarator, so it gets |
| 1785 | // added to the current scope. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1786 | DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl); |
| 1787 | |
| 1788 | // Parse the default argument, if any. We parse the default |
| 1789 | // arguments in all dialects; the semantic analysis in |
| 1790 | // ActOnParamDefaultArgument will reject the default argument in |
| 1791 | // C. |
| 1792 | if (Tok.is(tok::equal)) { |
| 1793 | SourceLocation EqualLoc = Tok.getLocation(); |
| 1794 | |
| 1795 | // Consume the '='. |
| 1796 | ConsumeToken(); |
| 1797 | |
| 1798 | // Parse the default argument |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1799 | ExprOwner DefArgResult(Actions, ParseAssignmentExpression()); |
| 1800 | if (DefArgResult.isInvalid()) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1801 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 1802 | } else { |
| 1803 | // Inform the actions module about the default argument |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1804 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
| 1805 | DefArgResult.move()); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1806 | } |
| 1807 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1808 | |
| 1809 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1810 | ParmDecl.getIdentifierLoc(), Param)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | // If the next token is a comma, consume it and keep reading arguments. |
| 1814 | if (Tok.isNot(tok::comma)) break; |
| 1815 | |
| 1816 | // Consume the comma. |
| 1817 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1820 | // Leave prototype scope. |
| 1821 | ExitScope(); |
| 1822 | |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1823 | // If we have the closing ')', eat it. |
| 1824 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1825 | |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1826 | DeclSpec DS; |
| 1827 | if (getLang().CPlusPlus) { |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1828 | // Parse cv-qualifier-seq[opt]. |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1829 | ParseTypeQualifierListOpt(DS); |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1830 | |
| 1831 | // Parse exception-specification[opt]. |
| 1832 | if (Tok.is(tok::kw_throw)) |
| 1833 | ParseExceptionSpecification(); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1836 | // Remember that we parsed a function type, and remember the attributes. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1837 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic, |
| 1838 | &ParamInfo[0], ParamInfo.size(), |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1839 | DS.getTypeQualifiers(), |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1840 | LParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1843 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 1844 | /// we found a K&R-style identifier list instead of a type argument list. The |
| 1845 | /// current token is known to be the first identifier in the list. |
| 1846 | /// |
| 1847 | /// identifier-list: [C99 6.7.5] |
| 1848 | /// identifier |
| 1849 | /// identifier-list ',' identifier |
| 1850 | /// |
| 1851 | void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc, |
| 1852 | Declarator &D) { |
| 1853 | // Build up an array of information about the parsed arguments. |
| 1854 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 1855 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 1856 | |
| 1857 | // If there was no identifier specified for the declarator, either we are in |
| 1858 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 1859 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 1860 | // diagnose this. |
| 1861 | if (!D.getIdentifier()) |
| 1862 | Diag(Tok, diag::ext_ident_list_in_param); |
| 1863 | |
| 1864 | // Tok is known to be the first identifier in the list. Remember this |
| 1865 | // identifier in ParamInfo. |
Chris Lattner | 3825c2e | 2008-04-06 06:50:56 +0000 | [diff] [blame] | 1866 | ParamsSoFar.insert(Tok.getIdentifierInfo()); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1867 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(), |
| 1868 | Tok.getLocation(), 0)); |
| 1869 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1870 | ConsumeToken(); // eat the first identifier. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1871 | |
| 1872 | while (Tok.is(tok::comma)) { |
| 1873 | // Eat the comma. |
| 1874 | ConsumeToken(); |
| 1875 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1876 | // If this isn't an identifier, report the error and skip until ')'. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1877 | if (Tok.isNot(tok::identifier)) { |
| 1878 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1879 | SkipUntil(tok::r_paren); |
| 1880 | return; |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1881 | } |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 1882 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1883 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 1884 | |
| 1885 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 1886 | if (Actions.isTypeName(*ParmII, CurScope)) |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 1887 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1888 | |
| 1889 | // Verify that the argument identifier has not already been mentioned. |
| 1890 | if (!ParamsSoFar.insert(ParmII)) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 1891 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1892 | } else { |
| 1893 | // Remember this identifier in ParamInfo. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1894 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 1895 | Tok.getLocation(), 0)); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1896 | } |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1897 | |
| 1898 | // Eat the identifier. |
| 1899 | ConsumeToken(); |
| 1900 | } |
| 1901 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1902 | // Remember that we parsed a function type, and remember the attributes. This |
| 1903 | // function type is always a K&R style function type, which is not varargs and |
| 1904 | // has no prototype. |
| 1905 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false, |
| 1906 | &ParamInfo[0], ParamInfo.size(), |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1907 | /*TypeQuals*/0, LParenLoc)); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1908 | |
| 1909 | // If we have the closing ')', eat it and we're done. |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1910 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1911 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1912 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1913 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1914 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1915 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1916 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1917 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1918 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 1919 | SourceLocation StartLoc = ConsumeBracket(); |
| 1920 | |
| 1921 | // If valid, this location is the position where we read the 'static' keyword. |
| 1922 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1923 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1924 | StaticLoc = ConsumeToken(); |
| 1925 | |
| 1926 | // If there is a type-qualifier-list, read it now. |
| 1927 | DeclSpec DS; |
| 1928 | ParseTypeQualifierListOpt(DS); |
| 1929 | |
| 1930 | // If we haven't already read 'static', check to see if there is one after the |
| 1931 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1932 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1933 | StaticLoc = ConsumeToken(); |
| 1934 | |
| 1935 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 1936 | bool isStar = false; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1937 | ExprOwner NumElements(Actions); |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 1938 | |
| 1939 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 1940 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 1941 | // the the token after the star is a ']'. Since stars in arrays are |
| 1942 | // infrequent, use of lookahead is not costly here. |
| 1943 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 1944 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1945 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 1946 | if (StaticLoc.isValid()) |
| 1947 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
| 1948 | StaticLoc = SourceLocation(); // Drop the static. |
| 1949 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1950 | } else if (Tok.isNot(tok::r_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1951 | // Parse the assignment-expression now. |
| 1952 | NumElements = ParseAssignmentExpression(); |
| 1953 | } |
| 1954 | |
| 1955 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1956 | if (NumElements.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1957 | // If the expression was invalid, skip it. |
| 1958 | SkipUntil(tok::r_square); |
| 1959 | return; |
| 1960 | } |
| 1961 | |
| 1962 | MatchRHSPunctuation(tok::r_square, StartLoc); |
| 1963 | |
| 1964 | // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if |
| 1965 | // it was not a constant expression. |
| 1966 | if (!getLang().C99) { |
| 1967 | // TODO: check C90 array constant exprness. |
| 1968 | if (isStar || StaticLoc.isValid() || |
| 1969 | 0/*TODO: NumElts is not a C90 constantexpr */) |
| 1970 | Diag(StartLoc, diag::ext_c99_array_usage); |
| 1971 | } |
| 1972 | |
| 1973 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1974 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
| 1975 | StaticLoc.isValid(), isStar, |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1976 | NumElements.move(), StartLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 1979 | /// [GNU] typeof-specifier: |
| 1980 | /// typeof ( expressions ) |
| 1981 | /// typeof ( type-name ) |
| 1982 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1983 | /// |
| 1984 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1985 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1986 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1987 | SourceLocation StartLoc = ConsumeToken(); |
| 1988 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1989 | if (Tok.isNot(tok::l_paren)) { |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 1990 | if (!getLang().CPlusPlus) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1991 | Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII; |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 1992 | return; |
| 1993 | } |
| 1994 | |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1995 | ExprOwner Result(Actions, ParseCastExpression(true/*isUnaryExpression*/)); |
| 1996 | if (Result.isInvalid()) |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 1997 | return; |
| 1998 | |
| 1999 | const char *PrevSpec = 0; |
| 2000 | // Check for duplicate type specifiers. |
| 2001 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2002 | Result.move())) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2003 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2004 | |
| 2005 | // FIXME: Not accurate, the range gets one token more than it should. |
| 2006 | DS.SetRangeEnd(Tok.getLocation()); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 2007 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2008 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2009 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2010 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
| 2011 | |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 2012 | if (isTypeIdInParens()) { |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2013 | TypeTy *Ty = ParseTypeName(); |
| 2014 | |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 2015 | assert(Ty && "Parser::ParseTypeofSpecifier(): missing type"); |
| 2016 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2017 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 2018 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 2019 | return; |
| 2020 | } |
| 2021 | RParenLoc = ConsumeParen(); |
| 2022 | const char *PrevSpec = 0; |
| 2023 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 2024 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty)) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2025 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2026 | } else { // we have an expression. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2027 | ExprOwner Result(Actions, ParseExpression()); |
| 2028 | |
| 2029 | if (Result.isInvalid() || Tok.isNot(tok::r_paren)) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 2030 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 2031 | return; |
| 2032 | } |
| 2033 | RParenLoc = ConsumeParen(); |
| 2034 | const char *PrevSpec = 0; |
| 2035 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 2036 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2037 | Result.move())) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2038 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2039 | } |
Argyrios Kyrtzidis | 0919f9e | 2008-08-16 10:21:33 +0000 | [diff] [blame] | 2040 | DS.SetRangeEnd(RParenLoc); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 2043 | |