1 //===--- RAIIObjectsForParser.h - RAII helpers for the parser ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines and implements the some simple RAII objects that are used
11 // by the parser to manage bits in recursion.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_PARSE_RAII_OBJECTS_FOR_PARSER_H
16 #define LLVM_CLANG_PARSE_RAII_OBJECTS_FOR_PARSER_H
18 #include "clang/Parse/ParseDiagnostic.h"
19 #include "clang/Parse/Parser.h"
20 #include "clang/Sema/DelayedDiagnostic.h"
21 #include "clang/Sema/Sema.h"
24 // TODO: move ParsingClassDefinition here.
25 // TODO: move TentativeParsingAction here.
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 {
46 sema::DelayedDiagnosticPool DiagnosticPool;
47 Sema::ParsingDeclState State;
51 /// Begin suppressing access-like checks
52 SuppressAccessChecks(Parser &P, bool activate = true)
53 : S(P.getActions()), DiagnosticPool(nullptr) {
55 State = S.PushParsingDeclaration(DiagnosticPool);
63 assert(Active && "trying to end an inactive suppression");
64 S.PopParsingDeclaration(State, nullptr);
69 assert(!Active && "redelaying without having ended first");
70 if (!DiagnosticPool.pool_empty())
71 S.redelayDiagnostics(DiagnosticPool);
72 assert(DiagnosticPool.pool_empty());
75 ~SuppressAccessChecks() {
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 {
86 sema::DelayedDiagnosticPool DiagnosticPool;
87 Sema::ParsingDeclState State;
90 ParsingDeclRAIIObject(const ParsingDeclRAIIObject &) LLVM_DELETED_FUNCTION;
91 void operator=(const ParsingDeclRAIIObject &) LLVM_DELETED_FUNCTION;
94 enum NoParent_t { NoParent };
95 ParsingDeclRAIIObject(Parser &P, NoParent_t _)
96 : Actions(P.getActions()), DiagnosticPool(nullptr) {
100 /// Creates a RAII object whose pool is optionally parented by another.
101 ParsingDeclRAIIObject(Parser &P,
102 const sema::DelayedDiagnosticPool *parentPool)
103 : Actions(P.getActions()), DiagnosticPool(parentPool) {
107 /// Creates a RAII object and, optionally, initialize its
108 /// diagnostics pool by stealing the diagnostics from another
109 /// RAII object (which is assumed to be the current top pool).
110 ParsingDeclRAIIObject(Parser &P, ParsingDeclRAIIObject *other)
111 : Actions(P.getActions()),
112 DiagnosticPool(other ? other->DiagnosticPool.getParent() : nullptr) {
114 DiagnosticPool.steal(other->DiagnosticPool);
120 ~ParsingDeclRAIIObject() {
124 sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() {
125 return DiagnosticPool;
127 const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const {
128 return DiagnosticPool;
131 /// Resets the RAII object for a new declaration.
137 /// Signals that the context was completed without an appropriate
138 /// declaration being parsed.
143 void complete(Decl *D) {
144 assert(!Popped && "ParsingDeclaration has already been popped!");
148 /// Unregister this object from Sema, but remember all the
149 /// diagnostics that were emitted into it.
150 void abortAndRemember() {
156 State = Actions.PushParsingDeclaration(DiagnosticPool);
162 Actions.PopParsingDeclaration(State, D);
168 /// A class for parsing a DeclSpec.
169 class ParsingDeclSpec : public DeclSpec {
170 ParsingDeclRAIIObject ParsingRAII;
173 ParsingDeclSpec(Parser &P)
174 : DeclSpec(P.getAttrFactory()),
175 ParsingRAII(P, ParsingDeclRAIIObject::NoParent) {}
176 ParsingDeclSpec(Parser &P, ParsingDeclRAIIObject *RAII)
177 : DeclSpec(P.getAttrFactory()),
178 ParsingRAII(P, RAII) {}
180 const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const {
181 return ParsingRAII.getDelayedDiagnosticPool();
184 void complete(Decl *D) {
185 ParsingRAII.complete(D);
193 /// A class for parsing a declarator.
194 class ParsingDeclarator : public Declarator {
195 ParsingDeclRAIIObject ParsingRAII;
198 ParsingDeclarator(Parser &P, const ParsingDeclSpec &DS, TheContext C)
199 : Declarator(DS, C), ParsingRAII(P, &DS.getDelayedDiagnosticPool()) {
202 const ParsingDeclSpec &getDeclSpec() const {
203 return static_cast<const ParsingDeclSpec&>(Declarator::getDeclSpec());
206 ParsingDeclSpec &getMutableDeclSpec() const {
207 return const_cast<ParsingDeclSpec&>(getDeclSpec());
215 void complete(Decl *D) {
216 ParsingRAII.complete(D);
220 /// A class for parsing a field declarator.
221 class ParsingFieldDeclarator : public FieldDeclarator {
222 ParsingDeclRAIIObject ParsingRAII;
225 ParsingFieldDeclarator(Parser &P, const ParsingDeclSpec &DS)
226 : FieldDeclarator(DS), ParsingRAII(P, &DS.getDelayedDiagnosticPool()) {
229 const ParsingDeclSpec &getDeclSpec() const {
230 return static_cast<const ParsingDeclSpec&>(D.getDeclSpec());
233 ParsingDeclSpec &getMutableDeclSpec() const {
234 return const_cast<ParsingDeclSpec&>(getDeclSpec());
237 void complete(Decl *D) {
238 ParsingRAII.complete(D);
242 /// ExtensionRAIIObject - This saves the state of extension warnings when
243 /// constructed and disables them. When destructed, it restores them back to
244 /// the way they used to be. This is used to handle __extension__ in the
246 class ExtensionRAIIObject {
247 ExtensionRAIIObject(const ExtensionRAIIObject &) LLVM_DELETED_FUNCTION;
248 void operator=(const ExtensionRAIIObject &) LLVM_DELETED_FUNCTION;
250 DiagnosticsEngine &Diags;
252 ExtensionRAIIObject(DiagnosticsEngine &diags) : Diags(diags) {
253 Diags.IncrementAllExtensionsSilenced();
256 ~ExtensionRAIIObject() {
257 Diags.DecrementAllExtensionsSilenced();
261 /// ColonProtectionRAIIObject - This sets the Parser::ColonIsSacred bool and
262 /// restores it when destroyed. This says that "foo:" should not be
263 /// considered a possible typo for "foo::" for error recovery purposes.
264 class ColonProtectionRAIIObject {
268 ColonProtectionRAIIObject(Parser &p, bool Value = true)
269 : P(p), OldVal(P.ColonIsSacred) {
270 P.ColonIsSacred = Value;
273 /// restore - This can be used to restore the state early, before the dtor
276 P.ColonIsSacred = OldVal;
279 ~ColonProtectionRAIIObject() {
284 /// \brief RAII object that makes '>' behave either as an operator
285 /// or as the closing angle bracket for a template argument list.
286 class GreaterThanIsOperatorScope {
287 bool &GreaterThanIsOperator;
288 bool OldGreaterThanIsOperator;
290 GreaterThanIsOperatorScope(bool >IO, bool Val)
291 : GreaterThanIsOperator(GTIO), OldGreaterThanIsOperator(GTIO) {
292 GreaterThanIsOperator = Val;
295 ~GreaterThanIsOperatorScope() {
296 GreaterThanIsOperator = OldGreaterThanIsOperator;
300 class InMessageExpressionRAIIObject {
301 bool &InMessageExpression;
305 InMessageExpressionRAIIObject(Parser &P, bool Value)
306 : InMessageExpression(P.InMessageExpression),
307 OldValue(P.InMessageExpression) {
308 InMessageExpression = Value;
311 ~InMessageExpressionRAIIObject() {
312 InMessageExpression = OldValue;
316 /// \brief RAII object that makes sure paren/bracket/brace count is correct
317 /// after declaration/statement parsing, even when there's a parsing error.
318 class ParenBraceBracketBalancer {
320 unsigned short ParenCount, BracketCount, BraceCount;
322 ParenBraceBracketBalancer(Parser &p)
323 : P(p), ParenCount(p.ParenCount), BracketCount(p.BracketCount),
324 BraceCount(p.BraceCount) { }
326 ~ParenBraceBracketBalancer() {
327 P.ParenCount = ParenCount;
328 P.BracketCount = BracketCount;
329 P.BraceCount = BraceCount;
333 class PoisonSEHIdentifiersRAIIObject {
334 PoisonIdentifierRAIIObject Ident_AbnormalTermination;
335 PoisonIdentifierRAIIObject Ident_GetExceptionCode;
336 PoisonIdentifierRAIIObject Ident_GetExceptionInfo;
337 PoisonIdentifierRAIIObject Ident__abnormal_termination;
338 PoisonIdentifierRAIIObject Ident__exception_code;
339 PoisonIdentifierRAIIObject Ident__exception_info;
340 PoisonIdentifierRAIIObject Ident___abnormal_termination;
341 PoisonIdentifierRAIIObject Ident___exception_code;
342 PoisonIdentifierRAIIObject Ident___exception_info;
344 PoisonSEHIdentifiersRAIIObject(Parser &Self, bool NewValue)
345 : Ident_AbnormalTermination(Self.Ident_AbnormalTermination, NewValue),
346 Ident_GetExceptionCode(Self.Ident_GetExceptionCode, NewValue),
347 Ident_GetExceptionInfo(Self.Ident_GetExceptionInfo, NewValue),
348 Ident__abnormal_termination(Self.Ident__abnormal_termination, NewValue),
349 Ident__exception_code(Self.Ident__exception_code, NewValue),
350 Ident__exception_info(Self.Ident__exception_info, NewValue),
351 Ident___abnormal_termination(Self.Ident___abnormal_termination, NewValue),
352 Ident___exception_code(Self.Ident___exception_code, NewValue),
353 Ident___exception_info(Self.Ident___exception_info, NewValue) {
357 /// \brief RAII class that helps handle the parsing of an open/close delimiter
358 /// pair, such as braces { ... } or parentheses ( ... ).
359 class BalancedDelimiterTracker : public GreaterThanIsOperatorScope {
361 tok::TokenKind Kind, Close, FinalToken;
362 SourceLocation (Parser::*Consumer)();
363 SourceLocation LOpen, LClose;
365 unsigned short &getDepth() {
367 case tok::l_brace: return P.BraceCount;
368 case tok::l_square: return P.BracketCount;
369 case tok::l_paren: return P.ParenCount;
370 default: llvm_unreachable("Wrong token kind");
374 enum { MaxDepth = 256 };
376 bool diagnoseOverflow();
377 bool diagnoseMissingClose();
380 BalancedDelimiterTracker(Parser& p, tok::TokenKind k,
381 tok::TokenKind FinalToken = tok::semi)
382 : GreaterThanIsOperatorScope(p.GreaterThanIsOperator, true),
383 P(p), Kind(k), FinalToken(FinalToken)
386 default: llvm_unreachable("Unexpected balanced token");
388 Close = tok::r_brace;
389 Consumer = &Parser::ConsumeBrace;
392 Close = tok::r_paren;
393 Consumer = &Parser::ConsumeParen;
397 Close = tok::r_square;
398 Consumer = &Parser::ConsumeBracket;
403 SourceLocation getOpenLocation() const { return LOpen; }
404 SourceLocation getCloseLocation() const { return LClose; }
405 SourceRange getRange() const { return SourceRange(LOpen, LClose); }
411 if (getDepth() < P.getLangOpts().BracketDepth) {
412 LOpen = (P.*Consumer)();
416 return diagnoseOverflow();
419 bool expectAndConsume(unsigned DiagID = diag::err_expected,
420 const char *Msg = "",
421 tok::TokenKind SkipToTok = tok::unknown);
422 bool consumeClose() {
423 if (P.Tok.is(Close)) {
424 LClose = (P.*Consumer)();
428 return diagnoseMissingClose();
433 } // end namespace clang