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