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