RAIIObjectsForParser.h revision 218893
1111375Simp//===--- RAIIObjectsForParser.h - RAII helpers for the parser ---*- C++ -*-===//
2111375Simp//
3111375Simp//                     The LLVM Compiler Infrastructure
4111375Simp//
5111375Simp// This file is distributed under the University of Illinois Open Source
6111375Simp// License. See LICENSE.TXT for details.
7111375Simp//
8111375Simp//===----------------------------------------------------------------------===//
9111375Simp//
10111375Simp// This file defines and implements the some simple RAII objects that are used
11111375Simp// by the parser to manage bits in recursion.
12111375Simp//
13111375Simp//===----------------------------------------------------------------------===//
14111375Simp
15111375Simp#ifndef LLVM_CLANG_PARSE_RAII_OBJECTS_FOR_PARSER_H
16111375Simp#define LLVM_CLANG_PARSE_RAII_OBJECTS_FOR_PARSER_H
17111375Simp
18111375Simp#include "clang/Parse/ParseDiagnostic.h"
19111375Simp
20111375Simpnamespace clang {
21111375Simp  // TODO: move ParsingDeclRAIIObject here.
22111375Simp  // TODO: move ParsingClassDefinition here.
23111375Simp  // TODO: move TentativeParsingAction here.
24111375Simp
25111762Sru
26111375Simp  /// ExtensionRAIIObject - This saves the state of extension warnings when
27111375Simp  /// constructed and disables them.  When destructed, it restores them back to
28111375Simp  /// the way they used to be.  This is used to handle __extension__ in the
29111375Simp  /// parser.
30111375Simp  class ExtensionRAIIObject {
31111375Simp    void operator=(const ExtensionRAIIObject &);     // DO NOT IMPLEMENT
32119169Simp    ExtensionRAIIObject(const ExtensionRAIIObject&); // DO NOT IMPLEMENT
33111375Simp    Diagnostic &Diags;
34111375Simp  public:
35111375Simp    ExtensionRAIIObject(Diagnostic &diags) : Diags(diags) {
36111375Simp      Diags.IncrementAllExtensionsSilenced();
37111375Simp    }
38111375Simp
39115880Sru    ~ExtensionRAIIObject() {
40111375Simp      Diags.DecrementAllExtensionsSilenced();
41    }
42  };
43
44  /// ColonProtectionRAIIObject - This sets the Parser::ColonIsSacred bool and
45  /// restores it when destroyed.  This says that "foo:" should not be
46  /// considered a possible typo for "foo::" for error recovery purposes.
47  class ColonProtectionRAIIObject {
48    Parser &P;
49    bool OldVal;
50  public:
51    ColonProtectionRAIIObject(Parser &p, bool Value = true)
52      : P(p), OldVal(P.ColonIsSacred) {
53      P.ColonIsSacred = Value;
54    }
55
56    /// restore - This can be used to restore the state early, before the dtor
57    /// is run.
58    void restore() {
59      P.ColonIsSacred = OldVal;
60    }
61
62    ~ColonProtectionRAIIObject() {
63      restore();
64    }
65  };
66
67  /// \brief RAII object that makes '>' behave either as an operator
68  /// or as the closing angle bracket for a template argument list.
69  class GreaterThanIsOperatorScope {
70    bool &GreaterThanIsOperator;
71    bool OldGreaterThanIsOperator;
72  public:
73    GreaterThanIsOperatorScope(bool &GTIO, bool Val)
74    : GreaterThanIsOperator(GTIO), OldGreaterThanIsOperator(GTIO) {
75      GreaterThanIsOperator = Val;
76    }
77
78    ~GreaterThanIsOperatorScope() {
79      GreaterThanIsOperator = OldGreaterThanIsOperator;
80    }
81  };
82
83  class InMessageExpressionRAIIObject {
84    bool &InMessageExpression;
85    bool OldValue;
86
87  public:
88    InMessageExpressionRAIIObject(Parser &P, bool Value)
89      : InMessageExpression(P.InMessageExpression),
90        OldValue(P.InMessageExpression) {
91      InMessageExpression = Value;
92    }
93
94    ~InMessageExpressionRAIIObject() {
95      InMessageExpression = OldValue;
96    }
97  };
98
99  /// \brief RAII object that makes sure paren/bracket/brace count is correct
100  /// after declaration/statement parsing, even when there's a parsing error.
101  class ParenBraceBracketBalancer {
102    Parser &P;
103    unsigned short ParenCount, BracketCount, BraceCount;
104  public:
105    ParenBraceBracketBalancer(Parser &p)
106      : P(p), ParenCount(p.ParenCount), BracketCount(p.BracketCount),
107        BraceCount(p.BraceCount) { }
108
109    ~ParenBraceBracketBalancer() {
110      P.ParenCount = ParenCount;
111      P.BracketCount = BracketCount;
112      P.BraceCount = BraceCount;
113    }
114  };
115
116} // end namespace clang
117
118#endif
119