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