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 | 288e86ff1 | 2006-11-11 23:03:42 +0000 | [diff] [blame] | 15 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Scope.h" |
Chris Lattner | ad9ac94 | 2007-01-23 01:14:52 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | // C99 6.7: Declarations. |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 24 | /// ParseTypeName |
| 25 | /// type-name: [C99 6.7.6] |
| 26 | /// specifier-qualifier-list abstract-declarator[opt] |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 27 | Parser::TypeTy *Parser::ParseTypeName() { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 28 | // Parse the common declaration-specifiers piece. |
| 29 | DeclSpec DS; |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 30 | ParseSpecifierQualifierList(DS); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 31 | |
| 32 | // Parse the abstract-declarator, if present. |
| 33 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 34 | ParseDeclarator(DeclaratorInfo); |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 35 | |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 36 | return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 39 | /// ParseAttributes - Parse a non-empty attributes list. |
| 40 | /// |
| 41 | /// [GNU] attributes: |
| 42 | /// attribute |
| 43 | /// attributes attribute |
| 44 | /// |
| 45 | /// [GNU] attribute: |
| 46 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 47 | /// |
| 48 | /// [GNU] attribute-list: |
| 49 | /// attrib |
| 50 | /// attribute_list ',' attrib |
| 51 | /// |
| 52 | /// [GNU] attrib: |
| 53 | /// empty |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 54 | /// attrib-name |
| 55 | /// attrib-name '(' identifier ')' |
| 56 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 57 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 58 | /// |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 59 | /// [GNU] attrib-name: |
| 60 | /// identifier |
| 61 | /// typespec |
| 62 | /// typequal |
| 63 | /// storageclass |
| 64 | /// |
| 65 | /// FIXME: The GCC grammar/code for this construct implies we need two |
| 66 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 67 | /// which is followed by a comma or close parenthesis, then the arguments |
| 68 | /// start with that identifier; otherwise they are an expression list." |
| 69 | /// |
| 70 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 71 | /// any attributes that don't work (based on my limited testing). Most |
| 72 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 73 | /// a pressing need to implement the 2 token lookahead. |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 74 | |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 75 | AttributeList *Parser::ParseAttributes() { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 76 | assert(Tok.is(tok::kw___attribute) && "Not an attribute list!"); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 77 | |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 78 | AttributeList *CurrAttr = 0; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 80 | while (Tok.is(tok::kw___attribute)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 81 | ConsumeToken(); |
| 82 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 83 | "attribute")) { |
| 84 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 85 | return CurrAttr; |
| 86 | } |
| 87 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 88 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 89 | return CurrAttr; |
| 90 | } |
| 91 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 92 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 93 | Tok.is(tok::comma)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 95 | if (Tok.is(tok::comma)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 96 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 97 | ConsumeToken(); |
| 98 | continue; |
| 99 | } |
| 100 | // we have an identifier or declaration specifier (const, int, etc.) |
| 101 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 102 | SourceLocation AttrNameLoc = ConsumeToken(); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 103 | |
| 104 | // check if we have a "paramterized" attribute |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 105 | if (Tok.is(tok::l_paren)) { |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 106 | ConsumeParen(); // ignore the left paren loc for now |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 108 | if (Tok.is(tok::identifier)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 109 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 110 | SourceLocation ParmLoc = ConsumeToken(); |
| 111 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 112 | if (Tok.is(tok::r_paren)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 113 | // __attribute__(( mode(byte) )) |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 114 | ConsumeParen(); // ignore the right paren loc for now |
| 115 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 116 | ParmName, ParmLoc, 0, 0, CurrAttr); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 117 | } else if (Tok.is(tok::comma)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 118 | ConsumeToken(); |
| 119 | // __attribute__(( format(printf, 1, 2) )) |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 120 | llvm::SmallVector<ExprTy*, 8> ArgExprs; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 121 | bool ArgExprsOk = true; |
| 122 | |
| 123 | // now parse the non-empty comma separated list of expressions |
| 124 | while (1) { |
| 125 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 126 | if (ArgExpr.isInvalid) { |
| 127 | ArgExprsOk = false; |
| 128 | SkipUntil(tok::r_paren); |
| 129 | break; |
| 130 | } else { |
| 131 | ArgExprs.push_back(ArgExpr.Val); |
| 132 | } |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 133 | if (Tok.isNot(tok::comma)) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 134 | break; |
| 135 | ConsumeToken(); // Eat the comma, move to the next argument |
| 136 | } |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 137 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 138 | ConsumeParen(); // ignore the right paren loc for now |
| 139 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName, |
| 140 | ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } else { // not an identifier |
| 144 | // parse a possibly empty comma separated list of expressions |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 145 | if (Tok.is(tok::r_paren)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 146 | // __attribute__(( nonnull() )) |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 147 | ConsumeParen(); // ignore the right paren loc for now |
| 148 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 149 | 0, SourceLocation(), 0, 0, CurrAttr); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 150 | } else { |
| 151 | // __attribute__(( aligned(16) )) |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 152 | llvm::SmallVector<ExprTy*, 8> ArgExprs; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 153 | bool ArgExprsOk = true; |
| 154 | |
| 155 | // now parse the list of expressions |
| 156 | while (1) { |
| 157 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 158 | if (ArgExpr.isInvalid) { |
| 159 | ArgExprsOk = false; |
| 160 | SkipUntil(tok::r_paren); |
| 161 | break; |
| 162 | } else { |
| 163 | ArgExprs.push_back(ArgExpr.Val); |
| 164 | } |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 165 | if (Tok.isNot(tok::comma)) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 166 | break; |
| 167 | ConsumeToken(); // Eat the comma, move to the next argument |
| 168 | } |
| 169 | // Match the ')'. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 170 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 171 | ConsumeParen(); // ignore the right paren loc for now |
| 172 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
| 173 | SourceLocation(), &ArgExprs[0], ArgExprs.size(), |
| 174 | CurrAttr); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |
| 178 | } else { |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 179 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 180 | 0, SourceLocation(), 0, 0, CurrAttr); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
Steve Naroff | 98d153c | 2007-06-06 23:19:11 +0000 | [diff] [blame] | 183 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 184 | SkipUntil(tok::r_paren, false); |
| 185 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 186 | SkipUntil(tok::r_paren, false); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 187 | } |
| 188 | return CurrAttr; |
| 189 | } |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 191 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 192 | /// declaration-specifiers, some number of declarators, and a semicolon. |
| 193 | /// 'Context' should be a Declarator::TheContext value. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 194 | /// |
| 195 | /// declaration: [C99 6.7] |
| 196 | /// block-declaration -> |
| 197 | /// simple-declaration |
| 198 | /// others [FIXME] |
| 199 | /// [C++] namespace-definition |
| 200 | /// others... [FIXME] |
| 201 | /// |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 202 | Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) { |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 203 | switch (Tok.getKind()) { |
| 204 | case tok::kw_namespace: |
| 205 | return ParseNamespace(Context); |
| 206 | default: |
| 207 | return ParseSimpleDeclaration(Context); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 212 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 213 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 214 | /// [OMP] threadprivate-directive [TODO] |
| 215 | Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 216 | // Parse the common declaration-specifiers piece. |
| 217 | DeclSpec DS; |
| 218 | ParseDeclarationSpecifiers(DS); |
| 219 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 220 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 221 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 222 | if (Tok.is(tok::semi)) { |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 223 | ConsumeToken(); |
Chris Lattner | 200bdc3 | 2006-11-19 02:43:37 +0000 | [diff] [blame] | 224 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 227 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 228 | ParseDeclarator(DeclaratorInfo); |
| 229 | |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 230 | return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 233 | |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 234 | /// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after |
| 235 | /// parsing 'declaration-specifiers declarator'. This method is split out this |
| 236 | /// way to handle the ambiguity between top-level function-definitions and |
| 237 | /// declarations. |
| 238 | /// |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 239 | /// init-declarator-list: [C99 6.7] |
| 240 | /// init-declarator |
| 241 | /// init-declarator-list ',' init-declarator |
| 242 | /// init-declarator: [C99 6.7] |
| 243 | /// declarator |
| 244 | /// declarator '=' initializer |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 245 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 246 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 247 | /// |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 248 | Parser::DeclTy *Parser:: |
| 249 | ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { |
| 250 | |
| 251 | // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so |
| 252 | // that they can be chained properly if the actions want this. |
| 253 | Parser::DeclTy *LastDeclInGroup = 0; |
| 254 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 255 | // At this point, we know that it is not a function definition. Parse the |
| 256 | // rest of the init-declarator-list. |
| 257 | while (1) { |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 258 | // If a simple-asm-expr is present, parse it. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 259 | if (Tok.is(tok::kw_asm)) |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 260 | ParseSimpleAsm(); |
| 261 | |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 262 | // If attributes are present, parse them. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 263 | if (Tok.is(tok::kw___attribute)) |
Steve Naroff | 0f05a7a | 2007-06-09 23:38:17 +0000 | [diff] [blame] | 264 | D.AddAttributes(ParseAttributes()); |
Steve Naroff | 6109140 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 265 | |
| 266 | // Inform the current actions module that we just parsed this declarator. |
| 267 | // FIXME: pass asm & attributes. |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 268 | LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup); |
Steve Naroff | 6109140 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 270 | // Parse declarator '=' initializer. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 271 | ExprResult Init; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 272 | if (Tok.is(tok::equal)) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 273 | ConsumeToken(); |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 274 | Init = ParseInitializer(); |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 275 | if (Init.isInvalid) { |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 276 | SkipUntil(tok::semi); |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 277 | return 0; |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 278 | } |
Steve Naroff | 6109140 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 279 | Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 282 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 283 | // error, bail out. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 284 | if (Tok.isNot(tok::comma)) |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 285 | break; |
| 286 | |
| 287 | // Consume the comma. |
| 288 | ConsumeToken(); |
| 289 | |
| 290 | // Parse the next declarator. |
| 291 | D.clear(); |
| 292 | ParseDeclarator(D); |
| 293 | } |
| 294 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 295 | if (Tok.is(tok::semi)) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 296 | ConsumeToken(); |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 297 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 298 | } |
Fariborz Jahanian | e908cab | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 299 | // If this is an ObjC2 for-each loop, this is a successful declarator |
| 300 | // parse. The syntax for these looks like: |
| 301 | // 'for' '(' declaration 'in' expr ')' statement |
Fariborz Jahanian | 3622e59 | 2008-01-04 23:04:08 +0000 | [diff] [blame] | 302 | if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) { |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 303 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 304 | } |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 305 | Diag(Tok, diag::err_parse_error); |
| 306 | // Skip to end of block or statement |
Chris Lattner | 43ba251 | 2007-08-21 18:36:18 +0000 | [diff] [blame] | 307 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 308 | if (Tok.is(tok::semi)) |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 309 | ConsumeToken(); |
| 310 | return 0; |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 313 | /// ParseSpecifierQualifierList |
| 314 | /// specifier-qualifier-list: |
| 315 | /// type-specifier specifier-qualifier-list[opt] |
| 316 | /// type-qualifier specifier-qualifier-list[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 317 | /// [GNU] attributes specifier-qualifier-list[opt] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 318 | /// |
| 319 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 320 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 321 | /// parse declaration-specifiers and complain about extra stuff. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 322 | ParseDeclarationSpecifiers(DS); |
| 323 | |
| 324 | // Validate declspec for type-name. |
| 325 | unsigned Specs = DS.getParsedSpecifiers(); |
| 326 | if (Specs == DeclSpec::PQ_None) |
| 327 | Diag(Tok, diag::err_typename_requires_specqual); |
| 328 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 329 | // Issue diagnostic and remove storage class if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 330 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 331 | if (DS.getStorageClassSpecLoc().isValid()) |
| 332 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 333 | else |
| 334 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 335 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 336 | } |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 337 | |
| 338 | // Issue diagnostic and remove function specfier if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 339 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 340 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 341 | DS.ClearFunctionSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 344 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 345 | /// ParseDeclarationSpecifiers |
| 346 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 347 | /// storage-class-specifier declaration-specifiers[opt] |
| 348 | /// type-specifier declaration-specifiers[opt] |
| 349 | /// type-qualifier declaration-specifiers[opt] |
| 350 | /// [C99] function-specifier declaration-specifiers[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 351 | /// [GNU] attributes declaration-specifiers[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 352 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 353 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 354 | /// 'typedef' |
| 355 | /// 'extern' |
| 356 | /// 'static' |
| 357 | /// 'auto' |
| 358 | /// 'register' |
| 359 | /// [GNU] '__thread' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 360 | /// type-specifier: [C99 6.7.2] |
| 361 | /// 'void' |
| 362 | /// 'char' |
| 363 | /// 'short' |
| 364 | /// 'int' |
| 365 | /// 'long' |
| 366 | /// 'float' |
| 367 | /// 'double' |
| 368 | /// 'signed' |
| 369 | /// 'unsigned' |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 370 | /// struct-or-union-specifier |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 371 | /// enum-specifier |
Chris Lattner | 3b4fdda3 | 2006-08-14 00:45:39 +0000 | [diff] [blame] | 372 | /// typedef-name |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 373 | /// [C++] 'bool' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 374 | /// [C99] '_Bool' |
| 375 | /// [C99] '_Complex' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 376 | /// [C99] '_Imaginary' // Removed in TC2? |
| 377 | /// [GNU] '_Decimal32' |
| 378 | /// [GNU] '_Decimal64' |
| 379 | /// [GNU] '_Decimal128' |
Steve Naroff | 872da80 | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 380 | /// [GNU] typeof-specifier |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 381 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
Steve Naroff | 7e901fd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 382 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 383 | /// type-qualifier: |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 384 | /// 'const' |
| 385 | /// 'volatile' |
| 386 | /// [C99] 'restrict' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 387 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 388 | /// [C99] 'inline' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 389 | /// |
| 390 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) { |
Chris Lattner | 02c0439 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 391 | DS.Range.setBegin(Tok.getLocation()); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 392 | while (1) { |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 393 | int isInvalid = false; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 394 | const char *PrevSpec = 0; |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 395 | SourceLocation Loc = Tok.getLocation(); |
| 396 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 397 | switch (Tok.getKind()) { |
Chris Lattner | 3b4fdda3 | 2006-08-14 00:45:39 +0000 | [diff] [blame] | 398 | // typedef-name |
| 399 | case tok::identifier: |
| 400 | // This identifier can only be a typedef name if we haven't already seen |
Chris Lattner | 5646b3e | 2006-08-15 05:12:01 +0000 | [diff] [blame] | 401 | // a type-specifier. Without this check we misparse: |
| 402 | // typedef int X; struct Y { short X; }; as 'short int'. |
Chris Lattner | f055d43 | 2006-11-28 04:28:12 +0000 | [diff] [blame] | 403 | if (!DS.hasTypeSpecifier()) { |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 404 | // It has to be available as a typedef too! |
| 405 | if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), |
| 406 | CurScope)) { |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 407 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 408 | TypeRep); |
Steve Naroff | 7e901fd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 409 | if (isInvalid) |
| 410 | break; |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 411 | // FIXME: restrict this to "id" and ObjC classnames. |
| 412 | DS.Range.setEnd(Tok.getLocation()); |
| 413 | ConsumeToken(); // The identifier |
| 414 | if (Tok.is(tok::less)) { |
Steve Naroff | c548404 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 415 | SourceLocation endProtoLoc; |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 416 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
Steve Naroff | c548404 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 417 | ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 418 | llvm::SmallVector<DeclTy *, 8> *ProtocolDecl = |
| 419 | new llvm::SmallVector<DeclTy *, 8>; |
| 420 | DS.setProtocolQualifiers(ProtocolDecl); |
| 421 | Actions.FindProtocolDeclaration(Loc, |
| 422 | &ProtocolRefs[0], ProtocolRefs.size(), |
| 423 | *ProtocolDecl); |
Steve Naroff | 7e901fd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 424 | } |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 425 | continue; |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 426 | } |
Chris Lattner | 3b4fdda3 | 2006-08-14 00:45:39 +0000 | [diff] [blame] | 427 | } |
| 428 | // FALL THROUGH. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 429 | default: |
| 430 | // If this is not a declaration specifier token, we're done reading decl |
| 431 | // specifiers. First verify that DeclSpec's are consistent. |
Ted Kremenek | d4e5fba | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 432 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 433 | return; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 434 | |
| 435 | // GNU attributes support. |
| 436 | case tok::kw___attribute: |
Steve Naroff | 0f05a7a | 2007-06-09 23:38:17 +0000 | [diff] [blame] | 437 | DS.AddAttributes(ParseAttributes()); |
Chris Lattner | b95cca0 | 2006-10-17 03:01:08 +0000 | [diff] [blame] | 438 | continue; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 439 | |
| 440 | // storage-class-specifier |
| 441 | case tok::kw_typedef: |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 442 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 443 | break; |
| 444 | case tok::kw_extern: |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 445 | if (DS.isThreadSpecified()) |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 446 | Diag(Tok, diag::ext_thread_before, "extern"); |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 447 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 448 | break; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 449 | case tok::kw___private_extern__: |
Steve Naroff | fda8209 | 2008-01-25 22:14:40 +0000 | [diff] [blame] | 450 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, PrevSpec); |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 451 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 452 | case tok::kw_static: |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 453 | if (DS.isThreadSpecified()) |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 454 | Diag(Tok, diag::ext_thread_before, "static"); |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 455 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 456 | break; |
| 457 | case tok::kw_auto: |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 458 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 459 | break; |
| 460 | case tok::kw_register: |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 461 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 462 | break; |
| 463 | case tok::kw___thread: |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 464 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 465 | break; |
| 466 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 467 | // type-specifiers |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 468 | case tok::kw_short: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 469 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 470 | break; |
| 471 | case tok::kw_long: |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 472 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 473 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 474 | else |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 475 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 476 | break; |
| 477 | case tok::kw_signed: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 478 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 479 | break; |
| 480 | case tok::kw_unsigned: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 481 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 482 | break; |
| 483 | case tok::kw__Complex: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 484 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 485 | break; |
| 486 | case tok::kw__Imaginary: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 487 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 488 | break; |
| 489 | case tok::kw_void: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 490 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 491 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 492 | case tok::kw_char: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 493 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 494 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 495 | case tok::kw_int: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 496 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 497 | break; |
| 498 | case tok::kw_float: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 499 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 500 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 501 | case tok::kw_double: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 502 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 503 | break; |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 504 | case tok::kw_bool: // [C++ 2.11p1] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 505 | case tok::kw__Bool: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 506 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 507 | break; |
| 508 | case tok::kw__Decimal32: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 509 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 510 | break; |
| 511 | case tok::kw__Decimal64: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 512 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 513 | break; |
| 514 | case tok::kw__Decimal128: |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 515 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 516 | break; |
| 517 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 518 | case tok::kw_struct: |
| 519 | case tok::kw_union: |
| 520 | ParseStructUnionSpecifier(DS); |
| 521 | continue; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 522 | case tok::kw_enum: |
| 523 | ParseEnumSpecifier(DS); |
| 524 | continue; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 525 | |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 526 | // GNU typeof support. |
| 527 | case tok::kw_typeof: |
| 528 | ParseTypeofSpecifier(DS); |
| 529 | continue; |
| 530 | |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 531 | // type-qualifier |
| 532 | case tok::kw_const: |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 533 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 534 | getLang())*2; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 535 | break; |
| 536 | case tok::kw_volatile: |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 537 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 538 | getLang())*2; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 539 | break; |
| 540 | case tok::kw_restrict: |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 541 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 542 | getLang())*2; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 543 | break; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 544 | |
| 545 | // function-specifier |
| 546 | case tok::kw_inline: |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 547 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 548 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 549 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 550 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 551 | if (isInvalid) { |
| 552 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 553 | if (isInvalid == 1) // Error. |
| 554 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 555 | else // extwarn. |
| 556 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 557 | } |
Chris Lattner | 02c0439 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 558 | DS.Range.setEnd(Tok.getLocation()); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 559 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 563 | /// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where |
| 564 | /// the first token has already been read and has been turned into an instance |
| 565 | /// of DeclSpec::TST (TagType). This returns true if there is an error parsing, |
| 566 | /// otherwise it returns false and fills in Decl. |
| 567 | bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){ |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 568 | AttributeList *Attr = 0; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 569 | // If attributes exist after tag, parse them. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 570 | if (Tok.is(tok::kw___attribute)) |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 571 | Attr = ParseAttributes(); |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 572 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 573 | // Must have either 'struct name' or 'struct {...}'. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 574 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 575 | Diag(Tok, diag::err_expected_ident_lbrace); |
Chris Lattner | 02c0439 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 576 | |
| 577 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 578 | SkipUntil(tok::comma, true); |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 579 | return true; |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Chris Lattner | 8c6519a | 2007-01-22 07:41:36 +0000 | [diff] [blame] | 582 | // If an identifier is present, consume and remember it. |
| 583 | IdentifierInfo *Name = 0; |
| 584 | SourceLocation NameLoc; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 585 | if (Tok.is(tok::identifier)) { |
Chris Lattner | 8c6519a | 2007-01-22 07:41:36 +0000 | [diff] [blame] | 586 | Name = Tok.getIdentifierInfo(); |
| 587 | NameLoc = ConsumeToken(); |
| 588 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 8c6519a | 2007-01-22 07:41:36 +0000 | [diff] [blame] | 590 | // There are three options here. If we have 'struct foo;', then this is a |
| 591 | // forward declaration. If we have 'struct foo {...' then this is a |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 592 | // definition. Otherwise we have something like 'struct foo xyz', a reference. |
Chris Lattner | 8799cf2 | 2007-01-23 01:57:16 +0000 | [diff] [blame] | 593 | // |
| 594 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 595 | // struct foo {..}; void bar() { struct foo; } <- new foo in bar. |
| 596 | // struct foo {..}; void bar() { struct foo x; } <- use of old foo. |
| 597 | // |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 598 | Action::TagKind TK; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 599 | if (Tok.is(tok::l_brace)) |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 600 | TK = Action::TK_Definition; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 601 | else if (Tok.is(tok::semi)) |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 602 | TK = Action::TK_Declaration; |
| 603 | else |
| 604 | TK = Action::TK_Reference; |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 605 | Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr); |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 606 | return false; |
| 607 | } |
| 608 | |
| 609 | |
| 610 | /// ParseStructUnionSpecifier |
| 611 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 612 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 613 | /// struct-or-union identifier |
| 614 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 615 | /// '}' attributes[opt] |
| 616 | /// [GNU] struct-or-union attributes[opt] identifier |
| 617 | /// struct-or-union: |
| 618 | /// 'struct' |
| 619 | /// 'union' |
| 620 | /// |
| 621 | void Parser::ParseStructUnionSpecifier(DeclSpec &DS) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 622 | assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) && |
| 623 | "Not a struct/union specifier"); |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 624 | DeclSpec::TST TagType = |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 625 | Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct; |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 626 | SourceLocation StartLoc = ConsumeToken(); |
| 627 | |
| 628 | // Parse the tag portion of this. |
| 629 | DeclTy *TagDecl; |
| 630 | if (ParseTag(TagDecl, TagType, StartLoc)) |
| 631 | return; |
Chris Lattner | bf0b798 | 2007-01-23 04:27:41 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 633 | // If there is a body, parse it and inform the actions module. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 634 | if (Tok.is(tok::l_brace)) |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 635 | ParseStructUnionBody(StartLoc, TagType, TagDecl); |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 636 | |
| 637 | const char *PrevSpec = 0; |
Chris Lattner | b9d572a | 2007-01-23 04:58:34 +0000 | [diff] [blame] | 638 | if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl)) |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 639 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 642 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 643 | /// semicolon. |
| 644 | /// |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 645 | /// struct-declaration: |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 646 | /// specifier-qualifier-list struct-declarator-list |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 647 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 648 | /// [GNU] specifier-qualifier-list |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 649 | /// struct-declarator-list: |
| 650 | /// struct-declarator |
| 651 | /// struct-declarator-list ',' struct-declarator |
| 652 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 653 | /// struct-declarator: |
| 654 | /// declarator |
| 655 | /// [GNU] declarator attributes[opt] |
| 656 | /// declarator[opt] ':' constant-expression |
| 657 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 658 | /// |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 659 | void Parser::ParseStructDeclaration(DeclTy *TagDecl, |
Steve Naroff | 4ca5d5a | 2007-08-28 16:31:47 +0000 | [diff] [blame] | 660 | llvm::SmallVectorImpl<DeclTy*> &FieldDecls) { |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 661 | // FIXME: When __extension__ is specified, disable extension diagnostics. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 662 | if (Tok.is(tok::kw___extension__)) |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 663 | ConsumeToken(); |
| 664 | |
| 665 | // Parse the common specifier-qualifiers-list piece. |
| 666 | DeclSpec DS; |
| 667 | SourceLocation SpecQualLoc = Tok.getLocation(); |
| 668 | ParseSpecifierQualifierList(DS); |
| 669 | // TODO: Does specifier-qualifier list correctly check that *something* is |
| 670 | // specified? |
| 671 | |
| 672 | // If there are no declarators, issue a warning. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 673 | if (Tok.is(tok::semi)) { |
Steve Naroff | bff7385 | 2008-02-11 22:40:08 +0000 | [diff] [blame] | 674 | Diag(SpecQualLoc, diag::w_no_declarators); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 675 | return; |
| 676 | } |
| 677 | |
| 678 | // Read struct-declarators until we find the semicolon. |
| 679 | Declarator DeclaratorInfo(DS, Declarator::MemberContext); |
| 680 | |
| 681 | while (1) { |
| 682 | /// struct-declarator: declarator |
| 683 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 684 | if (Tok.isNot(tok::colon)) |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 685 | ParseDeclarator(DeclaratorInfo); |
| 686 | |
| 687 | ExprTy *BitfieldSize = 0; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 688 | if (Tok.is(tok::colon)) { |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 689 | ConsumeToken(); |
| 690 | ExprResult Res = ParseConstantExpression(); |
| 691 | if (Res.isInvalid) { |
| 692 | SkipUntil(tok::semi, true, true); |
| 693 | } else { |
| 694 | BitfieldSize = Res.Val; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | // If attributes exist after the declarator, parse them. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 699 | if (Tok.is(tok::kw___attribute)) |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 700 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 701 | |
| 702 | // Install the declarator into the current TagDecl. |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 703 | DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, SpecQualLoc, |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 704 | DeclaratorInfo, BitfieldSize); |
| 705 | FieldDecls.push_back(Field); |
| 706 | |
| 707 | // If we don't have a comma, it is either the end of the list (a ';') |
| 708 | // or an error, bail out. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 709 | if (Tok.isNot(tok::comma)) |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 710 | return; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 711 | |
| 712 | // Consume the comma. |
| 713 | ConsumeToken(); |
| 714 | |
| 715 | // Parse the next declarator. |
| 716 | DeclaratorInfo.clear(); |
| 717 | |
| 718 | // Attributes are only allowed on the second declarator. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 719 | if (Tok.is(tok::kw___attribute)) |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 720 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 721 | } |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /// ParseStructUnionBody |
| 725 | /// struct-contents: |
| 726 | /// struct-declaration-list |
| 727 | /// [EXT] empty |
| 728 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 729 | /// struct-declaration-list: |
| 730 | /// struct-declaration |
| 731 | /// struct-declaration-list struct-declaration |
| 732 | /// [OBC] '@' 'defs' '(' class-name ')' [TODO] |
| 733 | /// |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 734 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
| 735 | unsigned TagType, DeclTy *TagDecl) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 736 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 737 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 738 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 739 | // C++. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 740 | if (Tok.is(tok::r_brace)) |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 741 | Diag(Tok, diag::ext_empty_struct_union_enum, |
| 742 | DeclSpec::getSpecifierName((DeclSpec::TST)TagType)); |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 743 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 744 | llvm::SmallVector<DeclTy*, 32> FieldDecls; |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 745 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 746 | // 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] | 747 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 748 | // Each iteration of this loop reads one struct-declaration. |
| 749 | |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 750 | // Check for extraneous top-level semicolon. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 751 | if (Tok.is(tok::semi)) { |
Chris Lattner | 36e46a2 | 2007-06-09 05:49:55 +0000 | [diff] [blame] | 752 | Diag(Tok, diag::ext_extra_struct_semi); |
| 753 | ConsumeToken(); |
| 754 | continue; |
| 755 | } |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 756 | ParseStructDeclaration(TagDecl, FieldDecls); |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 757 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 758 | if (Tok.is(tok::semi)) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 759 | ConsumeToken(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 760 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 0c7e82d | 2007-06-09 05:54:40 +0000 | [diff] [blame] | 761 | Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list); |
| 762 | break; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 763 | } else { |
| 764 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 765 | // Skip to end of block or statement |
| 766 | SkipUntil(tok::r_brace, true, true); |
| 767 | } |
| 768 | } |
| 769 | |
Steve Naroff | 33a1e80 | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 770 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 771 | |
Fariborz Jahanian | 343f709 | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 772 | Actions.ActOnFields(CurScope, |
Steve Naroff | 33a1e80 | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 773 | RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(), |
| 774 | LBraceLoc, RBraceLoc); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 775 | |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 776 | AttributeList *AttrList = 0; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 777 | // If attributes exist after struct contents, parse them. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 778 | if (Tok.is(tok::kw___attribute)) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 779 | AttrList = ParseAttributes(); // FIXME: where should I put them? |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 783 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 784 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 785 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
| 786 | /// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 787 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 788 | /// '}' attributes[opt] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 789 | /// 'enum' identifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 790 | /// [GNU] 'enum' attributes[opt] identifier |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 791 | void Parser::ParseEnumSpecifier(DeclSpec &DS) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 792 | assert(Tok.is(tok::kw_enum) && "Not an enum specifier"); |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 793 | SourceLocation StartLoc = ConsumeToken(); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 794 | |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 795 | // Parse the tag portion of this. |
| 796 | DeclTy *TagDecl; |
| 797 | if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc)) |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 798 | return; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 800 | if (Tok.is(tok::l_brace)) |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 801 | ParseEnumBody(StartLoc, TagDecl); |
| 802 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 803 | // TODO: semantic analysis on the declspec for enums. |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 804 | const char *PrevSpec = 0; |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 805 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl)) |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 806 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 809 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 810 | /// enumerator-list: |
| 811 | /// enumerator |
| 812 | /// enumerator-list ',' enumerator |
| 813 | /// enumerator: |
| 814 | /// enumeration-constant |
| 815 | /// enumeration-constant '=' constant-expression |
| 816 | /// enumeration-constant: |
| 817 | /// identifier |
| 818 | /// |
| 819 | void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) { |
| 820 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 821 | |
Chris Lattner | 37256fb | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 822 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 823 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 824 | Diag(Tok, diag::ext_empty_struct_union_enum, "enum"); |
| 825 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 826 | llvm::SmallVector<DeclTy*, 32> EnumConstantDecls; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 827 | |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 828 | DeclTy *LastEnumConstDecl = 0; |
| 829 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 830 | // Parse the enumerator-list. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 831 | while (Tok.is(tok::identifier)) { |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 832 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 833 | SourceLocation IdentLoc = ConsumeToken(); |
| 834 | |
| 835 | SourceLocation EqualLoc; |
| 836 | ExprTy *AssignedVal = 0; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 837 | if (Tok.is(tok::equal)) { |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 838 | EqualLoc = ConsumeToken(); |
| 839 | ExprResult Res = ParseConstantExpression(); |
| 840 | if (Res.isInvalid) |
Chris Lattner | da6c2ce | 2007-04-27 19:13:15 +0000 | [diff] [blame] | 841 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 842 | else |
| 843 | AssignedVal = Res.Val; |
| 844 | } |
| 845 | |
| 846 | // Install the enumerator constant into EnumDecl. |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 847 | DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl, |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 848 | LastEnumConstDecl, |
| 849 | IdentLoc, Ident, |
| 850 | EqualLoc, AssignedVal); |
| 851 | EnumConstantDecls.push_back(EnumConstDecl); |
| 852 | LastEnumConstDecl = EnumConstDecl; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 853 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 854 | if (Tok.isNot(tok::comma)) |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 855 | break; |
| 856 | SourceLocation CommaLoc = ConsumeToken(); |
| 857 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 858 | if (Tok.isNot(tok::identifier) && !getLang().C99) |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 859 | Diag(CommaLoc, diag::ext_c99_enumerator_list_comma); |
| 860 | } |
| 861 | |
| 862 | // Eat the }. |
| 863 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 864 | |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 865 | Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0], |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 866 | EnumConstantDecls.size()); |
| 867 | |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 868 | DeclTy *AttrList = 0; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 869 | // If attributes exist after the identifier list, parse them. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 870 | if (Tok.is(tok::kw___attribute)) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 871 | AttrList = ParseAttributes(); // FIXME: where do they do? |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 872 | } |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 873 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 874 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame^] | 875 | /// start of a type-qualifier-list. |
| 876 | bool Parser::isTypeQualifier() const { |
| 877 | switch (Tok.getKind()) { |
| 878 | default: return false; |
| 879 | // type-qualifier |
| 880 | case tok::kw_const: |
| 881 | case tok::kw_volatile: |
| 882 | case tok::kw_restrict: |
| 883 | return true; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 888 | /// start of a specifier-qualifier-list. |
| 889 | bool Parser::isTypeSpecifierQualifier() const { |
| 890 | switch (Tok.getKind()) { |
| 891 | default: return false; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 892 | // GNU attributes support. |
| 893 | case tok::kw___attribute: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 894 | // GNU typeof support. |
| 895 | case tok::kw_typeof: |
| 896 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 897 | // type-specifiers |
| 898 | case tok::kw_short: |
| 899 | case tok::kw_long: |
| 900 | case tok::kw_signed: |
| 901 | case tok::kw_unsigned: |
| 902 | case tok::kw__Complex: |
| 903 | case tok::kw__Imaginary: |
| 904 | case tok::kw_void: |
| 905 | case tok::kw_char: |
| 906 | case tok::kw_int: |
| 907 | case tok::kw_float: |
| 908 | case tok::kw_double: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 909 | case tok::kw_bool: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 910 | case tok::kw__Bool: |
| 911 | case tok::kw__Decimal32: |
| 912 | case tok::kw__Decimal64: |
| 913 | case tok::kw__Decimal128: |
| 914 | |
| 915 | // struct-or-union-specifier |
| 916 | case tok::kw_struct: |
| 917 | case tok::kw_union: |
| 918 | // enum-specifier |
| 919 | case tok::kw_enum: |
| 920 | |
| 921 | // type-qualifier |
| 922 | case tok::kw_const: |
| 923 | case tok::kw_volatile: |
| 924 | case tok::kw_restrict: |
| 925 | return true; |
| 926 | |
| 927 | // typedef-name |
| 928 | case tok::identifier: |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 929 | return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 933 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 934 | /// declaration specifier. |
| 935 | bool Parser::isDeclarationSpecifier() const { |
| 936 | switch (Tok.getKind()) { |
| 937 | default: return false; |
| 938 | // storage-class-specifier |
| 939 | case tok::kw_typedef: |
| 940 | case tok::kw_extern: |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 941 | case tok::kw___private_extern__: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 942 | case tok::kw_static: |
| 943 | case tok::kw_auto: |
| 944 | case tok::kw_register: |
| 945 | case tok::kw___thread: |
| 946 | |
| 947 | // type-specifiers |
| 948 | case tok::kw_short: |
| 949 | case tok::kw_long: |
| 950 | case tok::kw_signed: |
| 951 | case tok::kw_unsigned: |
| 952 | case tok::kw__Complex: |
| 953 | case tok::kw__Imaginary: |
| 954 | case tok::kw_void: |
| 955 | case tok::kw_char: |
| 956 | case tok::kw_int: |
| 957 | case tok::kw_float: |
| 958 | case tok::kw_double: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 959 | case tok::kw_bool: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 960 | case tok::kw__Bool: |
| 961 | case tok::kw__Decimal32: |
| 962 | case tok::kw__Decimal64: |
| 963 | case tok::kw__Decimal128: |
| 964 | |
| 965 | // struct-or-union-specifier |
| 966 | case tok::kw_struct: |
| 967 | case tok::kw_union: |
| 968 | // enum-specifier |
| 969 | case tok::kw_enum: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 970 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 971 | // type-qualifier |
| 972 | case tok::kw_const: |
| 973 | case tok::kw_volatile: |
| 974 | case tok::kw_restrict: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 975 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 976 | // function-specifier |
| 977 | case tok::kw_inline: |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 978 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 979 | // GNU typeof support. |
| 980 | case tok::kw_typeof: |
| 981 | |
| 982 | // GNU attributes. |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 983 | case tok::kw___attribute: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 984 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 985 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 986 | // typedef-name |
| 987 | case tok::identifier: |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 988 | return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 992 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 993 | /// ParseTypeQualifierListOpt |
| 994 | /// type-qualifier-list: [C99 6.7.5] |
| 995 | /// type-qualifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 996 | /// [GNU] attributes |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 997 | /// type-qualifier-list type-qualifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 998 | /// [GNU] type-qualifier-list attributes |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 999 | /// |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1000 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) { |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1001 | while (1) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1002 | int isInvalid = false; |
| 1003 | const char *PrevSpec = 0; |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 1004 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1005 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1006 | switch (Tok.getKind()) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1007 | default: |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1008 | // If this is not a type-qualifier token, we're done reading type |
| 1009 | // qualifiers. First verify that DeclSpec's are consistent. |
Ted Kremenek | d4e5fba | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 1010 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1011 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1012 | case tok::kw_const: |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 1013 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 1014 | getLang())*2; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1015 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1016 | case tok::kw_volatile: |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 1017 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 1018 | getLang())*2; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1019 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1020 | case tok::kw_restrict: |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 1021 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 1022 | getLang())*2; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1023 | break; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1024 | case tok::kw___attribute: |
Steve Naroff | 0f05a7a | 2007-06-09 23:38:17 +0000 | [diff] [blame] | 1025 | DS.AddAttributes(ParseAttributes()); |
Steve Naroff | 98d153c | 2007-06-06 23:19:11 +0000 | [diff] [blame] | 1026 | continue; // do *not* consume the next token! |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1027 | } |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1028 | |
| 1029 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 1030 | if (isInvalid) { |
| 1031 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 1032 | if (isInvalid == 1) // Error. |
| 1033 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 1034 | else // extwarn. |
| 1035 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 1036 | } |
| 1037 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1041 | |
| 1042 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 1043 | /// |
| 1044 | void Parser::ParseDeclarator(Declarator &D) { |
| 1045 | /// This implements the 'declarator' production in the C grammar, then checks |
| 1046 | /// for well-formedness and issues diagnostics. |
| 1047 | ParseDeclaratorInternal(D); |
| 1048 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 1049 | // TODO: validate D. |
Chris Lattner | bf320c8 | 2006-08-07 05:05:30 +0000 | [diff] [blame] | 1050 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | /// ParseDeclaratorInternal |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1054 | /// declarator: [C99 6.7.5] |
| 1055 | /// pointer[opt] direct-declarator |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 1056 | /// [C++] '&' declarator [C++ 8p4, dcl.decl] |
| 1057 | /// [GNU] '&' restrict[opt] attributes[opt] declarator |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1058 | /// |
| 1059 | /// pointer: [C99 6.7.5] |
| 1060 | /// '*' type-qualifier-list[opt] |
| 1061 | /// '*' type-qualifier-list[opt] pointer |
| 1062 | /// |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1063 | void Parser::ParseDeclaratorInternal(Declarator &D) { |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1064 | tok::TokenKind Kind = Tok.getKind(); |
| 1065 | |
| 1066 | // Not a pointer or C++ reference. |
| 1067 | if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus)) |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1068 | return ParseDirectDeclarator(D); |
| 1069 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1070 | // Otherwise, '*' -> pointer or '&' -> reference. |
| 1071 | SourceLocation Loc = ConsumeToken(); // Eat the * or &. |
| 1072 | |
| 1073 | if (Kind == tok::star) { |
| 1074 | // Is a pointer |
| 1075 | DeclSpec DS; |
Steve Naroff | 98d153c | 2007-06-06 23:19:11 +0000 | [diff] [blame] | 1076 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1077 | ParseTypeQualifierListOpt(DS); |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1078 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1079 | // Recursively parse the declarator. |
| 1080 | ParseDeclaratorInternal(D); |
Chris Lattner | 9dfdb3c | 2006-11-13 07:38:09 +0000 | [diff] [blame] | 1081 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1082 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1083 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc)); |
| 1084 | } else { |
| 1085 | // Is a reference |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 1086 | DeclSpec DS; |
| 1087 | |
| 1088 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 1089 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 1090 | // template type argument, in which case the cv-qualifiers are ignored. |
| 1091 | // |
| 1092 | // [GNU] Retricted references are allowed. |
| 1093 | // [GNU] Attributes on references are allowed. |
| 1094 | ParseTypeQualifierListOpt(DS); |
| 1095 | |
| 1096 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 1097 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 1098 | Diag(DS.getConstSpecLoc(), |
| 1099 | diag::err_invalid_reference_qualifier_application, |
| 1100 | "const"); |
| 1101 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 1102 | Diag(DS.getVolatileSpecLoc(), |
| 1103 | diag::err_invalid_reference_qualifier_application, |
| 1104 | "volatile"); |
| 1105 | } |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1106 | |
| 1107 | // Recursively parse the declarator. |
| 1108 | ParseDeclaratorInternal(D); |
| 1109 | |
| 1110 | // Remember that we parsed a reference type. It doesn't have type-quals. |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 1111 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc)); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1112 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1115 | /// ParseDirectDeclarator |
| 1116 | /// direct-declarator: [C99 6.7.5] |
| 1117 | /// identifier |
| 1118 | /// '(' declarator ')' |
| 1119 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1120 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1121 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1122 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1123 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1124 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1125 | /// direct-declarator '(' parameter-type-list ')' |
| 1126 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1127 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1128 | /// parameter-type-list[opt] ')' |
| 1129 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1130 | void Parser::ParseDirectDeclarator(Declarator &D) { |
| 1131 | // Parse the first direct-declarator seen. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1132 | if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1133 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 1134 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1135 | ConsumeToken(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1136 | } else if (Tok.is(tok::l_paren)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1137 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1138 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1139 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 1140 | ParseParenDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1141 | } else if (D.mayOmitIdentifier()) { |
| 1142 | // This could be something simple like "int" (in which case the declarator |
| 1143 | // portion is empty), if an abstract-declarator is allowed. |
| 1144 | D.SetIdentifier(0, Tok.getLocation()); |
| 1145 | } else { |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 1146 | // Expected identifier or '('. |
| 1147 | Diag(Tok, diag::err_expected_ident_lparen); |
| 1148 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | assert(D.isPastIdentifier() && |
| 1152 | "Haven't past the location of the identifier yet?"); |
| 1153 | |
| 1154 | while (1) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1155 | if (Tok.is(tok::l_paren)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1156 | ParseParenDeclarator(D); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1157 | } else if (Tok.is(tok::l_square)) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1158 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1159 | } else { |
| 1160 | break; |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This may |
| 1166 | /// either be before the identifier (in which case these are just grouping |
| 1167 | /// parens for precedence) or it may be after the identifier, in which case |
| 1168 | /// these are function arguments. |
| 1169 | /// |
| 1170 | /// This method also handles this portion of the grammar: |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1171 | /// parameter-type-list: [C99 6.7.5] |
| 1172 | /// parameter-list |
| 1173 | /// parameter-list ',' '...' |
| 1174 | /// |
| 1175 | /// parameter-list: [C99 6.7.5] |
| 1176 | /// parameter-declaration |
| 1177 | /// parameter-list ',' parameter-declaration |
| 1178 | /// |
| 1179 | /// parameter-declaration: [C99 6.7.5] |
| 1180 | /// declaration-specifiers declarator |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1181 | /// [GNU] declaration-specifiers declarator attributes |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1182 | /// declaration-specifiers abstract-declarator[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1183 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1184 | /// |
| 1185 | /// identifier-list: [C99 6.7.5] |
| 1186 | /// identifier |
| 1187 | /// identifier-list ',' identifier |
| 1188 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1189 | void Parser::ParseParenDeclarator(Declarator &D) { |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 1190 | SourceLocation StartLoc = ConsumeParen(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1191 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1192 | // If we haven't past the identifier yet (or where the identifier would be |
| 1193 | // stored, if this is an abstract declarator), then this is probably just |
| 1194 | // grouping parens. |
| 1195 | if (!D.isPastIdentifier()) { |
| 1196 | // Okay, this is probably a grouping paren. However, if this could be an |
| 1197 | // abstract-declarator, then this could also be the start of function |
| 1198 | // arguments (consider 'void()'). |
| 1199 | bool isGrouping; |
| 1200 | |
| 1201 | if (!D.mayOmitIdentifier()) { |
| 1202 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 1203 | // paren, because we haven't seen the identifier yet. |
| 1204 | isGrouping = true; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1205 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1206 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
Chris Lattner | bb233fe | 2006-11-21 23:13:27 +0000 | [diff] [blame] | 1207 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 1208 | // considered to be a type, not a K&R identifier-list. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1209 | isGrouping = false; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1210 | } else { |
Chris Lattner | bb233fe | 2006-11-21 23:13:27 +0000 | [diff] [blame] | 1211 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1212 | isGrouping = true; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1213 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1214 | |
| 1215 | // If this is a grouping paren, handle: |
| 1216 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1217 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1218 | if (isGrouping) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1219 | if (Tok.is(tok::kw___attribute)) |
Steve Naroff | 0f05a7a | 2007-06-09 23:38:17 +0000 | [diff] [blame] | 1220 | D.AddAttributes(ParseAttributes()); |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1221 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1222 | ParseDeclaratorInternal(D); |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 1223 | // Match the ')'. |
Chris Lattner | 04f8019 | 2006-08-15 04:55:54 +0000 | [diff] [blame] | 1224 | MatchRHSPunctuation(tok::r_paren, StartLoc); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1225 | return; |
| 1226 | } |
| 1227 | |
| 1228 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
Chris Lattner | a350722 | 2006-08-07 00:33:37 +0000 | [diff] [blame] | 1229 | // argument list. Recognize that this declarator will never have an |
| 1230 | // identifier (and remember where it would have been), then fall through to |
| 1231 | // the handling of argument lists. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1232 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1235 | // Okay, this is the parameter list of a function definition, or it is an |
| 1236 | // identifier list of a K&R-style function. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1237 | bool IsVariadic; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1238 | bool HasPrototype; |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 1239 | bool ErrorEmitted = false; |
| 1240 | |
Chris Lattner | edc9e39 | 2006-12-02 06:21:46 +0000 | [diff] [blame] | 1241 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1242 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 1243 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
Chris Lattner | edc9e39 | 2006-12-02 06:21:46 +0000 | [diff] [blame] | 1244 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1245 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1246 | // int() -> no prototype, no '...'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1247 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1248 | HasPrototype = false; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1249 | } else if (Tok.is(tok::identifier) && |
Chris Lattner | bb233fe | 2006-11-21 23:13:27 +0000 | [diff] [blame] | 1250 | // K&R identifier lists can't have typedefs as identifiers, per |
| 1251 | // C99 6.7.5.3p11. |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 1252 | !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1253 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 1254 | // normal declarators, not for abstract-declarators. |
| 1255 | assert(D.isPastIdentifier() && "Identifier (if present) must be passed!"); |
| 1256 | |
| 1257 | // If there was no identifier specified, either we are in an |
| 1258 | // abstract-declarator, or we are in a parameter declarator which was found |
| 1259 | // to be abstract. In abstract-declarators, identifier lists are not valid, |
| 1260 | // diagnose this. |
| 1261 | if (!D.getIdentifier()) |
| 1262 | Diag(Tok, diag::ext_ident_list_in_param); |
Chris Lattner | edc9e39 | 2006-12-02 06:21:46 +0000 | [diff] [blame] | 1263 | |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1264 | // Remember this identifier in ParamInfo. |
| 1265 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(), |
| 1266 | Tok.getLocation(), 0)); |
| 1267 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1268 | ConsumeToken(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1269 | while (Tok.is(tok::comma)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1270 | // Eat the comma. |
| 1271 | ConsumeToken(); |
| 1272 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1273 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1274 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 1275 | ErrorEmitted = true; |
| 1276 | break; |
| 1277 | } |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1278 | |
Chris Lattner | 969ca15 | 2006-12-03 06:29:03 +0000 | [diff] [blame] | 1279 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 1280 | |
| 1281 | // Verify that the argument identifier has not already been mentioned. |
Chris Lattner | baf3366 | 2007-01-27 02:14:08 +0000 | [diff] [blame] | 1282 | if (!ParamsSoFar.insert(ParmII)) { |
Chris Lattner | ad9ac94 | 2007-01-23 01:14:52 +0000 | [diff] [blame] | 1283 | Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName()); |
| 1284 | ParmII = 0; |
| 1285 | } |
Chris Lattner | 969ca15 | 2006-12-03 06:29:03 +0000 | [diff] [blame] | 1286 | |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1287 | // Remember this identifier in ParamInfo. |
Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 1288 | if (ParmII) |
| 1289 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 1290 | Tok.getLocation(), 0)); |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1291 | |
| 1292 | // Eat the identifier. |
| 1293 | ConsumeToken(); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1296 | // K&R 'prototype'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1297 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1298 | HasPrototype = false; |
| 1299 | } else { |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1300 | // Finally, a normal, non-empty parameter type list. |
| 1301 | |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1302 | // Enter function-declaration scope, limiting any declarators for struct |
| 1303 | // tags to the function prototype scope. |
| 1304 | // FIXME: is this needed? |
Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1305 | EnterScope(Scope::DeclScope); |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1306 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1307 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1308 | while (1) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1309 | if (Tok.is(tok::ellipsis)) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1310 | IsVariadic = true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1311 | |
| 1312 | // Check to see if this is "void(...)" which is not allowed. |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1313 | if (ParamInfo.empty()) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1314 | // Otherwise, parse parameter type list. If it starts with an |
| 1315 | // ellipsis, diagnose the malformed function. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1316 | Diag(Tok, diag::err_ellipsis_first_arg); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1317 | IsVariadic = false; // Treat this like 'void()'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | // Consume the ellipsis. |
| 1321 | ConsumeToken(); |
| 1322 | break; |
| 1323 | } |
| 1324 | |
Chris Lattner | 7b8134f | 2008-02-10 23:08:00 +0000 | [diff] [blame] | 1325 | SourceLocation DSStart = Tok.getLocation(); |
| 1326 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1327 | // Parse the declaration-specifiers. |
| 1328 | DeclSpec DS; |
| 1329 | ParseDeclarationSpecifiers(DS); |
| 1330 | |
| 1331 | // Parse the declarator. This is "PrototypeContext", because we must |
| 1332 | // accept either 'declarator' or 'abstract-declarator' here. |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1333 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 1334 | ParseDeclarator(ParmDecl); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1336 | // Parse GNU attributes, if present. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1337 | if (Tok.is(tok::kw___attribute)) |
Steve Naroff | 0f05a7a | 2007-06-09 23:38:17 +0000 | [diff] [blame] | 1338 | ParmDecl.AddAttributes(ParseAttributes()); |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1339 | |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1340 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 1341 | // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted. |
| 1342 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && |
| 1343 | DS.getStorageClassSpec() != DeclSpec::SCS_register) { |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 1344 | Diag(DS.getStorageClassSpecLoc(), |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1345 | diag::err_invalid_storage_class_in_func_decl); |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 1346 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1347 | } |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 1348 | if (DS.isThreadSpecified()) { |
| 1349 | Diag(DS.getThreadSpecLoc(), |
| 1350 | diag::err_invalid_storage_class_in_func_decl); |
| 1351 | DS.ClearStorageClassSpecs(); |
| 1352 | } |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1353 | |
| 1354 | // Inform the actions module about the parameter declarator, so it gets |
| 1355 | // added to the current scope. |
Chris Lattner | 216d865 | 2006-12-02 06:47:41 +0000 | [diff] [blame] | 1356 | Action::TypeResult ParamTy = |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1357 | Actions.ActOnParamDeclaratorType(CurScope, ParmDecl); |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1358 | |
| 1359 | // Remember this parsed parameter in ParamInfo. |
Chris Lattner | 969ca15 | 2006-12-03 06:29:03 +0000 | [diff] [blame] | 1360 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
| 1361 | |
| 1362 | // Verify that the argument identifier has not already been mentioned. |
Chris Lattner | baf3366 | 2007-01-27 02:14:08 +0000 | [diff] [blame] | 1363 | if (ParmII && !ParamsSoFar.insert(ParmII)) { |
Chris Lattner | ad9ac94 | 2007-01-23 01:14:52 +0000 | [diff] [blame] | 1364 | Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition, |
| 1365 | ParmII->getName()); |
| 1366 | ParmII = 0; |
Chris Lattner | 969ca15 | 2006-12-03 06:29:03 +0000 | [diff] [blame] | 1367 | } |
Chris Lattner | 7f024fe | 2008-01-31 06:10:07 +0000 | [diff] [blame] | 1368 | |
| 1369 | // If no parameter was specified, verify that *something* was specified, |
| 1370 | // otherwise we have a missing type and identifier. |
Chris Lattner | 7b8134f | 2008-02-10 23:08:00 +0000 | [diff] [blame] | 1371 | if (DS.getParsedSpecifiers() == DeclSpec::PQ_None && |
| 1372 | ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) { |
| 1373 | Diag(DSStart, diag::err_missing_param); |
| 1374 | } else if (!DS.hasTypeSpecifier() && |
| 1375 | (getLang().C99 || getLang().CPlusPlus)) { |
| 1376 | // Otherwise, if something was specified but a type specifier wasn't, |
| 1377 | // (e.g. "x" or "restrict x" or "restrict"), this is a use of implicit |
| 1378 | // int. This is valid in C90, but not in C99 or C++. |
Chris Lattner | 7f024fe | 2008-01-31 06:10:07 +0000 | [diff] [blame] | 1379 | if (ParmII) |
| 1380 | Diag(ParmDecl.getIdentifierLoc(), |
Chris Lattner | 7b8134f | 2008-02-10 23:08:00 +0000 | [diff] [blame] | 1381 | diag::ext_param_requires_type_specifier, ParmII->getName()); |
Chris Lattner | 7f024fe | 2008-01-31 06:10:07 +0000 | [diff] [blame] | 1382 | else |
Chris Lattner | 7b8134f | 2008-02-10 23:08:00 +0000 | [diff] [blame] | 1383 | Diag(DSStart, diag::ext_anon_param_requires_type_specifier); |
Chris Lattner | 7f024fe | 2008-01-31 06:10:07 +0000 | [diff] [blame] | 1384 | } |
Chris Lattner | 969ca15 | 2006-12-03 06:29:03 +0000 | [diff] [blame] | 1385 | |
Steve Naroff | 7e6f7c2 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 1386 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Nate Begeman | a0f7897 | 2007-11-13 22:14:47 +0000 | [diff] [blame] | 1387 | ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType(), |
| 1388 | ParmDecl.getDeclSpec().getAttributes())); |
| 1389 | |
| 1390 | // Ownership of DeclSpec has been handed off to ParamInfo. |
| 1391 | DS.clearAttributes(); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1392 | |
| 1393 | // If the next token is a comma, consume it and keep reading arguments. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1394 | if (Tok.isNot(tok::comma)) break; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1395 | |
| 1396 | // Consume the comma. |
| 1397 | ConsumeToken(); |
| 1398 | } |
| 1399 | |
| 1400 | HasPrototype = true; |
Chris Lattner | 43e956c | 2006-11-28 04:05:37 +0000 | [diff] [blame] | 1401 | |
| 1402 | // Leave prototype scope. |
| 1403 | ExitScope(); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1406 | // Remember that we parsed a function type, and remember the attributes. |
Chris Lattner | d2e97c1 | 2006-12-03 02:03:33 +0000 | [diff] [blame] | 1407 | if (!ErrorEmitted) |
| 1408 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic, |
| 1409 | &ParamInfo[0], ParamInfo.size(), |
| 1410 | StartLoc)); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 1411 | |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 1412 | // If we have the closing ')', eat it and we're done. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1413 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 1414 | ConsumeParen(); |
| 1415 | } else { |
| 1416 | // If an error happened earlier parsing something else in the proto, don't |
| 1417 | // issue another error. |
| 1418 | if (!ErrorEmitted) |
| 1419 | Diag(Tok, diag::err_expected_rparen); |
| 1420 | SkipUntil(tok::r_paren); |
| 1421 | } |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 1422 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 1423 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1424 | |
| 1425 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1426 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1427 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1428 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1429 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1430 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 1431 | SourceLocation StartLoc = ConsumeBracket(); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1432 | |
| 1433 | // If valid, this location is the position where we read the 'static' keyword. |
| 1434 | SourceLocation StaticLoc; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1435 | if (Tok.is(tok::kw_static)) |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1436 | StaticLoc = ConsumeToken(); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1437 | |
| 1438 | // If there is a type-qualifier-list, read it now. |
| 1439 | DeclSpec DS; |
| 1440 | ParseTypeQualifierListOpt(DS); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1441 | |
| 1442 | // If we haven't already read 'static', check to see if there is one after the |
| 1443 | // type-qualifier-list. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1444 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1445 | StaticLoc = ConsumeToken(); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1446 | |
| 1447 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1448 | bool isStar = false; |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 1449 | ExprResult NumElements(false); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1450 | if (Tok.is(tok::star)) { |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 1451 | // Remember the '*' token, in case we have to un-get it. |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1452 | Token StarTok = Tok; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1453 | ConsumeToken(); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 1454 | |
| 1455 | // Check that the ']' token is present to avoid incorrectly parsing |
| 1456 | // expressions starting with '*' as [*]. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1457 | if (Tok.is(tok::r_square)) { |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 1458 | if (StaticLoc.isValid()) |
| 1459 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
| 1460 | StaticLoc = SourceLocation(); // Drop the static. |
| 1461 | isStar = true; |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 1462 | } else { |
| 1463 | // Otherwise, the * must have been some expression (such as '*ptr') that |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 1464 | // started an assignment-expr. We already consumed the token, but now we |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 1465 | // need to reparse it. This handles cases like 'X[*p + 4]' |
| 1466 | NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 1467 | } |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1468 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1469 | // Parse the assignment-expression now. |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 1470 | NumElements = ParseAssignmentExpression(); |
| 1471 | } |
| 1472 | |
| 1473 | // If there was an error parsing the assignment-expression, recover. |
| 1474 | if (NumElements.isInvalid) { |
| 1475 | // If the expression was invalid, skip it. |
| 1476 | SkipUntil(tok::r_square); |
| 1477 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Chris Lattner | 04f8019 | 2006-08-15 04:55:54 +0000 | [diff] [blame] | 1480 | MatchRHSPunctuation(tok::r_square, StartLoc); |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 1481 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1482 | // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if |
| 1483 | // it was not a constant expression. |
| 1484 | if (!getLang().C99) { |
| 1485 | // TODO: check C90 array constant exprness. |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1486 | if (isStar || StaticLoc.isValid() || |
| 1487 | 0/*TODO: NumElts is not a C90 constantexpr */) |
Chris Lattner | 8a39edc | 2006-08-06 18:33:32 +0000 | [diff] [blame] | 1488 | Diag(StartLoc, diag::ext_c99_array_usage); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1489 | } |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 1490 | |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1491 | // Remember that we parsed a pointer type, and remember the type-quals. |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 1492 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
| 1493 | StaticLoc.isValid(), isStar, |
| 1494 | NumElements.Val, StartLoc)); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1497 | /// [GNU] typeof-specifier: |
| 1498 | /// typeof ( expressions ) |
| 1499 | /// typeof ( type-name ) |
| 1500 | /// |
| 1501 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1502 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1503 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1504 | SourceLocation StartLoc = ConsumeToken(); |
| 1505 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1506 | if (Tok.isNot(tok::l_paren)) { |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1507 | Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName()); |
| 1508 | return; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1509 | } |
| 1510 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
| 1511 | |
| 1512 | if (isTypeSpecifierQualifier()) { |
| 1513 | TypeTy *Ty = ParseTypeName(); |
| 1514 | |
Steve Naroff | 872da80 | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1515 | assert(Ty && "Parser::ParseTypeofSpecifier(): missing type"); |
| 1516 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1517 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | 872da80 | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1518 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1519 | return; |
| 1520 | } |
| 1521 | RParenLoc = ConsumeParen(); |
| 1522 | const char *PrevSpec = 0; |
| 1523 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 1524 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty)) |
| 1525 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1526 | } else { // we have an expression. |
| 1527 | ExprResult Result = ParseExpression(); |
Steve Naroff | 872da80 | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1528 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1529 | if (Result.isInvalid || Tok.isNot(tok::r_paren)) { |
Steve Naroff | 872da80 | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1530 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1531 | return; |
| 1532 | } |
| 1533 | RParenLoc = ConsumeParen(); |
| 1534 | const char *PrevSpec = 0; |
| 1535 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 1536 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
| 1537 | Result.Val)) |
| 1538 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1539 | } |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |