blob: 93b9a82fdca86a4cfce1bc1c28a9e9f365605b74 [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:
51 ColonProtectionRAIIObject(Parser &p) : P(p), OldVal(P.ColonIsSacred) {
52 P.ColonIsSacred = true;
53 }
54
Chris Lattner6fb09c82009-12-10 00:38:54 +000055 /// restore - This can be used to restore the state early, before the dtor
56 /// is run.
57 void restore() {
Chris Lattner08d92ec2009-12-10 00:32:41 +000058 P.ColonIsSacred = OldVal;
59 }
Chris Lattner6fb09c82009-12-10 00:38:54 +000060
61 ~ColonProtectionRAIIObject() {
62 restore();
63 }
Chris Lattner08d92ec2009-12-10 00:32:41 +000064 };
65
Chris Lattnerd0d76f12009-12-10 00:44:03 +000066 /// \brief RAII object that makes '>' behave either as an operator
67 /// or as the closing angle bracket for a template argument list.
68 struct GreaterThanIsOperatorScope {
69 bool &GreaterThanIsOperator;
70 bool OldGreaterThanIsOperator;
71
72 GreaterThanIsOperatorScope(bool &GTIO, bool Val)
73 : GreaterThanIsOperator(GTIO), OldGreaterThanIsOperator(GTIO) {
74 GreaterThanIsOperator = Val;
75 }
76
77 ~GreaterThanIsOperatorScope() {
78 GreaterThanIsOperator = OldGreaterThanIsOperator;
79 }
80 };
81
Chris Lattner08d92ec2009-12-10 00:32:41 +000082} // end namespace clang
Chris Lattnerc46d1a12008-10-20 06:45:43 +000083
84#endif