1 //===--- RAIIObjectsForParser.h - RAII helpers for the parser ---*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines and implements the some simple RAII objects that are used
10 // by the parser to manage bits in recursion.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_PARSE_RAIIOBJECTSFORPARSER_H
15 #define LLVM_CLANG_LIB_PARSE_RAIIOBJECTSFORPARSER_H
17 #include "clang/Parse/ParseDiagnostic.h"
18 #include "clang/Parse/Parser.h"
19 #include "clang/Sema/DelayedDiagnostic.h"
20 #include "clang/Sema/ParsedTemplate.h"
21 #include "clang/Sema/Sema.h"
24 // TODO: move ParsingClassDefinition here.
25 // TODO: move TentativeParsingAction here.
27 /// 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);
61 SuppressAccessChecks(SuppressAccessChecks &&Other)
62 : S(Other.S), DiagnosticPool(std::move(Other.DiagnosticPool)),
63 State(Other.State), Active(Other.Active) {
66 void operator=(SuppressAccessChecks &&Other) = delete;
69 assert(Active && "trying to end an inactive suppression");
70 S.PopParsingDeclaration(State, nullptr);
75 assert(!Active && "redelaying without having ended first");
76 if (!DiagnosticPool.pool_empty())
77 S.redelayDiagnostics(DiagnosticPool);
78 assert(DiagnosticPool.pool_empty());
81 ~SuppressAccessChecks() {
86 /// RAII object used to inform the actions that we're
87 /// currently parsing a declaration. This is active when parsing a
88 /// variable's initializer, but not when parsing the body of a
89 /// class or function definition.
90 class ParsingDeclRAIIObject {
92 sema::DelayedDiagnosticPool DiagnosticPool;
93 Sema::ParsingDeclState State;
96 ParsingDeclRAIIObject(const ParsingDeclRAIIObject &) = delete;
97 void operator=(const ParsingDeclRAIIObject &) = delete;
100 enum NoParent_t { NoParent };
101 ParsingDeclRAIIObject(Parser &P, NoParent_t _)
102 : Actions(P.getActions()), DiagnosticPool(nullptr) {
106 /// Creates a RAII object whose pool is optionally parented by another.
107 ParsingDeclRAIIObject(Parser &P,
108 const sema::DelayedDiagnosticPool *parentPool)
109 : Actions(P.getActions()), DiagnosticPool(parentPool) {
113 /// Creates a RAII object and, optionally, initialize its
114 /// diagnostics pool by stealing the diagnostics from another
115 /// RAII object (which is assumed to be the current top pool).
116 ParsingDeclRAIIObject(Parser &P, ParsingDeclRAIIObject *other)
117 : Actions(P.getActions()),
118 DiagnosticPool(other ? other->DiagnosticPool.getParent() : nullptr) {
120 DiagnosticPool.steal(other->DiagnosticPool);
126 ~ParsingDeclRAIIObject() {
130 sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() {
131 return DiagnosticPool;
133 const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const {
134 return DiagnosticPool;
137 /// Resets the RAII object for a new declaration.
143 /// Signals that the context was completed without an appropriate
144 /// declaration being parsed.
149 void complete(Decl *D) {
150 assert(!Popped && "ParsingDeclaration has already been popped!");
154 /// Unregister this object from Sema, but remember all the
155 /// diagnostics that were emitted into it.
156 void abortAndRemember() {
162 State = Actions.PushParsingDeclaration(DiagnosticPool);
168 Actions.PopParsingDeclaration(State, D);
174 /// A class for parsing a DeclSpec.
175 class ParsingDeclSpec : public DeclSpec {
176 ParsingDeclRAIIObject ParsingRAII;
179 ParsingDeclSpec(Parser &P)
180 : DeclSpec(P.getAttrFactory()),
181 ParsingRAII(P, ParsingDeclRAIIObject::NoParent) {}
182 ParsingDeclSpec(Parser &P, ParsingDeclRAIIObject *RAII)
183 : DeclSpec(P.getAttrFactory()),
184 ParsingRAII(P, RAII) {}
186 const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const {
187 return ParsingRAII.getDelayedDiagnosticPool();
190 void complete(Decl *D) {
191 ParsingRAII.complete(D);
199 /// A class for parsing a declarator.
200 class ParsingDeclarator : public Declarator {
201 ParsingDeclRAIIObject ParsingRAII;
204 ParsingDeclarator(Parser &P, const ParsingDeclSpec &DS, DeclaratorContext C)
205 : Declarator(DS, C), ParsingRAII(P, &DS.getDelayedDiagnosticPool()) {
208 const ParsingDeclSpec &getDeclSpec() const {
209 return static_cast<const ParsingDeclSpec&>(Declarator::getDeclSpec());
212 ParsingDeclSpec &getMutableDeclSpec() const {
213 return const_cast<ParsingDeclSpec&>(getDeclSpec());
221 void complete(Decl *D) {
222 ParsingRAII.complete(D);
226 /// A class for parsing a field declarator.
227 class ParsingFieldDeclarator : public FieldDeclarator {
228 ParsingDeclRAIIObject ParsingRAII;
231 ParsingFieldDeclarator(Parser &P, const ParsingDeclSpec &DS)
232 : FieldDeclarator(DS), ParsingRAII(P, &DS.getDelayedDiagnosticPool()) {
235 const ParsingDeclSpec &getDeclSpec() const {
236 return static_cast<const ParsingDeclSpec&>(D.getDeclSpec());
239 ParsingDeclSpec &getMutableDeclSpec() const {
240 return const_cast<ParsingDeclSpec&>(getDeclSpec());
243 void complete(Decl *D) {
244 ParsingRAII.complete(D);
248 /// ExtensionRAIIObject - This saves the state of extension warnings when
249 /// constructed and disables them. When destructed, it restores them back to
250 /// the way they used to be. This is used to handle __extension__ in the
252 class ExtensionRAIIObject {
253 ExtensionRAIIObject(const ExtensionRAIIObject &) = delete;
254 void operator=(const ExtensionRAIIObject &) = delete;
256 DiagnosticsEngine &Diags;
258 ExtensionRAIIObject(DiagnosticsEngine &diags) : Diags(diags) {
259 Diags.IncrementAllExtensionsSilenced();
262 ~ExtensionRAIIObject() {
263 Diags.DecrementAllExtensionsSilenced();
267 /// ColonProtectionRAIIObject - This sets the Parser::ColonIsSacred bool and
268 /// restores it when destroyed. This says that "foo:" should not be
269 /// considered a possible typo for "foo::" for error recovery purposes.
270 class ColonProtectionRAIIObject {
274 ColonProtectionRAIIObject(Parser &p, bool Value = true)
275 : P(p), OldVal(P.ColonIsSacred) {
276 P.ColonIsSacred = Value;
279 /// restore - This can be used to restore the state early, before the dtor
282 P.ColonIsSacred = OldVal;
285 ~ColonProtectionRAIIObject() {
290 /// RAII object that makes '>' behave either as an operator
291 /// or as the closing angle bracket for a template argument list.
292 class GreaterThanIsOperatorScope {
293 bool &GreaterThanIsOperator;
294 bool OldGreaterThanIsOperator;
296 GreaterThanIsOperatorScope(bool >IO, bool Val)
297 : GreaterThanIsOperator(GTIO), OldGreaterThanIsOperator(GTIO) {
298 GreaterThanIsOperator = Val;
301 ~GreaterThanIsOperatorScope() {
302 GreaterThanIsOperator = OldGreaterThanIsOperator;
306 class InMessageExpressionRAIIObject {
307 bool &InMessageExpression;
311 InMessageExpressionRAIIObject(Parser &P, bool Value)
312 : InMessageExpression(P.InMessageExpression),
313 OldValue(P.InMessageExpression) {
314 InMessageExpression = Value;
317 ~InMessageExpressionRAIIObject() {
318 InMessageExpression = OldValue;
322 /// RAII object that makes sure paren/bracket/brace count is correct
323 /// after declaration/statement parsing, even when there's a parsing error.
324 class ParenBraceBracketBalancer {
326 unsigned short ParenCount, BracketCount, BraceCount;
328 ParenBraceBracketBalancer(Parser &p)
329 : P(p), ParenCount(p.ParenCount), BracketCount(p.BracketCount),
330 BraceCount(p.BraceCount) { }
332 ~ParenBraceBracketBalancer() {
333 P.AngleBrackets.clear(P);
334 P.ParenCount = ParenCount;
335 P.BracketCount = BracketCount;
336 P.BraceCount = BraceCount;
340 class PoisonSEHIdentifiersRAIIObject {
341 PoisonIdentifierRAIIObject Ident_AbnormalTermination;
342 PoisonIdentifierRAIIObject Ident_GetExceptionCode;
343 PoisonIdentifierRAIIObject Ident_GetExceptionInfo;
344 PoisonIdentifierRAIIObject Ident__abnormal_termination;
345 PoisonIdentifierRAIIObject Ident__exception_code;
346 PoisonIdentifierRAIIObject Ident__exception_info;
347 PoisonIdentifierRAIIObject Ident___abnormal_termination;
348 PoisonIdentifierRAIIObject Ident___exception_code;
349 PoisonIdentifierRAIIObject Ident___exception_info;
351 PoisonSEHIdentifiersRAIIObject(Parser &Self, bool NewValue)
352 : Ident_AbnormalTermination(Self.Ident_AbnormalTermination, NewValue),
353 Ident_GetExceptionCode(Self.Ident_GetExceptionCode, NewValue),
354 Ident_GetExceptionInfo(Self.Ident_GetExceptionInfo, NewValue),
355 Ident__abnormal_termination(Self.Ident__abnormal_termination, NewValue),
356 Ident__exception_code(Self.Ident__exception_code, NewValue),
357 Ident__exception_info(Self.Ident__exception_info, NewValue),
358 Ident___abnormal_termination(Self.Ident___abnormal_termination, NewValue),
359 Ident___exception_code(Self.Ident___exception_code, NewValue),
360 Ident___exception_info(Self.Ident___exception_info, NewValue) {
364 /// RAII class that helps handle the parsing of an open/close delimiter
365 /// pair, such as braces { ... } or parentheses ( ... ).
366 class BalancedDelimiterTracker : public GreaterThanIsOperatorScope {
368 tok::TokenKind Kind, Close, FinalToken;
369 SourceLocation (Parser::*Consumer)();
370 SourceLocation LOpen, LClose;
372 unsigned short &getDepth() {
374 case tok::l_brace: return P.BraceCount;
375 case tok::l_square: return P.BracketCount;
376 case tok::l_paren: return P.ParenCount;
377 default: llvm_unreachable("Wrong token kind");
381 bool diagnoseOverflow();
382 bool diagnoseMissingClose();
385 BalancedDelimiterTracker(Parser& p, tok::TokenKind k,
386 tok::TokenKind FinalToken = tok::semi)
387 : GreaterThanIsOperatorScope(p.GreaterThanIsOperator, true),
388 P(p), Kind(k), FinalToken(FinalToken)
391 default: llvm_unreachable("Unexpected balanced token");
393 Close = tok::r_brace;
394 Consumer = &Parser::ConsumeBrace;
397 Close = tok::r_paren;
398 Consumer = &Parser::ConsumeParen;
402 Close = tok::r_square;
403 Consumer = &Parser::ConsumeBracket;
408 SourceLocation getOpenLocation() const { return LOpen; }
409 SourceLocation getCloseLocation() const { return LClose; }
410 SourceRange getRange() const { return SourceRange(LOpen, LClose); }
416 if (getDepth() < P.getLangOpts().BracketDepth) {
417 LOpen = (P.*Consumer)();
421 return diagnoseOverflow();
424 bool expectAndConsume(unsigned DiagID = diag::err_expected,
425 const char *Msg = "",
426 tok::TokenKind SkipToTok = tok::unknown);
427 bool consumeClose() {
428 if (P.Tok.is(Close)) {
429 LClose = (P.*Consumer)();
431 } else if (P.Tok.is(tok::semi) && P.NextToken().is(Close)) {
432 SourceLocation SemiLoc = P.ConsumeToken();
433 P.Diag(SemiLoc, diag::err_unexpected_semi)
434 << Close << FixItHint::CreateRemoval(SourceRange(SemiLoc, SemiLoc));
435 LClose = (P.*Consumer)();
439 return diagnoseMissingClose();
444 /// RAIIObject to destroy the contents of a SmallVector of
445 /// TemplateIdAnnotation pointers and clear the vector.
446 class DestroyTemplateIdAnnotationsRAIIObj {
447 SmallVectorImpl<TemplateIdAnnotation *> &Container;
450 DestroyTemplateIdAnnotationsRAIIObj(
451 SmallVectorImpl<TemplateIdAnnotation *> &Container)
452 : Container(Container) {}
454 ~DestroyTemplateIdAnnotationsRAIIObj() {
455 for (SmallVectorImpl<TemplateIdAnnotation *>::iterator I =
463 } // end namespace clang