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