Skip to content

Commit

Permalink
[YAMLIO] Add support for numeric values in enums.
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226942 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Bigcheese committed Jan 23, 2015
1 parent 89eab6e commit 96c6092
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/llvm/Support/YAMLTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ class IO {

virtual void beginEnumScalar() = 0;
virtual bool matchEnumScalar(const char*, bool) = 0;
virtual bool matchEnumFallback() = 0;
virtual void endEnumScalar() = 0;

virtual bool beginBitSetScalar(bool &) = 0;
Expand All @@ -472,6 +473,15 @@ class IO {
}
}

template <typename FBT, typename T>
void enumFallback(T &Val) {
if ( matchEnumFallback() ) {
FBT Res = Val;
yamlize(*this, Res, true);
Val = Res;
}
}

template <typename T>
void bitSetCase(T &Val, const char* Str, const T ConstVal) {
if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
Expand Down Expand Up @@ -899,6 +909,7 @@ class Input : public IO {
void endFlowSequence() override;
void beginEnumScalar() override;
bool matchEnumScalar(const char*, bool) override;
bool matchEnumFallback() override;
void endEnumScalar() override;
bool beginBitSetScalar(bool &) override;
bool bitSetMatch(const char *, bool ) override;
Expand Down Expand Up @@ -1026,6 +1037,7 @@ class Output : public IO {
void endFlowSequence() override;
void beginEnumScalar() override;
bool matchEnumScalar(const char*, bool) override;
bool matchEnumFallback() override;
void endEnumScalar() override;
bool beginBitSetScalar(bool &) override;
bool bitSetMatch(const char *, bool ) override;
Expand Down
14 changes: 14 additions & 0 deletions lib/Support/YAMLTraits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ bool Input::matchEnumScalar(const char *Str, bool) {
return false;
}

bool Input::matchEnumFallback() {
if (ScalarMatchFound)
return false;
ScalarMatchFound = true;
return true;
}

void Input::endEnumScalar() {
if (!ScalarMatchFound) {
setError(CurrentNode, "unknown enumerated scalar");
Expand Down Expand Up @@ -508,6 +515,13 @@ bool Output::matchEnumScalar(const char *Str, bool Match) {
return false;
}

bool Output::matchEnumFallback() {
if (EnumerationMatchFound)
return false;
EnumerationMatchFound = true;
return true;
}

void Output::endEnumScalar() {
if (!EnumerationMatchFound)
llvm_unreachable("bad runtime enum value");
Expand Down

0 comments on commit 96c6092

Please sign in to comment.