blob: f5a6f8fcf94ae57a7dbd9070738cee9e41bb929b [file] [log] [blame]
Chris Lattner8a9a97a2009-12-10 00:21:05 +00001//===--- RAIIObjectsForParser.h - RAII helpers for the parser ---*- C++ -*-===//
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002//
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 Lattner8a9a97a2009-12-10 00:21:05 +000010// This file defines and implements the some simple RAII objects that are used
11// by the parser to manage bits in recursion.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner8a9a97a2009-12-10 00:21:05 +000015#ifndef LLVM_CLANG_PARSE_RAII_OBJECTS_FOR_PARSER_H
16#define LLVM_CLANG_PARSE_RAII_OBJECTS_FOR_PARSER_H
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000017
Chris Lattner60f36222009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall2ec85372012-05-07 06:16:41 +000019#include "clang/Parse/Parser.h"
20#include "clang/Sema/DelayedDiagnostic.h"
21#include "clang/Sema/Sema.h"
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000022
23namespace clang {
Chris Lattner57013862009-12-10 00:44:03 +000024 // TODO: move ParsingClassDefinition here.
25 // TODO: move TentativeParsingAction here.
John McCall2ec85372012-05-07 06:16:41 +000026
27 /// \brief RAII object used to inform the actions that we're
28 /// currently parsing a declaration. This is active when parsing a
29 /// variable's initializer, but not when parsing the body of a
30 /// class or function definition.
31 class ParsingDeclRAIIObject {
32 Sema &Actions;
33 sema::DelayedDiagnosticPool DiagnosticPool;
34 Sema::ParsingDeclState State;
35 bool Popped;
36
37 // Do not implement.
38 ParsingDeclRAIIObject(const ParsingDeclRAIIObject &other);
39 ParsingDeclRAIIObject &operator=(const ParsingDeclRAIIObject &other);
40
41 public:
42 enum NoParent_t { NoParent };
43 ParsingDeclRAIIObject(Parser &P, NoParent_t _)
44 : Actions(P.getActions()), DiagnosticPool(NULL) {
45 push();
46 }
47
48 /// Creates a RAII object whose pool is optionally parented by another.
49 ParsingDeclRAIIObject(Parser &P,
50 const sema::DelayedDiagnosticPool *parentPool)
51 : Actions(P.getActions()), DiagnosticPool(parentPool) {
52 push();
53 }
54
55 /// Creates a RAII object and, optionally, initialize its
56 /// diagnostics pool by stealing the diagnostics from another
57 /// RAII object (which is assumed to be the current top pool).
58 ParsingDeclRAIIObject(Parser &P, ParsingDeclRAIIObject *other)
59 : Actions(P.getActions()),
60 DiagnosticPool(other ? other->DiagnosticPool.getParent() : NULL) {
61 if (other) {
62 DiagnosticPool.steal(other->DiagnosticPool);
63 other->abort();
64 }
65 push();
66 }
67
68 ~ParsingDeclRAIIObject() {
69 abort();
70 }
71
72 sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() {
73 return DiagnosticPool;
74 }
75 const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const {
76 return DiagnosticPool;
77 }
78
79 /// Resets the RAII object for a new declaration.
80 void reset() {
81 abort();
82 push();
83 }
84
85 /// Signals that the context was completed without an appropriate
86 /// declaration being parsed.
87 void abort() {
88 pop(0);
89 }
90
91 void complete(Decl *D) {
92 assert(!Popped && "ParsingDeclaration has already been popped!");
93 pop(D);
94 }
95
96 private:
97 void steal(ParsingDeclRAIIObject &Other) {
98 DiagnosticPool.steal(Other.DiagnosticPool);
99 State = Other.State;
100 Popped = Other.Popped;
101 Other.Popped = true;
102 }
103
104 void push() {
105 State = Actions.PushParsingDeclaration(DiagnosticPool);
106 Popped = false;
107 }
108
109 void pop(Decl *D) {
110 if (!Popped) {
111 Actions.PopParsingDeclaration(State, D);
112 Popped = true;
113 }
114 }
115 };
116
117 /// A class for parsing a DeclSpec.
118 class ParsingDeclSpec : public DeclSpec {
119 ParsingDeclRAIIObject ParsingRAII;
120
121 public:
122 ParsingDeclSpec(Parser &P)
123 : DeclSpec(P.getAttrFactory()),
124 ParsingRAII(P, ParsingDeclRAIIObject::NoParent) {}
125 ParsingDeclSpec(Parser &P, ParsingDeclRAIIObject *RAII)
126 : DeclSpec(P.getAttrFactory()),
127 ParsingRAII(P, RAII) {}
128
129 const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const {
130 return ParsingRAII.getDelayedDiagnosticPool();
131 }
132
133 void complete(Decl *D) {
134 ParsingRAII.complete(D);
135 }
136
137 void abort() {
138 ParsingRAII.abort();
139 }
140 };
141
142 /// A class for parsing a declarator.
143 class ParsingDeclarator : public Declarator {
144 ParsingDeclRAIIObject ParsingRAII;
145
146 public:
147 ParsingDeclarator(Parser &P, const ParsingDeclSpec &DS, TheContext C)
148 : Declarator(DS, C), ParsingRAII(P, &DS.getDelayedDiagnosticPool()) {
149 }
150
151 const ParsingDeclSpec &getDeclSpec() const {
152 return static_cast<const ParsingDeclSpec&>(Declarator::getDeclSpec());
153 }
154
155 ParsingDeclSpec &getMutableDeclSpec() const {
156 return const_cast<ParsingDeclSpec&>(getDeclSpec());
157 }
158
159 void clear() {
160 Declarator::clear();
161 ParsingRAII.reset();
162 }
163
164 void complete(Decl *D) {
165 ParsingRAII.complete(D);
166 }
167 };
168
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000169 /// ExtensionRAIIObject - This saves the state of extension warnings when
170 /// constructed and disables them. When destructed, it restores them back to
171 /// the way they used to be. This is used to handle __extension__ in the
172 /// parser.
173 class ExtensionRAIIObject {
174 void operator=(const ExtensionRAIIObject &); // DO NOT IMPLEMENT
175 ExtensionRAIIObject(const ExtensionRAIIObject&); // DO NOT IMPLEMENT
David Blaikie9c902b52011-09-25 23:23:43 +0000176 DiagnosticsEngine &Diags;
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000177 public:
David Blaikie9c902b52011-09-25 23:23:43 +0000178 ExtensionRAIIObject(DiagnosticsEngine &diags) : Diags(diags) {
Chris Lattnere007de32009-04-15 07:01:18 +0000179 Diags.IncrementAllExtensionsSilenced();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000180 }
Mike Stump11289f42009-09-09 15:08:12 +0000181
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000182 ~ExtensionRAIIObject() {
Chris Lattnere007de32009-04-15 07:01:18 +0000183 Diags.DecrementAllExtensionsSilenced();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000184 }
185 };
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000186
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000187 /// ColonProtectionRAIIObject - This sets the Parser::ColonIsSacred bool and
188 /// restores it when destroyed. This says that "foo:" should not be
189 /// considered a possible typo for "foo::" for error recovery purposes.
190 class ColonProtectionRAIIObject {
191 Parser &P;
192 bool OldVal;
193 public:
Chris Lattner3c674cf2009-12-10 02:08:07 +0000194 ColonProtectionRAIIObject(Parser &p, bool Value = true)
195 : P(p), OldVal(P.ColonIsSacred) {
196 P.ColonIsSacred = Value;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000197 }
198
Chris Lattner125c0ee2009-12-10 00:38:54 +0000199 /// restore - This can be used to restore the state early, before the dtor
200 /// is run.
201 void restore() {
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000202 P.ColonIsSacred = OldVal;
203 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000204
205 ~ColonProtectionRAIIObject() {
206 restore();
207 }
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000208 };
209
Chris Lattner57013862009-12-10 00:44:03 +0000210 /// \brief RAII object that makes '>' behave either as an operator
211 /// or as the closing angle bracket for a template argument list.
Benjamin Kramere9ff5fe2009-12-10 21:50:21 +0000212 class GreaterThanIsOperatorScope {
Chris Lattner57013862009-12-10 00:44:03 +0000213 bool &GreaterThanIsOperator;
214 bool OldGreaterThanIsOperator;
Benjamin Kramere9ff5fe2009-12-10 21:50:21 +0000215 public:
Chris Lattner57013862009-12-10 00:44:03 +0000216 GreaterThanIsOperatorScope(bool &GTIO, bool Val)
217 : GreaterThanIsOperator(GTIO), OldGreaterThanIsOperator(GTIO) {
218 GreaterThanIsOperator = Val;
219 }
220
221 ~GreaterThanIsOperatorScope() {
222 GreaterThanIsOperator = OldGreaterThanIsOperator;
223 }
224 };
225
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000226 class InMessageExpressionRAIIObject {
227 bool &InMessageExpression;
228 bool OldValue;
229
230 public:
231 InMessageExpressionRAIIObject(Parser &P, bool Value)
232 : InMessageExpression(P.InMessageExpression),
233 OldValue(P.InMessageExpression) {
234 InMessageExpression = Value;
235 }
236
237 ~InMessageExpressionRAIIObject() {
238 InMessageExpression = OldValue;
239 }
240 };
241
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000242 /// \brief RAII object that makes sure paren/bracket/brace count is correct
243 /// after declaration/statement parsing, even when there's a parsing error.
244 class ParenBraceBracketBalancer {
245 Parser &P;
246 unsigned short ParenCount, BracketCount, BraceCount;
247 public:
248 ParenBraceBracketBalancer(Parser &p)
249 : P(p), ParenCount(p.ParenCount), BracketCount(p.BracketCount),
250 BraceCount(p.BraceCount) { }
251
252 ~ParenBraceBracketBalancer() {
253 P.ParenCount = ParenCount;
254 P.BracketCount = BracketCount;
255 P.BraceCount = BraceCount;
256 }
257 };
John Wiegley1c0675e2011-04-28 01:08:34 +0000258
259 class PoisonSEHIdentifiersRAIIObject {
260 PoisonIdentifierRAIIObject Ident_AbnormalTermination;
261 PoisonIdentifierRAIIObject Ident_GetExceptionCode;
262 PoisonIdentifierRAIIObject Ident_GetExceptionInfo;
263 PoisonIdentifierRAIIObject Ident__abnormal_termination;
264 PoisonIdentifierRAIIObject Ident__exception_code;
265 PoisonIdentifierRAIIObject Ident__exception_info;
266 PoisonIdentifierRAIIObject Ident___abnormal_termination;
267 PoisonIdentifierRAIIObject Ident___exception_code;
268 PoisonIdentifierRAIIObject Ident___exception_info;
269 public:
270 PoisonSEHIdentifiersRAIIObject(Parser &Self, bool NewValue)
271 : Ident_AbnormalTermination(Self.Ident_AbnormalTermination, NewValue),
272 Ident_GetExceptionCode(Self.Ident_GetExceptionCode, NewValue),
273 Ident_GetExceptionInfo(Self.Ident_GetExceptionInfo, NewValue),
274 Ident__abnormal_termination(Self.Ident__abnormal_termination, NewValue),
275 Ident__exception_code(Self.Ident__exception_code, NewValue),
276 Ident__exception_info(Self.Ident__exception_info, NewValue),
277 Ident___abnormal_termination(Self.Ident___abnormal_termination, NewValue),
278 Ident___exception_code(Self.Ident___exception_code, NewValue),
279 Ident___exception_info(Self.Ident___exception_info, NewValue) {
280 }
281 };
282
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000283} // end namespace clang
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000284
285#endif