Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | 545f39e | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Scope.h" |
Chris Lattner | daa5c00 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 17 | #include "ExtensionRAIIObject.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
| 19 | using namespace clang; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // C99 6.7: Declarations. |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | /// ParseTypeName |
| 26 | /// type-name: [C99 6.7.6] |
| 27 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 19fec9d | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 28 | /// |
| 29 | /// Called type-id in C++. |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 30 | Action::TypeResult Parser::ParseTypeName(SourceRange *Range) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 31 | // Parse the common declaration-specifiers piece. |
| 32 | DeclSpec DS; |
| 33 | ParseSpecifierQualifierList(DS); |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 35 | // Parse the abstract-declarator, if present. |
| 36 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 37 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 38 | if (Range) |
| 39 | *Range = DeclaratorInfo.getSourceRange(); |
| 40 | |
Chris Lattner | 34c6133 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 41 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 6c0f406 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 42 | return true; |
| 43 | |
| 44 | return Actions.ActOnTypeName(CurScope, DeclaratorInfo); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /// ParseAttributes - Parse a non-empty attributes list. |
| 48 | /// |
| 49 | /// [GNU] attributes: |
| 50 | /// attribute |
| 51 | /// attributes attribute |
| 52 | /// |
| 53 | /// [GNU] attribute: |
| 54 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 55 | /// |
| 56 | /// [GNU] attribute-list: |
| 57 | /// attrib |
| 58 | /// attribute_list ',' attrib |
| 59 | /// |
| 60 | /// [GNU] attrib: |
| 61 | /// empty |
| 62 | /// attrib-name |
| 63 | /// attrib-name '(' identifier ')' |
| 64 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 65 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 66 | /// |
| 67 | /// [GNU] attrib-name: |
| 68 | /// identifier |
| 69 | /// typespec |
| 70 | /// typequal |
| 71 | /// storageclass |
| 72 | /// |
| 73 | /// FIXME: The GCC grammar/code for this construct implies we need two |
| 74 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 75 | /// which is followed by a comma or close parenthesis, then the arguments |
| 76 | /// start with that identifier; otherwise they are an expression list." |
| 77 | /// |
| 78 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 79 | /// any attributes that don't work (based on my limited testing). Most |
| 80 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 81 | /// a pressing need to implement the 2 token lookahead. |
| 82 | |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 83 | AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) { |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 84 | assert(Tok.is(tok::kw___attribute) && "Not an attribute list!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 85 | |
| 86 | AttributeList *CurrAttr = 0; |
| 87 | |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 88 | while (Tok.is(tok::kw___attribute)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 89 | ConsumeToken(); |
| 90 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 91 | "attribute")) { |
| 92 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 93 | return CurrAttr; |
| 94 | } |
| 95 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 96 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 97 | return CurrAttr; |
| 98 | } |
| 99 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 100 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 101 | Tok.is(tok::comma)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 103 | if (Tok.is(tok::comma)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 104 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 105 | ConsumeToken(); |
| 106 | continue; |
| 107 | } |
| 108 | // we have an identifier or declaration specifier (const, int, etc.) |
| 109 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 110 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 111 | |
| 112 | // check if we have a "paramterized" attribute |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 113 | if (Tok.is(tok::l_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 114 | ConsumeParen(); // ignore the left paren loc for now |
| 115 | |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 116 | if (Tok.is(tok::identifier)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 117 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 118 | SourceLocation ParmLoc = ConsumeToken(); |
| 119 | |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 120 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 121 | // __attribute__(( mode(byte) )) |
| 122 | ConsumeParen(); // ignore the right paren loc for now |
| 123 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 124 | ParmName, ParmLoc, 0, 0, CurrAttr); |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 125 | } else if (Tok.is(tok::comma)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 126 | ConsumeToken(); |
| 127 | // __attribute__(( format(printf, 1, 2) )) |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 128 | ExprVector ArgExprs(Actions); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 129 | bool ArgExprsOk = true; |
| 130 | |
| 131 | // now parse the non-empty comma separated list of expressions |
| 132 | while (1) { |
Sebastian Redl | 14ca741 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 133 | OwningExprResult ArgExpr(ParseAssignmentExpression()); |
Sebastian Redl | bb4dae7 | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 134 | if (ArgExpr.isInvalid()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 135 | ArgExprsOk = false; |
| 136 | SkipUntil(tok::r_paren); |
| 137 | break; |
| 138 | } else { |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 139 | ArgExprs.push_back(ArgExpr.release()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 140 | } |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 141 | if (Tok.isNot(tok::comma)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 142 | break; |
| 143 | ConsumeToken(); // Eat the comma, move to the next argument |
| 144 | } |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 145 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 146 | ConsumeParen(); // ignore the right paren loc for now |
| 147 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName, |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 148 | ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } else { // not an identifier |
| 152 | // parse a possibly empty comma separated list of expressions |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 153 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 154 | // __attribute__(( nonnull() )) |
| 155 | ConsumeParen(); // ignore the right paren loc for now |
| 156 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 157 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 158 | } else { |
| 159 | // __attribute__(( aligned(16) )) |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 160 | ExprVector ArgExprs(Actions); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 161 | bool ArgExprsOk = true; |
| 162 | |
| 163 | // now parse the list of expressions |
| 164 | while (1) { |
Sebastian Redl | 14ca741 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 165 | OwningExprResult ArgExpr(ParseAssignmentExpression()); |
Sebastian Redl | bb4dae7 | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 166 | if (ArgExpr.isInvalid()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 167 | ArgExprsOk = false; |
| 168 | SkipUntil(tok::r_paren); |
| 169 | break; |
| 170 | } else { |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 171 | ArgExprs.push_back(ArgExpr.release()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 172 | } |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 173 | if (Tok.isNot(tok::comma)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 174 | break; |
| 175 | ConsumeToken(); // Eat the comma, move to the next argument |
| 176 | } |
| 177 | // Match the ')'. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 178 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 179 | ConsumeParen(); // ignore the right paren loc for now |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 180 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
| 181 | SourceLocation(), ArgExprs.take(), ArgExprs.size(), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 182 | CurrAttr); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } else { |
| 187 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 188 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 189 | } |
| 190 | } |
| 191 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 192 | SkipUntil(tok::r_paren, false); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 193 | SourceLocation Loc = Tok.getLocation();; |
| 194 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 195 | SkipUntil(tok::r_paren, false); |
| 196 | } |
| 197 | if (EndLoc) |
| 198 | *EndLoc = Loc; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 199 | } |
| 200 | return CurrAttr; |
| 201 | } |
| 202 | |
Steve Naroff | c5ab14f | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 203 | /// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this |
| 204 | /// routine is called to skip/ignore tokens that comprise the MS declspec. |
| 205 | void Parser::FuzzyParseMicrosoftDeclSpec() { |
| 206 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
| 207 | ConsumeToken(); |
| 208 | if (Tok.is(tok::l_paren)) { |
| 209 | unsigned short savedParenCount = ParenCount; |
| 210 | do { |
| 211 | ConsumeAnyToken(); |
| 212 | } while (ParenCount > savedParenCount && Tok.isNot(tok::eof)); |
| 213 | } |
| 214 | return; |
| 215 | } |
| 216 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 217 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 218 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 219 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 220 | /// location of the semicolon in DeclEnd. |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 221 | /// |
| 222 | /// declaration: [C99 6.7] |
| 223 | /// block-declaration -> |
| 224 | /// simple-declaration |
| 225 | /// others [FIXME] |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 226 | /// [C++] template-declaration |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 227 | /// [C++] namespace-definition |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 228 | /// [C++] using-directive |
| 229 | /// [C++] using-declaration [TODO] |
Sebastian Redl | a8cecf6 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 230 | /// [C++0x] static_assert-declaration |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 231 | /// others... [FIXME] |
| 232 | /// |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 233 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context, |
| 234 | SourceLocation &DeclEnd) { |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 235 | DeclPtrTy SingleDecl; |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 236 | switch (Tok.getKind()) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 237 | case tok::kw_template: |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 238 | case tok::kw_export: |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 239 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 240 | break; |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 241 | case tok::kw_namespace: |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 242 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 243 | break; |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 244 | case tok::kw_using: |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 245 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 246 | break; |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 247 | case tok::kw_static_assert: |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 248 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 249 | break; |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 250 | default: |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 251 | return ParseSimpleDeclaration(Context, DeclEnd); |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 252 | } |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 253 | |
| 254 | // This routine returns a DeclGroup, if the thing we parsed only contains a |
| 255 | // single decl, convert it now. |
| 256 | return Actions.ConvertDeclToDeclGroup(SingleDecl); |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 260 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 261 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 262 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | f801604 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 263 | /// |
| 264 | /// If RequireSemi is false, this does not check for a ';' at the end of the |
| 265 | /// declaration. |
| 266 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context, |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 267 | SourceLocation &DeclEnd, |
Chris Lattner | f801604 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 268 | bool RequireSemi) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 269 | // Parse the common declaration-specifiers piece. |
| 270 | DeclSpec DS; |
| 271 | ParseDeclarationSpecifiers(DS); |
| 272 | |
| 273 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 274 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 275 | if (Tok.is(tok::semi)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 276 | ConsumeToken(); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 277 | DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 278 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 282 | ParseDeclarator(DeclaratorInfo); |
| 283 | |
Chris Lattner | 2c41d48 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 284 | DeclGroupPtrTy DG = |
| 285 | ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
Chris Lattner | f801604 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 287 | DeclEnd = Tok.getLocation(); |
| 288 | |
Chris Lattner | f801604 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 289 | // If the client wants to check what comes after the declaration, just return |
| 290 | // immediately without checking anything! |
| 291 | if (!RequireSemi) return DG; |
Chris Lattner | 2c41d48 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 292 | |
| 293 | if (Tok.is(tok::semi)) { |
| 294 | ConsumeToken(); |
Chris Lattner | 2c41d48 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 295 | return DG; |
| 296 | } |
| 297 | |
Chris Lattner | 2c41d48 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 298 | Diag(Tok, diag::err_expected_semi_declation); |
| 299 | // Skip to end of block or statement |
| 300 | SkipUntil(tok::r_brace, true, true); |
| 301 | if (Tok.is(tok::semi)) |
| 302 | ConsumeToken(); |
| 303 | return DG; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 306 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 307 | /// declarator'. This method parses the remainder of the declaration |
| 308 | /// (including any attributes or initializer, among other things) and |
| 309 | /// finalizes the declaration. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 310 | /// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 311 | /// init-declarator: [C99 6.7] |
| 312 | /// declarator |
| 313 | /// declarator '=' initializer |
| 314 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 315 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 316 | /// [C++] declarator initializer[opt] |
| 317 | /// |
| 318 | /// [C++] initializer: |
| 319 | /// [C++] '=' initializer-clause |
| 320 | /// [C++] '(' expression-list ')' |
Sebastian Redl | a8cecf6 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 321 | /// [C++0x] '=' 'default' [TODO] |
| 322 | /// [C++0x] '=' 'delete' |
| 323 | /// |
| 324 | /// According to the standard grammar, =default and =delete are function |
| 325 | /// definitions, but that definitely doesn't fit with the parser here. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 326 | /// |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 327 | Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D) { |
| 328 | // If a simple-asm-expr is present, parse it. |
| 329 | if (Tok.is(tok::kw_asm)) { |
| 330 | SourceLocation Loc; |
| 331 | OwningExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 332 | if (AsmLabel.isInvalid()) { |
| 333 | SkipUntil(tok::semi, true, true); |
| 334 | return DeclPtrTy(); |
| 335 | } |
| 336 | |
| 337 | D.setAsmLabel(AsmLabel.release()); |
| 338 | D.SetRangeEnd(Loc); |
| 339 | } |
| 340 | |
| 341 | // If attributes are present, parse them. |
| 342 | if (Tok.is(tok::kw___attribute)) { |
| 343 | SourceLocation Loc; |
| 344 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 345 | D.AddAttributes(AttrList, Loc); |
| 346 | } |
| 347 | |
| 348 | // Inform the current actions module that we just parsed this declarator. |
| 349 | DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D); |
| 350 | |
| 351 | // Parse declarator '=' initializer. |
| 352 | if (Tok.is(tok::equal)) { |
| 353 | ConsumeToken(); |
| 354 | if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) { |
| 355 | SourceLocation DelLoc = ConsumeToken(); |
| 356 | Actions.SetDeclDeleted(ThisDecl, DelLoc); |
| 357 | } else { |
| 358 | OwningExprResult Init(ParseInitializer()); |
| 359 | if (Init.isInvalid()) { |
| 360 | SkipUntil(tok::semi, true, true); |
| 361 | return DeclPtrTy(); |
| 362 | } |
| 363 | Actions.AddInitializerToDecl(ThisDecl, move(Init)); |
| 364 | } |
| 365 | } else if (Tok.is(tok::l_paren)) { |
| 366 | // Parse C++ direct initializer: '(' expression-list ')' |
| 367 | SourceLocation LParenLoc = ConsumeParen(); |
| 368 | ExprVector Exprs(Actions); |
| 369 | CommaLocsTy CommaLocs; |
| 370 | |
| 371 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 372 | SkipUntil(tok::r_paren); |
| 373 | } else { |
| 374 | // Match the ')'. |
| 375 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 376 | |
| 377 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 378 | "Unexpected number of commas!"); |
| 379 | Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc, |
| 380 | move_arg(Exprs), |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 381 | CommaLocs.data(), RParenLoc); |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 382 | } |
| 383 | } else { |
| 384 | Actions.ActOnUninitializedDecl(ThisDecl); |
| 385 | } |
| 386 | |
| 387 | return ThisDecl; |
| 388 | } |
| 389 | |
| 390 | /// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after |
| 391 | /// parsing 'declaration-specifiers declarator'. This method is split out this |
| 392 | /// way to handle the ambiguity between top-level function-definitions and |
| 393 | /// declarations. |
| 394 | /// |
| 395 | /// init-declarator-list: [C99 6.7] |
| 396 | /// init-declarator |
| 397 | /// init-declarator-list ',' init-declarator |
| 398 | /// |
| 399 | /// According to the standard grammar, =default and =delete are function |
| 400 | /// definitions, but that definitely doesn't fit with the parser here. |
| 401 | /// |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 402 | Parser::DeclGroupPtrTy Parser:: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 403 | ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 404 | // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls |
| 405 | // that we parse together here. |
| 406 | llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 407 | |
| 408 | // At this point, we know that it is not a function definition. Parse the |
| 409 | // rest of the init-declarator-list. |
| 410 | while (1) { |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 411 | DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 412 | if (ThisDecl.get()) |
| 413 | DeclsInGroup.push_back(ThisDecl); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 415 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 416 | // error, bail out. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 417 | if (Tok.isNot(tok::comma)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 418 | break; |
| 419 | |
| 420 | // Consume the comma. |
| 421 | ConsumeToken(); |
| 422 | |
| 423 | // Parse the next declarator. |
| 424 | D.clear(); |
Chris Lattner | 926cf54 | 2008-10-20 04:57:38 +0000 | [diff] [blame] | 425 | |
| 426 | // Accept attributes in an init-declarator. In the first declarator in a |
| 427 | // declaration, these would be part of the declspec. In subsequent |
| 428 | // declarators, they become part of the declarator itself, so that they |
| 429 | // don't apply to declarators after *this* one. Examples: |
| 430 | // short __attribute__((common)) var; -> declspec |
| 431 | // short var __attribute__((common)); -> declarator |
| 432 | // short x, __attribute__((common)) var; -> declarator |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 433 | if (Tok.is(tok::kw___attribute)) { |
| 434 | SourceLocation Loc; |
| 435 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 436 | D.AddAttributes(AttrList, Loc); |
| 437 | } |
Chris Lattner | 926cf54 | 2008-10-20 04:57:38 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 439 | ParseDeclarator(D); |
| 440 | } |
| 441 | |
Eli Friedman | 4d57af2 | 2009-05-29 01:49:24 +0000 | [diff] [blame] | 442 | return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(), |
| 443 | DeclsInGroup.data(), |
Chris Lattner | 2c41d48 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 444 | DeclsInGroup.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /// ParseSpecifierQualifierList |
| 448 | /// specifier-qualifier-list: |
| 449 | /// type-specifier specifier-qualifier-list[opt] |
| 450 | /// type-qualifier specifier-qualifier-list[opt] |
| 451 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 452 | /// |
| 453 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 454 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 455 | /// parse declaration-specifiers and complain about extra stuff. |
| 456 | ParseDeclarationSpecifiers(DS); |
| 457 | |
| 458 | // Validate declspec for type-name. |
| 459 | unsigned Specs = DS.getParsedSpecifiers(); |
Chris Lattner | a52aec4 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 460 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 461 | !DS.getAttributes()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 462 | Diag(Tok, diag::err_typename_requires_specqual); |
| 463 | |
| 464 | // Issue diagnostic and remove storage class if present. |
| 465 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 466 | if (DS.getStorageClassSpecLoc().isValid()) |
| 467 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 468 | else |
| 469 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 470 | DS.ClearStorageClassSpecs(); |
| 471 | } |
| 472 | |
| 473 | // Issue diagnostic and remove function specfier if present. |
| 474 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 475 | if (DS.isInlineSpecified()) |
| 476 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 477 | if (DS.isVirtualSpecified()) |
| 478 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 479 | if (DS.isExplicitSpecified()) |
| 480 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 481 | DS.ClearFunctionSpecs(); |
| 482 | } |
| 483 | } |
| 484 | |
Chris Lattner | cc98d8c | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 485 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 486 | /// specified token is valid after the identifier in a declarator which |
| 487 | /// immediately follows the declspec. For example, these things are valid: |
| 488 | /// |
| 489 | /// int x [ 4]; // direct-declarator |
| 490 | /// int x ( int y); // direct-declarator |
| 491 | /// int(int x ) // direct-declarator |
| 492 | /// int x ; // simple-declaration |
| 493 | /// int x = 17; // init-declarator-list |
| 494 | /// int x , y; // init-declarator-list |
| 495 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | a52aec4 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 496 | /// int x : 4; // struct-declarator |
Chris Lattner | ca6cc36 | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 497 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | cc98d8c | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 498 | /// |
| 499 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 500 | /// ')' happens to be valid anyway). |
| 501 | /// int (x) |
| 502 | /// |
| 503 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 504 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 505 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | a52aec4 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 506 | T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon); |
Chris Lattner | cc98d8c | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Chris Lattner | 82353c6 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 509 | |
| 510 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 511 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 512 | /// the declspec has no type specifier. In this case, the declspec is either |
| 513 | /// malformed or is "implicit int" (in K&R and C89). |
| 514 | /// |
| 515 | /// This method handles diagnosing this prettily and returns false if the |
| 516 | /// declspec is done being processed. If it recovers and thinks there may be |
| 517 | /// other pieces of declspec after it, it returns true. |
| 518 | /// |
Chris Lattner | 52cd762 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 519 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 520 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | 82353c6 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 521 | AccessSpecifier AS) { |
Chris Lattner | 52cd762 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 522 | assert(Tok.is(tok::identifier) && "should have identifier"); |
| 523 | |
Chris Lattner | 82353c6 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 524 | SourceLocation Loc = Tok.getLocation(); |
| 525 | // If we see an identifier that is not a type name, we normally would |
| 526 | // parse it as the identifer being declared. However, when a typename |
| 527 | // is typo'd or the definition is not included, this will incorrectly |
| 528 | // parse the typename as the identifier name and fall over misparsing |
| 529 | // later parts of the diagnostic. |
| 530 | // |
| 531 | // As such, we try to do some look-ahead in cases where this would |
| 532 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 533 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 534 | // an identifier with implicit int, we'd get a parse error because the |
| 535 | // next token is obviously invalid for a type. Parse these as a case |
| 536 | // with an invalid type specifier. |
| 537 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
| 538 | |
| 539 | // Since we know that this either implicit int (which is rare) or an |
| 540 | // error, we'd do lookahead to try to do better recovery. |
| 541 | if (isValidAfterIdentifierInDeclarator(NextToken())) { |
| 542 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 543 | // we just avoid eating the identifier, so it will be parsed as the |
| 544 | // identifier in the declarator. |
| 545 | return false; |
| 546 | } |
| 547 | |
| 548 | // Otherwise, if we don't consume this token, we are going to emit an |
| 549 | // error anyway. Try to recover from various common problems. Check |
| 550 | // to see if this was a reference to a tag name without a tag specified. |
| 551 | // This is a common problem in C (saying 'foo' instead of 'struct foo'). |
Chris Lattner | 52cd762 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 552 | // |
| 553 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 554 | if (SS == 0) { |
| 555 | const char *TagName = 0; |
| 556 | tok::TokenKind TagKind = tok::unknown; |
Chris Lattner | 82353c6 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 82353c6 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 558 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) { |
| 559 | default: break; |
| 560 | case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break; |
| 561 | case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break; |
| 562 | case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break; |
| 563 | case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break; |
| 564 | } |
Chris Lattner | 82353c6 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 52cd762 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 566 | if (TagName) { |
| 567 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
| 568 | << Tok.getIdentifierInfo() << TagName |
| 569 | << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName); |
| 570 | |
| 571 | // Parse this as a tag as if the missing tag were present. |
| 572 | if (TagKind == tok::kw_enum) |
| 573 | ParseEnumSpecifier(Loc, DS, AS); |
| 574 | else |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 575 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | 52cd762 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 576 | return true; |
| 577 | } |
Chris Lattner | 82353c6 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // Since this is almost certainly an invalid type name, emit a |
| 581 | // diagnostic that says it, eat the token, and mark the declspec as |
| 582 | // invalid. |
Chris Lattner | 52cd762 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 583 | SourceRange R; |
| 584 | if (SS) R = SS->getRange(); |
| 585 | |
| 586 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
Chris Lattner | 82353c6 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 587 | const char *PrevSpec; |
| 588 | DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec); |
| 589 | DS.SetRangeEnd(Tok.getLocation()); |
| 590 | ConsumeToken(); |
| 591 | |
| 592 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 593 | // avoid rippling error messages on subsequent uses of the same type, |
| 594 | // could be useful if #include was forgotten. |
| 595 | return false; |
| 596 | } |
| 597 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 598 | /// ParseDeclarationSpecifiers |
| 599 | /// declaration-specifiers: [C99 6.7] |
| 600 | /// storage-class-specifier declaration-specifiers[opt] |
| 601 | /// type-specifier declaration-specifiers[opt] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 602 | /// [C99] function-specifier declaration-specifiers[opt] |
| 603 | /// [GNU] attributes declaration-specifiers[opt] |
| 604 | /// |
| 605 | /// storage-class-specifier: [C99 6.7.1] |
| 606 | /// 'typedef' |
| 607 | /// 'extern' |
| 608 | /// 'static' |
| 609 | /// 'auto' |
| 610 | /// 'register' |
Sebastian Redl | 9f5337b | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 611 | /// [C++] 'mutable' |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 612 | /// [GNU] '__thread' |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 613 | /// function-specifier: [C99 6.7.4] |
| 614 | /// [C99] 'inline' |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 615 | /// [C++] 'virtual' |
| 616 | /// [C++] 'explicit' |
Anders Carlsson | 6c2ad5a | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 617 | /// 'friend': [C++ dcl.friend] |
| 618 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 619 | /// |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 620 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 621 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | cc98d8c | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 622 | AccessSpecifier AS) { |
Chris Lattner | a4ff427 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 623 | DS.SetRangeStart(Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 624 | while (1) { |
| 625 | int isInvalid = false; |
| 626 | const char *PrevSpec = 0; |
| 627 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 628 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 629 | switch (Tok.getKind()) { |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 630 | default: |
Chris Lattner | b99d749 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 631 | DoneWithDeclSpec: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 632 | // If this is not a declaration specifier token, we're done reading decl |
| 633 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 1ba5cb3 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 634 | DS.Finish(Diags, PP); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 635 | return; |
Chris Lattner | 712f9a3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 636 | |
| 637 | case tok::coloncolon: // ::foo::bar |
| 638 | // Annotate C++ scope specifiers. If we get one, loop. |
| 639 | if (TryAnnotateCXXScopeToken()) |
| 640 | continue; |
| 641 | goto DoneWithDeclSpec; |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 642 | |
| 643 | case tok::annot_cxxscope: { |
| 644 | if (DS.hasTypeSpecifier()) |
| 645 | goto DoneWithDeclSpec; |
| 646 | |
| 647 | // We are looking for a qualified typename. |
Douglas Gregor | 80b95c5 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 648 | Token Next = NextToken(); |
| 649 | if (Next.is(tok::annot_template_id) && |
| 650 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | aabb850 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 651 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 80b95c5 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 652 | // We have a qualified template-id, e.g., N::A<int> |
| 653 | CXXScopeSpec SS; |
| 654 | ParseOptionalCXXScopeSpecifier(SS); |
| 655 | assert(Tok.is(tok::annot_template_id) && |
| 656 | "ParseOptionalCXXScopeSpecifier not working"); |
| 657 | AnnotateTemplateIdTokenAsType(&SS); |
| 658 | continue; |
| 659 | } |
| 660 | |
| 661 | if (Next.isNot(tok::identifier)) |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 662 | goto DoneWithDeclSpec; |
| 663 | |
| 664 | CXXScopeSpec SS; |
Douglas Gregor | 041e929 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 665 | SS.setScopeRep(Tok.getAnnotationValue()); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 666 | SS.setRange(Tok.getAnnotationRange()); |
| 667 | |
| 668 | // If the next token is the name of the class type that the C++ scope |
| 669 | // denotes, followed by a '(', then this is a constructor declaration. |
| 670 | // We're done with the decl-specifiers. |
Chris Lattner | 52cd762 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 671 | if (Actions.isCurrentClassName(*Next.getIdentifierInfo(), |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 672 | CurScope, &SS) && |
| 673 | GetLookAheadToken(2).is(tok::l_paren)) |
| 674 | goto DoneWithDeclSpec; |
| 675 | |
Douglas Gregor | 1075a16 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 676 | TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 677 | Next.getLocation(), CurScope, &SS); |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 678 | |
Chris Lattner | 52cd762 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 679 | // If the referenced identifier is not a type, then this declspec is |
| 680 | // erroneous: We already checked about that it has no type specifier, and |
| 681 | // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the |
| 682 | // typename. |
| 683 | if (TypeRep == 0) { |
| 684 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 685 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue; |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 686 | goto DoneWithDeclSpec; |
Chris Lattner | 52cd762 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 687 | } |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 688 | |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 689 | ConsumeToken(); // The C++ scope. |
| 690 | |
Douglas Gregor | a60c62e | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 691 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 692 | TypeRep); |
| 693 | if (isInvalid) |
| 694 | break; |
| 695 | |
| 696 | DS.SetRangeEnd(Tok.getLocation()); |
| 697 | ConsumeToken(); // The typename. |
| 698 | |
| 699 | continue; |
| 700 | } |
Chris Lattner | c297b72 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 701 | |
| 702 | case tok::annot_typename: { |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 703 | if (Tok.getAnnotationValue()) |
| 704 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
| 705 | Tok.getAnnotationValue()); |
| 706 | else |
| 707 | DS.SetTypeSpecError(); |
Chris Lattner | c297b72 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 708 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 709 | ConsumeToken(); // The typename |
| 710 | |
| 711 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 712 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 713 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 714 | // just a normal reference to a typedef name. |
| 715 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 716 | continue; |
| 717 | |
| 718 | SourceLocation EndProtoLoc; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 719 | llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl; |
Chris Lattner | c297b72 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 720 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
| 721 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
| 722 | |
| 723 | DS.SetRangeEnd(EndProtoLoc); |
| 724 | continue; |
| 725 | } |
| 726 | |
Chris Lattner | fda18db | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 727 | // typedef-name |
| 728 | case tok::identifier: { |
Chris Lattner | 712f9a3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 729 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 730 | // so handle it as such. This is important for ctor parsing. |
Chris Lattner | 5bb837e | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 731 | if (getLang().CPlusPlus && TryAnnotateCXXScopeToken()) |
| 732 | continue; |
Chris Lattner | 712f9a3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 733 | |
Chris Lattner | fda18db | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 734 | // This identifier can only be a typedef name if we haven't already seen |
| 735 | // a type-specifier. Without this check we misparse: |
| 736 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 737 | if (DS.hasTypeSpecifier()) |
| 738 | goto DoneWithDeclSpec; |
| 739 | |
| 740 | // It has to be available as a typedef too! |
Douglas Gregor | 1075a16 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 741 | TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 742 | Tok.getLocation(), CurScope); |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 743 | |
Chris Lattner | cc98d8c | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 744 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 745 | // it must be an implicit int or an error. |
| 746 | if (TypeRep == 0) { |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 747 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue; |
Chris Lattner | fda18db | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 748 | goto DoneWithDeclSpec; |
Chris Lattner | cc98d8c | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 749 | } |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 750 | |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 751 | // C++: If the identifier is actually the name of the class type |
| 752 | // being defined and the next token is a '(', then this is a |
| 753 | // constructor declaration. We're done with the decl-specifiers |
| 754 | // and will treat this token as an identifier. |
Chris Lattner | cc98d8c | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 755 | if (getLang().CPlusPlus && CurScope->isClassScope() && |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 756 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) && |
| 757 | NextToken().getKind() == tok::l_paren) |
| 758 | goto DoneWithDeclSpec; |
| 759 | |
Douglas Gregor | a60c62e | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 760 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Chris Lattner | fda18db | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 761 | TypeRep); |
| 762 | if (isInvalid) |
| 763 | break; |
| 764 | |
| 765 | DS.SetRangeEnd(Tok.getLocation()); |
| 766 | ConsumeToken(); // The identifier |
| 767 | |
| 768 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 769 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 770 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 771 | // just a normal reference to a typedef name. |
| 772 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 773 | continue; |
| 774 | |
| 775 | SourceLocation EndProtoLoc; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 776 | llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl; |
Chris Lattner | 2bdedd6 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 777 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
Chris Lattner | ada6379 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 778 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
Chris Lattner | fda18db | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 779 | |
| 780 | DS.SetRangeEnd(EndProtoLoc); |
| 781 | |
Steve Naroff | f768330 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 782 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 783 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 784 | continue; |
Chris Lattner | fda18db | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 785 | } |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 786 | |
| 787 | // type-name |
| 788 | case tok::annot_template_id: { |
| 789 | TemplateIdAnnotation *TemplateId |
| 790 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | aabb850 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 791 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 792 | // This template-id does not refer to a type name, so we're |
| 793 | // done with the type-specifiers. |
| 794 | goto DoneWithDeclSpec; |
| 795 | } |
| 796 | |
| 797 | // Turn the template-id annotation token into a type annotation |
| 798 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 799 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 800 | continue; |
| 801 | } |
| 802 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 803 | // GNU attributes support. |
| 804 | case tok::kw___attribute: |
| 805 | DS.AddAttributes(ParseAttributes()); |
| 806 | continue; |
Steve Naroff | c5ab14f | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 807 | |
| 808 | // Microsoft declspec support. |
| 809 | case tok::kw___declspec: |
| 810 | if (!PP.getLangOptions().Microsoft) |
| 811 | goto DoneWithDeclSpec; |
| 812 | FuzzyParseMicrosoftDeclSpec(); |
| 813 | continue; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 814 | |
Steve Naroff | edd04d5 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 815 | // Microsoft single token adornments. |
Steve Naroff | ad62040 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 816 | case tok::kw___forceinline: |
| 817 | case tok::kw___w64: |
Steve Naroff | edd04d5 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 818 | case tok::kw___cdecl: |
| 819 | case tok::kw___stdcall: |
| 820 | case tok::kw___fastcall: |
| 821 | if (!PP.getLangOptions().Microsoft) |
| 822 | goto DoneWithDeclSpec; |
| 823 | // Just ignore it. |
| 824 | break; |
| 825 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 826 | // storage-class-specifier |
| 827 | case tok::kw_typedef: |
| 828 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec); |
| 829 | break; |
| 830 | case tok::kw_extern: |
| 831 | if (DS.isThreadSpecified()) |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 832 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 833 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec); |
| 834 | break; |
Steve Naroff | f258a0f | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 835 | case tok::kw___private_extern__: |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 836 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, |
| 837 | PrevSpec); |
Steve Naroff | f258a0f | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 838 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 839 | case tok::kw_static: |
| 840 | if (DS.isThreadSpecified()) |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 841 | Diag(Tok, diag::ext_thread_before) << "static"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 842 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec); |
| 843 | break; |
| 844 | case tok::kw_auto: |
| 845 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec); |
| 846 | break; |
| 847 | case tok::kw_register: |
| 848 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec); |
| 849 | break; |
Sebastian Redl | 9f5337b | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 850 | case tok::kw_mutable: |
| 851 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec); |
| 852 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 853 | case tok::kw___thread: |
| 854 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2; |
| 855 | break; |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 856 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 857 | // function-specifier |
| 858 | case tok::kw_inline: |
| 859 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec); |
| 860 | break; |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 861 | case tok::kw_virtual: |
| 862 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec); |
| 863 | break; |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 864 | case tok::kw_explicit: |
| 865 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec); |
| 866 | break; |
Chris Lattner | c297b72 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 867 | |
Anders Carlsson | 6c2ad5a | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 868 | // friend |
| 869 | case tok::kw_friend: |
| 870 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec); |
| 871 | break; |
| 872 | |
Chris Lattner | c297b72 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 873 | // type-specifier |
| 874 | case tok::kw_short: |
| 875 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 876 | break; |
| 877 | case tok::kw_long: |
| 878 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
| 879 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 880 | else |
| 881 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
| 882 | break; |
| 883 | case tok::kw_signed: |
| 884 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 885 | break; |
| 886 | case tok::kw_unsigned: |
| 887 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 888 | break; |
| 889 | case tok::kw__Complex: |
| 890 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
| 891 | break; |
| 892 | case tok::kw__Imaginary: |
| 893 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
| 894 | break; |
| 895 | case tok::kw_void: |
| 896 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 897 | break; |
| 898 | case tok::kw_char: |
| 899 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 900 | break; |
| 901 | case tok::kw_int: |
| 902 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 903 | break; |
| 904 | case tok::kw_float: |
| 905 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 906 | break; |
| 907 | case tok::kw_double: |
| 908 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 909 | break; |
| 910 | case tok::kw_wchar_t: |
| 911 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec); |
| 912 | break; |
| 913 | case tok::kw_bool: |
| 914 | case tok::kw__Bool: |
| 915 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 916 | break; |
| 917 | case tok::kw__Decimal32: |
| 918 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
| 919 | break; |
| 920 | case tok::kw__Decimal64: |
| 921 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
| 922 | break; |
| 923 | case tok::kw__Decimal128: |
| 924 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
| 925 | break; |
| 926 | |
| 927 | // class-specifier: |
| 928 | case tok::kw_class: |
| 929 | case tok::kw_struct: |
Chris Lattner | 197b434 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 930 | case tok::kw_union: { |
| 931 | tok::TokenKind Kind = Tok.getKind(); |
| 932 | ConsumeToken(); |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 933 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | c297b72 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 934 | continue; |
Chris Lattner | 197b434 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 935 | } |
Chris Lattner | c297b72 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 936 | |
| 937 | // enum-specifier: |
| 938 | case tok::kw_enum: |
Chris Lattner | 197b434 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 939 | ConsumeToken(); |
| 940 | ParseEnumSpecifier(Loc, DS, AS); |
Chris Lattner | c297b72 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 941 | continue; |
| 942 | |
| 943 | // cv-qualifier: |
| 944 | case tok::kw_const: |
| 945 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2; |
| 946 | break; |
| 947 | case tok::kw_volatile: |
| 948 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 949 | getLang())*2; |
| 950 | break; |
| 951 | case tok::kw_restrict: |
| 952 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 953 | getLang())*2; |
| 954 | break; |
| 955 | |
Douglas Gregor | d302260 | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 956 | // C++ typename-specifier: |
| 957 | case tok::kw_typename: |
| 958 | if (TryAnnotateTypeOrScopeToken()) |
| 959 | continue; |
| 960 | break; |
| 961 | |
Chris Lattner | c297b72 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 962 | // GNU typeof support. |
| 963 | case tok::kw_typeof: |
| 964 | ParseTypeofSpecifier(DS); |
| 965 | continue; |
| 966 | |
Steve Naroff | 5f0466b | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 967 | case tok::less: |
Chris Lattner | fda18db | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 968 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | b99d749 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 969 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 970 | // but we support it. |
Chris Lattner | fda18db | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 971 | if (DS.hasTypeSpecifier() || !getLang().ObjC1) |
Chris Lattner | b99d749 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 972 | goto DoneWithDeclSpec; |
| 973 | |
| 974 | { |
| 975 | SourceLocation EndProtoLoc; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 976 | llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl; |
Chris Lattner | 2bdedd6 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 977 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
Chris Lattner | ada6379 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 978 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
Chris Lattner | fda18db | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 979 | DS.SetRangeEnd(EndProtoLoc); |
| 980 | |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 981 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
Chris Lattner | b980c73 | 2009-04-03 18:38:42 +0000 | [diff] [blame] | 982 | << CodeModificationHint::CreateInsertion(Loc, "id") |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 983 | << SourceRange(Loc, EndProtoLoc); |
Steve Naroff | f768330 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 984 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 985 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 986 | continue; |
Steve Naroff | 5f0466b | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 987 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 988 | } |
| 989 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 990 | if (isInvalid) { |
| 991 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 992 | // Pick between error or extwarn. |
| 993 | unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination |
| 994 | : diag::ext_duplicate_declspec; |
| 995 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 996 | } |
Chris Lattner | a4ff427 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 997 | DS.SetRangeEnd(Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 998 | ConsumeToken(); |
| 999 | } |
| 1000 | } |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | d706dc8 | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 1002 | /// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1003 | /// primarily follow the C++ grammar with additions for C99 and GNU, |
| 1004 | /// which together subsume the C grammar. Note that the C++ |
| 1005 | /// type-specifier also includes the C type-qualifier (for const, |
| 1006 | /// volatile, and C99 restrict). Returns true if a type-specifier was |
| 1007 | /// found (and parsed), false otherwise. |
| 1008 | /// |
| 1009 | /// type-specifier: [C++ 7.1.5] |
| 1010 | /// simple-type-specifier |
| 1011 | /// class-specifier |
| 1012 | /// enum-specifier |
| 1013 | /// elaborated-type-specifier [TODO] |
| 1014 | /// cv-qualifier |
| 1015 | /// |
| 1016 | /// cv-qualifier: [C++ 7.1.5.1] |
| 1017 | /// 'const' |
| 1018 | /// 'volatile' |
| 1019 | /// [C99] 'restrict' |
| 1020 | /// |
| 1021 | /// simple-type-specifier: [ C++ 7.1.5.2] |
| 1022 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 1023 | /// '::'[opt] nested-name-specifier 'template' template-id [TODO] |
| 1024 | /// 'char' |
| 1025 | /// 'wchar_t' |
| 1026 | /// 'bool' |
| 1027 | /// 'short' |
| 1028 | /// 'int' |
| 1029 | /// 'long' |
| 1030 | /// 'signed' |
| 1031 | /// 'unsigned' |
| 1032 | /// 'float' |
| 1033 | /// 'double' |
| 1034 | /// 'void' |
| 1035 | /// [C99] '_Bool' |
| 1036 | /// [C99] '_Complex' |
| 1037 | /// [C99] '_Imaginary' // Removed in TC2? |
| 1038 | /// [GNU] '_Decimal32' |
| 1039 | /// [GNU] '_Decimal64' |
| 1040 | /// [GNU] '_Decimal128' |
| 1041 | /// [GNU] typeof-specifier |
| 1042 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
| 1043 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Chris Lattner | d706dc8 | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 1044 | bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid, |
| 1045 | const char *&PrevSpec, |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1046 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1047 | SourceLocation Loc = Tok.getLocation(); |
| 1048 | |
| 1049 | switch (Tok.getKind()) { |
Chris Lattner | b75fde6 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1050 | case tok::identifier: // foo::bar |
Douglas Gregor | d302260 | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1051 | case tok::kw_typename: // typename foo::bar |
Chris Lattner | b75fde6 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1052 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1053 | // recurse to handle whatever we get. |
| 1054 | if (TryAnnotateTypeOrScopeToken()) |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1055 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo); |
Chris Lattner | b75fde6 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1056 | // Otherwise, not a type specifier. |
| 1057 | return false; |
| 1058 | case tok::coloncolon: // ::foo::bar |
| 1059 | if (NextToken().is(tok::kw_new) || // ::new |
| 1060 | NextToken().is(tok::kw_delete)) // ::delete |
| 1061 | return false; |
| 1062 | |
| 1063 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1064 | // recurse to handle whatever we get. |
| 1065 | if (TryAnnotateTypeOrScopeToken()) |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1066 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo); |
Chris Lattner | b75fde6 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1067 | // Otherwise, not a type specifier. |
| 1068 | return false; |
| 1069 | |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1070 | // simple-type-specifier: |
Chris Lattner | 5d7eace | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1071 | case tok::annot_typename: { |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1072 | if (Tok.getAnnotationValue()) |
| 1073 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
| 1074 | Tok.getAnnotationValue()); |
| 1075 | else |
| 1076 | DS.SetTypeSpecError(); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1077 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1078 | ConsumeToken(); // The typename |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1079 | |
| 1080 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1081 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 1082 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 1083 | // just a normal reference to a typedef name. |
| 1084 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 1085 | return true; |
| 1086 | |
| 1087 | SourceLocation EndProtoLoc; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1088 | llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl; |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1089 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
| 1090 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
| 1091 | |
| 1092 | DS.SetRangeEnd(EndProtoLoc); |
| 1093 | return true; |
| 1094 | } |
| 1095 | |
| 1096 | case tok::kw_short: |
| 1097 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 1098 | break; |
| 1099 | case tok::kw_long: |
| 1100 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
| 1101 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 1102 | else |
| 1103 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
| 1104 | break; |
| 1105 | case tok::kw_signed: |
| 1106 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 1107 | break; |
| 1108 | case tok::kw_unsigned: |
| 1109 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 1110 | break; |
| 1111 | case tok::kw__Complex: |
| 1112 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
| 1113 | break; |
| 1114 | case tok::kw__Imaginary: |
| 1115 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
| 1116 | break; |
| 1117 | case tok::kw_void: |
| 1118 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 1119 | break; |
| 1120 | case tok::kw_char: |
| 1121 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 1122 | break; |
| 1123 | case tok::kw_int: |
| 1124 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 1125 | break; |
| 1126 | case tok::kw_float: |
| 1127 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 1128 | break; |
| 1129 | case tok::kw_double: |
| 1130 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 1131 | break; |
| 1132 | case tok::kw_wchar_t: |
| 1133 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec); |
| 1134 | break; |
| 1135 | case tok::kw_bool: |
| 1136 | case tok::kw__Bool: |
| 1137 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 1138 | break; |
| 1139 | case tok::kw__Decimal32: |
| 1140 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
| 1141 | break; |
| 1142 | case tok::kw__Decimal64: |
| 1143 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
| 1144 | break; |
| 1145 | case tok::kw__Decimal128: |
| 1146 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
| 1147 | break; |
| 1148 | |
| 1149 | // class-specifier: |
| 1150 | case tok::kw_class: |
| 1151 | case tok::kw_struct: |
Chris Lattner | 197b434 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1152 | case tok::kw_union: { |
| 1153 | tok::TokenKind Kind = Tok.getKind(); |
| 1154 | ConsumeToken(); |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1155 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo); |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1156 | return true; |
Chris Lattner | 197b434 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1157 | } |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1158 | |
| 1159 | // enum-specifier: |
| 1160 | case tok::kw_enum: |
Chris Lattner | 197b434 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1161 | ConsumeToken(); |
| 1162 | ParseEnumSpecifier(Loc, DS); |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1163 | return true; |
| 1164 | |
| 1165 | // cv-qualifier: |
| 1166 | case tok::kw_const: |
| 1167 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 1168 | getLang())*2; |
| 1169 | break; |
| 1170 | case tok::kw_volatile: |
| 1171 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 1172 | getLang())*2; |
| 1173 | break; |
| 1174 | case tok::kw_restrict: |
| 1175 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 1176 | getLang())*2; |
| 1177 | break; |
| 1178 | |
| 1179 | // GNU typeof support. |
| 1180 | case tok::kw_typeof: |
| 1181 | ParseTypeofSpecifier(DS); |
| 1182 | return true; |
| 1183 | |
Steve Naroff | edd04d5 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1184 | case tok::kw___cdecl: |
| 1185 | case tok::kw___stdcall: |
| 1186 | case tok::kw___fastcall: |
Chris Lattner | 5bb837e | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 1187 | if (!PP.getLangOptions().Microsoft) return false; |
| 1188 | ConsumeToken(); |
| 1189 | return true; |
Steve Naroff | edd04d5 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1190 | |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1191 | default: |
| 1192 | // Not a type-specifier; do nothing. |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
| 1196 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 1197 | if (isInvalid) { |
| 1198 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1199 | // Pick between error or extwarn. |
| 1200 | unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination |
| 1201 | : diag::ext_duplicate_declspec; |
| 1202 | Diag(Tok, DiagID) << PrevSpec; |
Douglas Gregor | 3a6a307 | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1203 | } |
| 1204 | DS.SetRangeEnd(Tok.getLocation()); |
| 1205 | ConsumeToken(); // whatever we parsed above. |
| 1206 | return true; |
| 1207 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1208 | |
Chris Lattner | ced5b4f | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1209 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 1210 | /// semicolon. |
| 1211 | /// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1212 | /// struct-declaration: |
Chris Lattner | ced5b4f | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1213 | /// specifier-qualifier-list struct-declarator-list |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1214 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | ced5b4f | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1215 | /// [GNU] specifier-qualifier-list |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1216 | /// struct-declarator-list: |
| 1217 | /// struct-declarator |
| 1218 | /// struct-declarator-list ',' struct-declarator |
| 1219 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 1220 | /// struct-declarator: |
| 1221 | /// declarator |
| 1222 | /// [GNU] declarator attributes[opt] |
| 1223 | /// declarator[opt] ':' constant-expression |
| 1224 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 1225 | /// |
Chris Lattner | 3dd8d39 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1226 | void Parser:: |
| 1227 | ParseStructDeclaration(DeclSpec &DS, |
| 1228 | llvm::SmallVectorImpl<FieldDeclarator> &Fields) { |
Chris Lattner | daa5c00 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 1229 | if (Tok.is(tok::kw___extension__)) { |
| 1230 | // __extension__ silences extension warnings in the subexpression. |
| 1231 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1232 | ConsumeToken(); |
Chris Lattner | daa5c00 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 1233 | return ParseStructDeclaration(DS, Fields); |
| 1234 | } |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1235 | |
| 1236 | // Parse the common specifier-qualifiers-list piece. |
Chris Lattner | 12e8a4c | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 1237 | SourceLocation DSStart = Tok.getLocation(); |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1238 | ParseSpecifierQualifierList(DS); |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1239 | |
Douglas Gregor | b748fc5 | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 1240 | // If there are no declarators, this is a free-standing declaration |
| 1241 | // specifier. Let the actions module cope with it. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1242 | if (Tok.is(tok::semi)) { |
Douglas Gregor | b748fc5 | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 1243 | Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1244 | return; |
| 1245 | } |
| 1246 | |
| 1247 | // Read struct-declarators until we find the semicolon. |
Chris Lattner | f62fb73 | 2008-04-10 16:37:40 +0000 | [diff] [blame] | 1248 | Fields.push_back(FieldDeclarator(DS)); |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1249 | while (1) { |
Chris Lattner | 3dd8d39 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1250 | FieldDeclarator &DeclaratorInfo = Fields.back(); |
| 1251 | |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1252 | /// struct-declarator: declarator |
| 1253 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1254 | if (Tok.isNot(tok::colon)) |
Chris Lattner | 3dd8d39 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1255 | ParseDeclarator(DeclaratorInfo.D); |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1256 | |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1257 | if (Tok.is(tok::colon)) { |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1258 | ConsumeToken(); |
Sebastian Redl | 14ca741 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1259 | OwningExprResult Res(ParseConstantExpression()); |
Sebastian Redl | bb4dae7 | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1260 | if (Res.isInvalid()) |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1261 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 12e8a4c | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 1262 | else |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1263 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1264 | } |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1265 | |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1266 | // If attributes exist after the declarator, parse them. |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1267 | if (Tok.is(tok::kw___attribute)) { |
| 1268 | SourceLocation Loc; |
| 1269 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 1270 | DeclaratorInfo.D.AddAttributes(AttrList, Loc); |
| 1271 | } |
| 1272 | |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1273 | // If we don't have a comma, it is either the end of the list (a ';') |
| 1274 | // or an error, bail out. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1275 | if (Tok.isNot(tok::comma)) |
Chris Lattner | ced5b4f | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1276 | return; |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1277 | |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1278 | // Consume the comma. |
| 1279 | ConsumeToken(); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1280 | |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1281 | // Parse the next declarator. |
Chris Lattner | f62fb73 | 2008-04-10 16:37:40 +0000 | [diff] [blame] | 1282 | Fields.push_back(FieldDeclarator(DS)); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1283 | |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1284 | // Attributes are only allowed on the second declarator. |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1285 | if (Tok.is(tok::kw___attribute)) { |
| 1286 | SourceLocation Loc; |
| 1287 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 1288 | Fields.back().D.AddAttributes(AttrList, Loc); |
| 1289 | } |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1290 | } |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | /// ParseStructUnionBody |
| 1294 | /// struct-contents: |
| 1295 | /// struct-declaration-list |
| 1296 | /// [EXT] empty |
| 1297 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 1298 | /// struct-declaration-list: |
| 1299 | /// struct-declaration |
| 1300 | /// struct-declaration-list struct-declaration |
Chris Lattner | 1bf58f6 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1301 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | a9adf11 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1302 | /// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1303 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1304 | unsigned TagType, DeclPtrTy TagDecl) { |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 1305 | PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions, |
| 1306 | PP.getSourceManager(), |
| 1307 | "parsing struct/union body"); |
Chris Lattner | 7efd75e | 2009-03-05 02:25:03 +0000 | [diff] [blame] | 1308 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1309 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 1310 | |
Douglas Gregor | cab994d | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 1311 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | db568cf | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1312 | Actions.ActOnTagStartDefinition(CurScope, TagDecl); |
| 1313 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1314 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 1315 | // C++. |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1316 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1317 | Diag(Tok, diag::ext_empty_struct_union_enum) |
| 1318 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1319 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1320 | llvm::SmallVector<DeclPtrTy, 32> FieldDecls; |
Chris Lattner | 3dd8d39 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1321 | llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators; |
| 1322 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1323 | // While we still have something to read, read the declarations in the struct. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1324 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1325 | // Each iteration of this loop reads one struct-declaration. |
| 1326 | |
| 1327 | // Check for extraneous top-level semicolon. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1328 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 1ba5cb3 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1329 | Diag(Tok, diag::ext_extra_struct_semi) |
| 1330 | << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1331 | ConsumeToken(); |
| 1332 | continue; |
| 1333 | } |
Chris Lattner | 3dd8d39 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1334 | |
| 1335 | // Parse all the comma separated declarators. |
| 1336 | DeclSpec DS; |
| 1337 | FieldDeclarators.clear(); |
Chris Lattner | 1bf58f6 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1338 | if (!Tok.is(tok::at)) { |
| 1339 | ParseStructDeclaration(DS, FieldDeclarators); |
| 1340 | |
| 1341 | // Convert them all to fields. |
| 1342 | for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) { |
| 1343 | FieldDeclarator &FD = FieldDeclarators[i]; |
| 1344 | // Install the declarator into the current TagDecl. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1345 | DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl, |
| 1346 | DS.getSourceRange().getBegin(), |
| 1347 | FD.D, FD.BitfieldSize); |
Chris Lattner | 1bf58f6 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1348 | FieldDecls.push_back(Field); |
| 1349 | } |
| 1350 | } else { // Handle @defs |
| 1351 | ConsumeToken(); |
| 1352 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 1353 | Diag(Tok, diag::err_unexpected_at); |
| 1354 | SkipUntil(tok::semi, true, true); |
| 1355 | continue; |
| 1356 | } |
| 1357 | ConsumeToken(); |
| 1358 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 1359 | if (!Tok.is(tok::identifier)) { |
| 1360 | Diag(Tok, diag::err_expected_ident); |
| 1361 | SkipUntil(tok::semi, true, true); |
| 1362 | continue; |
| 1363 | } |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1364 | llvm::SmallVector<DeclPtrTy, 16> Fields; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1365 | Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(), |
| 1366 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 1bf58f6 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1367 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 1368 | ConsumeToken(); |
| 1369 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
| 1370 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1371 | |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1372 | if (Tok.is(tok::semi)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1373 | ConsumeToken(); |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1374 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1375 | Diag(Tok, diag::ext_expected_semi_decl_list); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1376 | break; |
| 1377 | } else { |
| 1378 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 1379 | // Skip to end of block or statement |
| 1380 | SkipUntil(tok::r_brace, true, true); |
| 1381 | } |
| 1382 | } |
| 1383 | |
Steve Naroff | 1a7fa7b | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1384 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1385 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1386 | AttributeList *AttrList = 0; |
| 1387 | // If attributes exist after struct contents, parse them. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1388 | if (Tok.is(tok::kw___attribute)) |
Daniel Dunbar | 3b90807 | 2008-10-03 16:42:10 +0000 | [diff] [blame] | 1389 | AttrList = ParseAttributes(); |
Daniel Dunbar | f394444 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1390 | |
| 1391 | Actions.ActOnFields(CurScope, |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1392 | RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(), |
Daniel Dunbar | f394444 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1393 | LBraceLoc, RBraceLoc, |
Douglas Gregor | db568cf | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1394 | AttrList); |
| 1395 | StructScope.Exit(); |
| 1396 | Actions.ActOnTagFinishDefinition(CurScope, TagDecl); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | |
| 1400 | /// ParseEnumSpecifier |
| 1401 | /// enum-specifier: [C99 6.7.2.2] |
| 1402 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1403 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1404 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 1405 | /// '}' attributes[opt] |
| 1406 | /// 'enum' identifier |
| 1407 | /// [GNU] 'enum' attributes[opt] identifier |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1408 | /// |
| 1409 | /// [C++] elaborated-type-specifier: |
| 1410 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 1411 | /// |
Chris Lattner | 197b434 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1412 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
| 1413 | AccessSpecifier AS) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1414 | // Parse the tag portion of this. |
Argiris Kirtzidis | 2298f01 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1415 | |
| 1416 | AttributeList *Attr = 0; |
| 1417 | // If attributes exist after tag, parse them. |
| 1418 | if (Tok.is(tok::kw___attribute)) |
| 1419 | Attr = ParseAttributes(); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1420 | |
| 1421 | CXXScopeSpec SS; |
Chris Lattner | d706dc8 | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 1422 | if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) { |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1423 | if (Tok.isNot(tok::identifier)) { |
| 1424 | Diag(Tok, diag::err_expected_ident); |
| 1425 | if (Tok.isNot(tok::l_brace)) { |
| 1426 | // Has no name and is not a definition. |
| 1427 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 1428 | SkipUntil(tok::comma, true); |
| 1429 | return; |
| 1430 | } |
| 1431 | } |
| 1432 | } |
Argiris Kirtzidis | 2298f01 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1433 | |
| 1434 | // Must have either 'enum name' or 'enum {...}'. |
| 1435 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) { |
| 1436 | Diag(Tok, diag::err_expected_ident_lbrace); |
| 1437 | |
| 1438 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 1439 | SkipUntil(tok::comma, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1440 | return; |
Argiris Kirtzidis | 2298f01 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | // If an identifier is present, consume and remember it. |
| 1444 | IdentifierInfo *Name = 0; |
| 1445 | SourceLocation NameLoc; |
| 1446 | if (Tok.is(tok::identifier)) { |
| 1447 | Name = Tok.getIdentifierInfo(); |
| 1448 | NameLoc = ConsumeToken(); |
| 1449 | } |
| 1450 | |
| 1451 | // There are three options here. If we have 'enum foo;', then this is a |
| 1452 | // forward declaration. If we have 'enum foo {...' then this is a |
| 1453 | // definition. Otherwise we have something like 'enum foo xyz', a reference. |
| 1454 | // |
| 1455 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 1456 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 1457 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 1458 | // |
| 1459 | Action::TagKind TK; |
| 1460 | if (Tok.is(tok::l_brace)) |
| 1461 | TK = Action::TK_Definition; |
| 1462 | else if (Tok.is(tok::semi)) |
| 1463 | TK = Action::TK_Declaration; |
| 1464 | else |
| 1465 | TK = Action::TK_Reference; |
Douglas Gregor | 71f0603 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1466 | bool Owned = false; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1467 | DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, |
Douglas Gregor | 71f0603 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1468 | StartLoc, SS, Name, NameLoc, Attr, AS, |
| 1469 | Owned); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1471 | if (Tok.is(tok::l_brace)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1472 | ParseEnumBody(StartLoc, TagDecl); |
| 1473 | |
| 1474 | // TODO: semantic analysis on the declspec for enums. |
| 1475 | const char *PrevSpec = 0; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1476 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, |
Douglas Gregor | 71f0603 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1477 | TagDecl.getAs<void>(), Owned)) |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1478 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 1482 | /// enumerator-list: |
| 1483 | /// enumerator |
| 1484 | /// enumerator-list ',' enumerator |
| 1485 | /// enumerator: |
| 1486 | /// enumeration-constant |
| 1487 | /// enumeration-constant '=' constant-expression |
| 1488 | /// enumeration-constant: |
| 1489 | /// identifier |
| 1490 | /// |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1491 | void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) { |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1492 | // Enter the scope of the enum body and start the definition. |
| 1493 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | db568cf | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1494 | Actions.ActOnTagStartDefinition(CurScope, EnumDecl); |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1495 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1496 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 1497 | |
Chris Lattner | c9a9245 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 1498 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1499 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1500 | Diag(Tok, diag::ext_empty_struct_union_enum) << "enum"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1501 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1502 | llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1503 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1504 | DeclPtrTy LastEnumConstDecl; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1505 | |
| 1506 | // Parse the enumerator-list. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1507 | while (Tok.is(tok::identifier)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1508 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 1509 | SourceLocation IdentLoc = ConsumeToken(); |
| 1510 | |
| 1511 | SourceLocation EqualLoc; |
Sebastian Redl | 6226104 | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1512 | OwningExprResult AssignedVal(Actions); |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1513 | if (Tok.is(tok::equal)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1514 | EqualLoc = ConsumeToken(); |
Sebastian Redl | bb4dae7 | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1515 | AssignedVal = ParseConstantExpression(); |
| 1516 | if (AssignedVal.isInvalid()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1517 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | // Install the enumerator constant into EnumDecl. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1521 | DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl, |
| 1522 | LastEnumConstDecl, |
| 1523 | IdentLoc, Ident, |
| 1524 | EqualLoc, |
| 1525 | AssignedVal.release()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1526 | EnumConstantDecls.push_back(EnumConstDecl); |
| 1527 | LastEnumConstDecl = EnumConstDecl; |
| 1528 | |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1529 | if (Tok.isNot(tok::comma)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1530 | break; |
| 1531 | SourceLocation CommaLoc = ConsumeToken(); |
| 1532 | |
Douglas Gregor | 1ba5cb3 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1533 | if (Tok.isNot(tok::identifier) && |
| 1534 | !(getLang().C99 || getLang().CPlusPlus0x)) |
| 1535 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
| 1536 | << getLang().CPlusPlus |
| 1537 | << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | // Eat the }. |
Mike Stump | 155750e | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 1541 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1542 | |
Mike Stump | 155750e | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 1543 | Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl, |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1544 | EnumConstantDecls.data(), EnumConstantDecls.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1545 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1546 | Action::AttrTy *AttrList = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1547 | // If attributes exist after the identifier list, parse them. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1548 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1549 | AttrList = ParseAttributes(); // FIXME: where do they do? |
Douglas Gregor | db568cf | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1550 | |
| 1551 | EnumScope.Exit(); |
| 1552 | Actions.ActOnTagFinishDefinition(CurScope, EnumDecl); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 6f9f955 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1556 | /// start of a type-qualifier-list. |
| 1557 | bool Parser::isTypeQualifier() const { |
| 1558 | switch (Tok.getKind()) { |
| 1559 | default: return false; |
| 1560 | // type-qualifier |
| 1561 | case tok::kw_const: |
| 1562 | case tok::kw_volatile: |
| 1563 | case tok::kw_restrict: |
| 1564 | return true; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1569 | /// start of a specifier-qualifier-list. |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1570 | bool Parser::isTypeSpecifierQualifier() { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1571 | switch (Tok.getKind()) { |
| 1572 | default: return false; |
Chris Lattner | b75fde6 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1573 | |
| 1574 | case tok::identifier: // foo::bar |
Douglas Gregor | d302260 | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1575 | case tok::kw_typename: // typename T::type |
Chris Lattner | b75fde6 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1576 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1577 | // recurse to handle whatever we get. |
| 1578 | if (TryAnnotateTypeOrScopeToken()) |
| 1579 | return isTypeSpecifierQualifier(); |
| 1580 | // Otherwise, not a type specifier. |
| 1581 | return false; |
Douglas Gregor | d302260 | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1582 | |
Chris Lattner | b75fde6 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1583 | case tok::coloncolon: // ::foo::bar |
| 1584 | if (NextToken().is(tok::kw_new) || // ::new |
| 1585 | NextToken().is(tok::kw_delete)) // ::delete |
| 1586 | return false; |
| 1587 | |
| 1588 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1589 | // recurse to handle whatever we get. |
| 1590 | if (TryAnnotateTypeOrScopeToken()) |
| 1591 | return isTypeSpecifierQualifier(); |
| 1592 | // Otherwise, not a type specifier. |
| 1593 | return false; |
| 1594 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1595 | // GNU attributes support. |
| 1596 | case tok::kw___attribute: |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1597 | // GNU typeof support. |
| 1598 | case tok::kw_typeof: |
| 1599 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1600 | // type-specifiers |
| 1601 | case tok::kw_short: |
| 1602 | case tok::kw_long: |
| 1603 | case tok::kw_signed: |
| 1604 | case tok::kw_unsigned: |
| 1605 | case tok::kw__Complex: |
| 1606 | case tok::kw__Imaginary: |
| 1607 | case tok::kw_void: |
| 1608 | case tok::kw_char: |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1609 | case tok::kw_wchar_t: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1610 | case tok::kw_int: |
| 1611 | case tok::kw_float: |
| 1612 | case tok::kw_double: |
Chris Lattner | 2baef2e | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 1613 | case tok::kw_bool: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1614 | case tok::kw__Bool: |
| 1615 | case tok::kw__Decimal32: |
| 1616 | case tok::kw__Decimal64: |
| 1617 | case tok::kw__Decimal128: |
| 1618 | |
Chris Lattner | 2e78db3 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 1619 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 1620 | case tok::kw_class: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1621 | case tok::kw_struct: |
| 1622 | case tok::kw_union: |
| 1623 | // enum-specifier |
| 1624 | case tok::kw_enum: |
| 1625 | |
| 1626 | // type-qualifier |
| 1627 | case tok::kw_const: |
| 1628 | case tok::kw_volatile: |
| 1629 | case tok::kw_restrict: |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1630 | |
| 1631 | // typedef-name |
Chris Lattner | 5d7eace | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1632 | case tok::annot_typename: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1633 | return true; |
Chris Lattner | 9aefe72 | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 1634 | |
| 1635 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 1636 | case tok::less: |
| 1637 | return getLang().ObjC1; |
Steve Naroff | edd04d5 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1638 | |
| 1639 | case tok::kw___cdecl: |
| 1640 | case tok::kw___stdcall: |
| 1641 | case tok::kw___fastcall: |
| 1642 | return PP.getLangOptions().Microsoft; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 1647 | /// declaration specifier. |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1648 | bool Parser::isDeclarationSpecifier() { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1649 | switch (Tok.getKind()) { |
| 1650 | default: return false; |
Chris Lattner | b75fde6 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1651 | |
| 1652 | case tok::identifier: // foo::bar |
Steve Naroff | 73ec932 | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1653 | // Unfortunate hack to support "Class.factoryMethod" notation. |
| 1654 | if (getLang().ObjC1 && NextToken().is(tok::period)) |
| 1655 | return false; |
Douglas Gregor | d302260 | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1656 | // Fall through |
Steve Naroff | 73ec932 | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1657 | |
Douglas Gregor | d302260 | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1658 | case tok::kw_typename: // typename T::type |
Chris Lattner | b75fde6 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1659 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1660 | // recurse to handle whatever we get. |
| 1661 | if (TryAnnotateTypeOrScopeToken()) |
| 1662 | return isDeclarationSpecifier(); |
| 1663 | // Otherwise, not a declaration specifier. |
| 1664 | return false; |
| 1665 | case tok::coloncolon: // ::foo::bar |
| 1666 | if (NextToken().is(tok::kw_new) || // ::new |
| 1667 | NextToken().is(tok::kw_delete)) // ::delete |
| 1668 | return false; |
| 1669 | |
| 1670 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1671 | // recurse to handle whatever we get. |
| 1672 | if (TryAnnotateTypeOrScopeToken()) |
| 1673 | return isDeclarationSpecifier(); |
| 1674 | // Otherwise, not a declaration specifier. |
| 1675 | return false; |
| 1676 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1677 | // storage-class-specifier |
| 1678 | case tok::kw_typedef: |
| 1679 | case tok::kw_extern: |
Steve Naroff | f258a0f | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1680 | case tok::kw___private_extern__: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1681 | case tok::kw_static: |
| 1682 | case tok::kw_auto: |
| 1683 | case tok::kw_register: |
| 1684 | case tok::kw___thread: |
| 1685 | |
| 1686 | // type-specifiers |
| 1687 | case tok::kw_short: |
| 1688 | case tok::kw_long: |
| 1689 | case tok::kw_signed: |
| 1690 | case tok::kw_unsigned: |
| 1691 | case tok::kw__Complex: |
| 1692 | case tok::kw__Imaginary: |
| 1693 | case tok::kw_void: |
| 1694 | case tok::kw_char: |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1695 | case tok::kw_wchar_t: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1696 | case tok::kw_int: |
| 1697 | case tok::kw_float: |
| 1698 | case tok::kw_double: |
Chris Lattner | 2baef2e | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 1699 | case tok::kw_bool: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1700 | case tok::kw__Bool: |
| 1701 | case tok::kw__Decimal32: |
| 1702 | case tok::kw__Decimal64: |
| 1703 | case tok::kw__Decimal128: |
| 1704 | |
Chris Lattner | 2e78db3 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 1705 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 1706 | case tok::kw_class: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1707 | case tok::kw_struct: |
| 1708 | case tok::kw_union: |
| 1709 | // enum-specifier |
| 1710 | case tok::kw_enum: |
| 1711 | |
| 1712 | // type-qualifier |
| 1713 | case tok::kw_const: |
| 1714 | case tok::kw_volatile: |
| 1715 | case tok::kw_restrict: |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1716 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1717 | // function-specifier |
| 1718 | case tok::kw_inline: |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1719 | case tok::kw_virtual: |
| 1720 | case tok::kw_explicit: |
Chris Lattner | e35d258 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 1721 | |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1722 | // typedef-name |
Chris Lattner | 5d7eace | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1723 | case tok::annot_typename: |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1724 | |
Chris Lattner | b707a7a | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 1725 | // GNU typeof support. |
| 1726 | case tok::kw_typeof: |
| 1727 | |
| 1728 | // GNU attributes. |
Chris Lattner | e35d258 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 1729 | case tok::kw___attribute: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1730 | return true; |
Chris Lattner | 1b2251c | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 1731 | |
| 1732 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 1733 | case tok::less: |
| 1734 | return getLang().ObjC1; |
Steve Naroff | edd04d5 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1735 | |
Steve Naroff | ab1a363 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 1736 | case tok::kw___declspec: |
Steve Naroff | edd04d5 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1737 | case tok::kw___cdecl: |
| 1738 | case tok::kw___stdcall: |
| 1739 | case tok::kw___fastcall: |
| 1740 | return PP.getLangOptions().Microsoft; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | |
| 1745 | /// ParseTypeQualifierListOpt |
| 1746 | /// type-qualifier-list: [C99 6.7.5] |
| 1747 | /// type-qualifier |
Chris Lattner | 460696f | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1748 | /// [GNU] attributes [ only if AttributesAllowed=true ] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1749 | /// type-qualifier-list type-qualifier |
Chris Lattner | 460696f | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1750 | /// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1751 | /// |
Chris Lattner | 460696f | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1752 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1753 | while (1) { |
| 1754 | int isInvalid = false; |
| 1755 | const char *PrevSpec = 0; |
| 1756 | SourceLocation Loc = Tok.getLocation(); |
| 1757 | |
| 1758 | switch (Tok.getKind()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1759 | case tok::kw_const: |
| 1760 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 1761 | getLang())*2; |
| 1762 | break; |
| 1763 | case tok::kw_volatile: |
| 1764 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 1765 | getLang())*2; |
| 1766 | break; |
| 1767 | case tok::kw_restrict: |
| 1768 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 1769 | getLang())*2; |
| 1770 | break; |
Steve Naroff | ad62040 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 1771 | case tok::kw___ptr64: |
Steve Naroff | edd04d5 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1772 | case tok::kw___cdecl: |
| 1773 | case tok::kw___stdcall: |
| 1774 | case tok::kw___fastcall: |
| 1775 | if (!PP.getLangOptions().Microsoft) |
| 1776 | goto DoneWithTypeQuals; |
| 1777 | // Just ignore it. |
| 1778 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1779 | case tok::kw___attribute: |
Chris Lattner | 460696f | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1780 | if (AttributesAllowed) { |
| 1781 | DS.AddAttributes(ParseAttributes()); |
| 1782 | continue; // do *not* consume the next token! |
| 1783 | } |
| 1784 | // otherwise, FALL THROUGH! |
| 1785 | default: |
Steve Naroff | edd04d5 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1786 | DoneWithTypeQuals: |
Chris Lattner | 460696f | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1787 | // If this is not a type-qualifier token, we're done reading type |
| 1788 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 1ba5cb3 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1789 | DS.Finish(Diags, PP); |
Chris Lattner | 460696f | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1790 | return; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1791 | } |
Chris Lattner | 306d4df | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 1792 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1793 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 1794 | if (isInvalid) { |
| 1795 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1796 | // Pick between error or extwarn. |
| 1797 | unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination |
| 1798 | : diag::ext_duplicate_declspec; |
| 1799 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1800 | } |
| 1801 | ConsumeToken(); |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | |
| 1806 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 1807 | /// |
| 1808 | void Parser::ParseDeclarator(Declarator &D) { |
| 1809 | /// This implements the 'declarator' production in the C grammar, then checks |
| 1810 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 19fec9d | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1811 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Sebastian Redl | 19fec9d | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1814 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 1815 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 1816 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1817 | /// ptr-operator production. |
| 1818 | /// |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1819 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 1820 | /// [C] pointer[opt] direct-declarator |
| 1821 | /// [C++] direct-declarator |
| 1822 | /// [C++] ptr-operator declarator |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1823 | /// |
| 1824 | /// pointer: [C99 6.7.5] |
| 1825 | /// '*' type-qualifier-list[opt] |
| 1826 | /// '*' type-qualifier-list[opt] pointer |
| 1827 | /// |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1828 | /// ptr-operator: |
| 1829 | /// '*' cv-qualifier-seq[opt] |
| 1830 | /// '&' |
Sebastian Redl | 9951dbc | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 1831 | /// [C++0x] '&&' |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1832 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 9951dbc | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 1833 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1834 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 19fec9d | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1835 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 1836 | DirectDeclParseFunction DirectDeclParser) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1837 | |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1838 | // C++ member pointers start with a '::' or a nested-name. |
| 1839 | // Member pointers get special handling, since there's no place for the |
| 1840 | // scope spec in the generic path below. |
Chris Lattner | 053dd2d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 1841 | if (getLang().CPlusPlus && |
| 1842 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 1843 | Tok.is(tok::annot_cxxscope))) { |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1844 | CXXScopeSpec SS; |
| 1845 | if (ParseOptionalCXXScopeSpecifier(SS)) { |
| 1846 | if(Tok.isNot(tok::star)) { |
| 1847 | // The scope spec really belongs to the direct-declarator. |
| 1848 | D.getCXXScopeSpec() = SS; |
| 1849 | if (DirectDeclParser) |
| 1850 | (this->*DirectDeclParser)(D); |
| 1851 | return; |
| 1852 | } |
| 1853 | |
| 1854 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1855 | D.SetRangeEnd(Loc); |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1856 | DeclSpec DS; |
| 1857 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1858 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1859 | |
| 1860 | // Recurse to parse whatever is left. |
| 1861 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 1862 | |
| 1863 | // Sema will have to catch (syntactically invalid) pointers into global |
| 1864 | // scope. It has to catch pointers into namespace scope anyway. |
| 1865 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1866 | Loc, DS.TakeAttributes()), |
| 1867 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1868 | return; |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 7aa5475 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1873 | // Not a pointer, C++ reference, or block. |
Chris Lattner | c14c7f0 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 1874 | if (Kind != tok::star && Kind != tok::caret && |
Chris Lattner | 053dd2d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 1875 | (Kind != tok::amp || !getLang().CPlusPlus) && |
Sebastian Redl | 4e67adb | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 1876 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
Chris Lattner | c14c7f0 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 1877 | (Kind != tok::ampamp || !getLang().CPlusPlus)) { |
Sebastian Redl | 19fec9d | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1878 | if (DirectDeclParser) |
| 1879 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1880 | return; |
| 1881 | } |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1882 | |
Sebastian Redl | 9951dbc | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 1883 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 1884 | // '&&' -> rvalue reference |
Sebastian Redl | 4e67adb | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 1885 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1886 | D.SetRangeEnd(Loc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1887 | |
Chris Lattner | c14c7f0 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 1888 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 69f0193 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 1889 | // Is a pointer. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1890 | DeclSpec DS; |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1891 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1892 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1893 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1894 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1895 | // Recursively parse the declarator. |
Sebastian Redl | 19fec9d | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1896 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 7aa5475 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1897 | if (Kind == tok::star) |
| 1898 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1899 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1900 | DS.TakeAttributes()), |
| 1901 | SourceLocation()); |
Steve Naroff | 7aa5475 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1902 | else |
| 1903 | // Remember that we parsed a Block type, and remember the type-quals. |
| 1904 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
Mike Stump | 7ff82e7 | 2009-04-21 00:51:43 +0000 | [diff] [blame] | 1905 | Loc, DS.TakeAttributes()), |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1906 | SourceLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1907 | } else { |
| 1908 | // Is a reference |
| 1909 | DeclSpec DS; |
| 1910 | |
Sebastian Redl | 4e67adb | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 1911 | // Complain about rvalue references in C++03, but then go on and build |
| 1912 | // the declarator. |
| 1913 | if (Kind == tok::ampamp && !getLang().CPlusPlus0x) |
| 1914 | Diag(Loc, diag::err_rvalue_reference); |
| 1915 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1916 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 1917 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 1918 | // template type argument, in which case the cv-qualifiers are ignored. |
| 1919 | // |
| 1920 | // [GNU] Retricted references are allowed. |
| 1921 | // [GNU] Attributes on references are allowed. |
| 1922 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1923 | D.ExtendWithDeclSpec(DS); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1924 | |
| 1925 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 1926 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 1927 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1928 | diag::err_invalid_reference_qualifier_application) << "const"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1929 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 1930 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1931 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | // Recursively parse the declarator. |
Sebastian Redl | 19fec9d | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1935 | ParseDeclaratorInternal(D, DirectDeclParser); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1936 | |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1937 | if (D.getNumTypeObjects() > 0) { |
| 1938 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 1939 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 1940 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | 8f7db15 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 1941 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 1942 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 1943 | << II; |
| 1944 | else |
| 1945 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 1946 | << "type name"; |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1947 | |
Sebastian Redl | 19fec9d | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1948 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1949 | // can go ahead and build the (technically ill-formed) |
| 1950 | // declarator: reference collapsing will take care of it. |
| 1951 | } |
| 1952 | } |
| 1953 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1954 | // Remember that we parsed a reference type. It doesn't have type-quals. |
Chris Lattner | 69f0193 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 1955 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 9951dbc | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 1956 | DS.TakeAttributes(), |
| 1957 | Kind == tok::amp), |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1958 | SourceLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | /// ParseDirectDeclarator |
| 1963 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1964 | /// [C99] identifier |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1965 | /// '(' declarator ')' |
| 1966 | /// [GNU] '(' attributes declarator ')' |
| 1967 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1968 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1969 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1970 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1971 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1972 | /// direct-declarator '(' parameter-type-list ')' |
| 1973 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1974 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1975 | /// parameter-type-list[opt] ')' |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1976 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 1977 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1978 | /// [C++] declarator-id |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1979 | /// |
| 1980 | /// declarator-id: [C++ 8] |
| 1981 | /// id-expression |
| 1982 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 1983 | /// |
| 1984 | /// id-expression: [C++ 5.1] |
| 1985 | /// unqualified-id |
| 1986 | /// qualified-id [TODO] |
| 1987 | /// |
| 1988 | /// unqualified-id: [C++ 5.1] |
| 1989 | /// identifier |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1990 | /// operator-function-id |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1991 | /// conversion-function-id [TODO] |
| 1992 | /// '~' class-name |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1993 | /// template-id |
Argiris Kirtzidis | c9e909c | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1994 | /// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1995 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argiris Kirtzidis | ebdc8ea | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1996 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1997 | |
Argiris Kirtzidis | ebdc8ea | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1998 | if (getLang().CPlusPlus) { |
| 1999 | if (D.mayHaveIdentifier()) { |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2000 | // ParseDeclaratorInternal might already have parsed the scope. |
| 2001 | bool afterCXXScope = D.getCXXScopeSpec().isSet() || |
| 2002 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec()); |
Argiris Kirtzidis | ebdc8ea | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2003 | if (afterCXXScope) { |
| 2004 | // Change the declaration context for name lookup, until this function |
| 2005 | // is exited (and the declarator has been parsed). |
| 2006 | DeclScopeObj.EnterDeclaratorScope(); |
| 2007 | } |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2008 | |
Argiris Kirtzidis | ebdc8ea | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2009 | if (Tok.is(tok::identifier)) { |
| 2010 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
Anders Carlsson | e19759d | 2009-04-30 22:41:11 +0000 | [diff] [blame] | 2011 | |
| 2012 | // If this identifier is the name of the current class, it's a |
| 2013 | // constructor name. |
| 2014 | if (!D.getDeclSpec().hasTypeSpecifier() && |
| 2015 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) { |
| 2016 | D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2017 | Tok.getLocation(), CurScope), |
| 2018 | Tok.getLocation()); |
| 2019 | // This is a normal identifier. |
| 2020 | } else |
| 2021 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
Argiris Kirtzidis | ebdc8ea | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2022 | ConsumeToken(); |
| 2023 | goto PastIdentifier; |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2024 | } else if (Tok.is(tok::annot_template_id)) { |
| 2025 | TemplateIdAnnotation *TemplateId |
| 2026 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 2027 | |
| 2028 | // FIXME: Could this template-id name a constructor? |
| 2029 | |
| 2030 | // FIXME: This is an egregious hack, where we silently ignore |
| 2031 | // the specialization (which should be a function template |
| 2032 | // specialization name) and use the name instead. This hack |
| 2033 | // will go away when we have support for function |
| 2034 | // specializations. |
| 2035 | D.SetIdentifier(TemplateId->Name, Tok.getLocation()); |
| 2036 | TemplateId->Destroy(); |
| 2037 | ConsumeToken(); |
| 2038 | goto PastIdentifier; |
Douglas Gregor | 853dd39 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2039 | } else if (Tok.is(tok::kw_operator)) { |
| 2040 | SourceLocation OperatorLoc = Tok.getLocation(); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2041 | SourceLocation EndLoc; |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2042 | |
Douglas Gregor | 853dd39 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2043 | // First try the name of an overloaded operator |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2044 | if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) { |
| 2045 | D.setOverloadedOperator(Op, OperatorLoc, EndLoc); |
Douglas Gregor | 853dd39 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2046 | } else { |
| 2047 | // This must be a conversion function (C++ [class.conv.fct]). |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2048 | if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc)) |
| 2049 | D.setConversionFunction(ConvType, OperatorLoc, EndLoc); |
| 2050 | else { |
Douglas Gregor | 853dd39 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2051 | D.SetIdentifier(0, Tok.getLocation()); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2052 | } |
Douglas Gregor | 853dd39 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2053 | } |
| 2054 | goto PastIdentifier; |
| 2055 | } else if (Tok.is(tok::tilde)) { |
Argiris Kirtzidis | ebdc8ea | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2056 | // This should be a C++ destructor. |
| 2057 | SourceLocation TildeLoc = ConsumeToken(); |
| 2058 | if (Tok.is(tok::identifier)) { |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2059 | // FIXME: Inaccurate. |
| 2060 | SourceLocation NameLoc = Tok.getLocation(); |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 2061 | SourceLocation EndLoc; |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2062 | TypeResult Type = ParseClassName(EndLoc); |
| 2063 | if (Type.isInvalid()) |
Argiris Kirtzidis | ebdc8ea | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2064 | D.SetIdentifier(0, TildeLoc); |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2065 | else |
| 2066 | D.setDestructor(Type.get(), TildeLoc, NameLoc); |
Argiris Kirtzidis | ebdc8ea | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2067 | } else { |
| 2068 | Diag(Tok, diag::err_expected_class_name); |
| 2069 | D.SetIdentifier(0, TildeLoc); |
| 2070 | } |
| 2071 | goto PastIdentifier; |
| 2072 | } |
| 2073 | |
| 2074 | // If we reached this point, token is not identifier and not '~'. |
| 2075 | |
| 2076 | if (afterCXXScope) { |
| 2077 | Diag(Tok, diag::err_expected_unqualified_id); |
| 2078 | D.SetIdentifier(0, Tok.getLocation()); |
| 2079 | D.setInvalidType(true); |
| 2080 | goto PastIdentifier; |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2081 | } |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2082 | } |
Argiris Kirtzidis | ebdc8ea | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | // If we reached this point, we are either in C/ObjC or the token didn't |
| 2086 | // satisfy any of the C++-specific checks. |
Argiris Kirtzidis | ebdc8ea | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2087 | if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
| 2088 | assert(!getLang().CPlusPlus && |
| 2089 | "There's a C++-specific check for tok::identifier above"); |
| 2090 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 2091 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 2092 | ConsumeToken(); |
| 2093 | } else if (Tok.is(tok::l_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2094 | // direct-declarator: '(' declarator ')' |
| 2095 | // direct-declarator: '(' attributes declarator ')' |
| 2096 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 2097 | ParseParenDeclarator(D); |
Argiris Kirtzidis | ebdc8ea | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2098 | } else if (D.mayOmitIdentifier()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2099 | // This could be something simple like "int" (in which case the declarator |
| 2100 | // portion is empty), if an abstract-declarator is allowed. |
| 2101 | D.SetIdentifier(0, Tok.getLocation()); |
| 2102 | } else { |
Douglas Gregor | f03265d | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 2103 | if (D.getContext() == Declarator::MemberContext) |
| 2104 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 2105 | << D.getDeclSpec().getSourceRange(); |
| 2106 | else if (getLang().CPlusPlus) |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2107 | Diag(Tok, diag::err_expected_unqualified_id); |
| 2108 | else |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2109 | Diag(Tok, diag::err_expected_ident_lparen); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2110 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | cd61d59 | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 2111 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Argiris Kirtzidis | ebdc8ea | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2114 | PastIdentifier: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2115 | assert(D.isPastIdentifier() && |
| 2116 | "Haven't past the location of the identifier yet?"); |
| 2117 | |
| 2118 | while (1) { |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2119 | if (Tok.is(tok::l_paren)) { |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2120 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 2121 | // In such a case, check if we actually have a function declarator; if it |
| 2122 | // is not, the declarator has been fully parsed. |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2123 | if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 2124 | // When not in file scope, warn for ambiguous function declarators, just |
| 2125 | // in case the author intended it as a variable definition. |
| 2126 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 2127 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 2128 | break; |
| 2129 | } |
Chris Lattner | a0d056d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2130 | ParseFunctionDeclarator(ConsumeParen(), D); |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2131 | } else if (Tok.is(tok::l_square)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2132 | ParseBracketDeclarator(D); |
| 2133 | } else { |
| 2134 | break; |
| 2135 | } |
| 2136 | } |
| 2137 | } |
| 2138 | |
Chris Lattner | a0d056d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2139 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 2140 | /// only called before the identifier, so these are most likely just grouping |
| 2141 | /// parens for precedence. If we find that these are actually function |
| 2142 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 2143 | /// |
| 2144 | /// direct-declarator: |
| 2145 | /// '(' declarator ')' |
| 2146 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2147 | /// direct-declarator '(' parameter-type-list ')' |
| 2148 | /// direct-declarator '(' identifier-list[opt] ')' |
| 2149 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 2150 | /// parameter-type-list[opt] ')' |
Chris Lattner | a0d056d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2151 | /// |
| 2152 | void Parser::ParseParenDeclarator(Declarator &D) { |
| 2153 | SourceLocation StartLoc = ConsumeParen(); |
| 2154 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
| 2155 | |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2156 | // Eat any attributes before we look at whether this is a grouping or function |
| 2157 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 2158 | // the type being built up, for example: |
| 2159 | // int (__attribute__(()) *x)(long y) |
| 2160 | // If this ends up not being a grouping paren, the attribute applies to the |
| 2161 | // first argument, for example: |
| 2162 | // int (__attribute__(()) int x) |
| 2163 | // In either case, we need to eat any attributes to be able to determine what |
| 2164 | // sort of paren this is. |
| 2165 | // |
| 2166 | AttributeList *AttrList = 0; |
| 2167 | bool RequiresArg = false; |
| 2168 | if (Tok.is(tok::kw___attribute)) { |
| 2169 | AttrList = ParseAttributes(); |
| 2170 | |
| 2171 | // We require that the argument list (if this is a non-grouping paren) be |
| 2172 | // present even if the attribute list was empty. |
| 2173 | RequiresArg = true; |
| 2174 | } |
Steve Naroff | edd04d5 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2175 | // Eat any Microsoft extensions. |
Douglas Gregor | e51b7c8 | 2009-01-10 00:48:18 +0000 | [diff] [blame] | 2176 | while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
| 2177 | (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft) |
Steve Naroff | edd04d5 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2178 | ConsumeToken(); |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2179 | |
Chris Lattner | a0d056d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2180 | // If we haven't past the identifier yet (or where the identifier would be |
| 2181 | // stored, if this is an abstract declarator), then this is probably just |
| 2182 | // grouping parens. However, if this could be an abstract-declarator, then |
| 2183 | // this could also be the start of function arguments (consider 'void()'). |
| 2184 | bool isGrouping; |
| 2185 | |
| 2186 | if (!D.mayOmitIdentifier()) { |
| 2187 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 2188 | // paren, because we haven't seen the identifier yet. |
| 2189 | isGrouping = true; |
| 2190 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Argiris Kirtzidis | 1c64fdc | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 2191 | (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | a0d056d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2192 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 2193 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 2194 | // considered to be a type, not a K&R identifier-list. |
| 2195 | isGrouping = false; |
| 2196 | } else { |
| 2197 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 2198 | isGrouping = true; |
| 2199 | } |
| 2200 | |
| 2201 | // If this is a grouping paren, handle: |
| 2202 | // direct-declarator: '(' declarator ')' |
| 2203 | // direct-declarator: '(' attributes declarator ')' |
| 2204 | if (isGrouping) { |
Argiris Kirtzidis | 0941ff4 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 2205 | bool hadGroupingParens = D.hasGroupingParens(); |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2206 | D.setGroupingParens(true); |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2207 | if (AttrList) |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2208 | D.AddAttributes(AttrList, SourceLocation()); |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2209 | |
Sebastian Redl | 19fec9d | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2210 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | a0d056d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2211 | // Match the ')'. |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2212 | SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc); |
Argiris Kirtzidis | 0941ff4 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 2213 | |
| 2214 | D.setGroupingParens(hadGroupingParens); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2215 | D.SetRangeEnd(Loc); |
Chris Lattner | a0d056d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2216 | return; |
| 2217 | } |
| 2218 | |
| 2219 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 2220 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2221 | // identifier (and remember where it would have been), then call into |
| 2222 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | a0d056d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2223 | D.SetIdentifier(0, Tok.getLocation()); |
| 2224 | |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2225 | ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg); |
Chris Lattner | a0d056d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 2229 | /// declarator D up to a paren, which indicates that we are parsing function |
| 2230 | /// arguments. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2231 | /// |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2232 | /// If AttrList is non-null, then the caller parsed those arguments immediately |
| 2233 | /// after the open paren - they should be considered to be the first argument of |
| 2234 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 2235 | /// function is required to be present and required to not be an identifier |
| 2236 | /// list. |
| 2237 | /// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2238 | /// This method also handles this portion of the grammar: |
| 2239 | /// parameter-type-list: [C99 6.7.5] |
| 2240 | /// parameter-list |
| 2241 | /// parameter-list ',' '...' |
| 2242 | /// |
| 2243 | /// parameter-list: [C99 6.7.5] |
| 2244 | /// parameter-declaration |
| 2245 | /// parameter-list ',' parameter-declaration |
| 2246 | /// |
| 2247 | /// parameter-declaration: [C99 6.7.5] |
| 2248 | /// declaration-specifiers declarator |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2249 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2250 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | a8cecf6 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 2251 | /// declaration-specifiers abstract-declarator[opt] |
| 2252 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2253 | /// '=' assignment-expression |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2254 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 2255 | /// |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2256 | /// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]" |
Sebastian Redl | a8cecf6 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 2257 | /// and "exception-specification[opt]". |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2258 | /// |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2259 | void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, |
| 2260 | AttributeList *AttrList, |
| 2261 | bool RequiresArg) { |
Chris Lattner | a0d056d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2262 | // lparen is already consumed! |
| 2263 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2264 | |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2265 | // This parameter list may be empty. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2266 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2267 | if (RequiresArg) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2268 | Diag(Tok, diag::err_argument_required_after_attribute); |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2269 | delete AttrList; |
| 2270 | } |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2271 | |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2272 | SourceLocation Loc = ConsumeParen(); // Eat the closing ')'. |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2273 | |
| 2274 | // cv-qualifier-seq[opt]. |
| 2275 | DeclSpec DS; |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2276 | bool hasExceptionSpec = false; |
| 2277 | bool hasAnyExceptionSpec = false; |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2278 | llvm::SmallVector<TypeTy*, 2> Exceptions; |
| 2279 | llvm::SmallVector<SourceRange, 2> ExceptionRanges; |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2280 | if (getLang().CPlusPlus) { |
Chris Lattner | 460696f | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2281 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2282 | if (!DS.getSourceRange().getEnd().isInvalid()) |
| 2283 | Loc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2284 | |
| 2285 | // Parse exception-specification[opt]. |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2286 | if (Tok.is(tok::kw_throw)) { |
| 2287 | hasExceptionSpec = true; |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2288 | ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges, |
| 2289 | hasAnyExceptionSpec); |
| 2290 | assert(Exceptions.size() == ExceptionRanges.size() && |
| 2291 | "Produced different number of exception types and ranges."); |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2292 | } |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2295 | // Remember that we parsed a function type, and remember the attributes. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2296 | // int() -> no prototype, no '...'. |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2297 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus, |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2298 | /*variadic*/ false, |
Douglas Gregor | 88a25f8 | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2299 | SourceLocation(), |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2300 | /*arglist*/ 0, 0, |
| 2301 | DS.getTypeQualifiers(), |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2302 | hasExceptionSpec, |
| 2303 | hasAnyExceptionSpec, |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2304 | Exceptions.data(), |
| 2305 | ExceptionRanges.data(), |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2306 | Exceptions.size(), |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2307 | LParenLoc, D), |
| 2308 | Loc); |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2309 | return; |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2312 | // Alternatively, this parameter list may be an identifier list form for a |
| 2313 | // K&R-style function: void foo(a,b,c) |
Steve Naroff | 3f3f3b4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 2314 | if (!getLang().CPlusPlus && Tok.is(tok::identifier)) { |
Steve Naroff | 965f5d7 | 2009-01-30 14:23:32 +0000 | [diff] [blame] | 2315 | if (!TryAnnotateTypeOrScopeToken()) { |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2316 | // K&R identifier lists can't have typedefs as identifiers, per |
| 2317 | // C99 6.7.5.3p11. |
Steve Naroff | 3f3f3b4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 2318 | if (RequiresArg) { |
| 2319 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 2320 | delete AttrList; |
| 2321 | } |
Steve Naroff | 3f3f3b4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 2322 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 2323 | // normal declarators, not for abstract-declarators. |
| 2324 | return ParseFunctionDeclaratorIdentifierList(LParenLoc, D); |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2325 | } |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | // Finally, a normal, non-empty parameter type list. |
| 2329 | |
| 2330 | // Build up an array of information about the parsed arguments. |
| 2331 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2332 | |
| 2333 | // Enter function-declaration scope, limiting any declarators to the |
| 2334 | // function prototype scope, including parameter declarators. |
Chris Lattner | c24b889 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 2335 | ParseScope PrototypeScope(this, |
| 2336 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2337 | |
| 2338 | bool IsVariadic = false; |
Douglas Gregor | 88a25f8 | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2339 | SourceLocation EllipsisLoc; |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2340 | while (1) { |
| 2341 | if (Tok.is(tok::ellipsis)) { |
| 2342 | IsVariadic = true; |
Douglas Gregor | 88a25f8 | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2343 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2344 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2347 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2348 | |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2349 | // Parse the declaration-specifiers. |
| 2350 | DeclSpec DS; |
Chris Lattner | 1f18529 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2351 | |
| 2352 | // If the caller parsed attributes for the first argument, add them now. |
| 2353 | if (AttrList) { |
| 2354 | DS.AddAttributes(AttrList); |
| 2355 | AttrList = 0; // Only apply the attributes to the first parameter. |
| 2356 | } |
Chris Lattner | 9e785f5 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 2357 | ParseDeclarationSpecifiers(DS); |
| 2358 | |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2359 | // Parse the declarator. This is "PrototypeContext", because we must |
| 2360 | // accept either 'declarator' or 'abstract-declarator' here. |
| 2361 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 2362 | ParseDeclarator(ParmDecl); |
| 2363 | |
| 2364 | // Parse GNU attributes, if present. |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2365 | if (Tok.is(tok::kw___attribute)) { |
| 2366 | SourceLocation Loc; |
| 2367 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 2368 | ParmDecl.AddAttributes(AttrList, Loc); |
| 2369 | } |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2370 | |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2371 | // Remember this parsed parameter in ParamInfo. |
| 2372 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
| 2373 | |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2374 | // DefArgToks is used when the parsing of default arguments needs |
| 2375 | // to be delayed. |
| 2376 | CachedTokens *DefArgToks = 0; |
| 2377 | |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2378 | // If no parameter was specified, verify that *something* was specified, |
| 2379 | // otherwise we have a missing type and identifier. |
Chris Lattner | 9e785f5 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 2380 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 2381 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2382 | // Completely missing, emit error. |
| 2383 | Diag(DSStart, diag::err_missing_param); |
| 2384 | } else { |
| 2385 | // Otherwise, we have something. Add it and let semantic analysis try |
| 2386 | // to grok it and add the result to the ParamInfo we are building. |
| 2387 | |
| 2388 | // Inform the actions module about the parameter declarator, so it gets |
| 2389 | // added to the current scope. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2390 | DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2391 | |
| 2392 | // Parse the default argument, if any. We parse the default |
| 2393 | // arguments in all dialects; the semantic analysis in |
| 2394 | // ActOnParamDefaultArgument will reject the default argument in |
| 2395 | // C. |
| 2396 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2397 | SourceLocation EqualLoc = Tok.getLocation(); |
| 2398 | |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2399 | // Parse the default argument |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2400 | if (D.getContext() == Declarator::MemberContext) { |
| 2401 | // If we're inside a class definition, cache the tokens |
| 2402 | // corresponding to the default argument. We'll actually parse |
| 2403 | // them when we see the end of the class definition. |
| 2404 | // FIXME: Templates will require something similar. |
| 2405 | // FIXME: Can we use a smart pointer for Toks? |
| 2406 | DefArgToks = new CachedTokens; |
| 2407 | |
| 2408 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
| 2409 | tok::semi, false)) { |
| 2410 | delete DefArgToks; |
| 2411 | DefArgToks = 0; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2412 | Actions.ActOnParamDefaultArgumentError(Param); |
| 2413 | } else |
| 2414 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2415 | } else { |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2416 | // Consume the '='. |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2417 | ConsumeToken(); |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2418 | |
| 2419 | OwningExprResult DefArgResult(ParseAssignmentExpression()); |
| 2420 | if (DefArgResult.isInvalid()) { |
| 2421 | Actions.ActOnParamDefaultArgumentError(Param); |
| 2422 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 2423 | } else { |
| 2424 | // Inform the actions module about the default argument |
| 2425 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2426 | move(DefArgResult)); |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2427 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2428 | } |
| 2429 | } |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2430 | |
| 2431 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2432 | ParmDecl.getIdentifierLoc(), Param, |
| 2433 | DefArgToks)); |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | // If the next token is a comma, consume it and keep reading arguments. |
| 2437 | if (Tok.isNot(tok::comma)) break; |
| 2438 | |
| 2439 | // Consume the comma. |
| 2440 | ConsumeToken(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2443 | // Leave prototype scope. |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 2444 | PrototypeScope.Exit(); |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2445 | |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2446 | // If we have the closing ')', eat it. |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2447 | SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2448 | |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2449 | DeclSpec DS; |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2450 | bool hasExceptionSpec = false; |
| 2451 | bool hasAnyExceptionSpec = false; |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2452 | llvm::SmallVector<TypeTy*, 2> Exceptions; |
| 2453 | llvm::SmallVector<SourceRange, 2> ExceptionRanges; |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2454 | if (getLang().CPlusPlus) { |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2455 | // Parse cv-qualifier-seq[opt]. |
Chris Lattner | 460696f | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2456 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2457 | if (!DS.getSourceRange().getEnd().isInvalid()) |
| 2458 | Loc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2459 | |
| 2460 | // Parse exception-specification[opt]. |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2461 | if (Tok.is(tok::kw_throw)) { |
| 2462 | hasExceptionSpec = true; |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2463 | ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges, |
| 2464 | hasAnyExceptionSpec); |
| 2465 | assert(Exceptions.size() == ExceptionRanges.size() && |
| 2466 | "Produced different number of exception types and ranges."); |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2467 | } |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2470 | // Remember that we parsed a function type, and remember the attributes. |
Chris Lattner | 9f7564b | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2471 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic, |
Douglas Gregor | 88a25f8 | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2472 | EllipsisLoc, |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2473 | ParamInfo.data(), ParamInfo.size(), |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2474 | DS.getTypeQualifiers(), |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2475 | hasExceptionSpec, |
| 2476 | hasAnyExceptionSpec, |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2477 | Exceptions.data(), |
| 2478 | ExceptionRanges.data(), |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2479 | Exceptions.size(), LParenLoc, D), |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2480 | Loc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
Chris Lattner | 35d9c91 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2483 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 2484 | /// we found a K&R-style identifier list instead of a type argument list. The |
| 2485 | /// current token is known to be the first identifier in the list. |
| 2486 | /// |
| 2487 | /// identifier-list: [C99 6.7.5] |
| 2488 | /// identifier |
| 2489 | /// identifier-list ',' identifier |
| 2490 | /// |
| 2491 | void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc, |
| 2492 | Declarator &D) { |
| 2493 | // Build up an array of information about the parsed arguments. |
| 2494 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 2495 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 2496 | |
| 2497 | // If there was no identifier specified for the declarator, either we are in |
| 2498 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 2499 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 2500 | // diagnose this. |
| 2501 | if (!D.getIdentifier()) |
| 2502 | Diag(Tok, diag::ext_ident_list_in_param); |
| 2503 | |
| 2504 | // Tok is known to be the first identifier in the list. Remember this |
| 2505 | // identifier in ParamInfo. |
Chris Lattner | c337fa2 | 2008-04-06 06:50:56 +0000 | [diff] [blame] | 2506 | ParamsSoFar.insert(Tok.getIdentifierInfo()); |
Chris Lattner | 35d9c91 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2507 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(), |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2508 | Tok.getLocation(), |
| 2509 | DeclPtrTy())); |
Chris Lattner | 35d9c91 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2510 | |
Chris Lattner | 113a56b | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2511 | ConsumeToken(); // eat the first identifier. |
Chris Lattner | 35d9c91 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2512 | |
| 2513 | while (Tok.is(tok::comma)) { |
| 2514 | // Eat the comma. |
| 2515 | ConsumeToken(); |
| 2516 | |
Chris Lattner | 113a56b | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2517 | // If this isn't an identifier, report the error and skip until ')'. |
Chris Lattner | 35d9c91 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2518 | if (Tok.isNot(tok::identifier)) { |
| 2519 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 113a56b | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2520 | SkipUntil(tok::r_paren); |
| 2521 | return; |
Chris Lattner | 35d9c91 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2522 | } |
Chris Lattner | acb67d9 | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 2523 | |
Chris Lattner | 35d9c91 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2524 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
Chris Lattner | acb67d9 | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 2525 | |
| 2526 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
Douglas Gregor | 1075a16 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 2527 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope)) |
Chris Lattner | 8f7db15 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 2528 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
Chris Lattner | 35d9c91 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2529 | |
| 2530 | // Verify that the argument identifier has not already been mentioned. |
| 2531 | if (!ParamsSoFar.insert(ParmII)) { |
Chris Lattner | 8f7db15 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 2532 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
Chris Lattner | 113a56b | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2533 | } else { |
| 2534 | // Remember this identifier in ParamInfo. |
Chris Lattner | 35d9c91 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2535 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2536 | Tok.getLocation(), |
| 2537 | DeclPtrTy())); |
Chris Lattner | 113a56b | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2538 | } |
Chris Lattner | 35d9c91 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2539 | |
| 2540 | // Eat the identifier. |
| 2541 | ConsumeToken(); |
| 2542 | } |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2543 | |
| 2544 | // If we have the closing ')', eat it and we're done. |
| 2545 | SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 2546 | |
Chris Lattner | 113a56b | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2547 | // Remember that we parsed a function type, and remember the attributes. This |
| 2548 | // function type is always a K&R style function type, which is not varargs and |
| 2549 | // has no prototype. |
| 2550 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false, |
Douglas Gregor | 88a25f8 | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2551 | SourceLocation(), |
Chris Lattner | 113a56b | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2552 | &ParamInfo[0], ParamInfo.size(), |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2553 | /*TypeQuals*/0, |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2554 | /*exception*/false, false, 0, 0, 0, |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2555 | LParenLoc, D), |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2556 | RLoc); |
Chris Lattner | 35d9c91 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2557 | } |
Chris Lattner | a0d056d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2558 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2559 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 2560 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 2561 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 2562 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 2563 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 2564 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 2565 | SourceLocation StartLoc = ConsumeBracket(); |
| 2566 | |
Chris Lattner | 1525c3a | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2567 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 2568 | // This code does a fast path to handle some of the most obvious cases. |
| 2569 | if (Tok.getKind() == tok::r_square) { |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2570 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
Chris Lattner | 1525c3a | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2571 | // Remember that we parsed the empty array type. |
| 2572 | OwningExprResult NumElements(Actions); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2573 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc), |
| 2574 | EndLoc); |
Chris Lattner | 1525c3a | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2575 | return; |
| 2576 | } else if (Tok.getKind() == tok::numeric_constant && |
| 2577 | GetLookAheadToken(1).is(tok::r_square)) { |
| 2578 | // [4] is very common. Parse the numeric constant expression. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2579 | OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok)); |
Chris Lattner | 1525c3a | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2580 | ConsumeToken(); |
| 2581 | |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2582 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
Chris Lattner | 1525c3a | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2583 | |
| 2584 | // If there was an error parsing the assignment-expression, recover. |
| 2585 | if (ExprRes.isInvalid()) |
| 2586 | ExprRes.release(); // Deallocate expr, just use []. |
| 2587 | |
| 2588 | // Remember that we parsed a array type, and remember its features. |
| 2589 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2590 | ExprRes.release(), StartLoc), |
| 2591 | EndLoc); |
Chris Lattner | 1525c3a | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2592 | return; |
| 2593 | } |
| 2594 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2595 | // If valid, this location is the position where we read the 'static' keyword. |
| 2596 | SourceLocation StaticLoc; |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2597 | if (Tok.is(tok::kw_static)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2598 | StaticLoc = ConsumeToken(); |
| 2599 | |
| 2600 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | 306d4df | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 2601 | // Type qualifiers in an array subscript are a C99 feature. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2602 | DeclSpec DS; |
Chris Lattner | 460696f | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2603 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2604 | |
| 2605 | // If we haven't already read 'static', check to see if there is one after the |
| 2606 | // type-qualifier-list. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2607 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2608 | StaticLoc = ConsumeToken(); |
| 2609 | |
| 2610 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 2611 | bool isStar = false; |
Sebastian Redl | 6226104 | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 2612 | OwningExprResult NumElements(Actions); |
Chris Lattner | 44f6d9d | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 2613 | |
| 2614 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 2615 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 2616 | // the the token after the star is a ']'. Since stars in arrays are |
| 2617 | // infrequent, use of lookahead is not costly here. |
| 2618 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | 1bb3951 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 2619 | ConsumeToken(); // Eat the '*'. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2620 | |
Chris Lattner | 306d4df | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 2621 | if (StaticLoc.isValid()) { |
Chris Lattner | 44f6d9d | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 2622 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | 306d4df | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 2623 | StaticLoc = SourceLocation(); // Drop the static. |
| 2624 | } |
Chris Lattner | 44f6d9d | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 2625 | isStar = true; |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2626 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 1525c3a | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2627 | // Note, in C89, this production uses the constant-expr production instead |
| 2628 | // of assignment-expr. The only difference is that assignment-expr allows |
| 2629 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 2630 | // are not i-c-e's, so we don't need to distinguish between the two here. |
| 2631 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2632 | // Parse the assignment-expression now. |
| 2633 | NumElements = ParseAssignmentExpression(); |
| 2634 | } |
| 2635 | |
| 2636 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | bb4dae7 | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2637 | if (NumElements.isInvalid()) { |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2638 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2639 | // If the expression was invalid, skip it. |
| 2640 | SkipUntil(tok::r_square); |
| 2641 | return; |
| 2642 | } |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2643 | |
| 2644 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
| 2645 | |
Chris Lattner | 1525c3a | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2646 | // Remember that we parsed a array type, and remember its features. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2647 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
| 2648 | StaticLoc.isValid(), isStar, |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2649 | NumElements.release(), StartLoc), |
| 2650 | EndLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
Argiris Kirtzidis | c2a384d | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2653 | /// [GNU] typeof-specifier: |
| 2654 | /// typeof ( expressions ) |
| 2655 | /// typeof ( type-name ) |
| 2656 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2657 | /// |
| 2658 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2659 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argiris Kirtzidis | 4c90fb2 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 2660 | Token OpTok = Tok; |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2661 | SourceLocation StartLoc = ConsumeToken(); |
| 2662 | |
Argiris Kirtzidis | 4c90fb2 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 2663 | bool isCastExpr; |
| 2664 | TypeTy *CastTy; |
| 2665 | SourceRange CastRange; |
| 2666 | OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok, |
| 2667 | isCastExpr, |
| 2668 | CastTy, |
| 2669 | CastRange); |
| 2670 | |
| 2671 | if (CastRange.getEnd().isInvalid()) |
Argiris Kirtzidis | 53f0548 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 2672 | // FIXME: Not accurate, the range gets one token more than it should. |
| 2673 | DS.SetRangeEnd(Tok.getLocation()); |
Argiris Kirtzidis | 4c90fb2 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 2674 | else |
| 2675 | DS.SetRangeEnd(CastRange.getEnd()); |
| 2676 | |
| 2677 | if (isCastExpr) { |
| 2678 | if (!CastTy) { |
| 2679 | DS.SetTypeSpecError(); |
Argiris Kirtzidis | c2a384d | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2680 | return; |
Douglas Gregor | 6c0f406 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 2681 | } |
Argiris Kirtzidis | c2a384d | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2682 | |
Argiris Kirtzidis | 4c90fb2 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 2683 | const char *PrevSpec = 0; |
| 2684 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 2685 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
| 2686 | CastTy)) |
| 2687 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
| 2688 | return; |
Argiris Kirtzidis | 53f0548 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 2689 | } |
Argiris Kirtzidis | c2a384d | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2690 | |
Argiris Kirtzidis | 53f0548 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 2691 | // If we get here, the operand to the typeof was an expresion. |
| 2692 | if (Operand.isInvalid()) { |
| 2693 | DS.SetTypeSpecError(); |
Steve Naroff | 14bbce8 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 2694 | return; |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2695 | } |
Argiris Kirtzidis | c2a384d | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2696 | |
Argiris Kirtzidis | 53f0548 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 2697 | const char *PrevSpec = 0; |
| 2698 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 2699 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
| 2700 | Operand.release())) |
| 2701 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2702 | } |