blob: 06bbbc23a01016573ac0303cfa67fbe3aa0014e0 [file] [log] [blame]
Chris Lattnerd167ca02009-12-10 00:21:05 +00001//===--- RAIIObjectsForParser.h - RAII helpers for the parser ---*- C++ -*-===//
Chris Lattnerc46d1a12008-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 Lattnerd167ca02009-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 Lattnerc46d1a12008-10-20 06:45:43 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerd167ca02009-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 Lattnerc46d1a12008-10-20 06:45:43 +000017
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000019
20namespace clang {
Chris Lattnerd0d76f12009-12-10 00:44:03 +000021 // TODO: move ParsingDeclRAIIObject here.
22 // TODO: move ParsingClassDefinition here.
23 // TODO: move TentativeParsingAction here.
24
25
Chris Lattnerc46d1a12008-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 Lattnerc46d1a12008-10-20 06:45:43 +000034 public:
35 ExtensionRAIIObject(Diagnostic &diags) : Diags(diags) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +000036 Diags.IncrementAllExtensionsSilenced();
Chris Lattnerc46d1a12008-10-20 06:45:43 +000037 }
Mike Stump1eb44332009-09-09 15:08:12 +000038
Chris Lattnerc46d1a12008-10-20 06:45:43 +000039 ~ExtensionRAIIObject() {
Chris Lattner27ceb9d2009-04-15 07:01:18 +000040 Diags.DecrementAllExtensionsSilenced();
Chris Lattnerc46d1a12008-10-20 06:45:43 +000041 }
42 };
Chris Lattner08d92ec2009-12-10 00:32:41 +000043
Chris Lattner08d92ec2009-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 Lattner932dff72009-12-10 02:08:07 +000051 ColonProtectionRAIIObject(Parser &p, bool Value = true)
52 : P(p), OldVal(P.ColonIsSacred) {
53 P.ColonIsSacred = Value;
Chris Lattner08d92ec2009-12-10 00:32:41 +000054 }
55
Chris Lattner6fb09c82009-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 Lattner08d92ec2009-12-10 00:32:41 +000059 P.ColonIsSacred = OldVal;
60 }
Chris Lattner6fb09c82009-12-10 00:38:54 +000061
62 ~ColonProtectionRAIIObject() {
63 restore();
64 }
Chris Lattner08d92ec2009-12-10 00:32:41 +000065 };
66
Chris Lattnerd0d76f12009-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 Kramer648d8462009-12-10 21:50:21 +000069 class GreaterThanIsOperatorScope {
Chris Lattnerd0d76f12009-12-10 00:44:03 +000070 bool &GreaterThanIsOperator;
71 bool OldGreaterThanIsOperator;
Benjamin Kramer648d8462009-12-10 21:50:21 +000072 public:
Chris Lattnerd0d76f12009-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 Lattner08d92ec2009-12-10 00:32:41 +000083} // end namespace clang
Chris Lattnerc46d1a12008-10-20 06:45:43 +000084
85#endif