2009-12-10 08:21:05 +08:00
|
|
|
//===--- RAIIObjectsForParser.h - RAII helpers for the parser ---*- C++ -*-===//
|
2008-10-20 14:45:43 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-12-10 08:21:05 +08:00
|
|
|
// This file defines and implements the some simple RAII objects that are used
|
|
|
|
// by the parser to manage bits in recursion.
|
2008-10-20 14:45:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-12-10 08:21:05 +08:00
|
|
|
#ifndef LLVM_CLANG_PARSE_RAII_OBJECTS_FOR_PARSER_H
|
|
|
|
#define LLVM_CLANG_PARSE_RAII_OBJECTS_FOR_PARSER_H
|
2008-10-20 14:45:43 +08:00
|
|
|
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
2008-10-20 14:45:43 +08:00
|
|
|
|
|
|
|
namespace clang {
|
2009-12-10 08:44:03 +08:00
|
|
|
// TODO: move ParsingDeclRAIIObject here.
|
|
|
|
// TODO: move ParsingClassDefinition here.
|
|
|
|
// TODO: move TentativeParsingAction here.
|
|
|
|
|
|
|
|
|
2008-10-20 14:45:43 +08:00
|
|
|
/// ExtensionRAIIObject - This saves the state of extension warnings when
|
|
|
|
/// constructed and disables them. When destructed, it restores them back to
|
|
|
|
/// the way they used to be. This is used to handle __extension__ in the
|
|
|
|
/// parser.
|
|
|
|
class ExtensionRAIIObject {
|
|
|
|
void operator=(const ExtensionRAIIObject &); // DO NOT IMPLEMENT
|
|
|
|
ExtensionRAIIObject(const ExtensionRAIIObject&); // DO NOT IMPLEMENT
|
|
|
|
Diagnostic &Diags;
|
|
|
|
public:
|
|
|
|
ExtensionRAIIObject(Diagnostic &diags) : Diags(diags) {
|
2009-04-15 15:01:18 +08:00
|
|
|
Diags.IncrementAllExtensionsSilenced();
|
2008-10-20 14:45:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-20 14:45:43 +08:00
|
|
|
~ExtensionRAIIObject() {
|
2009-04-15 15:01:18 +08:00
|
|
|
Diags.DecrementAllExtensionsSilenced();
|
2008-10-20 14:45:43 +08:00
|
|
|
}
|
|
|
|
};
|
2009-12-10 08:32:41 +08:00
|
|
|
|
|
|
|
/// ColonProtectionRAIIObject - This sets the Parser::ColonIsSacred bool and
|
|
|
|
/// restores it when destroyed. This says that "foo:" should not be
|
|
|
|
/// considered a possible typo for "foo::" for error recovery purposes.
|
|
|
|
class ColonProtectionRAIIObject {
|
|
|
|
Parser &P;
|
|
|
|
bool OldVal;
|
|
|
|
public:
|
2009-12-10 10:08:07 +08:00
|
|
|
ColonProtectionRAIIObject(Parser &p, bool Value = true)
|
|
|
|
: P(p), OldVal(P.ColonIsSacred) {
|
|
|
|
P.ColonIsSacred = Value;
|
2009-12-10 08:32:41 +08:00
|
|
|
}
|
|
|
|
|
2009-12-10 08:38:54 +08:00
|
|
|
/// restore - This can be used to restore the state early, before the dtor
|
|
|
|
/// is run.
|
|
|
|
void restore() {
|
2009-12-10 08:32:41 +08:00
|
|
|
P.ColonIsSacred = OldVal;
|
|
|
|
}
|
2009-12-10 08:38:54 +08:00
|
|
|
|
|
|
|
~ColonProtectionRAIIObject() {
|
|
|
|
restore();
|
|
|
|
}
|
2009-12-10 08:32:41 +08:00
|
|
|
};
|
|
|
|
|
2009-12-10 08:44:03 +08:00
|
|
|
/// \brief RAII object that makes '>' behave either as an operator
|
|
|
|
/// or as the closing angle bracket for a template argument list.
|
2009-12-11 05:50:21 +08:00
|
|
|
class GreaterThanIsOperatorScope {
|
2009-12-10 08:44:03 +08:00
|
|
|
bool &GreaterThanIsOperator;
|
|
|
|
bool OldGreaterThanIsOperator;
|
2009-12-11 05:50:21 +08:00
|
|
|
public:
|
2009-12-10 08:44:03 +08:00
|
|
|
GreaterThanIsOperatorScope(bool >IO, bool Val)
|
|
|
|
: GreaterThanIsOperator(GTIO), OldGreaterThanIsOperator(GTIO) {
|
|
|
|
GreaterThanIsOperator = Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
~GreaterThanIsOperatorScope() {
|
|
|
|
GreaterThanIsOperator = OldGreaterThanIsOperator;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-09-15 22:51:05 +08:00
|
|
|
class InMessageExpressionRAIIObject {
|
|
|
|
bool &InMessageExpression;
|
|
|
|
bool OldValue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InMessageExpressionRAIIObject(Parser &P, bool Value)
|
|
|
|
: InMessageExpression(P.InMessageExpression),
|
|
|
|
OldValue(P.InMessageExpression) {
|
|
|
|
InMessageExpression = Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
~InMessageExpressionRAIIObject() {
|
|
|
|
InMessageExpression = OldValue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-06-17 18:52:18 +08:00
|
|
|
/// \brief RAII object that makes sure paren/bracket/brace count is correct
|
|
|
|
/// after declaration/statement parsing, even when there's a parsing error.
|
|
|
|
class ParenBraceBracketBalancer {
|
|
|
|
Parser &P;
|
|
|
|
unsigned short ParenCount, BracketCount, BraceCount;
|
|
|
|
public:
|
|
|
|
ParenBraceBracketBalancer(Parser &p)
|
|
|
|
: P(p), ParenCount(p.ParenCount), BracketCount(p.BracketCount),
|
|
|
|
BraceCount(p.BraceCount) { }
|
|
|
|
|
|
|
|
~ParenBraceBracketBalancer() {
|
|
|
|
P.ParenCount = ParenCount;
|
|
|
|
P.BracketCount = BracketCount;
|
|
|
|
P.BraceCount = BraceCount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-12-10 08:32:41 +08:00
|
|
|
} // end namespace clang
|
2008-10-20 14:45:43 +08:00
|
|
|
|
|
|
|
#endif
|