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