blob: 06bbbc23a01016573ac0303cfa67fbe3aa0014e0 [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"
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000019
20namespace clang {
Chris Lattner57013862009-12-10 00:44:03 +000021 // TODO: move ParsingDeclRAIIObject here.
22 // TODO: move ParsingClassDefinition here.
23 // TODO: move TentativeParsingAction here.
24
25
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000026 /// ExtensionRAIIObject - This saves the state of extension warnings when
27 /// constructed and disables them. When destructed, it restores them back to
28 /// the way they used to be. This is used to handle __extension__ in the
29 /// parser.
30 class ExtensionRAIIObject {
31 void operator=(const ExtensionRAIIObject &); // DO NOT IMPLEMENT
32 ExtensionRAIIObject(const ExtensionRAIIObject&); // DO NOT IMPLEMENT
33 Diagnostic &Diags;
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000034 public:
35 ExtensionRAIIObject(Diagnostic &diags) : Diags(diags) {
Chris Lattnere007de32009-04-15 07:01:18 +000036 Diags.IncrementAllExtensionsSilenced();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000037 }
Mike Stump11289f42009-09-09 15:08:12 +000038
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000039 ~ExtensionRAIIObject() {
Chris Lattnere007de32009-04-15 07:01:18 +000040 Diags.DecrementAllExtensionsSilenced();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000041 }
42 };
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +000043
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +000044 /// 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:
Chris Lattner3c674cf2009-12-10 02:08:07 +000051 ColonProtectionRAIIObject(Parser &p, bool Value = true)
52 : P(p), OldVal(P.ColonIsSacred) {
53 P.ColonIsSacred = Value;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +000054 }
55
Chris Lattner125c0ee2009-12-10 00:38:54 +000056 /// restore - This can be used to restore the state early, before the dtor
57 /// is run.
58 void restore() {
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +000059 P.ColonIsSacred = OldVal;
60 }
Chris Lattner125c0ee2009-12-10 00:38:54 +000061
62 ~ColonProtectionRAIIObject() {
63 restore();
64 }
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +000065 };
66
Chris Lattner57013862009-12-10 00:44:03 +000067 /// \brief RAII object that makes '>' behave either as an operator
68 /// or as the closing angle bracket for a template argument list.
Benjamin Kramere9ff5fe2009-12-10 21:50:21 +000069 class GreaterThanIsOperatorScope {
Chris Lattner57013862009-12-10 00:44:03 +000070 bool &GreaterThanIsOperator;
71 bool OldGreaterThanIsOperator;
Benjamin Kramere9ff5fe2009-12-10 21:50:21 +000072 public:
Chris Lattner57013862009-12-10 00:44:03 +000073 GreaterThanIsOperatorScope(bool &GTIO, bool Val)
74 : GreaterThanIsOperator(GTIO), OldGreaterThanIsOperator(GTIO) {
75 GreaterThanIsOperator = Val;
76 }
77
78 ~GreaterThanIsOperatorScope() {
79 GreaterThanIsOperator = OldGreaterThanIsOperator;
80 }
81 };
82
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +000083} // end namespace clang
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000084
85#endif