Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Parser.cpp - C Language Family Parser ----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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 | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | 545f39e | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 16 | #include "clang/Parse/DeclSpec.h" |
| 17 | #include "clang/Parse/Scope.h" |
Chris Lattner | 6b3833c | 2009-03-05 07:24:28 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | daa5c00 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 19 | #include "ExtensionRAIIObject.h" |
Daniel Dunbar | 47f99c9 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 20 | #include "ParsePragma.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | Parser::Parser(Preprocessor &pp, Action &actions) |
Chris Lattner | 6b3833c | 2009-03-05 07:24:28 +0000 | [diff] [blame] | 24 | : CrashInfo(*this), PP(pp), Actions(actions), Diags(PP.getDiagnostics()), |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 25 | GreaterThanIsOperator(true) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 26 | Tok.setKind(tok::eof); |
| 27 | CurScope = 0; |
| 28 | NumCachedScopes = 0; |
| 29 | ParenCount = BracketCount = BraceCount = 0; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 30 | ObjCImpDecl = DeclPtrTy(); |
Daniel Dunbar | 47f99c9 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 31 | |
| 32 | // Add #pragma handlers. These are removed and destroyed in the |
| 33 | // destructor. |
Ted Kremenek | 86a631b | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 34 | PackHandler.reset(new |
| 35 | PragmaPackHandler(&PP.getIdentifierTable().get("pack"), actions)); |
| 36 | PP.AddPragmaHandler(0, PackHandler.get()); |
| 37 | |
| 38 | UnusedHandler.reset(new |
| 39 | PragmaUnusedHandler(&PP.getIdentifierTable().get("unused"), actions, |
| 40 | *this)); |
| 41 | PP.AddPragmaHandler(0, UnusedHandler.get()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Chris Lattner | 6b3833c | 2009-03-05 07:24:28 +0000 | [diff] [blame] | 44 | /// If a crash happens while the parser is active, print out a line indicating |
| 45 | /// what the current token is. |
| 46 | void PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const { |
| 47 | const Token &Tok = P.getCurToken(); |
Chris Lattner | 0e572c9 | 2009-03-05 07:27:50 +0000 | [diff] [blame] | 48 | if (Tok.is(tok::eof)) { |
Chris Lattner | 6b3833c | 2009-03-05 07:24:28 +0000 | [diff] [blame] | 49 | OS << "<eof> parser at end of file\n"; |
| 50 | return; |
| 51 | } |
| 52 | |
Chris Lattner | 0e572c9 | 2009-03-05 07:27:50 +0000 | [diff] [blame] | 53 | if (Tok.getLocation().isInvalid()) { |
| 54 | OS << "<unknown> parser at unknown location\n"; |
| 55 | return; |
| 56 | } |
| 57 | |
Chris Lattner | 6b3833c | 2009-03-05 07:24:28 +0000 | [diff] [blame] | 58 | const Preprocessor &PP = P.getPreprocessor(); |
| 59 | Tok.getLocation().print(OS, PP.getSourceManager()); |
| 60 | OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n"; |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 61 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 6b3833c | 2009-03-05 07:24:28 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 9943e98 | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 64 | DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) { |
Chris Lattner | 6b3833c | 2009-03-05 07:24:28 +0000 | [diff] [blame] | 65 | return Diags.Report(FullSourceLoc(Loc, PP.getSourceManager()), DiagID); |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 9943e98 | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 68 | DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 69 | return Diag(Tok.getLocation(), DiagID); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 72 | /// \brief Emits a diagnostic suggesting parentheses surrounding a |
| 73 | /// given range. |
| 74 | /// |
| 75 | /// \param Loc The location where we'll emit the diagnostic. |
| 76 | /// \param Loc The kind of diagnostic to emit. |
| 77 | /// \param ParenRange Source range enclosing code that should be parenthesized. |
| 78 | void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK, |
| 79 | SourceRange ParenRange) { |
Douglas Gregor | 61be360 | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 80 | SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 81 | if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 82 | // We can't display the parentheses, so just dig the |
| 83 | // warning/error and return. |
| 84 | Diag(Loc, DK); |
| 85 | return; |
| 86 | } |
| 87 | |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 88 | Diag(Loc, DK) |
Douglas Gregor | 61be360 | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 89 | << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 90 | << CodeModificationHint::CreateInsertion(EndLoc, ")"); |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 93 | /// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'), |
| 94 | /// this helper function matches and consumes the specified RHS token if |
| 95 | /// present. If not present, it emits the specified diagnostic indicating |
| 96 | /// that the parser failed to match the RHS of the token at LHSLoc. LHSName |
| 97 | /// should be the name of the unmatched LHS token. |
| 98 | SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok, |
| 99 | SourceLocation LHSLoc) { |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 101 | if (Tok.is(RHSTok)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 102 | return ConsumeAnyToken(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 104 | SourceLocation R = Tok.getLocation(); |
| 105 | const char *LHSName = "unknown"; |
| 106 | diag::kind DID = diag::err_parse_error; |
| 107 | switch (RHSTok) { |
| 108 | default: break; |
| 109 | case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break; |
| 110 | case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break; |
| 111 | case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break; |
| 112 | case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break; |
| 113 | } |
| 114 | Diag(Tok, DID); |
Chris Lattner | 921342c | 2008-11-23 23:17:07 +0000 | [diff] [blame] | 115 | Diag(LHSLoc, diag::note_matching) << LHSName; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 116 | SkipUntil(RHSTok); |
| 117 | return R; |
| 118 | } |
| 119 | |
| 120 | /// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the |
| 121 | /// input. If so, it is consumed and false is returned. |
| 122 | /// |
| 123 | /// If the input is malformed, this emits the specified diagnostic. Next, if |
| 124 | /// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is |
| 125 | /// returned. |
| 126 | bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID, |
| 127 | const char *Msg, tok::TokenKind SkipToTok) { |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 128 | if (Tok.is(ExpectedTok)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 129 | ConsumeAnyToken(); |
| 130 | return false; |
| 131 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 132 | |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 133 | const char *Spelling = 0; |
Douglas Gregor | 61be360 | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 134 | SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); |
| 135 | if (EndLoc.isValid() && |
| 136 | (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) { |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 137 | // Show what code to insert to fix this problem. |
Douglas Gregor | 61be360 | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 138 | Diag(EndLoc, DiagID) |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 139 | << Msg |
Douglas Gregor | 61be360 | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 140 | << CodeModificationHint::CreateInsertion(EndLoc, Spelling); |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 141 | } else |
| 142 | Diag(Tok, DiagID) << Msg; |
| 143 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 144 | if (SkipToTok != tok::unknown) |
| 145 | SkipUntil(SkipToTok); |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | //===----------------------------------------------------------------------===// |
| 150 | // Error recovery. |
| 151 | //===----------------------------------------------------------------------===// |
| 152 | |
| 153 | /// SkipUntil - Read tokens until we get to the specified token, then consume |
| 154 | /// it (unless DontConsume is true). Because we cannot guarantee that the |
| 155 | /// token will ever occur, this skips to the next token, or to some likely |
| 156 | /// good stopping point. If StopAtSemi is true, skipping will stop at a ';' |
| 157 | /// character. |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 158 | /// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 159 | /// If SkipUntil finds the specified token, it returns true, otherwise it |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 160 | /// returns false. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 161 | bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks, |
| 162 | bool StopAtSemi, bool DontConsume) { |
| 163 | // We always want this function to skip at least one token if the first token |
| 164 | // isn't T and if not at EOF. |
| 165 | bool isFirstTokenSkipped = true; |
| 166 | while (1) { |
| 167 | // If we found one of the tokens, stop and return true. |
| 168 | for (unsigned i = 0; i != NumToks; ++i) { |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 169 | if (Tok.is(Toks[i])) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 170 | if (DontConsume) { |
| 171 | // Noop, don't consume the token. |
| 172 | } else { |
| 173 | ConsumeAnyToken(); |
| 174 | } |
| 175 | return true; |
| 176 | } |
| 177 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 179 | switch (Tok.getKind()) { |
| 180 | case tok::eof: |
| 181 | // Ran out of tokens. |
| 182 | return false; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 184 | case tok::l_paren: |
| 185 | // Recursively skip properly-nested parens. |
| 186 | ConsumeParen(); |
| 187 | SkipUntil(tok::r_paren, false); |
| 188 | break; |
| 189 | case tok::l_square: |
| 190 | // Recursively skip properly-nested square brackets. |
| 191 | ConsumeBracket(); |
| 192 | SkipUntil(tok::r_square, false); |
| 193 | break; |
| 194 | case tok::l_brace: |
| 195 | // Recursively skip properly-nested braces. |
| 196 | ConsumeBrace(); |
| 197 | SkipUntil(tok::r_brace, false); |
| 198 | break; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 200 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 201 | // Since the user wasn't looking for this token (if they were, it would |
| 202 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 203 | // higher level, we will assume that this matches the unbalanced token |
| 204 | // and return it. Otherwise, this is a spurious RHS token, which we skip. |
| 205 | case tok::r_paren: |
| 206 | if (ParenCount && !isFirstTokenSkipped) |
| 207 | return false; // Matches something. |
| 208 | ConsumeParen(); |
| 209 | break; |
| 210 | case tok::r_square: |
| 211 | if (BracketCount && !isFirstTokenSkipped) |
| 212 | return false; // Matches something. |
| 213 | ConsumeBracket(); |
| 214 | break; |
| 215 | case tok::r_brace: |
| 216 | if (BraceCount && !isFirstTokenSkipped) |
| 217 | return false; // Matches something. |
| 218 | ConsumeBrace(); |
| 219 | break; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 221 | case tok::string_literal: |
| 222 | case tok::wide_string_literal: |
| 223 | ConsumeStringToken(); |
| 224 | break; |
| 225 | case tok::semi: |
| 226 | if (StopAtSemi) |
| 227 | return false; |
| 228 | // FALL THROUGH. |
| 229 | default: |
| 230 | // Skip this token. |
| 231 | ConsumeToken(); |
| 232 | break; |
| 233 | } |
| 234 | isFirstTokenSkipped = false; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 235 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | //===----------------------------------------------------------------------===// |
| 239 | // Scope manipulation |
| 240 | //===----------------------------------------------------------------------===// |
| 241 | |
| 242 | /// EnterScope - Start a new scope. |
| 243 | void Parser::EnterScope(unsigned ScopeFlags) { |
| 244 | if (NumCachedScopes) { |
| 245 | Scope *N = ScopeCache[--NumCachedScopes]; |
| 246 | N->Init(CurScope, ScopeFlags); |
| 247 | CurScope = N; |
| 248 | } else { |
| 249 | CurScope = new Scope(CurScope, ScopeFlags); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /// ExitScope - Pop a scope off the scope stack. |
| 254 | void Parser::ExitScope() { |
| 255 | assert(CurScope && "Scope imbalance!"); |
| 256 | |
Chris Lattner | 6223149 | 2007-10-09 20:37:18 +0000 | [diff] [blame] | 257 | // Inform the actions module that this scope is going away if there are any |
| 258 | // decls in it. |
| 259 | if (!CurScope->decl_empty()) |
Steve Naroff | 9637a9b | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 260 | Actions.ActOnPopScope(Tok.getLocation(), CurScope); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 262 | Scope *OldScope = CurScope; |
| 263 | CurScope = OldScope->getParent(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 265 | if (NumCachedScopes == ScopeCacheSize) |
| 266 | delete OldScope; |
| 267 | else |
| 268 | ScopeCache[NumCachedScopes++] = OldScope; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | //===----------------------------------------------------------------------===// |
| 275 | // C99 6.9: External Definitions. |
| 276 | //===----------------------------------------------------------------------===// |
| 277 | |
| 278 | Parser::~Parser() { |
| 279 | // If we still have scopes active, delete the scope tree. |
| 280 | delete CurScope; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 282 | // Free the scope cache. |
| 283 | for (unsigned i = 0, e = NumCachedScopes; i != e; ++i) |
| 284 | delete ScopeCache[i]; |
Daniel Dunbar | 47f99c9 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 285 | |
| 286 | // Remove the pragma handlers we installed. |
Ted Kremenek | 86a631b | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 287 | PP.RemovePragmaHandler(0, PackHandler.get()); |
| 288 | PackHandler.reset(); |
| 289 | PP.RemovePragmaHandler(0, UnusedHandler.get()); |
| 290 | UnusedHandler.reset(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /// Initialize - Warm up the parser. |
| 294 | /// |
| 295 | void Parser::Initialize() { |
| 296 | // Prime the lexer look-ahead. |
| 297 | ConsumeToken(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 298 | |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 299 | // Create the translation unit scope. Install it as the current scope. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 300 | assert(CurScope == 0 && "A scope is already active?"); |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 301 | EnterScope(Scope::DeclScope); |
Steve Naroff | 9637a9b | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 302 | Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 304 | if (Tok.is(tok::eof) && |
Chris Lattner | 7bdc85d | 2007-08-25 05:47:03 +0000 | [diff] [blame] | 305 | !getLang().CPlusPlus) // Empty source file is an extension in C |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 306 | Diag(Tok, diag::ext_empty_source_file); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 3235246 | 2007-08-29 22:54:08 +0000 | [diff] [blame] | 308 | // Initialization for Objective-C context sensitive keywords recognition. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 309 | // Referenced in Parser::ParseObjCTypeQualifierList. |
Chris Lattner | 3235246 | 2007-08-29 22:54:08 +0000 | [diff] [blame] | 310 | if (getLang().ObjC1) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 311 | ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in"); |
| 312 | ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out"); |
| 313 | ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout"); |
| 314 | ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway"); |
| 315 | ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy"); |
| 316 | ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref"); |
Chris Lattner | 3235246 | 2007-08-29 22:54:08 +0000 | [diff] [blame] | 317 | } |
Daniel Dunbar | 4837ae7 | 2008-08-14 22:04:54 +0000 | [diff] [blame] | 318 | |
| 319 | Ident_super = &PP.getIdentifierTable().get("super"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the |
| 323 | /// action tells us to. This returns true if the EOF was encountered. |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 324 | bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) { |
| 325 | Result = DeclGroupPtrTy(); |
Chris Lattner | c1aea81 | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 326 | if (Tok.is(tok::eof)) { |
| 327 | Actions.ActOnEndOfTranslationUnit(); |
| 328 | return true; |
| 329 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 330 | |
Steve Naroff | ca44ffd | 2007-11-29 23:05:20 +0000 | [diff] [blame] | 331 | Result = ParseExternalDeclaration(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 332 | return false; |
| 333 | } |
| 334 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 335 | /// ParseTranslationUnit: |
| 336 | /// translation-unit: [C99 6.9] |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 337 | /// external-declaration |
| 338 | /// translation-unit external-declaration |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 339 | void Parser::ParseTranslationUnit() { |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 340 | Initialize(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 341 | |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 342 | DeclGroupPtrTy Res; |
Steve Naroff | ca44ffd | 2007-11-29 23:05:20 +0000 | [diff] [blame] | 343 | while (!ParseTopLevelDecl(Res)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 344 | /*parse them all*/; |
Chris Lattner | f7df4d1 | 2008-08-23 02:00:52 +0000 | [diff] [blame] | 345 | |
| 346 | ExitScope(); |
| 347 | assert(CurScope == 0 && "Scope imbalance!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /// ParseExternalDeclaration: |
Chris Lattner | eb54a36 | 2008-12-08 21:59:01 +0000 | [diff] [blame] | 351 | /// |
Douglas Gregor | 61818c5 | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 352 | /// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl] |
Chris Lattner | 06f4e75 | 2007-08-10 20:57:02 +0000 | [diff] [blame] | 353 | /// function-definition |
| 354 | /// declaration |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 355 | /// [EXT] ';' |
| 356 | /// [GNU] asm-definition |
Chris Lattner | 06f4e75 | 2007-08-10 20:57:02 +0000 | [diff] [blame] | 357 | /// [GNU] __extension__ external-declaration |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 358 | /// [OBJC] objc-class-definition |
| 359 | /// [OBJC] objc-class-declaration |
| 360 | /// [OBJC] objc-alias-declaration |
| 361 | /// [OBJC] objc-protocol-definition |
| 362 | /// [OBJC] objc-method-definition |
| 363 | /// [OBJC] @end |
Douglas Gregor | 61818c5 | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 364 | /// [C++] linkage-specification |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 365 | /// [GNU] asm-definition: |
| 366 | /// simple-asm-expr ';' |
| 367 | /// |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 368 | Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration() { |
| 369 | DeclPtrTy SingleDecl; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 370 | switch (Tok.getKind()) { |
| 371 | case tok::semi: |
Douglas Gregor | 1ba5cb3 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 372 | Diag(Tok, diag::ext_top_level_semi) |
| 373 | << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 374 | ConsumeToken(); |
| 375 | // TODO: Invoke action for top-level semicolon. |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 376 | return DeclGroupPtrTy(); |
Chris Lattner | eb54a36 | 2008-12-08 21:59:01 +0000 | [diff] [blame] | 377 | case tok::r_brace: |
| 378 | Diag(Tok, diag::err_expected_external_declaration); |
| 379 | ConsumeBrace(); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 380 | return DeclGroupPtrTy(); |
Chris Lattner | eb54a36 | 2008-12-08 21:59:01 +0000 | [diff] [blame] | 381 | case tok::eof: |
| 382 | Diag(Tok, diag::err_expected_external_declaration); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 383 | return DeclGroupPtrTy(); |
Chris Lattner | 06f4e75 | 2007-08-10 20:57:02 +0000 | [diff] [blame] | 384 | case tok::kw___extension__: { |
Chris Lattner | daa5c00 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 385 | // __extension__ silences extension warnings in the subexpression. |
| 386 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Chris Lattner | 658e687 | 2008-10-20 06:51:33 +0000 | [diff] [blame] | 387 | ConsumeToken(); |
Chris Lattner | daa5c00 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 388 | return ParseExternalDeclaration(); |
Chris Lattner | 06f4e75 | 2007-08-10 20:57:02 +0000 | [diff] [blame] | 389 | } |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 390 | case tok::kw_asm: { |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 391 | OwningExprResult Result(ParseSimpleAsm()); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 392 | |
Anders Carlsson | f41100b | 2008-02-08 00:23:11 +0000 | [diff] [blame] | 393 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, |
| 394 | "top-level asm block"); |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 395 | |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 396 | if (Result.isInvalid()) |
| 397 | return DeclGroupPtrTy(); |
| 398 | SingleDecl = Actions.ActOnFileScopeAsmDecl(Tok.getLocation(), move(Result)); |
| 399 | break; |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 400 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 401 | case tok::at: |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 402 | // @ is not a legal token unless objc is enabled, no need to check for ObjC. |
| 403 | /// FIXME: ParseObjCAtDirectives should return a DeclGroup for things like |
| 404 | /// @class foo, bar; |
| 405 | SingleDecl = ParseObjCAtDirectives(); |
| 406 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 407 | case tok::minus: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 408 | case tok::plus: |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 409 | if (!getLang().ObjC1) { |
| 410 | Diag(Tok, diag::err_expected_external_declaration); |
| 411 | ConsumeToken(); |
| 412 | return DeclGroupPtrTy(); |
| 413 | } |
| 414 | SingleDecl = ParseObjCMethodDefinition(); |
| 415 | break; |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 416 | case tok::kw_using: |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 417 | case tok::kw_namespace: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 418 | case tok::kw_typedef: |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 419 | case tok::kw_template: |
| 420 | case tok::kw_export: // As in 'export template' |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 421 | case tok::kw_static_assert: |
Chris Lattner | 9c13572 | 2007-08-25 18:15:16 +0000 | [diff] [blame] | 422 | // A function definition cannot start with a these keywords. |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 423 | { |
| 424 | SourceLocation DeclEnd; |
| 425 | return ParseDeclaration(Declarator::FileContext, DeclEnd); |
| 426 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 427 | default: |
| 428 | // We can't tell whether this is a function-definition or declaration yet. |
| 429 | return ParseDeclarationOrFunctionDefinition(); |
| 430 | } |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 431 | |
| 432 | // This routine returns a DeclGroup, if the thing we parsed only contains a |
| 433 | // single decl, convert it now. |
| 434 | return Actions.ConvertDeclToDeclGroup(SingleDecl); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 437 | /// \brief Determine whether the current token, if it occurs after a |
| 438 | /// declarator, continues a declaration or declaration list. |
| 439 | bool Parser::isDeclarationAfterDeclarator() { |
| 440 | return Tok.is(tok::equal) || // int X()= -> not a function def |
| 441 | Tok.is(tok::comma) || // int X(), -> not a function def |
| 442 | Tok.is(tok::semi) || // int X(); -> not a function def |
| 443 | Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def |
| 444 | Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def |
| 445 | (getLang().CPlusPlus && |
| 446 | Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++] |
| 447 | } |
| 448 | |
| 449 | /// \brief Determine whether the current token, if it occurs after a |
| 450 | /// declarator, indicates the start of a function definition. |
| 451 | bool Parser::isStartOfFunctionDefinition() { |
| 452 | return Tok.is(tok::l_brace) || // int X() {} |
| 453 | (!getLang().CPlusPlus && |
| 454 | isDeclarationSpecifier()) || // int X(f) int f; {} |
| 455 | (getLang().CPlusPlus && |
| 456 | (Tok.is(tok::colon) || // X() : Base() {} (used for ctors) |
| 457 | Tok.is(tok::kw_try))); // X() try { ... } |
| 458 | } |
| 459 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 460 | /// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or |
| 461 | /// a declaration. We can't tell which we have until we read up to the |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 462 | /// compound-statement in function-definition. TemplateParams, if |
| 463 | /// non-NULL, provides the template parameters when we're parsing a |
| 464 | /// C++ template-declaration. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 465 | /// |
| 466 | /// function-definition: [C99 6.9.1] |
Chris Lattner | a15e9d2 | 2008-04-05 05:52:15 +0000 | [diff] [blame] | 467 | /// decl-specs declarator declaration-list[opt] compound-statement |
| 468 | /// [C90] function-definition: [C99 6.7.1] - implicit int result |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 469 | /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement |
Chris Lattner | a15e9d2 | 2008-04-05 05:52:15 +0000 | [diff] [blame] | 470 | /// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 471 | /// declaration: [C99 6.7] |
Chris Lattner | aac973e | 2007-08-22 06:06:56 +0000 | [diff] [blame] | 472 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 473 | /// [!C99] init-declarator-list ';' [TODO: warn in c99 mode] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 474 | /// [OMP] threadprivate-directive [TODO] |
| 475 | /// |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 476 | Parser::DeclGroupPtrTy |
Douglas Gregor | 7640620 | 2009-05-12 21:43:46 +0000 | [diff] [blame] | 477 | Parser::ParseDeclarationOrFunctionDefinition(AccessSpecifier AS) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 478 | // Parse the common declaration-specifiers piece. |
| 479 | DeclSpec DS; |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 480 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 481 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 482 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 483 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 484 | if (Tok.is(tok::semi)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 485 | ConsumeToken(); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 486 | DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 487 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 488 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 489 | |
Daniel Dunbar | 28680d1 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 490 | // ObjC2 allows prefix attributes on class interfaces and protocols. |
| 491 | // FIXME: This still needs better diagnostics. We should only accept |
| 492 | // attributes here, no types, etc. |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 493 | if (getLang().ObjC2 && Tok.is(tok::at)) { |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 494 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
Daniel Dunbar | 28680d1 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 495 | if (!Tok.isObjCAtKeyword(tok::objc_interface) && |
| 496 | !Tok.isObjCAtKeyword(tok::objc_protocol)) { |
| 497 | Diag(Tok, diag::err_objc_unexpected_attr); |
Chris Lattner | 847f5c1 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 498 | SkipUntil(tok::semi); // FIXME: better skip? |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 499 | return DeclGroupPtrTy(); |
Chris Lattner | 847f5c1 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 500 | } |
Fariborz Jahanian | f9c0a0d | 2008-01-02 19:17:38 +0000 | [diff] [blame] | 501 | const char *PrevSpec = 0; |
| 502 | if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec)) |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 503 | Diag(AtLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 504 | |
| 505 | DeclPtrTy TheDecl; |
Daniel Dunbar | 28680d1 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 506 | if (Tok.isObjCAtKeyword(tok::objc_protocol)) |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 507 | TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes()); |
| 508 | else |
| 509 | TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()); |
| 510 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 511 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 513 | // If the declspec consisted only of 'extern' and we have a string |
| 514 | // literal following it, this must be a C++ linkage specifier like |
| 515 | // 'extern "C"'. |
Chris Lattner | 1b5c9f7 | 2008-01-12 07:08:43 +0000 | [diff] [blame] | 516 | if (Tok.is(tok::string_literal) && getLang().CPlusPlus && |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 517 | DS.getStorageClassSpec() == DeclSpec::SCS_extern && |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 518 | DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) { |
| 519 | DeclPtrTy TheDecl = ParseLinkage(Declarator::FileContext); |
| 520 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
| 521 | } |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 522 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 523 | // Parse the first declarator. |
| 524 | Declarator DeclaratorInfo(DS, Declarator::FileContext); |
| 525 | ParseDeclarator(DeclaratorInfo); |
| 526 | // Error parsing the declarator? |
Douglas Gregor | 6704b31 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 527 | if (!DeclaratorInfo.hasName()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 528 | // If so, skip until the semi-colon or a }. |
Douglas Gregor | 6f73061 | 2008-12-01 23:03:32 +0000 | [diff] [blame] | 529 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 530 | if (Tok.is(tok::semi)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 531 | ConsumeToken(); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 532 | return DeclGroupPtrTy(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 535 | // If we have a declaration or declarator list, handle it. |
| 536 | if (isDeclarationAfterDeclarator()) { |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 537 | // Parse the init-declarator-list for a normal declaration. |
Chris Lattner | 2c41d48 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 538 | DeclGroupPtrTy DG = |
| 539 | ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
| 540 | // Eat the semi colon after the declaration. |
| 541 | ExpectAndConsume(tok::semi, diag::err_expected_semi_declation); |
| 542 | return DG; |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 545 | if (DeclaratorInfo.isFunctionDeclarator() && |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 546 | isStartOfFunctionDefinition()) { |
Steve Naroff | 8329885 | 2008-02-14 02:58:32 +0000 | [diff] [blame] | 547 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 548 | Diag(Tok, diag::err_function_declared_typedef); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 549 | |
Steve Naroff | 8329885 | 2008-02-14 02:58:32 +0000 | [diff] [blame] | 550 | if (Tok.is(tok::l_brace)) { |
| 551 | // This recovery skips the entire function body. It would be nice |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 552 | // to simply call ParseFunctionDefinition() below, however Sema |
Steve Naroff | 8329885 | 2008-02-14 02:58:32 +0000 | [diff] [blame] | 553 | // assumes the declarator represents a function, not a typedef. |
| 554 | ConsumeBrace(); |
| 555 | SkipUntil(tok::r_brace, true); |
| 556 | } else { |
| 557 | SkipUntil(tok::semi); |
| 558 | } |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 559 | return DeclGroupPtrTy(); |
Steve Naroff | 8329885 | 2008-02-14 02:58:32 +0000 | [diff] [blame] | 560 | } |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 561 | DeclPtrTy TheDecl = ParseFunctionDefinition(DeclaratorInfo); |
| 562 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 563 | } |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 564 | |
| 565 | if (DeclaratorInfo.isFunctionDeclarator()) |
| 566 | Diag(Tok, diag::err_expected_fn_body); |
| 567 | else |
| 568 | Diag(Tok, diag::err_invalid_token_after_toplevel_declarator); |
| 569 | SkipUntil(tok::semi); |
| 570 | return DeclGroupPtrTy(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /// ParseFunctionDefinition - We parsed and verified that the specified |
| 574 | /// Declarator is well formed. If this is a K&R-style function, read the |
| 575 | /// parameters declaration-list, then start the compound-statement. |
| 576 | /// |
Chris Lattner | a15e9d2 | 2008-04-05 05:52:15 +0000 | [diff] [blame] | 577 | /// function-definition: [C99 6.9.1] |
| 578 | /// decl-specs declarator declaration-list[opt] compound-statement |
| 579 | /// [C90] function-definition: [C99 6.7.1] - implicit int result |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 580 | /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 581 | /// [C++] function-definition: [C++ 8.4] |
Chris Lattner | 2c41d48 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 582 | /// decl-specifier-seq[opt] declarator ctor-initializer[opt] |
| 583 | /// function-body |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 584 | /// [C++] function-definition: [C++ 8.4] |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 585 | /// decl-specifier-seq[opt] declarator function-try-block |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 586 | /// |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 587 | Parser::DeclPtrTy Parser::ParseFunctionDefinition(Declarator &D) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 588 | const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0); |
| 589 | assert(FnTypeInfo.Kind == DeclaratorChunk::Function && |
| 590 | "This isn't a function declarator!"); |
| 591 | const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 592 | |
Chris Lattner | a15e9d2 | 2008-04-05 05:52:15 +0000 | [diff] [blame] | 593 | // If this is C90 and the declspecs were completely missing, fudge in an |
| 594 | // implicit int. We do this here because this is the only place where |
| 595 | // declaration-specifiers are completely optional in the grammar. |
Chris Lattner | 92eca3e | 2009-02-27 18:35:46 +0000 | [diff] [blame] | 596 | if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) { |
Chris Lattner | a15e9d2 | 2008-04-05 05:52:15 +0000 | [diff] [blame] | 597 | const char *PrevSpec; |
Chris Lattner | 509fc80 | 2008-10-20 02:01:34 +0000 | [diff] [blame] | 598 | D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int, |
| 599 | D.getIdentifierLoc(), |
| 600 | PrevSpec); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 601 | D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin()); |
Chris Lattner | a15e9d2 | 2008-04-05 05:52:15 +0000 | [diff] [blame] | 602 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 603 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 604 | // If this declaration was formed with a K&R-style identifier list for the |
| 605 | // arguments, parse declarations for all of the args next. |
| 606 | // int foo(a,b) int a; float b; {} |
| 607 | if (!FTI.hasPrototype && FTI.NumArgs != 0) |
| 608 | ParseKNRParamDeclarations(D); |
| 609 | |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 610 | // We should have either an opening brace or, in a C++ constructor, |
| 611 | // we may have a colon. |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 612 | if (Tok.isNot(tok::l_brace) && Tok.isNot(tok::colon) && |
| 613 | Tok.isNot(tok::kw_try)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 614 | Diag(Tok, diag::err_expected_fn_body); |
| 615 | |
| 616 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
| 617 | SkipUntil(tok::l_brace, true, true); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 618 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 619 | // If we didn't find the '{', bail out. |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 620 | if (Tok.isNot(tok::l_brace)) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 621 | return DeclPtrTy(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 622 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 623 | |
Chris Lattner | ea14870 | 2007-10-09 17:14:05 +0000 | [diff] [blame] | 624 | // Enter a scope for the function body. |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 625 | ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 626 | |
Chris Lattner | ea14870 | 2007-10-09 17:14:05 +0000 | [diff] [blame] | 627 | // Tell the actions module that we have entered a function definition with the |
| 628 | // specified Declarator for the function. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 629 | DeclPtrTy Res = Actions.ActOnStartOfFunctionDef(CurScope, D); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 630 | |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 631 | if (Tok.is(tok::kw_try)) |
| 632 | return ParseFunctionTryBlock(Res); |
| 633 | |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 634 | // If we have a colon, then we're probably parsing a C++ |
| 635 | // ctor-initializer. |
| 636 | if (Tok.is(tok::colon)) |
| 637 | ParseConstructorInitializer(Res); |
| 638 | |
Chris Lattner | 0818a7a | 2009-03-05 00:49:17 +0000 | [diff] [blame] | 639 | return ParseFunctionStatementBody(Res); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides |
| 643 | /// types for a function with a K&R-style identifier list for arguments. |
| 644 | void Parser::ParseKNRParamDeclarations(Declarator &D) { |
| 645 | // We know that the top-level of this declarator is a function. |
| 646 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 647 | |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 648 | // Enter function-declaration scope, limiting any declarators to the |
| 649 | // function prototype scope, including parameter declarators. |
Douglas Gregor | cab994d | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 650 | ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 652 | // Read all the argument declarations. |
| 653 | while (isDeclarationSpecifier()) { |
| 654 | SourceLocation DSStart = Tok.getLocation(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 656 | // Parse the common declaration-specifiers piece. |
| 657 | DeclSpec DS; |
| 658 | ParseDeclarationSpecifiers(DS); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 659 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 660 | // C99 6.9.1p6: 'each declaration in the declaration list shall have at |
| 661 | // least one declarator'. |
| 662 | // NOTE: GCC just makes this an ext-warn. It's not clear what it does with |
| 663 | // the declarations though. It's trivial to ignore them, really hard to do |
| 664 | // anything else with them. |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 665 | if (Tok.is(tok::semi)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 666 | Diag(DSStart, diag::err_declaration_does_not_declare_param); |
| 667 | ConsumeToken(); |
| 668 | continue; |
| 669 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 670 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 671 | // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other |
| 672 | // than register. |
| 673 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && |
| 674 | DS.getStorageClassSpec() != DeclSpec::SCS_register) { |
| 675 | Diag(DS.getStorageClassSpecLoc(), |
| 676 | diag::err_invalid_storage_class_in_func_decl); |
| 677 | DS.ClearStorageClassSpecs(); |
| 678 | } |
| 679 | if (DS.isThreadSpecified()) { |
| 680 | Diag(DS.getThreadSpecLoc(), |
| 681 | diag::err_invalid_storage_class_in_func_decl); |
| 682 | DS.ClearStorageClassSpecs(); |
| 683 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 684 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 685 | // Parse the first declarator attached to this declspec. |
| 686 | Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext); |
| 687 | ParseDeclarator(ParmDeclarator); |
| 688 | |
| 689 | // Handle the full declarator list. |
| 690 | while (1) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 691 | Action::AttrTy *AttrList; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 692 | // If attributes are present, parse them. |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 693 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 694 | // FIXME: attach attributes too. |
| 695 | AttrList = ParseAttributes(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 697 | // Ask the actions module to compute the type for this declarator. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 698 | Action::DeclPtrTy Param = |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 699 | Actions.ActOnParamDeclarator(CurScope, ParmDeclarator); |
Steve Naroff | faed3bf | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 700 | |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 701 | if (Param && |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 702 | // A missing identifier has already been diagnosed. |
| 703 | ParmDeclarator.getIdentifier()) { |
| 704 | |
| 705 | // Scan the argument list looking for the correct param to apply this |
| 706 | // type. |
| 707 | for (unsigned i = 0; ; ++i) { |
| 708 | // C99 6.9.1p6: those declarators shall declare only identifiers from |
| 709 | // the identifier list. |
| 710 | if (i == FTI.NumArgs) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 711 | Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param) |
Chris Lattner | b12ef86 | 2008-11-19 07:51:13 +0000 | [diff] [blame] | 712 | << ParmDeclarator.getIdentifier(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 713 | break; |
| 714 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 715 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 716 | if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) { |
| 717 | // Reject redefinitions of parameters. |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 718 | if (FTI.ArgInfo[i].Param) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 719 | Diag(ParmDeclarator.getIdentifierLoc(), |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 720 | diag::err_param_redefinition) |
Chris Lattner | b12ef86 | 2008-11-19 07:51:13 +0000 | [diff] [blame] | 721 | << ParmDeclarator.getIdentifier(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 722 | } else { |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 723 | FTI.ArgInfo[i].Param = Param; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 724 | } |
| 725 | break; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | // If we don't have a comma, it is either the end of the list (a ';') or |
| 731 | // an error, bail out. |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 732 | if (Tok.isNot(tok::comma)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 733 | break; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 734 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 735 | // Consume the comma. |
| 736 | ConsumeToken(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 738 | // Parse the next declarator. |
| 739 | ParmDeclarator.clear(); |
| 740 | ParseDeclarator(ParmDeclarator); |
| 741 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 742 | |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 743 | if (Tok.is(tok::semi)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 744 | ConsumeToken(); |
| 745 | } else { |
| 746 | Diag(Tok, diag::err_parse_error); |
| 747 | // Skip to end of block or statement |
| 748 | SkipUntil(tok::semi, true); |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 749 | if (Tok.is(tok::semi)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 750 | ConsumeToken(); |
| 751 | } |
| 752 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 753 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 754 | // The actions module must verify that all arguments were declared. |
Douglas Gregor | 3faaa81 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 755 | Actions.ActOnFinishKNRParamDeclarations(CurScope, D, Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | |
| 759 | /// ParseAsmStringLiteral - This is just a normal string-literal, but is not |
| 760 | /// allowed to be a wide string, and is not subject to character translation. |
| 761 | /// |
| 762 | /// [GNU] asm-string-literal: |
| 763 | /// string-literal |
| 764 | /// |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 765 | Parser::OwningExprResult Parser::ParseAsmStringLiteral() { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 766 | if (!isTokenStringLiteral()) { |
| 767 | Diag(Tok, diag::err_expected_string_literal); |
Sebastian Redl | 10c3295 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 768 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 769 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 770 | |
Sebastian Redl | 39d4f02 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 771 | OwningExprResult Res(ParseStringLiteralExpression()); |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 772 | if (Res.isInvalid()) return move(Res); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 773 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 774 | // TODO: Diagnose: wide string literal in 'asm' |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 775 | |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 776 | return move(Res); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /// ParseSimpleAsm |
| 780 | /// |
| 781 | /// [GNU] simple-asm-expr: |
| 782 | /// 'asm' '(' asm-string-literal ')' |
| 783 | /// |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 784 | Parser::OwningExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) { |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 785 | assert(Tok.is(tok::kw_asm) && "Not an asm!"); |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 786 | SourceLocation Loc = ConsumeToken(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 787 | |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 788 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 789 | Diag(Tok, diag::err_expected_lparen_after) << "asm"; |
Sebastian Redl | 10c3295 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 790 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 791 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 792 | |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 793 | Loc = ConsumeParen(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 794 | |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 795 | OwningExprResult Result(ParseAsmStringLiteral()); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 796 | |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 797 | if (Result.isInvalid()) { |
| 798 | SkipUntil(tok::r_paren, true, true); |
| 799 | if (EndLoc) |
| 800 | *EndLoc = Tok.getLocation(); |
| 801 | ConsumeAnyToken(); |
| 802 | } else { |
| 803 | Loc = MatchRHSPunctuation(tok::r_paren, Loc); |
| 804 | if (EndLoc) |
| 805 | *EndLoc = Loc; |
| 806 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 807 | |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 808 | return move(Result); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 811 | /// TryAnnotateTypeOrScopeToken - If the current token position is on a |
| 812 | /// typename (possibly qualified in C++) or a C++ scope specifier not followed |
| 813 | /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens |
| 814 | /// with a single annotation token representing the typename or C++ scope |
| 815 | /// respectively. |
| 816 | /// This simplifies handling of C++ scope specifiers and allows efficient |
| 817 | /// backtracking without the need to re-parse and resolve nested-names and |
| 818 | /// typenames. |
Argiris Kirtzidis | fc33232 | 2008-11-26 21:51:07 +0000 | [diff] [blame] | 819 | /// It will mainly be called when we expect to treat identifiers as typenames |
| 820 | /// (if they are typenames). For example, in C we do not expect identifiers |
| 821 | /// inside expressions to be treated as typenames so it will not be called |
| 822 | /// for expressions in C. |
| 823 | /// The benefit for C/ObjC is that a typename will be annotated and |
Steve Naroff | 7b36a1b | 2009-01-28 19:39:02 +0000 | [diff] [blame] | 824 | /// Actions.getTypeName will not be needed to be called again (e.g. getTypeName |
Argiris Kirtzidis | fc33232 | 2008-11-26 21:51:07 +0000 | [diff] [blame] | 825 | /// will not be called twice, once to check whether we have a declaration |
| 826 | /// specifier, and another one to get the actual type inside |
| 827 | /// ParseDeclarationSpecifiers). |
Chris Lattner | 1e01594 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 828 | /// |
| 829 | /// This returns true if the token was annotated. |
Chris Lattner | 2c30145 | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 830 | /// |
| 831 | /// Note that this routine emits an error if you call it with ::new or ::delete |
| 832 | /// as the current tokens, so only call it in contexts where these are invalid. |
Chris Lattner | 94a15bd | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 833 | bool Parser::TryAnnotateTypeOrScopeToken() { |
Douglas Gregor | d302260 | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 834 | assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) |
| 835 | || Tok.is(tok::kw_typename)) && |
Chris Lattner | 8376d2e | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 836 | "Cannot be a type or scope token!"); |
| 837 | |
Douglas Gregor | d302260 | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 838 | if (Tok.is(tok::kw_typename)) { |
| 839 | // Parse a C++ typename-specifier, e.g., "typename T::type". |
| 840 | // |
| 841 | // typename-specifier: |
| 842 | // 'typename' '::' [opt] nested-name-specifier identifier |
| 843 | // 'typename' '::' [opt] nested-name-specifier template [opt] |
Douglas Gregor | 77da580 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 844 | // simple-template-id |
Douglas Gregor | d302260 | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 845 | SourceLocation TypenameLoc = ConsumeToken(); |
| 846 | CXXScopeSpec SS; |
| 847 | bool HadNestedNameSpecifier = ParseOptionalCXXScopeSpecifier(SS); |
| 848 | if (!HadNestedNameSpecifier) { |
| 849 | Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename); |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | TypeResult Ty; |
| 854 | if (Tok.is(tok::identifier)) { |
| 855 | // FIXME: check whether the next token is '<', first! |
| 856 | Ty = Actions.ActOnTypenameType(TypenameLoc, SS, *Tok.getIdentifierInfo(), |
| 857 | Tok.getLocation()); |
Douglas Gregor | 77da580 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 858 | } else if (Tok.is(tok::annot_template_id)) { |
| 859 | TemplateIdAnnotation *TemplateId |
| 860 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 861 | if (TemplateId->Kind == TNK_Function_template) { |
| 862 | Diag(Tok, diag::err_typename_refers_to_non_type_template) |
| 863 | << Tok.getAnnotationRange(); |
| 864 | return false; |
| 865 | } |
Douglas Gregor | d302260 | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 866 | |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 867 | AnnotateTemplateIdTokenAsType(0); |
Douglas Gregor | 77da580 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 868 | assert(Tok.is(tok::annot_typename) && |
| 869 | "AnnotateTemplateIdTokenAsType isn't working properly"); |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 870 | if (Tok.getAnnotationValue()) |
| 871 | Ty = Actions.ActOnTypenameType(TypenameLoc, SS, SourceLocation(), |
| 872 | Tok.getAnnotationValue()); |
| 873 | else |
| 874 | Ty = true; |
Douglas Gregor | 77da580 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 875 | } else { |
| 876 | Diag(Tok, diag::err_expected_type_name_after_typename) |
| 877 | << SS.getRange(); |
| 878 | return false; |
| 879 | } |
| 880 | |
Douglas Gregor | 77da580 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 881 | Tok.setKind(tok::annot_typename); |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 882 | Tok.setAnnotationValue(Ty.isInvalid()? 0 : Ty.get()); |
Douglas Gregor | 77da580 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 883 | Tok.setAnnotationEndLoc(Tok.getLocation()); |
| 884 | Tok.setLocation(TypenameLoc); |
| 885 | PP.AnnotateCachedTokens(Tok); |
| 886 | return true; |
Douglas Gregor | d302260 | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 889 | CXXScopeSpec SS; |
Argiris Kirtzidis | 91c80dc | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 890 | if (getLang().CPlusPlus) |
Chris Lattner | d706dc8 | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 891 | ParseOptionalCXXScopeSpecifier(SS); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 892 | |
| 893 | if (Tok.is(tok::identifier)) { |
Chris Lattner | a9d6ec7 | 2009-01-05 01:49:50 +0000 | [diff] [blame] | 894 | // Determine whether the identifier is a type name. |
Steve Naroff | 7b36a1b | 2009-01-28 19:39:02 +0000 | [diff] [blame] | 895 | if (TypeTy *Ty = Actions.getTypeName(*Tok.getIdentifierInfo(), |
Douglas Gregor | 1075a16 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 896 | Tok.getLocation(), CurScope, &SS)) { |
Chris Lattner | a9d6ec7 | 2009-01-05 01:49:50 +0000 | [diff] [blame] | 897 | // This is a typename. Replace the current token in-place with an |
| 898 | // annotation type token. |
Chris Lattner | 5d7eace | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 899 | Tok.setKind(tok::annot_typename); |
Chris Lattner | a9d6ec7 | 2009-01-05 01:49:50 +0000 | [diff] [blame] | 900 | Tok.setAnnotationValue(Ty); |
| 901 | Tok.setAnnotationEndLoc(Tok.getLocation()); |
| 902 | if (SS.isNotEmpty()) // it was a C++ qualified type name. |
| 903 | Tok.setLocation(SS.getBeginLoc()); |
| 904 | |
| 905 | // In case the tokens were cached, have Preprocessor replace |
| 906 | // them with the annotation token. |
| 907 | PP.AnnotateCachedTokens(Tok); |
| 908 | return true; |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | if (!getLang().CPlusPlus) { |
Chris Lattner | a9d6ec7 | 2009-01-05 01:49:50 +0000 | [diff] [blame] | 912 | // If we're in C, we can't have :: tokens at all (the lexer won't return |
| 913 | // them). If the identifier is not a type, then it can't be scope either, |
| 914 | // just early exit. |
| 915 | return false; |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 916 | } |
Chris Lattner | a9d6ec7 | 2009-01-05 01:49:50 +0000 | [diff] [blame] | 917 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 918 | // If this is a template-id, annotate with a template-id or type token. |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 919 | if (NextToken().is(tok::less)) { |
Douglas Gregor | dd13e84 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 920 | TemplateTy Template; |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 921 | if (TemplateNameKind TNK |
| 922 | = Actions.isTemplateName(*Tok.getIdentifierInfo(), |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 923 | CurScope, Template, &SS)) |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 924 | AnnotateTemplateIdToken(Template, TNK, &SS); |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 925 | } |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 926 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 927 | // The current token, which is either an identifier or a |
| 928 | // template-id, is not part of the annotation. Fall through to |
| 929 | // push that token back into the stream and complete the C++ scope |
| 930 | // specifier annotation. |
Douglas Gregor | d302260 | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 931 | } |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 932 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 933 | if (Tok.is(tok::annot_template_id)) { |
| 934 | TemplateIdAnnotation *TemplateId |
| 935 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | aabb850 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 936 | if (TemplateId->Kind == TNK_Type_template) { |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 937 | // A template-id that refers to a type was parsed into a |
| 938 | // template-id annotation in a context where we weren't allowed |
| 939 | // to produce a type annotation token. Update the template-id |
| 940 | // annotation token to a type annotation token now. |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 941 | AnnotateTemplateIdTokenAsType(&SS); |
| 942 | return true; |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 943 | } |
| 944 | } |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 945 | |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 946 | if (SS.isEmpty()) |
Chris Lattner | 1e01594 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 947 | return false; |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 948 | |
| 949 | // A C++ scope specifier that isn't followed by a typename. |
| 950 | // Push the current token back into the token stream (or revert it if it is |
| 951 | // cached) and use an annotation scope token for current token. |
| 952 | if (PP.isBacktrackEnabled()) |
| 953 | PP.RevertCachedTokens(1); |
| 954 | else |
| 955 | PP.EnterToken(Tok); |
| 956 | Tok.setKind(tok::annot_cxxscope); |
Douglas Gregor | 041e929 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 957 | Tok.setAnnotationValue(SS.getScopeRep()); |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 958 | Tok.setAnnotationRange(SS.getRange()); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 959 | |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 960 | // In case the tokens were cached, have Preprocessor replace them with the |
| 961 | // annotation token. |
| 962 | PP.AnnotateCachedTokens(Tok); |
Chris Lattner | 1e01594 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 963 | return true; |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | /// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 967 | /// annotates C++ scope specifiers and template-ids. This returns |
| 968 | /// true if the token was annotated. |
Chris Lattner | 2c30145 | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 969 | /// |
| 970 | /// Note that this routine emits an error if you call it with ::new or ::delete |
| 971 | /// as the current tokens, so only call it in contexts where these are invalid. |
Chris Lattner | 712f9a3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 972 | bool Parser::TryAnnotateCXXScopeToken() { |
Argiris Kirtzidis | 91c80dc | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 973 | assert(getLang().CPlusPlus && |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 974 | "Call sites of this function should be guarded by checking for C++"); |
Chris Lattner | 8376d2e | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 975 | assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) && |
| 976 | "Cannot be a type or scope token!"); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 977 | |
Argiris Kirtzidis | 91c80dc | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 978 | CXXScopeSpec SS; |
Chris Lattner | d706dc8 | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 979 | if (!ParseOptionalCXXScopeSpecifier(SS)) |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 980 | return Tok.is(tok::annot_template_id); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 981 | |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 982 | // Push the current token back into the token stream (or revert it if it is |
| 983 | // cached) and use an annotation scope token for current token. |
| 984 | if (PP.isBacktrackEnabled()) |
| 985 | PP.RevertCachedTokens(1); |
| 986 | else |
| 987 | PP.EnterToken(Tok); |
| 988 | Tok.setKind(tok::annot_cxxscope); |
Douglas Gregor | 041e929 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 989 | Tok.setAnnotationValue(SS.getScopeRep()); |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 990 | Tok.setAnnotationRange(SS.getRange()); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 991 | |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 992 | // In case the tokens were cached, have Preprocessor replace them with the |
| 993 | // annotation token. |
| 994 | PP.AnnotateCachedTokens(Tok); |
Chris Lattner | 712f9a3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 995 | return true; |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 996 | } |