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