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