Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 1 | //===--- RAIIObjectsForParser.h - RAII helpers for the parser ---*- C++ -*-===// |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 10 | // This file defines and implements the some simple RAII objects that are used |
| 11 | // by the parser to manage bits in recursion. |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 15 | #ifndef LLVM_CLANG_PARSE_RAII_OBJECTS_FOR_PARSER_H |
| 16 | #define LLVM_CLANG_PARSE_RAII_OBJECTS_FOR_PARSER_H |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/Parse/ParseDiagnostic.h" |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Parser.h" |
| 20 | #include "clang/Sema/DelayedDiagnostic.h" |
| 21 | #include "clang/Sema/Sema.h" |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 22 | |
| 23 | namespace clang { |
Chris Lattner | d0d76f1 | 2009-12-10 00:44:03 +0000 | [diff] [blame] | 24 | // TODO: move ParsingClassDefinition here. |
| 25 | // TODO: move TentativeParsingAction here. |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 26 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 27 | /// \brief A RAII object used to temporarily suppress access-like |
| 28 | /// checking. Access-like checks are those associated with |
| 29 | /// controlling the use of a declaration, like C++ access control |
| 30 | /// errors and deprecation warnings. They are contextually |
| 31 | /// dependent, in that they can only be resolved with full |
| 32 | /// information about what's being declared. They are also |
| 33 | /// suppressed in certain contexts, like the template arguments of |
| 34 | /// an explicit instantiation. However, those suppression contexts |
| 35 | /// cannot necessarily be fully determined in advance; for |
| 36 | /// example, something starting like this: |
| 37 | /// template <> class std::vector<A::PrivateType> |
| 38 | /// might be the entirety of an explicit instantiation: |
| 39 | /// template <> class std::vector<A::PrivateType>; |
| 40 | /// or just an elaborated type specifier: |
| 41 | /// template <> class std::vector<A::PrivateType> make_vector<>(); |
| 42 | /// Therefore this class collects all the diagnostics and permits |
| 43 | /// them to be re-delayed in a new context. |
| 44 | class SuppressAccessChecks { |
| 45 | Sema &S; |
| 46 | sema::DelayedDiagnosticPool DiagnosticPool; |
| 47 | Sema::ParsingDeclState State; |
| 48 | bool Active; |
| 49 | |
| 50 | public: |
| 51 | /// Begin suppressing access-like checks |
| 52 | SuppressAccessChecks(Parser &P, bool activate = true) |
| 53 | : S(P.getActions()), DiagnosticPool(NULL) { |
| 54 | if (activate) { |
| 55 | State = S.PushParsingDeclaration(DiagnosticPool); |
| 56 | Active = true; |
| 57 | } else { |
| 58 | Active = false; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void done() { |
| 63 | assert(Active && "trying to end an inactive suppression"); |
| 64 | S.PopParsingDeclaration(State, NULL); |
| 65 | Active = false; |
| 66 | } |
| 67 | |
| 68 | void redelay() { |
| 69 | assert(!Active && "redelaying without having ended first"); |
| 70 | if (!DiagnosticPool.pool_empty()) |
| 71 | S.redelayDiagnostics(DiagnosticPool); |
| 72 | assert(DiagnosticPool.pool_empty()); |
| 73 | } |
| 74 | |
| 75 | ~SuppressAccessChecks() { |
| 76 | if (Active) done(); |
| 77 | } |
| 78 | }; |
| 79 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 80 | /// \brief RAII object used to inform the actions that we're |
| 81 | /// currently parsing a declaration. This is active when parsing a |
| 82 | /// variable's initializer, but not when parsing the body of a |
| 83 | /// class or function definition. |
| 84 | class ParsingDeclRAIIObject { |
| 85 | Sema &Actions; |
| 86 | sema::DelayedDiagnosticPool DiagnosticPool; |
| 87 | Sema::ParsingDeclState State; |
| 88 | bool Popped; |
| 89 | |
| 90 | // Do not implement. |
| 91 | ParsingDeclRAIIObject(const ParsingDeclRAIIObject &other); |
| 92 | ParsingDeclRAIIObject &operator=(const ParsingDeclRAIIObject &other); |
| 93 | |
| 94 | public: |
| 95 | enum NoParent_t { NoParent }; |
| 96 | ParsingDeclRAIIObject(Parser &P, NoParent_t _) |
| 97 | : Actions(P.getActions()), DiagnosticPool(NULL) { |
| 98 | push(); |
| 99 | } |
| 100 | |
| 101 | /// Creates a RAII object whose pool is optionally parented by another. |
| 102 | ParsingDeclRAIIObject(Parser &P, |
| 103 | const sema::DelayedDiagnosticPool *parentPool) |
| 104 | : Actions(P.getActions()), DiagnosticPool(parentPool) { |
| 105 | push(); |
| 106 | } |
| 107 | |
| 108 | /// Creates a RAII object and, optionally, initialize its |
| 109 | /// diagnostics pool by stealing the diagnostics from another |
| 110 | /// RAII object (which is assumed to be the current top pool). |
| 111 | ParsingDeclRAIIObject(Parser &P, ParsingDeclRAIIObject *other) |
| 112 | : Actions(P.getActions()), |
| 113 | DiagnosticPool(other ? other->DiagnosticPool.getParent() : NULL) { |
| 114 | if (other) { |
| 115 | DiagnosticPool.steal(other->DiagnosticPool); |
| 116 | other->abort(); |
| 117 | } |
| 118 | push(); |
| 119 | } |
| 120 | |
| 121 | ~ParsingDeclRAIIObject() { |
| 122 | abort(); |
| 123 | } |
| 124 | |
| 125 | sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() { |
| 126 | return DiagnosticPool; |
| 127 | } |
| 128 | const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const { |
| 129 | return DiagnosticPool; |
| 130 | } |
| 131 | |
| 132 | /// Resets the RAII object for a new declaration. |
| 133 | void reset() { |
| 134 | abort(); |
| 135 | push(); |
| 136 | } |
| 137 | |
| 138 | /// Signals that the context was completed without an appropriate |
| 139 | /// declaration being parsed. |
| 140 | void abort() { |
| 141 | pop(0); |
| 142 | } |
| 143 | |
| 144 | void complete(Decl *D) { |
| 145 | assert(!Popped && "ParsingDeclaration has already been popped!"); |
| 146 | pop(D); |
| 147 | } |
| 148 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 149 | /// Unregister this object from Sema, but remember all the |
| 150 | /// diagnostics that were emitted into it. |
| 151 | void abortAndRemember() { |
| 152 | pop(0); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 153 | } |
| 154 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 155 | private: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 156 | void push() { |
| 157 | State = Actions.PushParsingDeclaration(DiagnosticPool); |
| 158 | Popped = false; |
| 159 | } |
| 160 | |
| 161 | void pop(Decl *D) { |
| 162 | if (!Popped) { |
| 163 | Actions.PopParsingDeclaration(State, D); |
| 164 | Popped = true; |
| 165 | } |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | /// A class for parsing a DeclSpec. |
| 170 | class ParsingDeclSpec : public DeclSpec { |
| 171 | ParsingDeclRAIIObject ParsingRAII; |
| 172 | |
| 173 | public: |
| 174 | ParsingDeclSpec(Parser &P) |
| 175 | : DeclSpec(P.getAttrFactory()), |
| 176 | ParsingRAII(P, ParsingDeclRAIIObject::NoParent) {} |
| 177 | ParsingDeclSpec(Parser &P, ParsingDeclRAIIObject *RAII) |
| 178 | : DeclSpec(P.getAttrFactory()), |
| 179 | ParsingRAII(P, RAII) {} |
| 180 | |
| 181 | const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const { |
| 182 | return ParsingRAII.getDelayedDiagnosticPool(); |
| 183 | } |
| 184 | |
| 185 | void complete(Decl *D) { |
| 186 | ParsingRAII.complete(D); |
| 187 | } |
| 188 | |
| 189 | void abort() { |
| 190 | ParsingRAII.abort(); |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | /// A class for parsing a declarator. |
| 195 | class ParsingDeclarator : public Declarator { |
| 196 | ParsingDeclRAIIObject ParsingRAII; |
| 197 | |
| 198 | public: |
| 199 | ParsingDeclarator(Parser &P, const ParsingDeclSpec &DS, TheContext C) |
| 200 | : Declarator(DS, C), ParsingRAII(P, &DS.getDelayedDiagnosticPool()) { |
| 201 | } |
| 202 | |
| 203 | const ParsingDeclSpec &getDeclSpec() const { |
| 204 | return static_cast<const ParsingDeclSpec&>(Declarator::getDeclSpec()); |
| 205 | } |
| 206 | |
| 207 | ParsingDeclSpec &getMutableDeclSpec() const { |
| 208 | return const_cast<ParsingDeclSpec&>(getDeclSpec()); |
| 209 | } |
| 210 | |
| 211 | void clear() { |
| 212 | Declarator::clear(); |
| 213 | ParsingRAII.reset(); |
| 214 | } |
| 215 | |
| 216 | void complete(Decl *D) { |
| 217 | ParsingRAII.complete(D); |
| 218 | } |
| 219 | }; |
| 220 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 221 | /// ExtensionRAIIObject - This saves the state of extension warnings when |
| 222 | /// constructed and disables them. When destructed, it restores them back to |
| 223 | /// the way they used to be. This is used to handle __extension__ in the |
| 224 | /// parser. |
| 225 | class ExtensionRAIIObject { |
| 226 | void operator=(const ExtensionRAIIObject &); // DO NOT IMPLEMENT |
| 227 | ExtensionRAIIObject(const ExtensionRAIIObject&); // DO NOT IMPLEMENT |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 228 | DiagnosticsEngine &Diags; |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 229 | public: |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 230 | ExtensionRAIIObject(DiagnosticsEngine &diags) : Diags(diags) { |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 231 | Diags.IncrementAllExtensionsSilenced(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 232 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 234 | ~ExtensionRAIIObject() { |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 235 | Diags.DecrementAllExtensionsSilenced(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 236 | } |
| 237 | }; |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 239 | /// ColonProtectionRAIIObject - This sets the Parser::ColonIsSacred bool and |
| 240 | /// restores it when destroyed. This says that "foo:" should not be |
| 241 | /// considered a possible typo for "foo::" for error recovery purposes. |
| 242 | class ColonProtectionRAIIObject { |
| 243 | Parser &P; |
| 244 | bool OldVal; |
| 245 | public: |
Chris Lattner | 932dff7 | 2009-12-10 02:08:07 +0000 | [diff] [blame] | 246 | ColonProtectionRAIIObject(Parser &p, bool Value = true) |
| 247 | : P(p), OldVal(P.ColonIsSacred) { |
| 248 | P.ColonIsSacred = Value; |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Chris Lattner | 6fb09c8 | 2009-12-10 00:38:54 +0000 | [diff] [blame] | 251 | /// restore - This can be used to restore the state early, before the dtor |
| 252 | /// is run. |
| 253 | void restore() { |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 254 | P.ColonIsSacred = OldVal; |
| 255 | } |
Chris Lattner | 6fb09c8 | 2009-12-10 00:38:54 +0000 | [diff] [blame] | 256 | |
| 257 | ~ColonProtectionRAIIObject() { |
| 258 | restore(); |
| 259 | } |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
Chris Lattner | d0d76f1 | 2009-12-10 00:44:03 +0000 | [diff] [blame] | 262 | /// \brief RAII object that makes '>' behave either as an operator |
| 263 | /// or as the closing angle bracket for a template argument list. |
Benjamin Kramer | 648d846 | 2009-12-10 21:50:21 +0000 | [diff] [blame] | 264 | class GreaterThanIsOperatorScope { |
Chris Lattner | d0d76f1 | 2009-12-10 00:44:03 +0000 | [diff] [blame] | 265 | bool &GreaterThanIsOperator; |
| 266 | bool OldGreaterThanIsOperator; |
Benjamin Kramer | 648d846 | 2009-12-10 21:50:21 +0000 | [diff] [blame] | 267 | public: |
Chris Lattner | d0d76f1 | 2009-12-10 00:44:03 +0000 | [diff] [blame] | 268 | GreaterThanIsOperatorScope(bool >IO, bool Val) |
| 269 | : GreaterThanIsOperator(GTIO), OldGreaterThanIsOperator(GTIO) { |
| 270 | GreaterThanIsOperator = Val; |
| 271 | } |
| 272 | |
| 273 | ~GreaterThanIsOperatorScope() { |
| 274 | GreaterThanIsOperator = OldGreaterThanIsOperator; |
| 275 | } |
| 276 | }; |
| 277 | |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 278 | class InMessageExpressionRAIIObject { |
| 279 | bool &InMessageExpression; |
| 280 | bool OldValue; |
| 281 | |
| 282 | public: |
| 283 | InMessageExpressionRAIIObject(Parser &P, bool Value) |
| 284 | : InMessageExpression(P.InMessageExpression), |
| 285 | OldValue(P.InMessageExpression) { |
| 286 | InMessageExpression = Value; |
| 287 | } |
| 288 | |
| 289 | ~InMessageExpressionRAIIObject() { |
| 290 | InMessageExpression = OldValue; |
| 291 | } |
| 292 | }; |
| 293 | |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 294 | /// \brief RAII object that makes sure paren/bracket/brace count is correct |
| 295 | /// after declaration/statement parsing, even when there's a parsing error. |
| 296 | class ParenBraceBracketBalancer { |
| 297 | Parser &P; |
| 298 | unsigned short ParenCount, BracketCount, BraceCount; |
| 299 | public: |
| 300 | ParenBraceBracketBalancer(Parser &p) |
| 301 | : P(p), ParenCount(p.ParenCount), BracketCount(p.BracketCount), |
| 302 | BraceCount(p.BraceCount) { } |
| 303 | |
| 304 | ~ParenBraceBracketBalancer() { |
| 305 | P.ParenCount = ParenCount; |
| 306 | P.BracketCount = BracketCount; |
| 307 | P.BraceCount = BraceCount; |
| 308 | } |
| 309 | }; |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 310 | |
| 311 | class PoisonSEHIdentifiersRAIIObject { |
| 312 | PoisonIdentifierRAIIObject Ident_AbnormalTermination; |
| 313 | PoisonIdentifierRAIIObject Ident_GetExceptionCode; |
| 314 | PoisonIdentifierRAIIObject Ident_GetExceptionInfo; |
| 315 | PoisonIdentifierRAIIObject Ident__abnormal_termination; |
| 316 | PoisonIdentifierRAIIObject Ident__exception_code; |
| 317 | PoisonIdentifierRAIIObject Ident__exception_info; |
| 318 | PoisonIdentifierRAIIObject Ident___abnormal_termination; |
| 319 | PoisonIdentifierRAIIObject Ident___exception_code; |
| 320 | PoisonIdentifierRAIIObject Ident___exception_info; |
| 321 | public: |
| 322 | PoisonSEHIdentifiersRAIIObject(Parser &Self, bool NewValue) |
| 323 | : Ident_AbnormalTermination(Self.Ident_AbnormalTermination, NewValue), |
| 324 | Ident_GetExceptionCode(Self.Ident_GetExceptionCode, NewValue), |
| 325 | Ident_GetExceptionInfo(Self.Ident_GetExceptionInfo, NewValue), |
| 326 | Ident__abnormal_termination(Self.Ident__abnormal_termination, NewValue), |
| 327 | Ident__exception_code(Self.Ident__exception_code, NewValue), |
| 328 | Ident__exception_info(Self.Ident__exception_info, NewValue), |
| 329 | Ident___abnormal_termination(Self.Ident___abnormal_termination, NewValue), |
| 330 | Ident___exception_code(Self.Ident___exception_code, NewValue), |
| 331 | Ident___exception_info(Self.Ident___exception_info, NewValue) { |
| 332 | } |
| 333 | }; |
| 334 | |
Douglas Gregor | c86c40b | 2012-06-06 21:18:07 +0000 | [diff] [blame] | 335 | /// \brief RAII class that helps handle the parsing of an open/close delimiter |
| 336 | /// pair, such as braces { ... } or parentheses ( ... ). |
| 337 | class BalancedDelimiterTracker : public GreaterThanIsOperatorScope { |
| 338 | Parser& P; |
| 339 | tok::TokenKind Kind, Close; |
| 340 | SourceLocation (Parser::*Consumer)(); |
| 341 | SourceLocation LOpen, LClose; |
| 342 | |
| 343 | unsigned short &getDepth() { |
| 344 | switch (Kind) { |
| 345 | case tok::l_brace: return P.BraceCount; |
| 346 | case tok::l_square: return P.BracketCount; |
| 347 | case tok::l_paren: return P.ParenCount; |
| 348 | default: llvm_unreachable("Wrong token kind"); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | enum { MaxDepth = 256 }; |
| 353 | |
| 354 | bool diagnoseOverflow(); |
| 355 | bool diagnoseMissingClose(); |
| 356 | |
| 357 | public: |
| 358 | BalancedDelimiterTracker(Parser& p, tok::TokenKind k) |
| 359 | : GreaterThanIsOperatorScope(p.GreaterThanIsOperator, true), |
| 360 | P(p), Kind(k) |
| 361 | { |
| 362 | switch (Kind) { |
| 363 | default: llvm_unreachable("Unexpected balanced token"); |
| 364 | case tok::l_brace: |
| 365 | Close = tok::r_brace; |
| 366 | Consumer = &Parser::ConsumeBrace; |
| 367 | break; |
| 368 | case tok::l_paren: |
| 369 | Close = tok::r_paren; |
| 370 | Consumer = &Parser::ConsumeParen; |
| 371 | break; |
| 372 | |
| 373 | case tok::l_square: |
| 374 | Close = tok::r_square; |
| 375 | Consumer = &Parser::ConsumeBracket; |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | SourceLocation getOpenLocation() const { return LOpen; } |
| 381 | SourceLocation getCloseLocation() const { return LClose; } |
| 382 | SourceRange getRange() const { return SourceRange(LOpen, LClose); } |
| 383 | |
| 384 | bool consumeOpen() { |
| 385 | if (!P.Tok.is(Kind)) |
| 386 | return true; |
| 387 | |
| 388 | if (getDepth() < MaxDepth) { |
| 389 | LOpen = (P.*Consumer)(); |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | return diagnoseOverflow(); |
| 394 | } |
| 395 | |
| 396 | bool expectAndConsume(unsigned DiagID, |
| 397 | const char *Msg = "", |
| 398 | tok::TokenKind SkipToTok = tok::unknown); |
| 399 | bool consumeClose() { |
| 400 | if (P.Tok.is(Close)) { |
| 401 | LClose = (P.*Consumer)(); |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | return diagnoseMissingClose(); |
| 406 | } |
| 407 | void skipToEnd(); |
| 408 | }; |
| 409 | |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 410 | } // end namespace clang |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 411 | |
| 412 | #endif |