Chris Lattner | 7ad0fbe | 2006-11-05 07:46:30 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +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 | 60f3622 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Scope.h" |
| 17 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 18 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chris Lattner | 8a9a97a | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 19 | #include "RAIIObjectsForParser.h" |
Chris Lattner | ad9ac94 | 2007-01-23 01:14:52 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // C99 6.7: Declarations. |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 27 | /// ParseTypeName |
| 28 | /// type-name: [C99 6.7.6] |
| 29 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 30 | /// |
| 31 | /// Called type-id in C++. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 32 | TypeResult Parser::ParseTypeName(SourceRange *Range) { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 33 | // Parse the common declaration-specifiers piece. |
| 34 | DeclSpec DS; |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 35 | ParseSpecifierQualifierList(DS); |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 36 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 37 | // Parse the abstract-declarator, if present. |
| 38 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 39 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 40 | if (Range) |
| 41 | *Range = DeclaratorInfo.getSourceRange(); |
| 42 | |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 43 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 44 | return true; |
| 45 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 46 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 49 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 50 | /// |
| 51 | /// [GNU] attributes: |
| 52 | /// attribute |
| 53 | /// attributes attribute |
| 54 | /// |
| 55 | /// [GNU] attribute: |
| 56 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 57 | /// |
| 58 | /// [GNU] attribute-list: |
| 59 | /// attrib |
| 60 | /// attribute_list ',' attrib |
| 61 | /// |
| 62 | /// [GNU] attrib: |
| 63 | /// empty |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 64 | /// attrib-name |
| 65 | /// attrib-name '(' identifier ')' |
| 66 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 67 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 68 | /// |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 69 | /// [GNU] attrib-name: |
| 70 | /// identifier |
| 71 | /// typespec |
| 72 | /// typequal |
| 73 | /// storageclass |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | /// |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 75 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 77 | /// which is followed by a comma or close parenthesis, then the arguments |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 78 | /// start with that identifier; otherwise they are an expression list." |
| 79 | /// |
| 80 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 81 | /// any attributes that don't work (based on my limited testing). Most |
| 82 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 83 | /// a pressing need to implement the 2 token lookahead. |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 84 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 85 | AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) { |
| 86 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 88 | AttributeList *CurrAttr = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 90 | while (Tok.is(tok::kw___attribute)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 91 | ConsumeToken(); |
| 92 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 93 | "attribute")) { |
| 94 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 95 | return CurrAttr; |
| 96 | } |
| 97 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 98 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 99 | return CurrAttr; |
| 100 | } |
| 101 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 102 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 103 | Tok.is(tok::comma)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
| 105 | if (Tok.is(tok::comma)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 106 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 107 | ConsumeToken(); |
| 108 | continue; |
| 109 | } |
| 110 | // we have an identifier or declaration specifier (const, int, etc.) |
| 111 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 112 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Douglas Gregor | a2f4945 | 2010-03-16 19:09:18 +0000 | [diff] [blame] | 114 | // check if we have a "parameterized" attribute |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 115 | if (Tok.is(tok::l_paren)) { |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 116 | ConsumeParen(); // ignore the left paren loc for now |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 118 | if (Tok.is(tok::identifier)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 119 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 120 | SourceLocation ParmLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
| 122 | if (Tok.is(tok::r_paren)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 123 | // __attribute__(( mode(byte) )) |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 124 | ConsumeParen(); // ignore the right paren loc for now |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 125 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 126 | ParmName, ParmLoc, 0, 0, CurrAttr); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 127 | } else if (Tok.is(tok::comma)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 128 | ConsumeToken(); |
| 129 | // __attribute__(( format(printf, 1, 2) )) |
Sebastian Redl | 511ed55 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 130 | ExprVector ArgExprs(Actions); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 131 | bool ArgExprsOk = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 133 | // now parse the non-empty comma separated list of expressions |
| 134 | while (1) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 135 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 136 | if (ArgExpr.isInvalid()) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 137 | ArgExprsOk = false; |
| 138 | SkipUntil(tok::r_paren); |
| 139 | break; |
| 140 | } else { |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 141 | ArgExprs.push_back(ArgExpr.release()); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 143 | if (Tok.isNot(tok::comma)) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 144 | break; |
| 145 | ConsumeToken(); // Eat the comma, move to the next argument |
| 146 | } |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 147 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 148 | ConsumeParen(); // ignore the right paren loc for now |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 149 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
| 150 | AttrNameLoc, ParmName, ParmLoc, |
| 151 | ArgExprs.take(), ArgExprs.size(), |
| 152 | CurrAttr); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } else { // not an identifier |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 156 | switch (Tok.getKind()) { |
| 157 | case tok::r_paren: |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 158 | // parse a possibly empty comma separated list of expressions |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 159 | // __attribute__(( nonnull() )) |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 160 | ConsumeParen(); // ignore the right paren loc for now |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 161 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 162 | 0, SourceLocation(), 0, 0, CurrAttr); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 163 | break; |
| 164 | case tok::kw_char: |
| 165 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 166 | case tok::kw_char16_t: |
| 167 | case tok::kw_char32_t: |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 168 | case tok::kw_bool: |
| 169 | case tok::kw_short: |
| 170 | case tok::kw_int: |
| 171 | case tok::kw_long: |
| 172 | case tok::kw_signed: |
| 173 | case tok::kw_unsigned: |
| 174 | case tok::kw_float: |
| 175 | case tok::kw_double: |
| 176 | case tok::kw_void: |
| 177 | case tok::kw_typeof: |
Fariborz Jahanian | 9d7d3d8 | 2010-08-17 23:19:16 +0000 | [diff] [blame] | 178 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 179 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 180 | if (CurrAttr->getKind() == AttributeList::AT_IBOutletCollection) |
| 181 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 182 | // If it's a builtin type name, eat it and expect a rparen |
| 183 | // __attribute__(( vec_type_hint(char) )) |
| 184 | ConsumeToken(); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 185 | if (Tok.is(tok::r_paren)) |
| 186 | ConsumeParen(); |
| 187 | break; |
| 188 | default: |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 189 | // __attribute__(( aligned(16) )) |
Sebastian Redl | 511ed55 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 190 | ExprVector ArgExprs(Actions); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 191 | bool ArgExprsOk = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 193 | // now parse the list of expressions |
| 194 | while (1) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 195 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 196 | if (ArgExpr.isInvalid()) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 197 | ArgExprsOk = false; |
| 198 | SkipUntil(tok::r_paren); |
| 199 | break; |
| 200 | } else { |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 201 | ArgExprs.push_back(ArgExpr.release()); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 202 | } |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 203 | if (Tok.isNot(tok::comma)) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 204 | break; |
| 205 | ConsumeToken(); // Eat the comma, move to the next argument |
| 206 | } |
| 207 | // Match the ')'. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 208 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 209 | ConsumeParen(); // ignore the right paren loc for now |
Sebastian Redl | 511ed55 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 210 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 211 | AttrNameLoc, 0, SourceLocation(), ArgExprs.take(), |
| 212 | ArgExprs.size(), |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 213 | CurrAttr); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 214 | } |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 215 | break; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } else { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 219 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 220 | 0, SourceLocation(), 0, 0, CurrAttr); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
Steve Naroff | 98d153c | 2007-06-06 23:19:11 +0000 | [diff] [blame] | 223 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Steve Naroff | 98d153c | 2007-06-06 23:19:11 +0000 | [diff] [blame] | 224 | SkipUntil(tok::r_paren, false); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 225 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 226 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 227 | SkipUntil(tok::r_paren, false); |
| 228 | } |
| 229 | if (EndLoc) |
| 230 | *EndLoc = Loc; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 231 | } |
| 232 | return CurrAttr; |
| 233 | } |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 234 | |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 235 | /// ParseMicrosoftDeclSpec - Parse an __declspec construct |
| 236 | /// |
| 237 | /// [MS] decl-specifier: |
| 238 | /// __declspec ( extended-decl-modifier-seq ) |
| 239 | /// |
| 240 | /// [MS] extended-decl-modifier-seq: |
| 241 | /// extended-decl-modifier[opt] |
| 242 | /// extended-decl-modifier extended-decl-modifier-seq |
| 243 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 244 | AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) { |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 245 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 246 | |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 247 | ConsumeToken(); |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 248 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 249 | "declspec")) { |
| 250 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 251 | return CurrAttr; |
| 252 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 253 | while (Tok.getIdentifierInfo()) { |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 254 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 255 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 256 | if (Tok.is(tok::l_paren)) { |
| 257 | ConsumeParen(); |
| 258 | // FIXME: This doesn't parse __declspec(property(get=get_func_name)) |
| 259 | // correctly. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 260 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 261 | if (!ArgExpr.isInvalid()) { |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 262 | Expr *ExprList = ArgExpr.take(); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 263 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 264 | SourceLocation(), &ExprList, 1, |
| 265 | CurrAttr, true); |
| 266 | } |
| 267 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 268 | SkipUntil(tok::r_paren, false); |
| 269 | } else { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 270 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 271 | 0, SourceLocation(), 0, 0, CurrAttr, true); |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 275 | SkipUntil(tok::r_paren, false); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 276 | return CurrAttr; |
| 277 | } |
| 278 | |
| 279 | AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) { |
| 280 | // Treat these like attributes |
| 281 | // FIXME: Allow Sema to distinguish between these and real attributes! |
| 282 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 283 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
| 284 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) { |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 285 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 286 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 287 | if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) |
| 288 | // FIXME: Support these properly! |
| 289 | continue; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 290 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 291 | SourceLocation(), 0, 0, CurrAttr, true); |
| 292 | } |
| 293 | return CurrAttr; |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 296 | AttributeList* Parser::ParseBorlandTypeAttributes(AttributeList *CurrAttr) { |
| 297 | // Treat these like attributes |
| 298 | while (Tok.is(tok::kw___pascal)) { |
| 299 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 300 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 301 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 302 | SourceLocation(), 0, 0, CurrAttr, true); |
| 303 | } |
| 304 | return CurrAttr; |
| 305 | } |
| 306 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 307 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 308 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 309 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 310 | /// location of the semicolon in DeclEnd. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 311 | /// |
| 312 | /// declaration: [C99 6.7] |
| 313 | /// block-declaration -> |
| 314 | /// simple-declaration |
| 315 | /// others [FIXME] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 316 | /// [C++] template-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 317 | /// [C++] namespace-definition |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 318 | /// [C++] using-directive |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 319 | /// [C++] using-declaration |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 320 | /// [C++0x] static_assert-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 321 | /// others... [FIXME] |
| 322 | /// |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame^] | 323 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 324 | unsigned Context, |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 325 | SourceLocation &DeclEnd, |
| 326 | CXX0XAttributeList Attr) { |
Argyrios Kyrtzidis | 355094e | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 327 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
| 328 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 329 | Decl *SingleDecl = 0; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 330 | switch (Tok.getKind()) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 331 | case tok::kw_template: |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 332 | case tok::kw_export: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 333 | if (Attr.HasAttr) |
| 334 | Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) |
| 335 | << Attr.Range; |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 336 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 337 | break; |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 338 | case tok::kw_inline: |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 339 | // Could be the start of an inline namespace. Allowed as an ext in C++03. |
| 340 | if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 341 | if (Attr.HasAttr) |
| 342 | Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) |
| 343 | << Attr.Range; |
| 344 | SourceLocation InlineLoc = ConsumeToken(); |
| 345 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 346 | break; |
| 347 | } |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame^] | 348 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, Attr.AttrList, |
| 349 | true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 350 | case tok::kw_namespace: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 351 | if (Attr.HasAttr) |
| 352 | Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) |
| 353 | << Attr.Range; |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 354 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 355 | break; |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 356 | case tok::kw_using: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 357 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 358 | break; |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 359 | case tok::kw_static_assert: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 360 | if (Attr.HasAttr) |
| 361 | Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) |
| 362 | << Attr.Range; |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 363 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 364 | break; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 365 | default: |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame^] | 366 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, Attr.AttrList, |
| 367 | true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 368 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 370 | // This routine returns a DeclGroup, if the thing we parsed only contains a |
| 371 | // single decl, convert it now. |
| 372 | return Actions.ConvertDeclToDeclGroup(SingleDecl); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 376 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 377 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 378 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 379 | /// |
| 380 | /// If RequireSemi is false, this does not check for a ';' at the end of the |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 381 | /// declaration. If it is true, it checks for and eats it. |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame^] | 382 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts, |
| 383 | unsigned Context, |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 384 | SourceLocation &DeclEnd, |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 385 | AttributeList *Attr, |
| 386 | bool RequireSemi) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 387 | // Parse the common declaration-specifiers piece. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 388 | ParsingDeclSpec DS(*this); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 389 | if (Attr) |
| 390 | DS.AddAttributes(Attr); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 391 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
| 392 | getDeclSpecContextFromDeclaratorContext(Context)); |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame^] | 393 | StmtResult R = Actions.ActOnVlaStmt(DS); |
| 394 | if (R.isUsable()) |
| 395 | Stmts.push_back(R.release()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 397 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 398 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 399 | if (Tok.is(tok::semi)) { |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 400 | if (RequireSemi) ConsumeToken(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 401 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 402 | DS); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 403 | DS.complete(TheDecl); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 404 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 405 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 407 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 408 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 410 | /// ParseDeclGroup - Having concluded that this is either a function |
| 411 | /// definition or a group of object declarations, actually parse the |
| 412 | /// result. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 413 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 414 | unsigned Context, |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 415 | bool AllowFunctionDefinitions, |
| 416 | SourceLocation *DeclEnd) { |
| 417 | // Parse the first declarator. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 418 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 419 | ParseDeclarator(D); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 420 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 421 | // Bail out if the first declarator didn't seem well-formed. |
| 422 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
| 423 | // Skip until ; or }. |
| 424 | SkipUntil(tok::r_brace, true, true); |
| 425 | if (Tok.is(tok::semi)) |
| 426 | ConsumeToken(); |
| 427 | return DeclGroupPtrTy(); |
Chris Lattner | efb0f11 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 428 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 430 | // Check to see if we have a function *definition* which must have a body. |
| 431 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 432 | // Look at the next token to make sure that this isn't a function |
| 433 | // declaration. We have to check this because __attribute__ might be the |
| 434 | // start of a function definition in GCC-extended K&R C. |
| 435 | !isDeclarationAfterDeclarator()) { |
| 436 | |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 437 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 438 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 439 | Diag(Tok, diag::err_function_declared_typedef); |
| 440 | |
| 441 | // Recover by treating the 'typedef' as spurious. |
| 442 | DS.ClearStorageClassSpecs(); |
| 443 | } |
| 444 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 445 | Decl *TheDecl = ParseFunctionDefinition(D); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 446 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | if (isDeclarationSpecifier()) { |
| 450 | // If there is an invalid declaration specifier right after the function |
| 451 | // prototype, then we must be in a missing semicolon case where this isn't |
| 452 | // actually a body. Just fall through into the code that handles it as a |
| 453 | // prototype, and let the top-level code handle the erroneous declspec |
| 454 | // where it would otherwise expect a comma or semicolon. |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 455 | } else { |
| 456 | Diag(Tok, diag::err_expected_fn_body); |
| 457 | SkipUntil(tok::semi); |
| 458 | return DeclGroupPtrTy(); |
| 459 | } |
| 460 | } |
| 461 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 462 | llvm::SmallVector<Decl *, 8> DeclsInGroup; |
| 463 | Decl *FirstDecl = ParseDeclarationAfterDeclarator(D); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 464 | D.complete(FirstDecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 465 | if (FirstDecl) |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 466 | DeclsInGroup.push_back(FirstDecl); |
| 467 | |
| 468 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 469 | // error, bail out. |
| 470 | while (Tok.is(tok::comma)) { |
| 471 | // Consume the comma. |
Chris Lattner | efb0f11 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 472 | ConsumeToken(); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 473 | |
| 474 | // Parse the next declarator. |
| 475 | D.clear(); |
| 476 | |
| 477 | // Accept attributes in an init-declarator. In the first declarator in a |
| 478 | // declaration, these would be part of the declspec. In subsequent |
| 479 | // declarators, they become part of the declarator itself, so that they |
| 480 | // don't apply to declarators after *this* one. Examples: |
| 481 | // short __attribute__((common)) var; -> declspec |
| 482 | // short var __attribute__((common)); -> declarator |
| 483 | // short x, __attribute__((common)) var; -> declarator |
| 484 | if (Tok.is(tok::kw___attribute)) { |
| 485 | SourceLocation Loc; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 486 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 487 | D.AddAttributes(AttrList, Loc); |
| 488 | } |
| 489 | |
| 490 | ParseDeclarator(D); |
| 491 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 492 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 493 | D.complete(ThisDecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 494 | if (ThisDecl) |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 495 | DeclsInGroup.push_back(ThisDecl); |
| 496 | } |
| 497 | |
| 498 | if (DeclEnd) |
| 499 | *DeclEnd = Tok.getLocation(); |
| 500 | |
| 501 | if (Context != Declarator::ForContext && |
| 502 | ExpectAndConsume(tok::semi, |
| 503 | Context == Declarator::FileContext |
| 504 | ? diag::err_invalid_token_after_toplevel_declarator |
| 505 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 506 | // Okay, there was no semicolon and one was expected. If we see a |
| 507 | // declaration specifier, just assume it was missing and continue parsing. |
| 508 | // Otherwise things are very confused and we skip to recover. |
| 509 | if (!isDeclarationSpecifier()) { |
| 510 | SkipUntil(tok::r_brace, true, true); |
| 511 | if (Tok.is(tok::semi)) |
| 512 | ConsumeToken(); |
| 513 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 516 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 517 | DeclsInGroup.data(), |
| 518 | DeclsInGroup.size()); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 521 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 522 | /// declarator'. This method parses the remainder of the declaration |
| 523 | /// (including any attributes or initializer, among other things) and |
| 524 | /// finalizes the declaration. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 525 | /// |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 526 | /// init-declarator: [C99 6.7] |
| 527 | /// declarator |
| 528 | /// declarator '=' initializer |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 529 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 530 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 531 | /// [C++] declarator initializer[opt] |
| 532 | /// |
| 533 | /// [C++] initializer: |
| 534 | /// [C++] '=' initializer-clause |
| 535 | /// [C++] '(' expression-list ')' |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 536 | /// [C++0x] '=' 'default' [TODO] |
| 537 | /// [C++0x] '=' 'delete' |
| 538 | /// |
| 539 | /// According to the standard grammar, =default and =delete are function |
| 540 | /// definitions, but that definitely doesn't fit with the parser here. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 541 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 542 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 543 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 544 | // If a simple-asm-expr is present, parse it. |
| 545 | if (Tok.is(tok::kw_asm)) { |
| 546 | SourceLocation Loc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 547 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 548 | if (AsmLabel.isInvalid()) { |
| 549 | SkipUntil(tok::semi, true, true); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 550 | return 0; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 551 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 553 | D.setAsmLabel(AsmLabel.release()); |
| 554 | D.SetRangeEnd(Loc); |
| 555 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 557 | // If attributes are present, parse them. |
| 558 | if (Tok.is(tok::kw___attribute)) { |
| 559 | SourceLocation Loc; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 560 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 561 | D.AddAttributes(AttrList, Loc); |
| 562 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 564 | // Inform the current actions module that we just parsed this declarator. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 565 | Decl *ThisDecl = 0; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 566 | switch (TemplateInfo.Kind) { |
| 567 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 568 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 569 | break; |
| 570 | |
| 571 | case ParsedTemplateInfo::Template: |
| 572 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 573 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 574 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 575 | TemplateInfo.TemplateParams->data(), |
| 576 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 577 | D); |
| 578 | break; |
| 579 | |
| 580 | case ParsedTemplateInfo::ExplicitInstantiation: { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 581 | DeclResult ThisRes |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 582 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 583 | TemplateInfo.ExternLoc, |
| 584 | TemplateInfo.TemplateLoc, |
| 585 | D); |
| 586 | if (ThisRes.isInvalid()) { |
| 587 | SkipUntil(tok::semi, true, true); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 588 | return 0; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | ThisDecl = ThisRes.get(); |
| 592 | break; |
| 593 | } |
| 594 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 596 | // Parse declarator '=' initializer. |
| 597 | if (Tok.is(tok::equal)) { |
| 598 | ConsumeToken(); |
Anders Carlsson | 991285e | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 599 | if (Tok.is(tok::kw_delete)) { |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 600 | SourceLocation DelLoc = ConsumeToken(); |
Anders Carlsson | 991285e | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 601 | |
| 602 | if (!getLang().CPlusPlus0x) |
| 603 | Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension); |
| 604 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 605 | Actions.SetDeclDeleted(ThisDecl, DelLoc); |
| 606 | } else { |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 607 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
| 608 | EnterScope(0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 609 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 610 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 611 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 612 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 613 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 614 | ConsumeCodeCompletionToken(); |
| 615 | SkipUntil(tok::comma, true, true); |
| 616 | return ThisDecl; |
| 617 | } |
| 618 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 619 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 620 | |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 621 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 622 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 623 | ExitScope(); |
| 624 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 625 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 626 | if (Init.isInvalid()) { |
Douglas Gregor | 604c302 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 627 | SkipUntil(tok::comma, true, true); |
| 628 | Actions.ActOnInitializerError(ThisDecl); |
| 629 | } else |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 630 | Actions.AddInitializerToDecl(ThisDecl, Init.take()); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 631 | } |
| 632 | } else if (Tok.is(tok::l_paren)) { |
| 633 | // Parse C++ direct initializer: '(' expression-list ')' |
| 634 | SourceLocation LParenLoc = ConsumeParen(); |
| 635 | ExprVector Exprs(Actions); |
| 636 | CommaLocsTy CommaLocs; |
| 637 | |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 638 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
| 639 | EnterScope(0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 640 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 643 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 644 | SkipUntil(tok::r_paren); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 645 | |
| 646 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 647 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 648 | ExitScope(); |
| 649 | } |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 650 | } else { |
| 651 | // Match the ')'. |
| 652 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 653 | |
| 654 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 655 | "Unexpected number of commas!"); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 656 | |
| 657 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 658 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 659 | ExitScope(); |
| 660 | } |
| 661 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 662 | Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc, |
| 663 | move_arg(Exprs), |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 664 | RParenLoc); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 665 | } |
| 666 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | bool TypeContainsUndeducedAuto = |
Anders Carlsson | ae01993 | 2009-07-11 00:34:39 +0000 | [diff] [blame] | 668 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 669 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | return ThisDecl; |
| 673 | } |
| 674 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 675 | /// ParseSpecifierQualifierList |
| 676 | /// specifier-qualifier-list: |
| 677 | /// type-specifier specifier-qualifier-list[opt] |
| 678 | /// type-qualifier specifier-qualifier-list[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 679 | /// [GNU] attributes specifier-qualifier-list[opt] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 680 | /// |
| 681 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 682 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 683 | /// parse declaration-specifiers and complain about extra stuff. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 684 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 686 | // Validate declspec for type-name. |
| 687 | unsigned Specs = DS.getParsedSpecifiers(); |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 688 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 689 | !DS.getAttributes()) |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 690 | Diag(Tok, diag::err_typename_requires_specqual); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 692 | // Issue diagnostic and remove storage class if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 693 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 694 | if (DS.getStorageClassSpecLoc().isValid()) |
| 695 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 696 | else |
| 697 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 698 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 699 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 701 | // Issue diagnostic and remove function specfier if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 702 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 703 | if (DS.isInlineSpecified()) |
| 704 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 705 | if (DS.isVirtualSpecified()) |
| 706 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 707 | if (DS.isExplicitSpecified()) |
| 708 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 709 | DS.ClearFunctionSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 712 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 713 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 714 | /// specified token is valid after the identifier in a declarator which |
| 715 | /// immediately follows the declspec. For example, these things are valid: |
| 716 | /// |
| 717 | /// int x [ 4]; // direct-declarator |
| 718 | /// int x ( int y); // direct-declarator |
| 719 | /// int(int x ) // direct-declarator |
| 720 | /// int x ; // simple-declaration |
| 721 | /// int x = 17; // init-declarator-list |
| 722 | /// int x , y; // init-declarator-list |
| 723 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 724 | /// int x : 4; // struct-declarator |
Chris Lattner | 2b988c1 | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 725 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 726 | /// |
| 727 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 728 | /// ')' happens to be valid anyway). |
| 729 | /// int (x) |
| 730 | /// |
| 731 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 732 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 733 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 734 | T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon); |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 737 | |
| 738 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 739 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 740 | /// the declspec has no type specifier. In this case, the declspec is either |
| 741 | /// malformed or is "implicit int" (in K&R and C89). |
| 742 | /// |
| 743 | /// This method handles diagnosing this prettily and returns false if the |
| 744 | /// declspec is done being processed. If it recovers and thinks there may be |
| 745 | /// other pieces of declspec after it, it returns true. |
| 746 | /// |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 747 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 748 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 749 | AccessSpecifier AS) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 750 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 751 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 752 | SourceLocation Loc = Tok.getLocation(); |
| 753 | // If we see an identifier that is not a type name, we normally would |
| 754 | // parse it as the identifer being declared. However, when a typename |
| 755 | // is typo'd or the definition is not included, this will incorrectly |
| 756 | // parse the typename as the identifier name and fall over misparsing |
| 757 | // later parts of the diagnostic. |
| 758 | // |
| 759 | // As such, we try to do some look-ahead in cases where this would |
| 760 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 761 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 762 | // an identifier with implicit int, we'd get a parse error because the |
| 763 | // next token is obviously invalid for a type. Parse these as a case |
| 764 | // with an invalid type specifier. |
| 765 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 767 | // Since we know that this either implicit int (which is rare) or an |
| 768 | // error, we'd do lookahead to try to do better recovery. |
| 769 | if (isValidAfterIdentifierInDeclarator(NextToken())) { |
| 770 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 771 | // we just avoid eating the identifier, so it will be parsed as the |
| 772 | // identifier in the declarator. |
| 773 | return false; |
| 774 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 776 | // Otherwise, if we don't consume this token, we are going to emit an |
| 777 | // error anyway. Try to recover from various common problems. Check |
| 778 | // to see if this was a reference to a tag name without a tag specified. |
| 779 | // This is a common problem in C (saying 'foo' instead of 'struct foo'). |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 780 | // |
| 781 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 782 | if (SS == 0) { |
| 783 | const char *TagName = 0; |
| 784 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 786 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 787 | default: break; |
| 788 | case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break; |
| 789 | case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break; |
| 790 | case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break; |
| 791 | case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break; |
| 792 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 794 | if (TagName) { |
| 795 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
John McCall | 38200b0 | 2010-02-14 01:03:10 +0000 | [diff] [blame] | 796 | << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 797 | << FixItHint::CreateInsertion(Tok.getLocation(),TagName); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 798 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 799 | // Parse this as a tag as if the missing tag were present. |
| 800 | if (TagKind == tok::kw_enum) |
Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 801 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 802 | else |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 803 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 804 | return true; |
| 805 | } |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 806 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 808 | // This is almost certainly an invalid type name. Let the action emit a |
| 809 | // diagnostic and attempt to recover. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 810 | ParsedType T; |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 811 | if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc, |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 812 | getCurScope(), SS, T)) { |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 813 | // The action emitted a diagnostic, so we don't have to. |
| 814 | if (T) { |
| 815 | // The action has suggested that the type T could be used. Set that as |
| 816 | // the type in the declaration specifiers, consume the would-be type |
| 817 | // name token, and we're done. |
| 818 | const char *PrevSpec; |
| 819 | unsigned DiagID; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 820 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 821 | DS.SetRangeEnd(Tok.getLocation()); |
| 822 | ConsumeToken(); |
| 823 | |
| 824 | // There may be other declaration specifiers after this. |
| 825 | return true; |
| 826 | } |
| 827 | |
| 828 | // Fall through; the action had no suggestion for us. |
| 829 | } else { |
| 830 | // The action did not emit a diagnostic, so emit one now. |
| 831 | SourceRange R; |
| 832 | if (SS) R = SS->getRange(); |
| 833 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 834 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 836 | // Mark this as an error. |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 837 | const char *PrevSpec; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 838 | unsigned DiagID; |
| 839 | DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID); |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 840 | DS.SetRangeEnd(Tok.getLocation()); |
| 841 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 843 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 844 | // avoid rippling error messages on subsequent uses of the same type, |
| 845 | // could be useful if #include was forgotten. |
| 846 | return false; |
| 847 | } |
| 848 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 849 | /// \brief Determine the declaration specifier context from the declarator |
| 850 | /// context. |
| 851 | /// |
| 852 | /// \param Context the declarator context, which is one of the |
| 853 | /// Declarator::TheContext enumerator values. |
| 854 | Parser::DeclSpecContext |
| 855 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 856 | if (Context == Declarator::MemberContext) |
| 857 | return DSC_class; |
| 858 | if (Context == Declarator::FileContext) |
| 859 | return DSC_top_level; |
| 860 | return DSC_normal; |
| 861 | } |
| 862 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 863 | /// ParseDeclarationSpecifiers |
| 864 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 865 | /// storage-class-specifier declaration-specifiers[opt] |
| 866 | /// type-specifier declaration-specifiers[opt] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 867 | /// [C99] function-specifier declaration-specifiers[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 868 | /// [GNU] attributes declaration-specifiers[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 869 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 870 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 871 | /// 'typedef' |
| 872 | /// 'extern' |
| 873 | /// 'static' |
| 874 | /// 'auto' |
| 875 | /// 'register' |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 876 | /// [C++] 'mutable' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 877 | /// [GNU] '__thread' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 878 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 879 | /// [C99] 'inline' |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 880 | /// [C++] 'virtual' |
| 881 | /// [C++] 'explicit' |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 882 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 883 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 884 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 885 | /// |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 886 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 887 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 888 | AccessSpecifier AS, |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 889 | DeclSpecContext DSContext) { |
Chris Lattner | 2e23209 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 890 | DS.SetRangeStart(Tok.getLocation()); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 891 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 892 | bool isInvalid = false; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 893 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 894 | unsigned DiagID = 0; |
| 895 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 896 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 897 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 898 | switch (Tok.getKind()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | default: |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 900 | DoneWithDeclSpec: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 901 | // If this is not a declaration specifier token, we're done reading decl |
| 902 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 903 | DS.Finish(Diags, PP); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 904 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 906 | case tok::code_completion: { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 907 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 908 | if (DS.hasTypeSpecifier()) { |
| 909 | bool AllowNonIdentifiers |
| 910 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 911 | Scope::BlockScope | |
| 912 | Scope::TemplateParamScope | |
| 913 | Scope::FunctionPrototypeScope | |
| 914 | Scope::AtCatchScope)) == 0; |
| 915 | bool AllowNestedNameSpecifiers |
| 916 | = DSContext == DSC_top_level || |
| 917 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 918 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 919 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
| 920 | AllowNonIdentifiers, |
| 921 | AllowNestedNameSpecifiers); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 922 | ConsumeCodeCompletionToken(); |
| 923 | return; |
| 924 | } |
| 925 | |
| 926 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 927 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
| 928 | : Sema::PCC_Template; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 929 | else if (DSContext == DSC_class) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 930 | CCC = Sema::PCC_Class; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 931 | else if (ObjCImpDecl) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 932 | CCC = Sema::PCC_ObjCImplementation; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 933 | |
| 934 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
| 935 | ConsumeCodeCompletionToken(); |
| 936 | return; |
| 937 | } |
| 938 | |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 939 | case tok::coloncolon: // ::foo::bar |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 940 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 941 | if (TryAnnotateCXXScopeToken(true)) { |
| 942 | if (!DS.hasTypeSpecifier()) |
| 943 | DS.SetTypeSpecError(); |
| 944 | goto DoneWithDeclSpec; |
| 945 | } |
John McCall | 8bc2a70 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 946 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 947 | goto DoneWithDeclSpec; |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 948 | continue; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 949 | |
| 950 | case tok::annot_cxxscope: { |
| 951 | if (DS.hasTypeSpecifier()) |
| 952 | goto DoneWithDeclSpec; |
| 953 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 954 | CXXScopeSpec SS; |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 955 | SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue()); |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 956 | SS.setRange(Tok.getAnnotationRange()); |
| 957 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 958 | // We are looking for a qualified typename. |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 959 | Token Next = NextToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 960 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 961 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 962 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 963 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 964 | |
| 965 | // C++ [class.qual]p2: |
| 966 | // In a lookup in which the constructor is an acceptable lookup |
| 967 | // result and the nested-name-specifier nominates a class C: |
| 968 | // |
| 969 | // - if the name specified after the |
| 970 | // nested-name-specifier, when looked up in C, is the |
| 971 | // injected-class-name of C (Clause 9), or |
| 972 | // |
| 973 | // - if the name specified after the nested-name-specifier |
| 974 | // is the same as the identifier or the |
| 975 | // simple-template-id's template-name in the last |
| 976 | // component of the nested-name-specifier, |
| 977 | // |
| 978 | // the name is instead considered to name the constructor of |
| 979 | // class C. |
| 980 | // |
| 981 | // Thus, if the template-name is actually the constructor |
| 982 | // name, then the code is ill-formed; this interpretation is |
| 983 | // reinforced by the NAD status of core issue 635. |
| 984 | TemplateIdAnnotation *TemplateId |
| 985 | = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()); |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 986 | if ((DSContext == DSC_top_level || |
| 987 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 988 | TemplateId->Name && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 989 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 990 | if (isConstructorDeclarator()) { |
| 991 | // The user meant this to be an out-of-line constructor |
| 992 | // definition, but template arguments are not allowed |
| 993 | // there. Just allow this as a constructor; we'll |
| 994 | // complain about it later. |
| 995 | goto DoneWithDeclSpec; |
| 996 | } |
| 997 | |
| 998 | // The user meant this to name a type, but it actually names |
| 999 | // a constructor with some extraneous template |
| 1000 | // arguments. Complain, then parse it as a type as the user |
| 1001 | // intended. |
| 1002 | Diag(TemplateId->TemplateNameLoc, |
| 1003 | diag::err_out_of_line_template_id_names_constructor) |
| 1004 | << TemplateId->Name; |
| 1005 | } |
| 1006 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1007 | DS.getTypeSpecScope() = SS; |
| 1008 | ConsumeToken(); // The C++ scope. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1010 | "ParseOptionalCXXScopeSpecifier not working"); |
| 1011 | AnnotateTemplateIdTokenAsType(&SS); |
| 1012 | continue; |
| 1013 | } |
| 1014 | |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1015 | if (Next.is(tok::annot_typename)) { |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1016 | DS.getTypeSpecScope() = SS; |
| 1017 | ConsumeToken(); // The C++ scope. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1018 | if (Tok.getAnnotationValue()) { |
| 1019 | ParsedType T = getTypeAnnotation(Tok); |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1020 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1021 | PrevSpec, DiagID, T); |
| 1022 | } |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1023 | else |
| 1024 | DS.SetTypeSpecError(); |
| 1025 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1026 | ConsumeToken(); // The typename |
| 1027 | } |
| 1028 | |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1029 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1030 | goto DoneWithDeclSpec; |
| 1031 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1032 | // If we're in a context where the identifier could be a class name, |
| 1033 | // check whether this is a constructor declaration. |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1034 | if ((DSContext == DSC_top_level || |
| 1035 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1036 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1037 | &SS)) { |
| 1038 | if (isConstructorDeclarator()) |
| 1039 | goto DoneWithDeclSpec; |
| 1040 | |
| 1041 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 1042 | // of the class is qualified in a context where it could name |
| 1043 | // a constructor, its a constructor name. However, we've |
| 1044 | // looked at the declarator, and the user probably meant this |
| 1045 | // to be a type. Complain that it isn't supposed to be treated |
| 1046 | // as a type, then proceed to parse it as a type. |
| 1047 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 1048 | << Next.getIdentifierInfo(); |
| 1049 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1050 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1051 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 1052 | Next.getLocation(), |
| 1053 | getCurScope(), &SS); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1055 | // If the referenced identifier is not a type, then this declspec is |
| 1056 | // erroneous: We already checked about that it has no type specifier, and |
| 1057 | // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | // typename. |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1059 | if (TypeRep == 0) { |
| 1060 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1061 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1062 | goto DoneWithDeclSpec; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1063 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1065 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1066 | ConsumeToken(); // The C++ scope. |
| 1067 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1068 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1069 | DiagID, TypeRep); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1070 | if (isInvalid) |
| 1071 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1073 | DS.SetRangeEnd(Tok.getLocation()); |
| 1074 | ConsumeToken(); // The typename. |
| 1075 | |
| 1076 | continue; |
| 1077 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1079 | case tok::annot_typename: { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1080 | if (Tok.getAnnotationValue()) { |
| 1081 | ParsedType T = getTypeAnnotation(Tok); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1082 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1083 | DiagID, T); |
| 1084 | } else |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1085 | DS.SetTypeSpecError(); |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1086 | |
| 1087 | if (isInvalid) |
| 1088 | break; |
| 1089 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1090 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1091 | ConsumeToken(); // The typename |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1093 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1094 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 1095 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 1096 | // just a normal reference to a typedef name. |
| 1097 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 1098 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | |
Argyrios Kyrtzidis | fc1f9e4 | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1100 | SourceLocation LAngleLoc, EndProtoLoc; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1101 | llvm::SmallVector<Decl *, 8> ProtocolDecl; |
Argyrios Kyrtzidis | fc1f9e4 | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1102 | llvm::SmallVector<SourceLocation, 8> ProtocolLocs; |
| 1103 | ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false, |
| 1104 | LAngleLoc, EndProtoLoc); |
| 1105 | DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(), |
| 1106 | ProtocolLocs.data(), LAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1108 | DS.SetRangeEnd(EndProtoLoc); |
| 1109 | continue; |
| 1110 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1112 | // typedef-name |
| 1113 | case tok::identifier: { |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1114 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 1115 | // so handle it as such. This is important for ctor parsing. |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1116 | if (getLang().CPlusPlus) { |
| 1117 | if (TryAnnotateCXXScopeToken(true)) { |
| 1118 | if (!DS.hasTypeSpecifier()) |
| 1119 | DS.SetTypeSpecError(); |
| 1120 | goto DoneWithDeclSpec; |
| 1121 | } |
| 1122 | if (!Tok.is(tok::identifier)) |
| 1123 | continue; |
| 1124 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1126 | // This identifier can only be a typedef name if we haven't already seen |
| 1127 | // a type-specifier. Without this check we misparse: |
| 1128 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 1129 | if (DS.hasTypeSpecifier()) |
| 1130 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1132 | // Check for need to substitute AltiVec keyword tokens. |
| 1133 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 1134 | break; |
| 1135 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1136 | // It has to be available as a typedef too! |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1137 | ParsedType TypeRep = |
| 1138 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 1139 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1140 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1141 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 1142 | // it must be an implicit int or an error. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1143 | if (!TypeRep) { |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1144 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue; |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1145 | goto DoneWithDeclSpec; |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1146 | } |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1147 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1148 | // If we're in a context where the identifier could be a class name, |
| 1149 | // check whether this is a constructor declaration. |
| 1150 | if (getLang().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1151 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1152 | isConstructorDeclarator()) |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1153 | goto DoneWithDeclSpec; |
| 1154 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1155 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1156 | DiagID, TypeRep); |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1157 | if (isInvalid) |
| 1158 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1160 | DS.SetRangeEnd(Tok.getLocation()); |
| 1161 | ConsumeToken(); // The identifier |
| 1162 | |
| 1163 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1164 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 1165 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 1166 | // just a normal reference to a typedef name. |
| 1167 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 1168 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | |
Argyrios Kyrtzidis | fc1f9e4 | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1170 | SourceLocation LAngleLoc, EndProtoLoc; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1171 | llvm::SmallVector<Decl *, 8> ProtocolDecl; |
Argyrios Kyrtzidis | fc1f9e4 | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1172 | llvm::SmallVector<SourceLocation, 8> ProtocolLocs; |
| 1173 | ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false, |
| 1174 | LAngleLoc, EndProtoLoc); |
| 1175 | DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(), |
| 1176 | ProtocolLocs.data(), LAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1178 | DS.SetRangeEnd(EndProtoLoc); |
| 1179 | |
Steve Naroff | cd5e782 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 1180 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 1181 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 1182 | continue; |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1183 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1184 | |
| 1185 | // type-name |
| 1186 | case tok::annot_template_id: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1187 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1188 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1189 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1190 | // This template-id does not refer to a type name, so we're |
| 1191 | // done with the type-specifiers. |
| 1192 | goto DoneWithDeclSpec; |
| 1193 | } |
| 1194 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1195 | // If we're in a context where the template-id could be a |
| 1196 | // constructor name or specialization, check whether this is a |
| 1197 | // constructor declaration. |
| 1198 | if (getLang().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1199 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1200 | isConstructorDeclarator()) |
| 1201 | goto DoneWithDeclSpec; |
| 1202 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1203 | // Turn the template-id annotation token into a type annotation |
| 1204 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1205 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1206 | continue; |
| 1207 | } |
| 1208 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1209 | // GNU attributes support. |
| 1210 | case tok::kw___attribute: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1211 | DS.AddAttributes(ParseGNUAttributes()); |
Chris Lattner | b95cca0 | 2006-10-17 03:01:08 +0000 | [diff] [blame] | 1212 | continue; |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 1213 | |
| 1214 | // Microsoft declspec support. |
| 1215 | case tok::kw___declspec: |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 1216 | DS.AddAttributes(ParseMicrosoftDeclSpec()); |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 1217 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1219 | // Microsoft single token adornments. |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 1220 | case tok::kw___forceinline: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1221 | // FIXME: Add handling here! |
| 1222 | break; |
| 1223 | |
| 1224 | case tok::kw___ptr64: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 1225 | case tok::kw___w64: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1226 | case tok::kw___cdecl: |
| 1227 | case tok::kw___stdcall: |
| 1228 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 1229 | case tok::kw___thiscall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1230 | DS.AddAttributes(ParseMicrosoftTypeAttributes()); |
| 1231 | continue; |
| 1232 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1233 | // Borland single token adornments. |
| 1234 | case tok::kw___pascal: |
| 1235 | DS.AddAttributes(ParseBorlandTypeAttributes()); |
| 1236 | continue; |
| 1237 | |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 1238 | // storage-class-specifier |
| 1239 | case tok::kw_typedef: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1240 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec, |
| 1241 | DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 1242 | break; |
| 1243 | case tok::kw_extern: |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 1244 | if (DS.isThreadSpecified()) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1245 | Diag(Tok, diag::ext_thread_before) << "extern"; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1246 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec, |
| 1247 | DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 1248 | break; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1249 | case tok::kw___private_extern__: |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1250 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1251 | PrevSpec, DiagID); |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1252 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 1253 | case tok::kw_static: |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 1254 | if (DS.isThreadSpecified()) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1255 | Diag(Tok, diag::ext_thread_before) << "static"; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1256 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec, |
| 1257 | DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 1258 | break; |
| 1259 | case tok::kw_auto: |
Anders Carlsson | 082acde | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 1260 | if (getLang().CPlusPlus0x) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1261 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 1262 | DiagID); |
Anders Carlsson | 082acde | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 1263 | else |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1264 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec, |
| 1265 | DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 1266 | break; |
| 1267 | case tok::kw_register: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1268 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec, |
| 1269 | DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 1270 | break; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1271 | case tok::kw_mutable: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1272 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec, |
| 1273 | DiagID); |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1274 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 1275 | case tok::kw___thread: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1276 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 1277 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 1279 | // function-specifier |
| 1280 | case tok::kw_inline: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1281 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 1282 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1283 | case tok::kw_virtual: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1284 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1285 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1286 | case tok::kw_explicit: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1287 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1288 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1289 | |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1290 | // friend |
| 1291 | case tok::kw_friend: |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1292 | if (DSContext == DSC_class) |
| 1293 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 1294 | else { |
| 1295 | PrevSpec = ""; // not actually used by the diagnostic |
| 1296 | DiagID = diag::err_friend_invalid_in_context; |
| 1297 | isInvalid = true; |
| 1298 | } |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1299 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1301 | // constexpr |
| 1302 | case tok::kw_constexpr: |
| 1303 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 1304 | break; |
| 1305 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1306 | // type-specifier |
| 1307 | case tok::kw_short: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1308 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 1309 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1310 | break; |
| 1311 | case tok::kw_long: |
| 1312 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1313 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 1314 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1315 | else |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1316 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 1317 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1318 | break; |
| 1319 | case tok::kw_signed: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1320 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 1321 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1322 | break; |
| 1323 | case tok::kw_unsigned: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1324 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 1325 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1326 | break; |
| 1327 | case tok::kw__Complex: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1328 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 1329 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1330 | break; |
| 1331 | case tok::kw__Imaginary: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1332 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 1333 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1334 | break; |
| 1335 | case tok::kw_void: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1336 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 1337 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1338 | break; |
| 1339 | case tok::kw_char: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1340 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 1341 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1342 | break; |
| 1343 | case tok::kw_int: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1344 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 1345 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1346 | break; |
| 1347 | case tok::kw_float: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1348 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 1349 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1350 | break; |
| 1351 | case tok::kw_double: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1352 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 1353 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1354 | break; |
| 1355 | case tok::kw_wchar_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1356 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 1357 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1358 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1359 | case tok::kw_char16_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1360 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 1361 | DiagID); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1362 | break; |
| 1363 | case tok::kw_char32_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1364 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 1365 | DiagID); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1366 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1367 | case tok::kw_bool: |
| 1368 | case tok::kw__Bool: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1369 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 1370 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1371 | break; |
| 1372 | case tok::kw__Decimal32: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1373 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 1374 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1375 | break; |
| 1376 | case tok::kw__Decimal64: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1377 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 1378 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1379 | break; |
| 1380 | case tok::kw__Decimal128: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1381 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 1382 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1383 | break; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1384 | case tok::kw___vector: |
| 1385 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 1386 | break; |
| 1387 | case tok::kw___pixel: |
| 1388 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 1389 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1390 | |
| 1391 | // class-specifier: |
| 1392 | case tok::kw_class: |
| 1393 | case tok::kw_struct: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1394 | case tok::kw_union: { |
| 1395 | tok::TokenKind Kind = Tok.getKind(); |
| 1396 | ConsumeToken(); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1397 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1398 | continue; |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1399 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1400 | |
| 1401 | // enum-specifier: |
| 1402 | case tok::kw_enum: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1403 | ConsumeToken(); |
Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 1404 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1405 | continue; |
| 1406 | |
| 1407 | // cv-qualifier: |
| 1408 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1409 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
| 1410 | getLang()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1411 | break; |
| 1412 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1413 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
| 1414 | getLang()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1415 | break; |
| 1416 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1417 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
| 1418 | getLang()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1419 | break; |
| 1420 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1421 | // C++ typename-specifier: |
| 1422 | case tok::kw_typename: |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1423 | if (TryAnnotateTypeOrScopeToken()) { |
| 1424 | DS.SetTypeSpecError(); |
| 1425 | goto DoneWithDeclSpec; |
| 1426 | } |
| 1427 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1428 | continue; |
| 1429 | break; |
| 1430 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1431 | // GNU typeof support. |
| 1432 | case tok::kw_typeof: |
| 1433 | ParseTypeofSpecifier(DS); |
| 1434 | continue; |
| 1435 | |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1436 | case tok::kw_decltype: |
| 1437 | ParseDecltypeSpecifier(DS); |
| 1438 | continue; |
| 1439 | |
Steve Naroff | cfdf616 | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 1440 | case tok::less: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1441 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1442 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 1443 | // but we support it. |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1444 | if (DS.hasTypeSpecifier() || !getLang().ObjC1) |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1445 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1447 | { |
Argyrios Kyrtzidis | fc1f9e4 | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1448 | SourceLocation LAngleLoc, EndProtoLoc; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1449 | llvm::SmallVector<Decl *, 8> ProtocolDecl; |
Argyrios Kyrtzidis | fc1f9e4 | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1450 | llvm::SmallVector<SourceLocation, 8> ProtocolLocs; |
| 1451 | ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false, |
| 1452 | LAngleLoc, EndProtoLoc); |
| 1453 | DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(), |
| 1454 | ProtocolLocs.data(), LAngleLoc); |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1455 | DS.SetRangeEnd(EndProtoLoc); |
| 1456 | |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1457 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1458 | << FixItHint::CreateInsertion(Loc, "id") |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1459 | << SourceRange(Loc, EndProtoLoc); |
Steve Naroff | cd5e782 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 1460 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 1461 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 1462 | continue; |
Steve Naroff | cfdf616 | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 1463 | } |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1464 | } |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1465 | // If the specifier wasn't legal, issue a diagnostic. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 1466 | if (isInvalid) { |
| 1467 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1468 | assert(DiagID); |
Douglas Gregor | a05f5ab | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 1469 | |
| 1470 | if (DiagID == diag::ext_duplicate_declspec) |
| 1471 | Diag(Tok, DiagID) |
| 1472 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1473 | else |
| 1474 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 1475 | } |
Chris Lattner | 2e23209 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 1476 | DS.SetRangeEnd(Tok.getLocation()); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 1477 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1478 | } |
| 1479 | } |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1480 | |
Chris Lattner | a448d75 | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 1481 | /// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1482 | /// primarily follow the C++ grammar with additions for C99 and GNU, |
| 1483 | /// which together subsume the C grammar. Note that the C++ |
| 1484 | /// type-specifier also includes the C type-qualifier (for const, |
| 1485 | /// volatile, and C99 restrict). Returns true if a type-specifier was |
| 1486 | /// found (and parsed), false otherwise. |
| 1487 | /// |
| 1488 | /// type-specifier: [C++ 7.1.5] |
| 1489 | /// simple-type-specifier |
| 1490 | /// class-specifier |
| 1491 | /// enum-specifier |
| 1492 | /// elaborated-type-specifier [TODO] |
| 1493 | /// cv-qualifier |
| 1494 | /// |
| 1495 | /// cv-qualifier: [C++ 7.1.5.1] |
| 1496 | /// 'const' |
| 1497 | /// 'volatile' |
| 1498 | /// [C99] 'restrict' |
| 1499 | /// |
| 1500 | /// simple-type-specifier: [ C++ 7.1.5.2] |
| 1501 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 1502 | /// '::'[opt] nested-name-specifier 'template' template-id [TODO] |
| 1503 | /// 'char' |
| 1504 | /// 'wchar_t' |
| 1505 | /// 'bool' |
| 1506 | /// 'short' |
| 1507 | /// 'int' |
| 1508 | /// 'long' |
| 1509 | /// 'signed' |
| 1510 | /// 'unsigned' |
| 1511 | /// 'float' |
| 1512 | /// 'double' |
| 1513 | /// 'void' |
| 1514 | /// [C99] '_Bool' |
| 1515 | /// [C99] '_Complex' |
| 1516 | /// [C99] '_Imaginary' // Removed in TC2? |
| 1517 | /// [GNU] '_Decimal32' |
| 1518 | /// [GNU] '_Decimal64' |
| 1519 | /// [GNU] '_Decimal128' |
| 1520 | /// [GNU] typeof-specifier |
| 1521 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
| 1522 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1523 | /// [C++0x] 'decltype' ( expression ) |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1524 | /// [AltiVec] '__vector' |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1525 | bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid, |
Chris Lattner | a448d75 | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 1526 | const char *&PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1527 | unsigned &DiagID, |
Sebastian Redl | 2b37272 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 1528 | const ParsedTemplateInfo &TemplateInfo, |
| 1529 | bool SuppressDeclarations) { |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1530 | SourceLocation Loc = Tok.getLocation(); |
| 1531 | |
| 1532 | switch (Tok.getKind()) { |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1533 | case tok::identifier: // foo::bar |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1534 | // If we already have a type specifier, this identifier is not a type. |
| 1535 | if (DS.getTypeSpecType() != DeclSpec::TST_unspecified || |
| 1536 | DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified || |
| 1537 | DS.getTypeSpecSign() != DeclSpec::TSS_unspecified) |
| 1538 | return false; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1539 | // Check for need to substitute AltiVec keyword tokens. |
| 1540 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 1541 | break; |
| 1542 | // Fall through. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1543 | case tok::kw_typename: // typename foo::bar |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1544 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1545 | // recurse to handle whatever we get. |
| 1546 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1547 | return true; |
| 1548 | if (Tok.is(tok::identifier)) |
| 1549 | return false; |
| 1550 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 1551 | TemplateInfo, SuppressDeclarations); |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1552 | case tok::coloncolon: // ::foo::bar |
| 1553 | if (NextToken().is(tok::kw_new) || // ::new |
| 1554 | NextToken().is(tok::kw_delete)) // ::delete |
| 1555 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1557 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1558 | // recurse to handle whatever we get. |
| 1559 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1560 | return true; |
| 1561 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 1562 | TemplateInfo, SuppressDeclarations); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1564 | // simple-type-specifier: |
Chris Lattner | a8a3f73 | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1565 | case tok::annot_typename: { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1566 | if (ParsedType T = getTypeAnnotation(Tok)) { |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1567 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1568 | DiagID, T); |
| 1569 | } else |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1570 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1571 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1572 | ConsumeToken(); // The typename |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1574 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1575 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 1576 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 1577 | // just a normal reference to a typedef name. |
| 1578 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 1579 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | |
Argyrios Kyrtzidis | fc1f9e4 | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1581 | SourceLocation LAngleLoc, EndProtoLoc; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1582 | llvm::SmallVector<Decl *, 8> ProtocolDecl; |
Argyrios Kyrtzidis | fc1f9e4 | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1583 | llvm::SmallVector<SourceLocation, 8> ProtocolLocs; |
| 1584 | ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false, |
| 1585 | LAngleLoc, EndProtoLoc); |
| 1586 | DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(), |
| 1587 | ProtocolLocs.data(), LAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1589 | DS.SetRangeEnd(EndProtoLoc); |
| 1590 | return true; |
| 1591 | } |
| 1592 | |
| 1593 | case tok::kw_short: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1594 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1595 | break; |
| 1596 | case tok::kw_long: |
| 1597 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1598 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 1599 | DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1600 | else |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1601 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 1602 | DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1603 | break; |
| 1604 | case tok::kw_signed: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1605 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1606 | break; |
| 1607 | case tok::kw_unsigned: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1608 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 1609 | DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1610 | break; |
| 1611 | case tok::kw__Complex: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1612 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 1613 | DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1614 | break; |
| 1615 | case tok::kw__Imaginary: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1616 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 1617 | DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1618 | break; |
| 1619 | case tok::kw_void: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1620 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1621 | break; |
| 1622 | case tok::kw_char: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1623 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1624 | break; |
| 1625 | case tok::kw_int: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1626 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1627 | break; |
| 1628 | case tok::kw_float: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1629 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1630 | break; |
| 1631 | case tok::kw_double: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1632 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1633 | break; |
| 1634 | case tok::kw_wchar_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1635 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1636 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1637 | case tok::kw_char16_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1638 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1639 | break; |
| 1640 | case tok::kw_char32_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1641 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1642 | break; |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1643 | case tok::kw_bool: |
| 1644 | case tok::kw__Bool: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1645 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1646 | break; |
| 1647 | case tok::kw__Decimal32: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1648 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 1649 | DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1650 | break; |
| 1651 | case tok::kw__Decimal64: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1652 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 1653 | DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1654 | break; |
| 1655 | case tok::kw__Decimal128: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1656 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 1657 | DiagID); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1658 | break; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1659 | case tok::kw___vector: |
| 1660 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 1661 | break; |
| 1662 | case tok::kw___pixel: |
| 1663 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 1664 | break; |
| 1665 | |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1666 | // class-specifier: |
| 1667 | case tok::kw_class: |
| 1668 | case tok::kw_struct: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1669 | case tok::kw_union: { |
| 1670 | tok::TokenKind Kind = Tok.getKind(); |
| 1671 | ConsumeToken(); |
Sebastian Redl | 2b37272 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 1672 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none, |
| 1673 | SuppressDeclarations); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1674 | return true; |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1675 | } |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1676 | |
| 1677 | // enum-specifier: |
| 1678 | case tok::kw_enum: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1679 | ConsumeToken(); |
Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 1680 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1681 | return true; |
| 1682 | |
| 1683 | // cv-qualifier: |
| 1684 | case tok::kw_const: |
| 1685 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1686 | DiagID, getLang()); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1687 | break; |
| 1688 | case tok::kw_volatile: |
| 1689 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1690 | DiagID, getLang()); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1691 | break; |
| 1692 | case tok::kw_restrict: |
| 1693 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1694 | DiagID, getLang()); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1695 | break; |
| 1696 | |
| 1697 | // GNU typeof support. |
| 1698 | case tok::kw_typeof: |
| 1699 | ParseTypeofSpecifier(DS); |
| 1700 | return true; |
| 1701 | |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1702 | // C++0x decltype support. |
| 1703 | case tok::kw_decltype: |
| 1704 | ParseDecltypeSpecifier(DS); |
| 1705 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
Anders Carlsson | bae2737 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 1707 | // C++0x auto support. |
| 1708 | case tok::kw_auto: |
| 1709 | if (!getLang().CPlusPlus0x) |
| 1710 | return false; |
| 1711 | |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1712 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID); |
Anders Carlsson | bae2737 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 1713 | break; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1714 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1715 | case tok::kw___ptr64: |
| 1716 | case tok::kw___w64: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1717 | case tok::kw___cdecl: |
| 1718 | case tok::kw___stdcall: |
| 1719 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 1720 | case tok::kw___thiscall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1721 | DS.AddAttributes(ParseMicrosoftTypeAttributes()); |
Chris Lattner | 78ecd4f | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 1722 | return true; |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1723 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1724 | case tok::kw___pascal: |
| 1725 | DS.AddAttributes(ParseBorlandTypeAttributes()); |
| 1726 | return true; |
| 1727 | |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1728 | default: |
| 1729 | // Not a type-specifier; do nothing. |
| 1730 | return false; |
| 1731 | } |
| 1732 | |
| 1733 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 1734 | if (isInvalid) { |
| 1735 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1736 | // Pick between error or extwarn. |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1737 | Diag(Tok, DiagID) << PrevSpec; |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1738 | } |
| 1739 | DS.SetRangeEnd(Tok.getLocation()); |
| 1740 | ConsumeToken(); // whatever we parsed above. |
| 1741 | return true; |
| 1742 | } |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1743 | |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1744 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 1745 | /// semicolon. |
| 1746 | /// |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 1747 | /// struct-declaration: |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1748 | /// specifier-qualifier-list struct-declarator-list |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 1749 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1750 | /// [GNU] specifier-qualifier-list |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 1751 | /// struct-declarator-list: |
| 1752 | /// struct-declarator |
| 1753 | /// struct-declarator-list ',' struct-declarator |
| 1754 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 1755 | /// struct-declarator: |
| 1756 | /// declarator |
| 1757 | /// [GNU] declarator attributes[opt] |
| 1758 | /// declarator[opt] ':' constant-expression |
| 1759 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 1760 | /// |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1761 | void Parser:: |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1762 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 1763 | if (Tok.is(tok::kw___extension__)) { |
| 1764 | // __extension__ silences extension warnings in the subexpression. |
| 1765 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1766 | ConsumeToken(); |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 1767 | return ParseStructDeclaration(DS, Fields); |
| 1768 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1770 | // Parse the common specifier-qualifiers-list piece. |
Chris Lattner | 32295d3 | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 1771 | SourceLocation DSStart = Tok.getLocation(); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1772 | ParseSpecifierQualifierList(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1773 | |
Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 1774 | // If there are no declarators, this is a free-standing declaration |
| 1775 | // specifier. Let the actions module cope with it. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1776 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1777 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1778 | return; |
| 1779 | } |
| 1780 | |
| 1781 | // Read struct-declarators until we find the semicolon. |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1782 | bool FirstDeclarator = true; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1783 | while (1) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1784 | ParsingDeclRAIIObject PD(*this); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1785 | FieldDeclarator DeclaratorInfo(DS); |
| 1786 | |
| 1787 | // Attributes are only allowed here on successive declarators. |
| 1788 | if (!FirstDeclarator && Tok.is(tok::kw___attribute)) { |
| 1789 | SourceLocation Loc; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1790 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1791 | DeclaratorInfo.D.AddAttributes(AttrList, Loc); |
| 1792 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1794 | /// struct-declarator: declarator |
| 1795 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1796 | if (Tok.isNot(tok::colon)) { |
| 1797 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 1798 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1799 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1800 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1802 | if (Tok.is(tok::colon)) { |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1803 | ConsumeToken(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1804 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1805 | if (Res.isInvalid()) |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1806 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 32295d3 | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 1807 | else |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1808 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1809 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1810 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1811 | // If attributes exist after the declarator, parse them. |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1812 | if (Tok.is(tok::kw___attribute)) { |
| 1813 | SourceLocation Loc; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1814 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1815 | DeclaratorInfo.D.AddAttributes(AttrList, Loc); |
| 1816 | } |
| 1817 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1818 | // We're done with this declarator; invoke the callback. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1819 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1820 | PD.complete(D); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1821 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1822 | // If we don't have a comma, it is either the end of the list (a ';') |
| 1823 | // or an error, bail out. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1824 | if (Tok.isNot(tok::comma)) |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1825 | return; |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1826 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1827 | // Consume the comma. |
| 1828 | ConsumeToken(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1829 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1830 | FirstDeclarator = false; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1831 | } |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | /// ParseStructUnionBody |
| 1835 | /// struct-contents: |
| 1836 | /// struct-declaration-list |
| 1837 | /// [EXT] empty |
| 1838 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 1839 | /// struct-declaration-list: |
| 1840 | /// struct-declaration |
| 1841 | /// struct-declaration-list struct-declaration |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1842 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1843 | /// |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 1844 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1845 | unsigned TagType, Decl *TagDecl) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1846 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 1847 | "parsing struct/union body"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1848 | |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 1849 | SourceLocation LBraceLoc = ConsumeBrace(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | |
Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 1851 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1852 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1853 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 1854 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 1855 | // C++. |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1856 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Douglas Gregor | da2955e | 2010-07-29 14:29:34 +0000 | [diff] [blame] | 1857 | Diag(Tok, diag::ext_empty_struct_union) |
| 1858 | << (TagType == TST_union); |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 1859 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1860 | llvm::SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1861 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 1862 | // While we still have something to read, read the declarations in the struct. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1863 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 1864 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1865 | |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 1866 | // Check for extraneous top-level semicolon. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1867 | if (Tok.is(tok::semi)) { |
Douglas Gregor | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1868 | Diag(Tok, diag::ext_extra_struct_semi) |
Douglas Gregor | 13d0568 | 2010-06-16 23:08:59 +0000 | [diff] [blame] | 1869 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1870 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Chris Lattner | 36e46a2 | 2007-06-09 05:49:55 +0000 | [diff] [blame] | 1871 | ConsumeToken(); |
| 1872 | continue; |
| 1873 | } |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1874 | |
| 1875 | // Parse all the comma separated declarators. |
| 1876 | DeclSpec DS; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1878 | if (!Tok.is(tok::at)) { |
| 1879 | struct CFieldCallback : FieldCallback { |
| 1880 | Parser &P; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1881 | Decl *TagDecl; |
| 1882 | llvm::SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1883 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1884 | CFieldCallback(Parser &P, Decl *TagDecl, |
| 1885 | llvm::SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1886 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 1887 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1888 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1889 | // Install the declarator into the current TagDecl. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1890 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 5e6253b | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 1891 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 1892 | FD.D, FD.BitfieldSize); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1893 | FieldDecls.push_back(Field); |
| 1894 | return Field; |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 1895 | } |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1896 | } Callback(*this, TagDecl, FieldDecls); |
| 1897 | |
| 1898 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1899 | } else { // Handle @defs |
| 1900 | ConsumeToken(); |
| 1901 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 1902 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 1903 | SkipUntil(tok::semi, true); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1904 | continue; |
| 1905 | } |
| 1906 | ConsumeToken(); |
| 1907 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 1908 | if (!Tok.is(tok::identifier)) { |
| 1909 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 1910 | SkipUntil(tok::semi, true); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1911 | continue; |
| 1912 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1913 | llvm::SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1914 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1915 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1916 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 1917 | ConsumeToken(); |
| 1918 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | } |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 1920 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1921 | if (Tok.is(tok::semi)) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 1922 | ConsumeToken(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1923 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 1924 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Chris Lattner | 0c7e82d | 2007-06-09 05:54:40 +0000 | [diff] [blame] | 1925 | break; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 1926 | } else { |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 1927 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 1928 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 1929 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 1930 | // If we stopped at a ';', eat it. |
| 1931 | if (Tok.is(tok::semi)) ConsumeToken(); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 1932 | } |
| 1933 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | |
Steve Naroff | 33a1e80 | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1935 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1937 | llvm::OwningPtr<AttributeList> AttrList; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 1938 | // If attributes exist after struct contents, parse them. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1939 | if (Tok.is(tok::kw___attribute)) |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1940 | AttrList.reset(ParseGNUAttributes()); |
Daniel Dunbar | 15619c7 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1942 | Actions.ActOnFields(getCurScope(), |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1943 | RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(), |
Daniel Dunbar | 15619c7 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1944 | LBraceLoc, RBraceLoc, |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1945 | AttrList.get()); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1946 | StructScope.Exit(); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1947 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 1951 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1952 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 1953 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1954 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1955 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 1956 | /// '}' attributes[opt] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 1957 | /// 'enum' identifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1958 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1959 | /// |
| 1960 | /// [C++] elaborated-type-specifier: |
| 1961 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 1962 | /// |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1963 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 1964 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1965 | AccessSpecifier AS) { |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 1966 | // Parse the tag portion of this. |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1967 | if (Tok.is(tok::code_completion)) { |
| 1968 | // Code completion for an enum name. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1969 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 1970 | ConsumeCodeCompletionToken(); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1973 | llvm::OwningPtr<AttributeList> Attr; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1974 | // If attributes exist after tag, parse them. |
| 1975 | if (Tok.is(tok::kw___attribute)) |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1976 | Attr.reset(ParseGNUAttributes()); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1977 | |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 1978 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1979 | if (getLang().CPlusPlus) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1980 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1981 | return; |
| 1982 | |
| 1983 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1984 | Diag(Tok, diag::err_expected_ident); |
| 1985 | if (Tok.isNot(tok::l_brace)) { |
| 1986 | // Has no name and is not a definition. |
| 1987 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 1988 | SkipUntil(tok::comma, true); |
| 1989 | return; |
| 1990 | } |
| 1991 | } |
| 1992 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1994 | // Must have either 'enum name' or 'enum {...}'. |
| 1995 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) { |
| 1996 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1998 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 1999 | SkipUntil(tok::comma, true); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2000 | return; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2001 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2003 | // If an identifier is present, consume and remember it. |
| 2004 | IdentifierInfo *Name = 0; |
| 2005 | SourceLocation NameLoc; |
| 2006 | if (Tok.is(tok::identifier)) { |
| 2007 | Name = Tok.getIdentifierInfo(); |
| 2008 | NameLoc = ConsumeToken(); |
| 2009 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2010 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2011 | // There are three options here. If we have 'enum foo;', then this is a |
| 2012 | // forward declaration. If we have 'enum foo {...' then this is a |
| 2013 | // definition. Otherwise we have something like 'enum foo xyz', a reference. |
| 2014 | // |
| 2015 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 2016 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 2017 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 2018 | // |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2019 | Sema::TagUseKind TUK; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2020 | if (Tok.is(tok::l_brace)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2021 | TUK = Sema::TUK_Definition; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2022 | else if (Tok.is(tok::semi)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2023 | TUK = Sema::TUK_Declaration; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2024 | else |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2025 | TUK = Sema::TUK_Reference; |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2026 | |
| 2027 | // enums cannot be templates, although they can be referenced from a |
| 2028 | // template. |
| 2029 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2030 | TUK != Sema::TUK_Reference) { |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2031 | Diag(Tok, diag::err_enum_template); |
| 2032 | |
| 2033 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2034 | SkipUntil(tok::comma, true); |
| 2035 | return; |
| 2036 | } |
| 2037 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2038 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2039 | bool IsDependent = false; |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2040 | SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc; |
| 2041 | const char *PrevSpec = 0; |
| 2042 | unsigned DiagID; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2043 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
| 2044 | StartLoc, SS, Name, NameLoc, Attr.get(), |
| 2045 | AS, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2046 | MultiTemplateParamsArg(Actions), |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2047 | Owned, IsDependent); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2048 | if (IsDependent) { |
| 2049 | // This enum has a dependent nested-name-specifier. Handle it as a |
| 2050 | // dependent tag. |
| 2051 | if (!Name) { |
| 2052 | DS.SetTypeSpecError(); |
| 2053 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 2054 | return; |
| 2055 | } |
| 2056 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2057 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2058 | TUK, SS, Name, StartLoc, |
| 2059 | NameLoc); |
| 2060 | if (Type.isInvalid()) { |
| 2061 | DS.SetTypeSpecError(); |
| 2062 | return; |
| 2063 | } |
| 2064 | |
| 2065 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2066 | Type.get())) |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2067 | Diag(StartLoc, DiagID) << PrevSpec; |
| 2068 | |
| 2069 | return; |
| 2070 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2072 | if (!TagDecl) { |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2073 | // The action failed to produce an enumeration tag. If this is a |
| 2074 | // definition, consume the entire definition. |
| 2075 | if (Tok.is(tok::l_brace)) { |
| 2076 | ConsumeBrace(); |
| 2077 | SkipUntil(tok::r_brace); |
| 2078 | } |
| 2079 | |
| 2080 | DS.SetTypeSpecError(); |
| 2081 | return; |
| 2082 | } |
| 2083 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2084 | if (Tok.is(tok::l_brace)) |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2085 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2086 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2087 | // FIXME: The DeclSpec should keep the locations of both the keyword |
| 2088 | // and the name (if there is one). |
Douglas Gregor | 7210063 | 2010-01-25 16:33:23 +0000 | [diff] [blame] | 2089 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2090 | TagDecl, Owned)) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2091 | Diag(StartLoc, DiagID) << PrevSpec; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2094 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 2095 | /// enumerator-list: |
| 2096 | /// enumerator |
| 2097 | /// enumerator-list ',' enumerator |
| 2098 | /// enumerator: |
| 2099 | /// enumeration-constant |
| 2100 | /// enumeration-constant '=' constant-expression |
| 2101 | /// enumeration-constant: |
| 2102 | /// identifier |
| 2103 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2104 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2105 | // Enter the scope of the enum body and start the definition. |
| 2106 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2107 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2108 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2109 | SourceLocation LBraceLoc = ConsumeBrace(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2110 | |
Chris Lattner | 37256fb | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 2111 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2112 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Fariborz Jahanian | 6e81492 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 2113 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2115 | llvm::SmallVector<Decl *, 32> EnumConstantDecls; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2116 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2117 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2118 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2119 | // Parse the enumerator-list. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2120 | while (Tok.is(tok::identifier)) { |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2121 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 2122 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2124 | SourceLocation EqualLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2125 | ExprResult AssignedVal; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2126 | if (Tok.is(tok::equal)) { |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2127 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2128 | AssignedVal = ParseConstantExpression(); |
| 2129 | if (AssignedVal.isInvalid()) |
Chris Lattner | da6c2ce | 2007-04-27 19:13:15 +0000 | [diff] [blame] | 2130 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2131 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2133 | // Install the enumerator constant into EnumDecl. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2134 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 2135 | LastEnumConstDecl, |
| 2136 | IdentLoc, Ident, |
| 2137 | EqualLoc, |
| 2138 | AssignedVal.release()); |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 2139 | EnumConstantDecls.push_back(EnumConstDecl); |
| 2140 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 2142 | if (Tok.is(tok::identifier)) { |
| 2143 | // We're missing a comma between enumerators. |
| 2144 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 2145 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
| 2146 | << FixItHint::CreateInsertion(Loc, ", "); |
| 2147 | continue; |
| 2148 | } |
| 2149 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2150 | if (Tok.isNot(tok::comma)) |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2151 | break; |
| 2152 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2153 | |
| 2154 | if (Tok.isNot(tok::identifier) && |
Douglas Gregor | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2155 | !(getLang().C99 || getLang().CPlusPlus0x)) |
| 2156 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
| 2157 | << getLang().CPlusPlus |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2158 | << FixItHint::CreateRemoval(CommaLoc); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2159 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2160 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2161 | // Eat the }. |
Mike Stump | 6814d1c | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 2162 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2163 | |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2164 | llvm::OwningPtr<AttributeList> Attr; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2165 | // If attributes exist after the identifier list, parse them. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2166 | if (Tok.is(tok::kw___attribute)) |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2167 | Attr.reset(ParseGNUAttributes()); // FIXME: where do they do? |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2168 | |
Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 2169 | Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl, |
| 2170 | EnumConstantDecls.data(), EnumConstantDecls.size(), |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2171 | getCurScope(), Attr.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2173 | EnumScope.Exit(); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2174 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 2175 | } |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2176 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 2177 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 2178 | /// start of a type-qualifier-list. |
| 2179 | bool Parser::isTypeQualifier() const { |
| 2180 | switch (Tok.getKind()) { |
| 2181 | default: return false; |
| 2182 | // type-qualifier |
| 2183 | case tok::kw_const: |
| 2184 | case tok::kw_volatile: |
| 2185 | case tok::kw_restrict: |
| 2186 | return true; |
| 2187 | } |
| 2188 | } |
| 2189 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 2190 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 2191 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 2192 | /// specifier or if we're not sure. |
| 2193 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 2194 | switch (Tok.getKind()) { |
| 2195 | default: return false; |
| 2196 | // type-specifiers |
| 2197 | case tok::kw_short: |
| 2198 | case tok::kw_long: |
| 2199 | case tok::kw_signed: |
| 2200 | case tok::kw_unsigned: |
| 2201 | case tok::kw__Complex: |
| 2202 | case tok::kw__Imaginary: |
| 2203 | case tok::kw_void: |
| 2204 | case tok::kw_char: |
| 2205 | case tok::kw_wchar_t: |
| 2206 | case tok::kw_char16_t: |
| 2207 | case tok::kw_char32_t: |
| 2208 | case tok::kw_int: |
| 2209 | case tok::kw_float: |
| 2210 | case tok::kw_double: |
| 2211 | case tok::kw_bool: |
| 2212 | case tok::kw__Bool: |
| 2213 | case tok::kw__Decimal32: |
| 2214 | case tok::kw__Decimal64: |
| 2215 | case tok::kw__Decimal128: |
| 2216 | case tok::kw___vector: |
| 2217 | |
| 2218 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 2219 | case tok::kw_class: |
| 2220 | case tok::kw_struct: |
| 2221 | case tok::kw_union: |
| 2222 | // enum-specifier |
| 2223 | case tok::kw_enum: |
| 2224 | |
| 2225 | // typedef-name |
| 2226 | case tok::annot_typename: |
| 2227 | return true; |
| 2228 | } |
| 2229 | } |
| 2230 | |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 2231 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 2232 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2233 | bool Parser::isTypeSpecifierQualifier() { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 2234 | switch (Tok.getKind()) { |
| 2235 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2237 | case tok::identifier: // foo::bar |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2238 | if (TryAltiVecVectorToken()) |
| 2239 | return true; |
| 2240 | // Fall through. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2241 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2242 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2243 | // recurse to handle whatever we get. |
| 2244 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2245 | return true; |
| 2246 | if (Tok.is(tok::identifier)) |
| 2247 | return false; |
| 2248 | return isTypeSpecifierQualifier(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2249 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2250 | case tok::coloncolon: // ::foo::bar |
| 2251 | if (NextToken().is(tok::kw_new) || // ::new |
| 2252 | NextToken().is(tok::kw_delete)) // ::delete |
| 2253 | return false; |
| 2254 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2255 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2256 | return true; |
| 2257 | return isTypeSpecifierQualifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2258 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2259 | // GNU attributes support. |
| 2260 | case tok::kw___attribute: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2261 | // GNU typeof support. |
| 2262 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2263 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 2264 | // type-specifiers |
| 2265 | case tok::kw_short: |
| 2266 | case tok::kw_long: |
| 2267 | case tok::kw_signed: |
| 2268 | case tok::kw_unsigned: |
| 2269 | case tok::kw__Complex: |
| 2270 | case tok::kw__Imaginary: |
| 2271 | case tok::kw_void: |
| 2272 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2273 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2274 | case tok::kw_char16_t: |
| 2275 | case tok::kw_char32_t: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 2276 | case tok::kw_int: |
| 2277 | case tok::kw_float: |
| 2278 | case tok::kw_double: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 2279 | case tok::kw_bool: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 2280 | case tok::kw__Bool: |
| 2281 | case tok::kw__Decimal32: |
| 2282 | case tok::kw__Decimal64: |
| 2283 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2284 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2285 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 2286 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 2287 | case tok::kw_class: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 2288 | case tok::kw_struct: |
| 2289 | case tok::kw_union: |
| 2290 | // enum-specifier |
| 2291 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 2293 | // type-qualifier |
| 2294 | case tok::kw_const: |
| 2295 | case tok::kw_volatile: |
| 2296 | case tok::kw_restrict: |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2297 | |
| 2298 | // typedef-name |
Chris Lattner | a8a3f73 | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 2299 | case tok::annot_typename: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 2300 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | |
Chris Lattner | 409bf7d | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 2302 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 2303 | case tok::less: |
| 2304 | return getLang().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2305 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2306 | case tok::kw___cdecl: |
| 2307 | case tok::kw___stdcall: |
| 2308 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2309 | case tok::kw___thiscall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2310 | case tok::kw___w64: |
| 2311 | case tok::kw___ptr64: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2312 | case tok::kw___pascal: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2313 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 2314 | } |
| 2315 | } |
| 2316 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2317 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 2318 | /// declaration specifier. |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 2319 | /// |
| 2320 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 2321 | /// this check is to disambiguate between an expression and a declaration. |
| 2322 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2323 | switch (Tok.getKind()) { |
| 2324 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2325 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2326 | case tok::identifier: // foo::bar |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 2327 | // Unfortunate hack to support "Class.factoryMethod" notation. |
| 2328 | if (getLang().ObjC1 && NextToken().is(tok::period)) |
| 2329 | return false; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2330 | if (TryAltiVecVectorToken()) |
| 2331 | return true; |
| 2332 | // Fall through. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2333 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2334 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2335 | // recurse to handle whatever we get. |
| 2336 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2337 | return true; |
| 2338 | if (Tok.is(tok::identifier)) |
| 2339 | return false; |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 2340 | |
| 2341 | // If we're in Objective-C and we have an Objective-C class type followed |
| 2342 | // by an identifier and then either ':' or ']', in a place where an |
| 2343 | // expression is permitted, then this is probably a class message send |
| 2344 | // missing the initial '['. In this case, we won't consider this to be |
| 2345 | // the start of a declaration. |
| 2346 | if (DisambiguatingWithExpression && |
| 2347 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 2348 | return false; |
| 2349 | |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2350 | return isDeclarationSpecifier(); |
| 2351 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2352 | case tok::coloncolon: // ::foo::bar |
| 2353 | if (NextToken().is(tok::kw_new) || // ::new |
| 2354 | NextToken().is(tok::kw_delete)) // ::delete |
| 2355 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2356 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2357 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2358 | // recurse to handle whatever we get. |
| 2359 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2360 | return true; |
| 2361 | return isDeclarationSpecifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2362 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2363 | // storage-class-specifier |
| 2364 | case tok::kw_typedef: |
| 2365 | case tok::kw_extern: |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2366 | case tok::kw___private_extern__: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2367 | case tok::kw_static: |
| 2368 | case tok::kw_auto: |
| 2369 | case tok::kw_register: |
| 2370 | case tok::kw___thread: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2372 | // type-specifiers |
| 2373 | case tok::kw_short: |
| 2374 | case tok::kw_long: |
| 2375 | case tok::kw_signed: |
| 2376 | case tok::kw_unsigned: |
| 2377 | case tok::kw__Complex: |
| 2378 | case tok::kw__Imaginary: |
| 2379 | case tok::kw_void: |
| 2380 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2381 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2382 | case tok::kw_char16_t: |
| 2383 | case tok::kw_char32_t: |
| 2384 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2385 | case tok::kw_int: |
| 2386 | case tok::kw_float: |
| 2387 | case tok::kw_double: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 2388 | case tok::kw_bool: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2389 | case tok::kw__Bool: |
| 2390 | case tok::kw__Decimal32: |
| 2391 | case tok::kw__Decimal64: |
| 2392 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2393 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2394 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 2395 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 2396 | case tok::kw_class: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2397 | case tok::kw_struct: |
| 2398 | case tok::kw_union: |
| 2399 | // enum-specifier |
| 2400 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2401 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2402 | // type-qualifier |
| 2403 | case tok::kw_const: |
| 2404 | case tok::kw_volatile: |
| 2405 | case tok::kw_restrict: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2406 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2407 | // function-specifier |
| 2408 | case tok::kw_inline: |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2409 | case tok::kw_virtual: |
| 2410 | case tok::kw_explicit: |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 2411 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2412 | // typedef-name |
Chris Lattner | a8a3f73 | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 2413 | case tok::annot_typename: |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2414 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 2415 | // GNU typeof support. |
| 2416 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2417 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 2418 | // GNU attributes. |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 2419 | case tok::kw___attribute: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2420 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | |
Chris Lattner | 8b2ec16 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 2422 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 2423 | case tok::less: |
| 2424 | return getLang().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2425 | |
Steve Naroff | f192fab | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 2426 | case tok::kw___declspec: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2427 | case tok::kw___cdecl: |
| 2428 | case tok::kw___stdcall: |
| 2429 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2430 | case tok::kw___thiscall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2431 | case tok::kw___w64: |
| 2432 | case tok::kw___ptr64: |
| 2433 | case tok::kw___forceinline: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2434 | case tok::kw___pascal: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2435 | return true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2439 | bool Parser::isConstructorDeclarator() { |
| 2440 | TentativeParsingAction TPA(*this); |
| 2441 | |
| 2442 | // Parse the C++ scope specifier. |
| 2443 | CXXScopeSpec SS; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2444 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2445 | TPA.Revert(); |
| 2446 | return false; |
| 2447 | } |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2448 | |
| 2449 | // Parse the constructor name. |
| 2450 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 2451 | // We already know that we have a constructor name; just consume |
| 2452 | // the token. |
| 2453 | ConsumeToken(); |
| 2454 | } else { |
| 2455 | TPA.Revert(); |
| 2456 | return false; |
| 2457 | } |
| 2458 | |
| 2459 | // Current class name must be followed by a left parentheses. |
| 2460 | if (Tok.isNot(tok::l_paren)) { |
| 2461 | TPA.Revert(); |
| 2462 | return false; |
| 2463 | } |
| 2464 | ConsumeParen(); |
| 2465 | |
| 2466 | // A right parentheses or ellipsis signals that we have a constructor. |
| 2467 | if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) { |
| 2468 | TPA.Revert(); |
| 2469 | return true; |
| 2470 | } |
| 2471 | |
| 2472 | // If we need to, enter the specified scope. |
| 2473 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2474 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2475 | DeclScopeObj.EnterDeclaratorScope(); |
| 2476 | |
| 2477 | // Check whether the next token(s) are part of a declaration |
| 2478 | // specifier, in which case we have the start of a parameter and, |
| 2479 | // therefore, we know that this is a constructor. |
| 2480 | bool IsConstructor = isDeclarationSpecifier(); |
| 2481 | TPA.Revert(); |
| 2482 | return IsConstructor; |
| 2483 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2484 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2485 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2486 | /// type-qualifier-list: [C99 6.7.5] |
| 2487 | /// type-qualifier |
| 2488 | /// [vendor] attributes |
| 2489 | /// [ only if VendorAttributesAllowed=true ] |
| 2490 | /// type-qualifier-list type-qualifier |
| 2491 | /// [vendor] type-qualifier-list attributes |
| 2492 | /// [ only if VendorAttributesAllowed=true ] |
| 2493 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 2494 | /// [ only if CXX0XAttributesAllowed=true ] |
| 2495 | /// Note: vendor can be GNU, MS, etc. |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2496 | /// |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2497 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 2498 | bool VendorAttributesAllowed, |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2499 | bool CXX0XAttributesAllowed) { |
| 2500 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) { |
| 2501 | SourceLocation Loc = Tok.getLocation(); |
| 2502 | CXX0XAttributeList Attr = ParseCXX0XAttributes(); |
| 2503 | if (CXX0XAttributesAllowed) |
| 2504 | DS.AddAttributes(Attr.AttrList); |
| 2505 | else |
| 2506 | Diag(Loc, diag::err_attributes_not_allowed); |
| 2507 | } |
| 2508 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2509 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2510 | bool isInvalid = false; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 2511 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2512 | unsigned DiagID = 0; |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 2513 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 2514 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2515 | switch (Tok.getKind()) { |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 2516 | case tok::code_completion: |
| 2517 | Actions.CodeCompleteTypeQualifiers(DS); |
| 2518 | ConsumeCodeCompletionToken(); |
| 2519 | break; |
| 2520 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2521 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2522 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
| 2523 | getLang()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 2524 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2525 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2526 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
| 2527 | getLang()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 2528 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2529 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2530 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
| 2531 | getLang()); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2532 | break; |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2533 | case tok::kw___w64: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2534 | case tok::kw___ptr64: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2535 | case tok::kw___cdecl: |
| 2536 | case tok::kw___stdcall: |
| 2537 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2538 | case tok::kw___thiscall: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2539 | if (VendorAttributesAllowed) { |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2540 | DS.AddAttributes(ParseMicrosoftTypeAttributes()); |
| 2541 | continue; |
| 2542 | } |
| 2543 | goto DoneWithTypeQuals; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2544 | case tok::kw___pascal: |
| 2545 | if (VendorAttributesAllowed) { |
| 2546 | DS.AddAttributes(ParseBorlandTypeAttributes()); |
| 2547 | continue; |
| 2548 | } |
| 2549 | goto DoneWithTypeQuals; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2550 | case tok::kw___attribute: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2551 | if (VendorAttributesAllowed) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2552 | DS.AddAttributes(ParseGNUAttributes()); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2553 | continue; // do *not* consume the next token! |
| 2554 | } |
| 2555 | // otherwise, FALL THROUGH! |
| 2556 | default: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2557 | DoneWithTypeQuals: |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2558 | // If this is not a type-qualifier token, we're done reading type |
| 2559 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2560 | DS.Finish(Diags, PP); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2561 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2562 | } |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 2563 | |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 2564 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 2565 | if (isInvalid) { |
| 2566 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2567 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 2568 | } |
| 2569 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2570 | } |
| 2571 | } |
| 2572 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 2573 | |
| 2574 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 2575 | /// |
| 2576 | void Parser::ParseDeclarator(Declarator &D) { |
| 2577 | /// This implements the 'declarator' production in the C grammar, then checks |
| 2578 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2579 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2582 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 2583 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 2584 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2585 | /// ptr-operator production. |
| 2586 | /// |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2587 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 2588 | /// [C] pointer[opt] direct-declarator |
| 2589 | /// [C++] direct-declarator |
| 2590 | /// [C++] ptr-operator declarator |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 2591 | /// |
| 2592 | /// pointer: [C99 6.7.5] |
| 2593 | /// '*' type-qualifier-list[opt] |
| 2594 | /// '*' type-qualifier-list[opt] pointer |
| 2595 | /// |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2596 | /// ptr-operator: |
| 2597 | /// '*' cv-qualifier-seq[opt] |
| 2598 | /// '&' |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 2599 | /// [C++0x] '&&' |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2600 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 2601 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2602 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2603 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 2604 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2605 | if (Diags.hasAllExtensionsSilenced()) |
| 2606 | D.setExtension(); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2607 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2608 | // C++ member pointers start with a '::' or a nested-name. |
| 2609 | // Member pointers get special handling, since there's no place for the |
| 2610 | // scope spec in the generic path below. |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 2611 | if (getLang().CPlusPlus && |
| 2612 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 2613 | Tok.is(tok::annot_cxxscope))) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2614 | CXXScopeSpec SS; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2615 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2616 | |
Jeffrey Yasskin | 4e150f8 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 2617 | if (SS.isNotEmpty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2618 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2619 | // The scope spec really belongs to the direct-declarator. |
| 2620 | D.getCXXScopeSpec() = SS; |
| 2621 | if (DirectDeclParser) |
| 2622 | (this->*DirectDeclParser)(D); |
| 2623 | return; |
| 2624 | } |
| 2625 | |
| 2626 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2627 | D.SetRangeEnd(Loc); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2628 | DeclSpec DS; |
| 2629 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2630 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2631 | |
| 2632 | // Recurse to parse whatever is left. |
| 2633 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 2634 | |
| 2635 | // Sema will have to catch (syntactically invalid) pointers into global |
| 2636 | // scope. It has to catch pointers into namespace scope anyway. |
| 2637 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2638 | Loc, DS.TakeAttributes()), |
| 2639 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2640 | return; |
| 2641 | } |
| 2642 | } |
| 2643 | |
| 2644 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2645 | // Not a pointer, C++ reference, or block. |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 2646 | if (Kind != tok::star && Kind != tok::caret && |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 2647 | (Kind != tok::amp || !getLang().CPlusPlus) && |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 2648 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 2649 | (Kind != tok::ampamp || !getLang().CPlusPlus)) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2650 | if (DirectDeclParser) |
| 2651 | (this->*DirectDeclParser)(D); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2652 | return; |
| 2653 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2654 | |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 2655 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 2656 | // '&&' -> rvalue reference |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 2657 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2658 | D.SetRangeEnd(Loc); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2659 | |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 2660 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 2661 | // Is a pointer. |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2662 | DeclSpec DS; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2663 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2664 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2665 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2666 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2667 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2668 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2669 | if (Kind == tok::star) |
| 2670 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 2671 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2672 | DS.TakeAttributes()), |
| 2673 | SourceLocation()); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2674 | else |
| 2675 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2676 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
Mike Stump | 3214d12 | 2009-04-21 00:51:43 +0000 | [diff] [blame] | 2677 | Loc, DS.TakeAttributes()), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2678 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2679 | } else { |
| 2680 | // Is a reference |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 2681 | DeclSpec DS; |
| 2682 | |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 2683 | // Complain about rvalue references in C++03, but then go on and build |
| 2684 | // the declarator. |
| 2685 | if (Kind == tok::ampamp && !getLang().CPlusPlus0x) |
| 2686 | Diag(Loc, diag::err_rvalue_reference); |
| 2687 | |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 2688 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 2689 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 2690 | // template type argument, in which case the cv-qualifiers are ignored. |
| 2691 | // |
| 2692 | // [GNU] Retricted references are allowed. |
| 2693 | // [GNU] Attributes on references are allowed. |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2694 | // [C++0x] Attributes on references are not allowed. |
| 2695 | ParseTypeQualifierListOpt(DS, true, false); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2696 | D.ExtendWithDeclSpec(DS); |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 2697 | |
| 2698 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 2699 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 2700 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2701 | diag::err_invalid_reference_qualifier_application) << "const"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 2702 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 2703 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2704 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 2705 | } |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2706 | |
| 2707 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2708 | ParseDeclaratorInternal(D, DirectDeclParser); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2709 | |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 2710 | if (D.getNumTypeObjects() > 0) { |
| 2711 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 2712 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 2713 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | ebad6a2 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 2714 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 2715 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 2716 | << II; |
| 2717 | else |
| 2718 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 2719 | << "type name"; |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 2720 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2721 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 2722 | // can go ahead and build the (technically ill-formed) |
| 2723 | // declarator: reference collapsing will take care of it. |
| 2724 | } |
| 2725 | } |
| 2726 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2727 | // Remember that we parsed a reference type. It doesn't have type-quals. |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 2728 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 2729 | DS.TakeAttributes(), |
| 2730 | Kind == tok::amp), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2731 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2732 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2735 | /// ParseDirectDeclarator |
| 2736 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2737 | /// [C99] identifier |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2738 | /// '(' declarator ')' |
| 2739 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 2740 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 2741 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 2742 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 2743 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 2744 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2745 | /// direct-declarator '(' parameter-type-list ')' |
| 2746 | /// direct-declarator '(' identifier-list[opt] ')' |
| 2747 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 2748 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2749 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 2750 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2751 | /// [C++] declarator-id |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2752 | /// |
| 2753 | /// declarator-id: [C++ 8] |
| 2754 | /// id-expression |
| 2755 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 2756 | /// |
| 2757 | /// id-expression: [C++ 5.1] |
| 2758 | /// unqualified-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 2759 | /// qualified-id |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2760 | /// |
| 2761 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2762 | /// identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2763 | /// operator-function-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 2764 | /// conversion-function-id |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | /// '~' class-name |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2766 | /// template-id |
Argyrios Kyrtzidis | e442635 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 2767 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2768 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2769 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2770 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2771 | if (getLang().CPlusPlus && D.mayHaveIdentifier()) { |
| 2772 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2773 | if (D.getCXXScopeSpec().isEmpty()) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2774 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2777 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2778 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 2779 | // Change the declaration context for name lookup, until this function |
| 2780 | // is exited (and the declarator has been parsed). |
| 2781 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2784 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 2785 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 2786 | // We found something that indicates the start of an unqualified-id. |
| 2787 | // Parse that unqualified-id. |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2788 | bool AllowConstructorName; |
| 2789 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 2790 | AllowConstructorName = false; |
| 2791 | else if (D.getCXXScopeSpec().isSet()) |
| 2792 | AllowConstructorName = |
| 2793 | (D.getContext() == Declarator::FileContext || |
| 2794 | (D.getContext() == Declarator::MemberContext && |
| 2795 | D.getDeclSpec().isFriendSpecified())); |
| 2796 | else |
| 2797 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 2798 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2799 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 2800 | /*EnteringContext=*/true, |
| 2801 | /*AllowDestructorName=*/true, |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2802 | AllowConstructorName, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2803 | ParsedType(), |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2804 | D.getName()) || |
| 2805 | // Once we're past the identifier, if the scope was bad, mark the |
| 2806 | // whole declarator bad. |
| 2807 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2808 | D.SetIdentifier(0, Tok.getLocation()); |
| 2809 | D.setInvalidType(true); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2810 | } else { |
| 2811 | // Parsed the unqualified-id; update range information and move along. |
| 2812 | if (D.getSourceRange().getBegin().isInvalid()) |
| 2813 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 2814 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2815 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2816 | goto PastIdentifier; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2817 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2818 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2819 | assert(!getLang().CPlusPlus && |
| 2820 | "There's a C++-specific check for tok::identifier above"); |
| 2821 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 2822 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 2823 | ConsumeToken(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2824 | goto PastIdentifier; |
| 2825 | } |
| 2826 | |
| 2827 | if (Tok.is(tok::l_paren)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2828 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2829 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2830 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 2831 | ParseParenDeclarator(D); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2832 | |
| 2833 | // If the declarator was parenthesized, we entered the declarator |
| 2834 | // scope when parsing the parenthesized declarator, then exited |
| 2835 | // the scope already. Re-enter the scope, if we need to. |
| 2836 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 2837 | // If there was an error parsing parenthesized declarator, declarator |
| 2838 | // scope may have been enterred before. Don't do it again. |
| 2839 | if (!D.isInvalidType() && |
| 2840 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2841 | // Change the declaration context for name lookup, until this function |
| 2842 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 2843 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2844 | } |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2845 | } else if (D.mayOmitIdentifier()) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2846 | // This could be something simple like "int" (in which case the declarator |
| 2847 | // portion is empty), if an abstract-declarator is allowed. |
| 2848 | D.SetIdentifier(0, Tok.getLocation()); |
| 2849 | } else { |
Douglas Gregor | d9f92e2 | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 2850 | if (D.getContext() == Declarator::MemberContext) |
| 2851 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 2852 | << D.getDeclSpec().getSourceRange(); |
| 2853 | else if (getLang().CPlusPlus) |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2854 | Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2855 | else |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2856 | Diag(Tok, diag::err_expected_ident_lparen); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 2857 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 8c5dd73 | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 2858 | D.setInvalidType(true); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2859 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2860 | |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2861 | PastIdentifier: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2862 | assert(D.isPastIdentifier() && |
| 2863 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2864 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2865 | // Don't parse attributes unless we have an identifier. |
Douglas Gregor | 0286b46 | 2010-02-19 16:47:56 +0000 | [diff] [blame] | 2866 | if (D.getIdentifier() && getLang().CPlusPlus0x |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2867 | && isCXX0XAttributeSpecifier(true)) { |
| 2868 | SourceLocation AttrEndLoc; |
| 2869 | CXX0XAttributeList Attr = ParseCXX0XAttributes(); |
| 2870 | D.AddAttributes(Attr.AttrList, AttrEndLoc); |
| 2871 | } |
| 2872 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2873 | while (1) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2874 | if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2875 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 2876 | // In such a case, check if we actually have a function declarator; if it |
| 2877 | // is not, the declarator has been fully parsed. |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2878 | if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 2879 | // When not in file scope, warn for ambiguous function declarators, just |
| 2880 | // in case the author intended it as a variable definition. |
| 2881 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 2882 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 2883 | break; |
| 2884 | } |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2885 | ParseFunctionDeclarator(ConsumeParen(), D); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2886 | } else if (Tok.is(tok::l_square)) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 2887 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2888 | } else { |
| 2889 | break; |
| 2890 | } |
| 2891 | } |
| 2892 | } |
| 2893 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2894 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 2895 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2896 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2897 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 2898 | /// |
| 2899 | /// direct-declarator: |
| 2900 | /// '(' declarator ')' |
| 2901 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2902 | /// direct-declarator '(' parameter-type-list ')' |
| 2903 | /// direct-declarator '(' identifier-list[opt] ')' |
| 2904 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 2905 | /// parameter-type-list[opt] ')' |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2906 | /// |
| 2907 | void Parser::ParseParenDeclarator(Declarator &D) { |
| 2908 | SourceLocation StartLoc = ConsumeParen(); |
| 2909 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2910 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2911 | // Eat any attributes before we look at whether this is a grouping or function |
| 2912 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 2913 | // the type being built up, for example: |
| 2914 | // int (__attribute__(()) *x)(long y) |
| 2915 | // If this ends up not being a grouping paren, the attribute applies to the |
| 2916 | // first argument, for example: |
| 2917 | // int (__attribute__(()) int x) |
| 2918 | // In either case, we need to eat any attributes to be able to determine what |
| 2919 | // sort of paren this is. |
| 2920 | // |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2921 | llvm::OwningPtr<AttributeList> AttrList; |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2922 | bool RequiresArg = false; |
| 2923 | if (Tok.is(tok::kw___attribute)) { |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2924 | AttrList.reset(ParseGNUAttributes()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2926 | // We require that the argument list (if this is a non-grouping paren) be |
| 2927 | // present even if the attribute list was empty. |
| 2928 | RequiresArg = true; |
| 2929 | } |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2930 | // Eat any Microsoft extensions. |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2931 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2932 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
| 2933 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) { |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2934 | AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take())); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2935 | } |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2936 | // Eat any Borland extensions. |
| 2937 | if (Tok.is(tok::kw___pascal)) { |
| 2938 | AttrList.reset(ParseBorlandTypeAttributes(AttrList.take())); |
| 2939 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2940 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2941 | // If we haven't past the identifier yet (or where the identifier would be |
| 2942 | // stored, if this is an abstract declarator), then this is probably just |
| 2943 | // grouping parens. However, if this could be an abstract-declarator, then |
| 2944 | // this could also be the start of function arguments (consider 'void()'). |
| 2945 | bool isGrouping; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2946 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2947 | if (!D.mayOmitIdentifier()) { |
| 2948 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 2949 | // paren, because we haven't seen the identifier yet. |
| 2950 | isGrouping = true; |
| 2951 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Argyrios Kyrtzidis | e8addf5 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 2952 | (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2953 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 2954 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 2955 | // considered to be a type, not a K&R identifier-list. |
| 2956 | isGrouping = false; |
| 2957 | } else { |
| 2958 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 2959 | isGrouping = true; |
| 2960 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2961 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2962 | // If this is a grouping paren, handle: |
| 2963 | // direct-declarator: '(' declarator ')' |
| 2964 | // direct-declarator: '(' attributes declarator ')' |
| 2965 | if (isGrouping) { |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 2966 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2967 | D.setGroupingParens(true); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2968 | if (AttrList) |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2969 | D.AddAttributes(AttrList.take(), SourceLocation()); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2970 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2971 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2972 | // Match the ')'. |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2973 | SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc); |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 2974 | |
| 2975 | D.setGroupingParens(hadGroupingParens); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2976 | D.SetRangeEnd(Loc); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2977 | return; |
| 2978 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2979 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2980 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 2981 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2982 | // identifier (and remember where it would have been), then call into |
| 2983 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2984 | D.SetIdentifier(0, Tok.getLocation()); |
| 2985 | |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2986 | ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 2990 | /// declarator D up to a paren, which indicates that we are parsing function |
| 2991 | /// arguments. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2992 | /// |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2993 | /// If AttrList is non-null, then the caller parsed those arguments immediately |
| 2994 | /// after the open paren - they should be considered to be the first argument of |
| 2995 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 2996 | /// function is required to be present and required to not be an identifier |
| 2997 | /// list. |
| 2998 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 2999 | /// This method also handles this portion of the grammar: |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3000 | /// parameter-type-list: [C99 6.7.5] |
| 3001 | /// parameter-list |
| 3002 | /// parameter-list ',' '...' |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 3003 | /// [C++] parameter-list '...' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3004 | /// |
| 3005 | /// parameter-list: [C99 6.7.5] |
| 3006 | /// parameter-declaration |
| 3007 | /// parameter-list ',' parameter-declaration |
| 3008 | /// |
| 3009 | /// parameter-declaration: [C99 6.7.5] |
| 3010 | /// declaration-specifiers declarator |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3011 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3012 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 3013 | /// declaration-specifiers abstract-declarator[opt] |
| 3014 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3015 | /// '=' assignment-expression |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3016 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3017 | /// |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3018 | /// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]" |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 3019 | /// and "exception-specification[opt]". |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3020 | /// |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3021 | void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, |
| 3022 | AttributeList *AttrList, |
| 3023 | bool RequiresArg) { |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3024 | // lparen is already consumed! |
| 3025 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3026 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3027 | // This parameter list may be empty. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3028 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3029 | if (RequiresArg) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3030 | Diag(Tok, diag::err_argument_required_after_attribute); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3031 | delete AttrList; |
| 3032 | } |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3033 | |
Argyrios Kyrtzidis | 20cf191 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3034 | SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'. |
| 3035 | SourceLocation EndLoc = RParenLoc; |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3036 | |
| 3037 | // cv-qualifier-seq[opt]. |
| 3038 | DeclSpec DS; |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3039 | bool hasExceptionSpec = false; |
Sebastian Redl | fb3f179 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3040 | SourceLocation ThrowLoc; |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3041 | bool hasAnyExceptionSpec = false; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3042 | llvm::SmallVector<ParsedType, 2> Exceptions; |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3043 | llvm::SmallVector<SourceRange, 2> ExceptionRanges; |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3044 | if (getLang().CPlusPlus) { |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3045 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3046 | if (!DS.getSourceRange().getEnd().isInvalid()) |
Argyrios Kyrtzidis | 20cf191 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3047 | EndLoc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3048 | |
| 3049 | // Parse exception-specification[opt]. |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3050 | if (Tok.is(tok::kw_throw)) { |
| 3051 | hasExceptionSpec = true; |
Sebastian Redl | fb3f179 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3052 | ThrowLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 20cf191 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3053 | ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges, |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3054 | hasAnyExceptionSpec); |
| 3055 | assert(Exceptions.size() == ExceptionRanges.size() && |
| 3056 | "Produced different number of exception types and ranges."); |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3057 | } |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3060 | // Remember that we parsed a function type, and remember the attributes. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 3061 | // int() -> no prototype, no '...'. |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3062 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus, |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3063 | /*variadic*/ false, |
Douglas Gregor | 94349fd | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3064 | SourceLocation(), |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3065 | /*arglist*/ 0, 0, |
| 3066 | DS.getTypeQualifiers(), |
Sebastian Redl | fb3f179 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3067 | hasExceptionSpec, ThrowLoc, |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3068 | hasAnyExceptionSpec, |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3069 | Exceptions.data(), |
| 3070 | ExceptionRanges.data(), |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3071 | Exceptions.size(), |
Argyrios Kyrtzidis | 20cf191 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3072 | LParenLoc, RParenLoc, D), |
| 3073 | EndLoc); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3074 | return; |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3077 | // Alternatively, this parameter list may be an identifier list form for a |
| 3078 | // K&R-style function: void foo(a,b,c) |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3079 | if (!getLang().CPlusPlus && Tok.is(tok::identifier) |
| 3080 | && !TryAltiVecVectorToken()) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3081 | if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) { |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3082 | // K&R identifier lists can't have typedefs as identifiers, per |
| 3083 | // C99 6.7.5.3p11. |
Steve Naroff | b048672 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 3084 | if (RequiresArg) { |
| 3085 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 3086 | delete AttrList; |
| 3087 | } |
Chris Lattner | 9453ab8 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3088 | |
Steve Naroff | b048672 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 3089 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
Chris Lattner | 9453ab8 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3090 | // normal declarators, not for abstract-declarators. Get the first |
| 3091 | // identifier. |
Chris Lattner | ff895c1 | 2010-05-14 17:44:56 +0000 | [diff] [blame] | 3092 | Token FirstTok = Tok; |
Chris Lattner | 9453ab8 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3093 | ConsumeToken(); // eat the first identifier. |
Chris Lattner | ff895c1 | 2010-05-14 17:44:56 +0000 | [diff] [blame] | 3094 | |
| 3095 | // Identifier lists follow a really simple grammar: the identifiers can |
| 3096 | // be followed *only* by a ", moreidentifiers" or ")". However, K&R |
| 3097 | // identifier lists are really rare in the brave new modern world, and it |
| 3098 | // is very common for someone to typo a type in a non-k&r style list. If |
| 3099 | // we are presented with something like: "void foo(intptr x, float y)", |
| 3100 | // we don't want to start parsing the function declarator as though it is |
| 3101 | // a K&R style declarator just because intptr is an invalid type. |
| 3102 | // |
| 3103 | // To handle this, we check to see if the token after the first identifier |
| 3104 | // is a "," or ")". Only if so, do we parse it as an identifier list. |
| 3105 | if (Tok.is(tok::comma) || Tok.is(tok::r_paren)) |
| 3106 | return ParseFunctionDeclaratorIdentifierList(LParenLoc, |
| 3107 | FirstTok.getIdentifierInfo(), |
| 3108 | FirstTok.getLocation(), D); |
| 3109 | |
| 3110 | // If we get here, the code is invalid. Push the first identifier back |
| 3111 | // into the token stream and parse the first argument as an (invalid) |
| 3112 | // normal argument declarator. |
| 3113 | PP.EnterToken(Tok); |
| 3114 | Tok = FirstTok; |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3115 | } |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3116 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3118 | // Finally, a normal, non-empty parameter type list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3119 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3120 | // Build up an array of information about the parsed arguments. |
| 3121 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3122 | |
| 3123 | // Enter function-declaration scope, limiting any declarators to the |
| 3124 | // function prototype scope, including parameter declarators. |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 3125 | ParseScope PrototypeScope(this, |
| 3126 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3127 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3128 | bool IsVariadic = false; |
Douglas Gregor | 94349fd | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3129 | SourceLocation EllipsisLoc; |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3130 | while (1) { |
| 3131 | if (Tok.is(tok::ellipsis)) { |
| 3132 | IsVariadic = true; |
Douglas Gregor | 94349fd | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3133 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3134 | break; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 3135 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3136 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3137 | SourceLocation DSStart = Tok.getLocation(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3138 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3139 | // Parse the declaration-specifiers. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3140 | // Just use the ParsingDeclaration "scope" of the declarator. |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3141 | DeclSpec DS; |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3142 | |
| 3143 | // If the caller parsed attributes for the first argument, add them now. |
| 3144 | if (AttrList) { |
| 3145 | DS.AddAttributes(AttrList); |
| 3146 | AttrList = 0; // Only apply the attributes to the first parameter. |
| 3147 | } |
Chris Lattner | de39c3e | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 3148 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3149 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3150 | // Parse the declarator. This is "PrototypeContext", because we must |
| 3151 | // accept either 'declarator' or 'abstract-declarator' here. |
| 3152 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 3153 | ParseDeclarator(ParmDecl); |
| 3154 | |
| 3155 | // Parse GNU attributes, if present. |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3156 | if (Tok.is(tok::kw___attribute)) { |
| 3157 | SourceLocation Loc; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3158 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3159 | ParmDecl.AddAttributes(AttrList, Loc); |
| 3160 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3162 | // Remember this parsed parameter in ParamInfo. |
| 3163 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3164 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3165 | // DefArgToks is used when the parsing of default arguments needs |
| 3166 | // to be delayed. |
| 3167 | CachedTokens *DefArgToks = 0; |
| 3168 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3169 | // If no parameter was specified, verify that *something* was specified, |
| 3170 | // otherwise we have a missing type and identifier. |
Chris Lattner | de39c3e | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 3171 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 3172 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3173 | // Completely missing, emit error. |
| 3174 | Diag(DSStart, diag::err_missing_param); |
| 3175 | } else { |
| 3176 | // Otherwise, we have something. Add it and let semantic analysis try |
| 3177 | // to grok it and add the result to the ParamInfo we are building. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3178 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3179 | // Inform the actions module about the parameter declarator, so it gets |
| 3180 | // added to the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3181 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3182 | |
| 3183 | // Parse the default argument, if any. We parse the default |
| 3184 | // arguments in all dialects; the semantic analysis in |
| 3185 | // ActOnParamDefaultArgument will reject the default argument in |
| 3186 | // C. |
| 3187 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3188 | SourceLocation EqualLoc = Tok.getLocation(); |
| 3189 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3190 | // Parse the default argument |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3191 | if (D.getContext() == Declarator::MemberContext) { |
| 3192 | // If we're inside a class definition, cache the tokens |
| 3193 | // corresponding to the default argument. We'll actually parse |
| 3194 | // them when we see the end of the class definition. |
| 3195 | // FIXME: Templates will require something similar. |
| 3196 | // FIXME: Can we use a smart pointer for Toks? |
| 3197 | DefArgToks = new CachedTokens; |
| 3198 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3199 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 3200 | /*StopAtSemi=*/true, |
| 3201 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3202 | delete DefArgToks; |
| 3203 | DefArgToks = 0; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3204 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 3205 | } else { |
| 3206 | // Mark the end of the default argument so that we know when to |
| 3207 | // stop when we parse it later on. |
| 3208 | Token DefArgEnd; |
| 3209 | DefArgEnd.startToken(); |
| 3210 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 3211 | DefArgEnd.setLocation(Tok.getLocation()); |
| 3212 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3213 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3214 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 3215 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3216 | } else { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3217 | // Consume the '='. |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3218 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3219 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3220 | // The argument isn't actually potentially evaluated unless it is |
| 3221 | // used. |
| 3222 | EnterExpressionEvaluationContext Eval(Actions, |
| 3223 | Sema::PotentiallyEvaluatedIfUsed); |
| 3224 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3225 | ExprResult DefArgResult(ParseAssignmentExpression()); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3226 | if (DefArgResult.isInvalid()) { |
| 3227 | Actions.ActOnParamDefaultArgumentError(Param); |
| 3228 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 3229 | } else { |
| 3230 | // Inform the actions module about the default argument |
| 3231 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3232 | DefArgResult.take()); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3233 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3234 | } |
| 3235 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3236 | |
| 3237 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 3238 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3239 | DefArgToks)); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | // If the next token is a comma, consume it and keep reading arguments. |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 3243 | if (Tok.isNot(tok::comma)) { |
| 3244 | if (Tok.is(tok::ellipsis)) { |
| 3245 | IsVariadic = true; |
| 3246 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
| 3247 | |
| 3248 | if (!getLang().CPlusPlus) { |
| 3249 | // We have ellipsis without a preceding ',', which is ill-formed |
| 3250 | // in C. Complain and provide the fix. |
| 3251 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3252 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | break; |
| 3257 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3258 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3259 | // Consume the comma. |
| 3260 | ConsumeToken(); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 3261 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3262 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3263 | // Leave prototype scope. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 3264 | PrototypeScope.Exit(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3265 | |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3266 | // If we have the closing ')', eat it. |
Argyrios Kyrtzidis | 20cf191 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3267 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 3268 | SourceLocation EndLoc = RParenLoc; |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3269 | |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3270 | DeclSpec DS; |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3271 | bool hasExceptionSpec = false; |
Sebastian Redl | fb3f179 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3272 | SourceLocation ThrowLoc; |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3273 | bool hasAnyExceptionSpec = false; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3274 | llvm::SmallVector<ParsedType, 2> Exceptions; |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3275 | llvm::SmallVector<SourceRange, 2> ExceptionRanges; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3276 | |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3277 | if (getLang().CPlusPlus) { |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3278 | // Parse cv-qualifier-seq[opt]. |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3279 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3280 | if (!DS.getSourceRange().getEnd().isInvalid()) |
Argyrios Kyrtzidis | 20cf191 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3281 | EndLoc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3282 | |
| 3283 | // Parse exception-specification[opt]. |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3284 | if (Tok.is(tok::kw_throw)) { |
| 3285 | hasExceptionSpec = true; |
Sebastian Redl | fb3f179 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3286 | ThrowLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 20cf191 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3287 | ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges, |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3288 | hasAnyExceptionSpec); |
| 3289 | assert(Exceptions.size() == ExceptionRanges.size() && |
| 3290 | "Produced different number of exception types and ranges."); |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3291 | } |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 3294 | // Remember that we parsed a function type, and remember the attributes. |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3295 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic, |
Douglas Gregor | 94349fd | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3296 | EllipsisLoc, |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 3297 | ParamInfo.data(), ParamInfo.size(), |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3298 | DS.getTypeQualifiers(), |
Sebastian Redl | fb3f179 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3299 | hasExceptionSpec, ThrowLoc, |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3300 | hasAnyExceptionSpec, |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3301 | Exceptions.data(), |
| 3302 | ExceptionRanges.data(), |
Argyrios Kyrtzidis | 20cf191 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3303 | Exceptions.size(), |
| 3304 | LParenLoc, RParenLoc, D), |
| 3305 | EndLoc); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3306 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 3307 | |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3308 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 3309 | /// we found a K&R-style identifier list instead of a type argument list. The |
Chris Lattner | 9453ab8 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3310 | /// first identifier has already been consumed, and the current token is the |
| 3311 | /// token right after it. |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3312 | /// |
| 3313 | /// identifier-list: [C99 6.7.5] |
| 3314 | /// identifier |
| 3315 | /// identifier-list ',' identifier |
| 3316 | /// |
| 3317 | void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc, |
Chris Lattner | 9453ab8 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3318 | IdentifierInfo *FirstIdent, |
| 3319 | SourceLocation FirstIdentLoc, |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3320 | Declarator &D) { |
| 3321 | // Build up an array of information about the parsed arguments. |
| 3322 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 3323 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3324 | |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3325 | // If there was no identifier specified for the declarator, either we are in |
| 3326 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 3327 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 3328 | // diagnose this. |
| 3329 | if (!D.getIdentifier()) |
Chris Lattner | 9453ab8 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3330 | Diag(FirstIdentLoc, diag::ext_ident_list_in_param); |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3331 | |
Chris Lattner | 9453ab8 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3332 | // The first identifier was already read, and is known to be the first |
| 3333 | // identifier in the list. Remember this identifier in ParamInfo. |
| 3334 | ParamsSoFar.insert(FirstIdent); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3335 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3336 | |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3337 | while (Tok.is(tok::comma)) { |
| 3338 | // Eat the comma. |
| 3339 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3340 | |
Chris Lattner | 9186f55 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3341 | // If this isn't an identifier, report the error and skip until ')'. |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3342 | if (Tok.isNot(tok::identifier)) { |
| 3343 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 9186f55 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3344 | SkipUntil(tok::r_paren); |
| 3345 | return; |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3346 | } |
Chris Lattner | 67b450c | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 3347 | |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3348 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
Chris Lattner | 67b450c | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 3349 | |
| 3350 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3351 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
Chris Lattner | ebad6a2 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3352 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3353 | |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3354 | // Verify that the argument identifier has not already been mentioned. |
| 3355 | if (!ParamsSoFar.insert(ParmII)) { |
Chris Lattner | ebad6a2 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3356 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
Chris Lattner | 9186f55 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3357 | } else { |
| 3358 | // Remember this identifier in ParamInfo. |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3359 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3360 | Tok.getLocation(), |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3361 | 0)); |
Chris Lattner | 9186f55 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3362 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3363 | |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3364 | // Eat the identifier. |
| 3365 | ConsumeToken(); |
| 3366 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3367 | |
| 3368 | // If we have the closing ')', eat it and we're done. |
| 3369 | SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 3370 | |
Chris Lattner | 9186f55 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3371 | // Remember that we parsed a function type, and remember the attributes. This |
| 3372 | // function type is always a K&R style function type, which is not varargs and |
| 3373 | // has no prototype. |
| 3374 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false, |
Douglas Gregor | 94349fd | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3375 | SourceLocation(), |
Chris Lattner | 9186f55 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3376 | &ParamInfo[0], ParamInfo.size(), |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3377 | /*TypeQuals*/0, |
Sebastian Redl | fb3f179 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3378 | /*exception*/false, |
| 3379 | SourceLocation(), false, 0, 0, 0, |
Argyrios Kyrtzidis | 20cf191 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3380 | LParenLoc, RLoc, D), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3381 | RLoc); |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3382 | } |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3383 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 3384 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3385 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3386 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3387 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3388 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 3389 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 3390 | SourceLocation StartLoc = ConsumeBracket(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3391 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3392 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 3393 | // This code does a fast path to handle some of the most obvious cases. |
| 3394 | if (Tok.getKind() == tok::r_square) { |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3395 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3396 | //FIXME: Use these |
| 3397 | CXX0XAttributeList Attr; |
| 3398 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) { |
| 3399 | Attr = ParseCXX0XAttributes(); |
| 3400 | } |
| 3401 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3402 | // Remember that we parsed the empty array type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3403 | ExprResult NumElements; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3404 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
| 3405 | StartLoc, EndLoc), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3406 | EndLoc); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3407 | return; |
| 3408 | } else if (Tok.getKind() == tok::numeric_constant && |
| 3409 | GetLookAheadToken(1).is(tok::r_square)) { |
| 3410 | // [4] is very common. Parse the numeric constant expression. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3411 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok)); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3412 | ConsumeToken(); |
| 3413 | |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3414 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3415 | //FIXME: Use these |
| 3416 | CXX0XAttributeList Attr; |
| 3417 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) { |
| 3418 | Attr = ParseCXX0XAttributes(); |
| 3419 | } |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3420 | |
| 3421 | // If there was an error parsing the assignment-expression, recover. |
| 3422 | if (ExprRes.isInvalid()) |
| 3423 | ExprRes.release(); // Deallocate expr, just use []. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3424 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3425 | // Remember that we parsed a array type, and remember its features. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3426 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(), |
| 3427 | StartLoc, EndLoc), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3428 | EndLoc); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3429 | return; |
| 3430 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 3432 | // If valid, this location is the position where we read the 'static' keyword. |
| 3433 | SourceLocation StaticLoc; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3434 | if (Tok.is(tok::kw_static)) |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 3435 | StaticLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 3437 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3438 | // Type qualifiers in an array subscript are a C99 feature. |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 3439 | DeclSpec DS; |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3440 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3441 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 3442 | // If we haven't already read 'static', check to see if there is one after the |
| 3443 | // type-qualifier-list. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3444 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 3445 | StaticLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3446 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 3447 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 3448 | bool isStar = false; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3449 | ExprResult NumElements; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3450 | |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 3451 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 3452 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 3453 | // the the token after the star is a ']'. Since stars in arrays are |
| 3454 | // infrequent, use of lookahead is not costly here. |
| 3455 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | c439f0d | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 3456 | ConsumeToken(); // Eat the '*'. |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 3457 | |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3458 | if (StaticLoc.isValid()) { |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 3459 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3460 | StaticLoc = SourceLocation(); // Drop the static. |
| 3461 | } |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 3462 | isStar = true; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3463 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3464 | // Note, in C89, this production uses the constant-expr production instead |
| 3465 | // of assignment-expr. The only difference is that assignment-expr allows |
| 3466 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 3467 | // are not i-c-e's, so we don't need to distinguish between the two here. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3468 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3469 | // Parse the constant-expression or assignment-expression now (depending |
| 3470 | // on dialect). |
| 3471 | if (getLang().CPlusPlus) |
| 3472 | NumElements = ParseConstantExpression(); |
| 3473 | else |
| 3474 | NumElements = ParseAssignmentExpression(); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 3475 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3476 | |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 3477 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3478 | if (NumElements.isInvalid()) { |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 3479 | D.setInvalidType(true); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 3480 | // If the expression was invalid, skip it. |
| 3481 | SkipUntil(tok::r_square); |
| 3482 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 3483 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3484 | |
| 3485 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
| 3486 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3487 | //FIXME: Use these |
| 3488 | CXX0XAttributeList Attr; |
| 3489 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) { |
| 3490 | Attr = ParseCXX0XAttributes(); |
| 3491 | } |
| 3492 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3493 | // Remember that we parsed a array type, and remember its features. |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 3494 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
| 3495 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3496 | NumElements.release(), |
| 3497 | StartLoc, EndLoc), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3498 | EndLoc); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 3501 | /// [GNU] typeof-specifier: |
| 3502 | /// typeof ( expressions ) |
| 3503 | /// typeof ( type-name ) |
| 3504 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3505 | /// |
| 3506 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3507 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3508 | Token OpTok = Tok; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3509 | SourceLocation StartLoc = ConsumeToken(); |
| 3510 | |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 3511 | const bool hasParens = Tok.is(tok::l_paren); |
| 3512 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3513 | bool isCastExpr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3514 | ParsedType CastTy; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3515 | SourceRange CastRange; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3516 | ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok, |
John McCall | 6caebb1 | 2010-08-25 02:45:51 +0000 | [diff] [blame] | 3517 | isCastExpr, |
| 3518 | CastTy, |
| 3519 | CastRange); |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 3520 | if (hasParens) |
| 3521 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3522 | |
| 3523 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 3524 | // FIXME: Not accurate, the range gets one token more than it should. |
| 3525 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3526 | else |
| 3527 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3528 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3529 | if (isCastExpr) { |
| 3530 | if (!CastTy) { |
| 3531 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 3532 | return; |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 3533 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 3534 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3535 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3536 | unsigned DiagID; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3537 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 3538 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3539 | DiagID, CastTy)) |
| 3540 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3541 | return; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 3542 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 3543 | |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 3544 | // If we get here, the operand to the typeof was an expresion. |
| 3545 | if (Operand.isInvalid()) { |
| 3546 | DS.SetTypeSpecError(); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 3547 | return; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3548 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 3549 | |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 3550 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3551 | unsigned DiagID; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 3552 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 3553 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3554 | DiagID, Operand.get())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3555 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3556 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 3557 | |
| 3558 | |
| 3559 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 3560 | /// from TryAltiVecVectorToken. |
| 3561 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 3562 | Token Next = NextToken(); |
| 3563 | switch (Next.getKind()) { |
| 3564 | default: return false; |
| 3565 | case tok::kw_short: |
| 3566 | case tok::kw_long: |
| 3567 | case tok::kw_signed: |
| 3568 | case tok::kw_unsigned: |
| 3569 | case tok::kw_void: |
| 3570 | case tok::kw_char: |
| 3571 | case tok::kw_int: |
| 3572 | case tok::kw_float: |
| 3573 | case tok::kw_double: |
| 3574 | case tok::kw_bool: |
| 3575 | case tok::kw___pixel: |
| 3576 | Tok.setKind(tok::kw___vector); |
| 3577 | return true; |
| 3578 | case tok::identifier: |
| 3579 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 3580 | Tok.setKind(tok::kw___vector); |
| 3581 | return true; |
| 3582 | } |
| 3583 | return false; |
| 3584 | } |
| 3585 | } |
| 3586 | |
| 3587 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 3588 | const char *&PrevSpec, unsigned &DiagID, |
| 3589 | bool &isInvalid) { |
| 3590 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 3591 | Token Next = NextToken(); |
| 3592 | switch (Next.getKind()) { |
| 3593 | case tok::kw_short: |
| 3594 | case tok::kw_long: |
| 3595 | case tok::kw_signed: |
| 3596 | case tok::kw_unsigned: |
| 3597 | case tok::kw_void: |
| 3598 | case tok::kw_char: |
| 3599 | case tok::kw_int: |
| 3600 | case tok::kw_float: |
| 3601 | case tok::kw_double: |
| 3602 | case tok::kw_bool: |
| 3603 | case tok::kw___pixel: |
| 3604 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 3605 | return true; |
| 3606 | case tok::identifier: |
| 3607 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 3608 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 3609 | return true; |
| 3610 | } |
| 3611 | break; |
| 3612 | default: |
| 3613 | break; |
| 3614 | } |
Douglas Gregor | 9938e3b | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 3615 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 3616 | DS.isTypeAltiVecVector()) { |
| 3617 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 3618 | return true; |
| 3619 | } |
| 3620 | return false; |
| 3621 | } |
| 3622 | |