[WebAssembly] Clean up comments and fix a missing #include dependency.
[oota-llvm.git] / lib / Support / YAMLTraits.cpp
index e2be15be50ebbf66908979657bd07a9c11da5d79..2aa6e9b7468333976e97d03919a7a09df41ffded 100644 (file)
@@ -7,22 +7,20 @@
 //
 //===----------------------------------------------------------------------===//
 
-#define BUILDING_YAMLIO
 #include "llvm/Support/YAMLTraits.h"
-
+#include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/Errc.h"
 #include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/format.h"
-#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/Format.h"
+#include "llvm/Support/LineIterator.h"
 #include "llvm/Support/YAMLParser.h"
-
+#include "llvm/Support/raw_ostream.h"
+#include <cctype>
 #include <cstring>
-
-namespace llvm {
-namespace yaml {
-
-
+using namespace llvm;
+using namespace yaml;
 
 //===----------------------------------------------------------------------===//
 //  IO
@@ -42,75 +40,114 @@ void IO::setContext(void *Context) {
   Ctxt = Context;
 }
 
-
 //===----------------------------------------------------------------------===//
 //  Input
 //===----------------------------------------------------------------------===//
 
-Input::Input(StringRef InputContent, void *Ctxt)
-    : IO(Ctxt), CurrentNode(NULL) {
-  Strm = new Stream(InputContent, SrcMgr);
+Input::Input(StringRef InputContent,
+             void *Ctxt,
+             SourceMgr::DiagHandlerTy DiagHandler,
+             void *DiagHandlerCtxt)
+  : IO(Ctxt),
+    Strm(new Stream(InputContent, SrcMgr)),
+    CurrentNode(nullptr) {
+  if (DiagHandler)
+    SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
   DocIterator = Strm->begin();
 }
 
-
-llvm::error_code Input::error() {
-  return EC;
+Input::~Input() {
 }
 
-void Input::setDiagHandler(llvm::SourceMgr::DiagHandlerTy Handler, void *Ctxt) {
-  SrcMgr.setDiagHandler(Handler, Ctxt);
-}
+std::error_code Input::error() { return EC; }
+
+// Pin the vtables to this file.
+void Input::HNode::anchor() {}
+void Input::EmptyHNode::anchor() {}
+void Input::ScalarHNode::anchor() {}
+void Input::MapHNode::anchor() {}
+void Input::SequenceHNode::anchor() {}
 
 bool Input::outputting() {
   return false;
 }
 
 bool Input::setCurrentDocument() {
-  if ( DocIterator != Strm->end() ) {
+  if (DocIterator != Strm->end()) {
     Node *N = DocIterator->getRoot();
-    if (llvm::isa<NullNode>(N)) {
+    if (!N) {
+      assert(Strm->failed() && "Root is NULL iff parsing failed");
+      EC = make_error_code(errc::invalid_argument);
+      return false;
+    }
+
+    if (isa<NullNode>(N)) {
       // Empty files are allowed and ignored
       ++DocIterator;
       return setCurrentDocument();
     }
-    CurrentNode = this->createHNodes(N);
+    TopNode = this->createHNodes(N);
+    CurrentNode = TopNode.get();
     return true;
   }
   return false;
 }
 
-void Input::nextDocument() {
-  ++DocIterator;
+bool Input::nextDocument() {
+  return ++DocIterator != Strm->end();
+}
+
+const Node *Input::getCurrentNode() const {
+  return CurrentNode ? CurrentNode->_node : nullptr;
+}
+
+bool Input::mapTag(StringRef Tag, bool Default) {
+  std::string foundTag = CurrentNode->_node->getVerbatimTag();
+  if (foundTag.empty()) {
+    // If no tag found and 'Tag' is the default, say it was found.
+    return Default;
+  }
+  // Return true iff found tag matches supplied tag.
+  return Tag.equals(foundTag);
 }
 
 void Input::beginMapping() {
-  if ( EC )
+  if (EC)
     return;
-  MapHNode *MN = llvm::dyn_cast<MapHNode>(CurrentNode);
-  if ( MN ) {
+  // CurrentNode can be null if the document is empty.
+  MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
+  if (MN) {
     MN->ValidKeys.clear();
   }
 }
 
-bool Input::preflightKey(const char *Key, bool Required, bool,
-                                          bool &UseDefault, void *&SaveInfo) {
+bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
+                         void *&SaveInfo) {
   UseDefault = false;
-  if ( EC )
+  if (EC)
     return false;
-  MapHNode *MN = llvm::dyn_cast<MapHNode>(CurrentNode);
-  if ( !MN ) {
+
+  // CurrentNode is null for empty documents, which is an error in case required
+  // nodes are present.
+  if (!CurrentNode) {
+    if (Required)
+      EC = make_error_code(errc::invalid_argument);
+    return false;
+  }
+
+  MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
+  if (!MN) {
     setError(CurrentNode, "not a mapping");
     return false;
   }
   MN->ValidKeys.push_back(Key);
-  HNode *Value = MN->Mapping[Key];
-  if ( !Value ) {
-    if ( Required )
+  HNode *Value = MN->Mapping[Key].get();
+  if (!Value) {
+    if (Required)
       setError(CurrentNode, Twine("missing required key '") + Key + "'");
     else
       UseDefault = true;
-   return false;
+    return false;
   }
   SaveInfo = CurrentNode;
   CurrentNode = Value;
@@ -118,79 +155,90 @@ bool Input::preflightKey(const char *Key, bool Required, bool,
 }
 
 void Input::postflightKey(void *saveInfo) {
-  CurrentNode = reinterpret_cast<HNode*>(saveInfo);
+  CurrentNode = reinterpret_cast<HNode *>(saveInfo);
 }
 
 void Input::endMapping() {
-  if ( EC )
+  if (EC)
     return;
-  MapHNode *MN = llvm::dyn_cast<MapHNode>(CurrentNode);
-  if ( !MN )
+  // CurrentNode can be null if the document is empty.
+  MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
+  if (!MN)
     return;
-  for (MapHNode::NameToNode::iterator i=MN->Mapping.begin(),
-                                        End=MN->Mapping.end(); i != End; ++i) {
-    if ( ! MN->isValidKey(i->first) ) {
-       setError(i->second, Twine("unknown key '") + i->first + "'" );
+  for (const auto &NN : MN->Mapping) {
+    if (!MN->isValidKey(NN.first())) {
+      setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
       break;
     }
   }
 }
 
+void Input::beginFlowMapping() { beginMapping(); }
+
+void Input::endFlowMapping() { endMapping(); }
 
 unsigned Input::beginSequence() {
-  if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {
+  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
     return SQ->Entries.size();
-  }
+  if (isa<EmptyHNode>(CurrentNode))
+    return 0;
+  // Treat case where there's a scalar "null" value as an empty sequence.
+  if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
+    if (isNull(SN->value()))
+      return 0;
+  }
+  // Any other type of HNode is an error.
+  setError(CurrentNode, "not a sequence");
   return 0;
 }
+
 void Input::endSequence() {
 }
+
 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
-  if ( EC )
+  if (EC)
     return false;
-  if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {
+  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
     SaveInfo = CurrentNode;
-    CurrentNode = SQ->Entries[Index];
+    CurrentNode = SQ->Entries[Index].get();
     return true;
   }
   return false;
 }
+
 void Input::postflightElement(void *SaveInfo) {
-  CurrentNode = reinterpret_cast<HNode*>(SaveInfo);
+  CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
 }
 
-unsigned Input::beginFlowSequence() {
-   if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {
-    return SQ->Entries.size();
-  }
-  return 0;
-}
+unsigned Input::beginFlowSequence() { return beginSequence(); }
+
 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
-  if ( EC )
+  if (EC)
     return false;
-  if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {
+  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
     SaveInfo = CurrentNode;
-    CurrentNode = SQ->Entries[index];
+    CurrentNode = SQ->Entries[index].get();
     return true;
   }
   return false;
 }
+
 void Input::postflightFlowElement(void *SaveInfo) {
-  CurrentNode = reinterpret_cast<HNode*>(SaveInfo);
+  CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
 }
+
 void Input::endFlowSequence() {
 }
 
-
 void Input::beginEnumScalar() {
   ScalarMatchFound = false;
 }
 
 bool Input::matchEnumScalar(const char *Str, bool) {
-  if ( ScalarMatchFound )
+  if (ScalarMatchFound)
     return false;
-  if ( ScalarHNode *SN = llvm::dyn_cast<ScalarHNode>(CurrentNode) ) {
-    if ( SN->value().equals(Str) ) {
+  if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
+    if (SN->value().equals(Str)) {
       ScalarMatchFound = true;
       return true;
     }
@@ -198,20 +246,24 @@ 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 ) {
+  if (!ScalarMatchFound) {
     setError(CurrentNode, "unknown enumerated scalar");
   }
 }
 
-
-
 bool Input::beginBitSetScalar(bool &DoClear) {
   BitValuesUsed.clear();
-  if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {
+  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
     BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
-  }
-  else {
+  } else {
     setError(CurrentNode, "expected sequence of bit values");
   }
   DoClear = true;
@@ -219,55 +271,53 @@ bool Input::beginBitSetScalar(bool &DoClear) {
 }
 
 bool Input::bitSetMatch(const char *Str, bool) {
-  if ( EC )
+  if (EC)
     return false;
-  if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {
+  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
     unsigned Index = 0;
-    for (std::vector<HNode*>::iterator i=SQ->Entries.begin(),
-                                       End=SQ->Entries.end(); i != End; ++i) {
-      if ( ScalarHNode *SN = llvm::dyn_cast<ScalarHNode>(*i) ) {
-        if ( SN->value().equals(Str) ) {
+    for (auto &N : SQ->Entries) {
+      if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
+        if (SN->value().equals(Str)) {
           BitValuesUsed[Index] = true;
           return true;
         }
-      }
-      else {
+      } else {
         setError(CurrentNode, "unexpected scalar in sequence of bit values");
       }
       ++Index;
     }
-  }
-  else {
+  } else {
     setError(CurrentNode, "expected sequence of bit values");
   }
   return false;
 }
 
 void Input::endBitSetScalar() {
-  if ( EC )
+  if (EC)
     return;
-  if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {
+  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
     assert(BitValuesUsed.size() == SQ->Entries.size());
-    for ( unsigned i=0; i < SQ->Entries.size(); ++i ) {
-      if ( !BitValuesUsed[i] ) {
-        setError(SQ->Entries[i], "unknown bit value");
+    for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
+      if (!BitValuesUsed[i]) {
+        setError(SQ->Entries[i].get(), "unknown bit value");
         return;
       }
     }
   }
 }
 
-
-void Input::scalarString(StringRef &S) {
-  if ( ScalarHNode *SN = llvm::dyn_cast<ScalarHNode>(CurrentNode) ) {
+void Input::scalarString(StringRef &S, bool) {
+  if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
     S = SN->value();
-  }
-  else {
+  } else {
     setError(CurrentNode, "unexpected scalar");
   }
 }
 
+void Input::blockScalarString(StringRef &S) { scalarString(S, false); }
+
 void Input::setError(HNode *hnode, const Twine &message) {
+  assert(hnode && "HNode must not be NULL");
   this->setError(hnode->_node, message);
 }
 
@@ -276,63 +326,59 @@ void Input::setError(Node *node, const Twine &message) {
   EC = make_error_code(errc::invalid_argument);
 }
 
-Input::HNode *Input::createHNodes(Node *N) {
-  llvm::SmallString<128> StringStorage;
-  if ( ScalarNode *SN = llvm::dyn_cast<ScalarNode>(N) ) {
+std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
+  SmallString<128> StringStorage;
+  if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
     StringRef KeyStr = SN->getValue(StringStorage);
-    if ( !StringStorage.empty() ) {
+    if (!StringStorage.empty()) {
       // Copy string to permanent storage
-      unsigned Len = StringStorage.size();
-      char* Buf = Allocator.Allocate<char>(Len);
-      memcpy(Buf, &StringStorage[0], Len);
-      KeyStr = StringRef(Buf, Len);
+      KeyStr = StringStorage.str().copy(StringAllocator);
     }
-    return new (Allocator) ScalarHNode(N, KeyStr);
-  }
-  else if ( SequenceNode *SQ = llvm::dyn_cast<SequenceNode>(N) ) {
-    SequenceHNode *SQHNode = new (Allocator) SequenceHNode(N);
-    for (SequenceNode::iterator i=SQ->begin(),End=SQ->end(); i != End; ++i ) {
-      HNode *Entry = this->createHNodes(i);
-      if ( EC )
+    return llvm::make_unique<ScalarHNode>(N, KeyStr);
+  } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
+    StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
+    return llvm::make_unique<ScalarHNode>(N, ValueCopy);
+  } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
+    auto SQHNode = llvm::make_unique<SequenceHNode>(N);
+    for (Node &SN : *SQ) {
+      auto Entry = this->createHNodes(&SN);
+      if (EC)
         break;
-      SQHNode->Entries.push_back(Entry);
+      SQHNode->Entries.push_back(std::move(Entry));
     }
-    return SQHNode;
-  }
-  else if ( MappingNode *Map = llvm::dyn_cast<MappingNode>(N) ) {
-    MapHNode *mapHNode = new (Allocator) MapHNode(N);
-    for (MappingNode::iterator i=Map->begin(), End=Map->end(); i != End; ++i ) {
-      ScalarNode *KeyScalar = llvm::dyn_cast<ScalarNode>(i->getKey());
+    return std::move(SQHNode);
+  } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
+    auto mapHNode = llvm::make_unique<MapHNode>(N);
+    for (KeyValueNode &KVN : *Map) {
+      Node *KeyNode = KVN.getKey();
+      ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
+      if (!KeyScalar) {
+        setError(KeyNode, "Map key must be a scalar");
+        break;
+      }
       StringStorage.clear();
-      llvm::StringRef KeyStr = KeyScalar->getValue(StringStorage);
-      if ( !StringStorage.empty() ) {
+      StringRef KeyStr = KeyScalar->getValue(StringStorage);
+      if (!StringStorage.empty()) {
         // Copy string to permanent storage
-        unsigned Len = StringStorage.size();
-        char* Buf = Allocator.Allocate<char>(Len);
-        memcpy(Buf, &StringStorage[0], Len);
-        KeyStr = StringRef(Buf, Len);
+        KeyStr = StringStorage.str().copy(StringAllocator);
       }
-     HNode *ValueHNode = this->createHNodes(i->getValue());
-      if ( EC )
+      auto ValueHNode = this->createHNodes(KVN.getValue());
+      if (EC)
         break;
-      mapHNode->Mapping[KeyStr] = ValueHNode;
+      mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
     }
-    return mapHNode;
-  }
-  else if ( llvm::isa<NullNode>(N) ) {
-    return new (Allocator) EmptyHNode(N);
-  }
-  else {
+    return std::move(mapHNode);
+  } else if (isa<NullNode>(N)) {
+    return llvm::make_unique<EmptyHNode>(N);
+  } else {
     setError(N, "unknown node kind");
-    return NULL;
+    return nullptr;
   }
 }
 
-
 bool Input::MapHNode::isValidKey(StringRef Key) {
-  for (SmallVector<const char*, 6>::iterator i=ValidKeys.begin(),
-                                  End=ValidKeys.end(); i != End; ++i) {
-    if ( Key.equals(*i) )
+  for (const char *K : ValidKeys) {
+    if (Key.equals(K))
       return true;
   }
   return false;
@@ -342,15 +388,25 @@ void Input::setError(const Twine &Message) {
   this->setError(CurrentNode, Message);
 }
 
+bool Input::canElideEmptySequence() {
+  return false;
+}
 
 //===----------------------------------------------------------------------===//
 //  Output
 //===----------------------------------------------------------------------===//
 
-Output::Output(llvm::raw_ostream &yout, void *context)
-    : IO(context), Out(yout), Column(0), ColumnAtFlowStart(0),
-       NeedBitValueComma(false), NeedFlowSequenceComma(false),
-       EnumerationMatchFound(false), NeedsNewLine(false) {
+Output::Output(raw_ostream &yout, void *context, int WrapColumn)
+    : IO(context),
+      Out(yout),
+      WrapColumn(WrapColumn),
+      Column(0),
+      ColumnAtFlowStart(0),
+      ColumnAtMapFlowStart(0),
+      NeedBitValueComma(false),
+      NeedFlowSequenceComma(false),
+      EnumerationMatchFound(false),
+      NeedsNewLine(false) {
 }
 
 Output::~Output() {
@@ -365,35 +421,62 @@ void Output::beginMapping() {
   NeedsNewLine = true;
 }
 
+bool Output::mapTag(StringRef Tag, bool Use) {
+  if (Use) {
+    this->output(" ");
+    this->output(Tag);
+  }
+  return Use;
+}
+
 void Output::endMapping() {
   StateStack.pop_back();
 }
 
-
 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
-                                                bool &UseDefault, void *&) {
+                          bool &UseDefault, void *&) {
   UseDefault = false;
-  if ( Required || !SameAsDefault ) {
-    this->newLineCheck();
-    this->paddedKey(Key);
+  if (Required || !SameAsDefault) {
+    auto State = StateStack.back();
+    if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
+      flowKey(Key);
+    } else {
+      this->newLineCheck();
+      this->paddedKey(Key);
+    }
     return true;
   }
   return false;
 }
 
-void Output::postflightKey(void*) {
-  if ( StateStack.back() == inMapFirstKey ) {
+void Output::postflightKey(void *) {
+  if (StateStack.back() == inMapFirstKey) {
     StateStack.pop_back();
     StateStack.push_back(inMapOtherKey);
+  } else if (StateStack.back() == inFlowMapFirstKey) {
+    StateStack.pop_back();
+    StateStack.push_back(inFlowMapOtherKey);
   }
 }
 
+void Output::beginFlowMapping() {
+  StateStack.push_back(inFlowMapFirstKey);
+  this->newLineCheck();
+  ColumnAtMapFlowStart = Column;
+  output("{ ");
+}
+
+void Output::endFlowMapping() {
+  StateStack.pop_back();
+  this->outputUpToEndOfLine(" }");
+}
+
 void Output::beginDocuments() {
   this->outputUpToEndOfLine("---");
 }
 
 bool Output::preflightDocument(unsigned index) {
-  if ( index > 0 )
+  if (index > 0)
     this->outputUpToEndOfLine("\n---");
   return true;
 }
@@ -410,51 +493,55 @@ unsigned Output::beginSequence() {
   NeedsNewLine = true;
   return 0;
 }
+
 void Output::endSequence() {
   StateStack.pop_back();
 }
-bool Output::preflightElement(unsigned , void *&) {
+
+bool Output::preflightElement(unsigned, void *&) {
   return true;
 }
-void Output::postflightElement(void*) {
+
+void Output::postflightElement(void *) {
 }
 
 unsigned Output::beginFlowSequence() {
-  this->newLineCheck();
   StateStack.push_back(inFlowSeq);
+  this->newLineCheck();
   ColumnAtFlowStart = Column;
   output("[ ");
   NeedFlowSequenceComma = false;
   return 0;
 }
+
 void Output::endFlowSequence() {
   StateStack.pop_back();
   this->outputUpToEndOfLine(" ]");
 }
-bool Output::preflightFlowElement(unsigned , void *&) {
-  if ( NeedFlowSequenceComma )
+
+bool Output::preflightFlowElement(unsigned, void *&) {
+  if (NeedFlowSequenceComma)
     output(", ");
-  if ( Column > 70 ) {
+  if (WrapColumn && Column > WrapColumn) {
     output("\n");
-    for(int  i=0; i < ColumnAtFlowStart; ++i)
+    for (int i = 0; i < ColumnAtFlowStart; ++i)
       output(" ");
     Column = ColumnAtFlowStart;
     output("  ");
   }
   return true;
 }
-void Output::postflightFlowElement(void*) {
+
+void Output::postflightFlowElement(void *) {
   NeedFlowSequenceComma = true;
 }
 
-
-
 void Output::beginEnumScalar() {
   EnumerationMatchFound = false;
 }
 
 bool Output::matchEnumScalar(const char *Str, bool Match) {
-  if ( Match && !EnumerationMatchFound ) {
+  if (Match && !EnumerationMatchFound) {
     this->newLineCheck();
     this->outputUpToEndOfLine(Str);
     EnumerationMatchFound = true;
@@ -462,13 +549,18 @@ 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 )
+  if (!EnumerationMatchFound)
     llvm_unreachable("bad runtime enum value");
 }
 
-
-
 bool Output::beginBitSetScalar(bool &DoClear) {
   this->newLineCheck();
   output("[ ");
@@ -478,8 +570,8 @@ bool Output::beginBitSetScalar(bool &DoClear) {
 }
 
 bool Output::bitSetMatch(const char *Str, bool Matches) {
if ( Matches ) {
-    if ( NeedBitValueComma )
 if (Matches) {
+    if (NeedBitValueComma)
       output(", ");
     this->output(Str);
     NeedBitValueComma = true;
@@ -491,10 +583,16 @@ void Output::endBitSetScalar() {
   this->outputUpToEndOfLine(" ]");
 }
 
-void Output::scalarString(StringRef &S) {
+void Output::scalarString(StringRef &S, bool MustQuote) {
   this->newLineCheck();
-  if (S.find('\n') == StringRef::npos) {
-    // No embedded new-line chars, just print string.
+  if (S.empty()) {
+    // Print '' for the empty string because leaving the field empty is not
+    // allowed.
+    this->outputUpToEndOfLine("''");
+    return;
+  }
+  if (!MustQuote) {
+    // Only quote if we must.
     this->outputUpToEndOfLine(S);
     return;
   }
@@ -516,9 +614,39 @@ void Output::scalarString(StringRef &S) {
   this->outputUpToEndOfLine("'"); // Ending single quote.
 }
 
+void Output::blockScalarString(StringRef &S) {
+  if (!StateStack.empty())
+    newLineCheck();
+  output(" |");
+  outputNewLine();
+
+  unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
+
+  auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
+  for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
+    for (unsigned I = 0; I < Indent; ++I) {
+      output("  ");
+    }
+    output(*Lines);
+    outputNewLine();
+  }
+}
+
 void Output::setError(const Twine &message) {
 }
 
+bool Output::canElideEmptySequence() {
+  // Normally, with an optional key/value where the value is an empty sequence,
+  // the whole key/value can be not written.  But, that produces wrong yaml
+  // if the key/value is the only thing in the map and the map is used in
+  // a sequence.  This detects if the this sequence is the first key/value
+  // in map that itself is embedded in a sequnce.
+  if (StateStack.size() < 2)
+    return true;
+  if (StateStack.back() != inMapFirstKey)
+    return true;
+  return (StateStack[StateStack.size()-2] != inSeq);
+}
 
 void Output::output(StringRef s) {
   Column += s.size();
@@ -527,7 +655,9 @@ void Output::output(StringRef s) {
 
 void Output::outputUpToEndOfLine(StringRef s) {
   this->output(s);
-  if ( StateStack.back() != inFlowSeq )
+  if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
+                             StateStack.back() != inFlowMapFirstKey &&
+                             StateStack.back() != inFlowMapOtherKey))
     NeedsNewLine = true;
 }
 
@@ -541,7 +671,7 @@ void Output::outputNewLine() {
 //
 
 void Output::newLineCheck() {
-  if ( ! NeedsNewLine )
+  if (!NeedsNewLine)
     return;
   NeedsNewLine = false;
 
@@ -551,20 +681,20 @@ void Output::newLineCheck() {
   unsigned Indent = StateStack.size() - 1;
   bool OutputDash = false;
 
-  if ( StateStack.back() == inSeq ) {
+  if (StateStack.back() == inSeq) {
     OutputDash = true;
-  }
-  else if ( (StateStack.size() > 1)
-            && (StateStack.back() == inMapFirstKey)
-            && (StateStack[StateStack.size()-2] == inSeq) ) {
+  } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
+             (StateStack.back() == inFlowSeq) ||
+             (StateStack.back() == inFlowMapFirstKey)) &&
+             (StateStack[StateStack.size() - 2] == inSeq)) {
     --Indent;
     OutputDash = true;
   }
 
-  for (unsigned i=0; i < Indent; ++i) {
+  for (unsigned i = 0; i < Indent; ++i) {
     output("  ");
   }
-  if ( OutputDash ) {
+  if (OutputDash) {
     output("- ");
   }
 
@@ -574,308 +704,269 @@ void Output::paddedKey(StringRef key) {
   output(key);
   output(":");
   const char *spaces = "                ";
-  if ( key.size() < strlen(spaces) )
+  if (key.size() < strlen(spaces))
     output(&spaces[key.size()]);
   else
     output(" ");
 }
 
+void Output::flowKey(StringRef Key) {
+  if (StateStack.back() == inFlowMapOtherKey)
+    output(", ");
+  if (WrapColumn && Column > WrapColumn) {
+    output("\n");
+    for (int I = 0; I < ColumnAtMapFlowStart; ++I)
+      output(" ");
+    Column = ColumnAtMapFlowStart;
+    output("  ");
+  }
+  output(Key);
+  output(": ");
+}
+
 //===----------------------------------------------------------------------===//
 //  traits for built-in types
 //===----------------------------------------------------------------------===//
 
-template<>
-struct ScalarTraits<bool> {
-  static void output(const bool &Val, void*, llvm::raw_ostream &Out) {
-    Out << ( Val ? "true" : "false");
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, bool &Val) {
-    if ( Scalar.equals("true") ) {
-      Val = true;
-      return StringRef();
-    }
-    else if ( Scalar.equals("false") ) {
-      Val = false;
-      return StringRef();
-    }
-    return "invalid boolean";
-  }
-};
-
+void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
+  Out << (Val ? "true" : "false");
+}
 
-template<>
-struct ScalarTraits<StringRef> {
-  static void output(const StringRef &Val, void*, llvm::raw_ostream &Out) {
-    Out << Val;
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, StringRef &Val){
-    Val = Scalar;
+StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
+  if (Scalar.equals("true")) {
+    Val = true;
     return StringRef();
-  }
-};
-
-
-template<>
-struct ScalarTraits<uint8_t> {
-  static void output(const uint8_t &Val, void*, llvm::raw_ostream &Out) {
-    // use temp uin32_t because ostream thinks uint8_t is a character
-    uint32_t Num = Val;
-    Out << Num;
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, uint8_t &Val) {
-    uint64_t n;
-    if ( getAsUnsignedInteger(Scalar, 0, n) )
-      return "invalid number";
-    if ( n > 0xFF )
-      return "out of range number";
-    Val = n;
+  } else if (Scalar.equals("false")) {
+    Val = false;
     return StringRef();
   }
-};
+  return "invalid boolean";
+}
 
+void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
+                                     raw_ostream &Out) {
+  Out << Val;
+}
 
-template<>
-struct ScalarTraits<uint16_t> {
-  static void output(const uint16_t &Val, void*, llvm::raw_ostream &Out) {
-    Out << Val;
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, uint16_t &Val) {
-    uint64_t n;
-    if ( getAsUnsignedInteger(Scalar, 0, n) )
-      return "invalid number";
-    if ( n > 0xFFFF )
-      return "out of range number";
-    Val = n;
-    return StringRef();
-  }
-};
+StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
+                                         StringRef &Val) {
+  Val = Scalar;
+  return StringRef();
+}
 
-template<>
-struct ScalarTraits<uint32_t> {
-  static void output(const uint32_t &Val, void*, llvm::raw_ostream &Out) {
-    Out << Val;
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, uint32_t &Val) {
-    uint64_t n;
-    if ( getAsUnsignedInteger(Scalar, 0, n) )
-      return "invalid number";
-    if ( n > 0xFFFFFFFFUL )
-      return "out of range number";
-    Val = n;
-    return StringRef();
-  }
-};
+void ScalarTraits<std::string>::output(const std::string &Val, void *,
+                                     raw_ostream &Out) {
+  Out << Val;
+}
 
+StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
+                                         std::string &Val) {
+  Val = Scalar.str();
+  return StringRef();
+}
 
-template<>
-struct ScalarTraits<uint64_t> {
-  static void output(const uint64_t &Val, void*, llvm::raw_ostream &Out) {
-    Out << Val;
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, uint64_t &Val) {
-    if ( getAsUnsignedInteger(Scalar, 0, Val) )
-      return "invalid number";
-    return StringRef();
-  }
-};
+void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
+                                   raw_ostream &Out) {
+  // use temp uin32_t because ostream thinks uint8_t is a character
+  uint32_t Num = Val;
+  Out << Num;
+}
 
+StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
+  unsigned long long n;
+  if (getAsUnsignedInteger(Scalar, 0, n))
+    return "invalid number";
+  if (n > 0xFF)
+    return "out of range number";
+  Val = n;
+  return StringRef();
+}
 
-template<>
-struct ScalarTraits<int8_t> {
-  static void output(const int8_t &Val, void*, llvm::raw_ostream &Out) {
-    // use temp in32_t because ostream thinks int8_t is a character
-    int32_t Num = Val;
-    Out << Num;
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, int8_t &Val) {
-    int64_t n;
-    if ( getAsSignedInteger(Scalar, 0, n) )
-      return "invalid number";
-    if ( (n > 127) || (n < -128) )
-      return "out of range number";
-    Val = n;
-    return StringRef();
-  }
-};
+void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
+                                    raw_ostream &Out) {
+  Out << Val;
+}
 
+StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
+                                        uint16_t &Val) {
+  unsigned long long n;
+  if (getAsUnsignedInteger(Scalar, 0, n))
+    return "invalid number";
+  if (n > 0xFFFF)
+    return "out of range number";
+  Val = n;
+  return StringRef();
+}
 
-template<>
-struct ScalarTraits<int16_t> {
-  static void output(const int16_t &Val, void*, llvm::raw_ostream &Out) {
-    Out << Val;
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, int16_t &Val) {
-    int64_t n;
-    if ( getAsSignedInteger(Scalar, 0, n) )
-      return "invalid number";
-    if ( (n > INT16_MAX) || (n < INT16_MIN) )
-      return "out of range number";
-    Val = n;
-    return StringRef();
-  }
-};
+void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
+                                    raw_ostream &Out) {
+  Out << Val;
+}
 
+StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
+                                        uint32_t &Val) {
+  unsigned long long n;
+  if (getAsUnsignedInteger(Scalar, 0, n))
+    return "invalid number";
+  if (n > 0xFFFFFFFFUL)
+    return "out of range number";
+  Val = n;
+  return StringRef();
+}
 
-template<>
-struct ScalarTraits<int32_t> {
-  static void output(const int32_t &Val, void*, llvm::raw_ostream &Out) {
-    Out << Val;
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, int32_t &Val) {
-    int64_t n;
-    if ( getAsSignedInteger(Scalar, 0, n) )
-      return "invalid number";
-    if ( (n > INT32_MAX) || (n < INT32_MIN) )
-      return "out of range number";
-    Val = n;
-    return StringRef();
-  }
-};
+void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
+                                    raw_ostream &Out) {
+  Out << Val;
+}
 
-template<>
-struct ScalarTraits<int64_t> {
-  static void output(const int64_t &Val, void*, llvm::raw_ostream &Out) {
-    Out << Val;
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, int64_t &Val) {
-    if ( getAsSignedInteger(Scalar, 0, Val) )
-      return "invalid number";
-    return StringRef();
-  }
-};
+StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
+                                        uint64_t &Val) {
+  unsigned long long N;
+  if (getAsUnsignedInteger(Scalar, 0, N))
+    return "invalid number";
+  Val = N;
+  return StringRef();
+}
 
-template<>
-struct ScalarTraits<double> {
-  static void output(const double &Val, void*, llvm::raw_ostream &Out) {
-  Out << format("%g", Val);
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, double &Val) {
-    SmallString<32> buff(Scalar.begin(), Scalar.end());
-    char *end;
-    Val = strtod(buff.c_str(), &end);
-    if ( *end != '\0' )
-      return "invalid floating point number";
-    return StringRef();
-  }
-};
+void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
+  // use temp in32_t because ostream thinks int8_t is a character
+  int32_t Num = Val;
+  Out << Num;
+}
 
-template<>
-struct ScalarTraits<float> {
-  static void output(const float &Val, void*, llvm::raw_ostream &Out) {
-  Out << format("%g", Val);
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, float &Val) {
-    SmallString<32> buff(Scalar.begin(), Scalar.end());
-    char *end;
-    Val = strtod(buff.c_str(), &end);
-    if ( *end != '\0' )
-      return "invalid floating point number";
-    return StringRef();
-  }
-};
+StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
+  long long N;
+  if (getAsSignedInteger(Scalar, 0, N))
+    return "invalid number";
+  if ((N > 127) || (N < -128))
+    return "out of range number";
+  Val = N;
+  return StringRef();
+}
 
+void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
+                                   raw_ostream &Out) {
+  Out << Val;
+}
 
+StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
+  long long N;
+  if (getAsSignedInteger(Scalar, 0, N))
+    return "invalid number";
+  if ((N > INT16_MAX) || (N < INT16_MIN))
+    return "out of range number";
+  Val = N;
+  return StringRef();
+}
 
-template<>
-struct ScalarTraits<Hex8> {
-  static void output(const Hex8 &Val, void*, llvm::raw_ostream &Out) {
-    uint8_t Num = Val;
-    Out << format("0x%02X", Num);
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, Hex8 &Val) {
-    uint64_t n;
-    if ( getAsUnsignedInteger(Scalar, 0, n) )
-      return "invalid hex8 number";
-    if ( n > 0xFF )
-      return "out of range hex8 number";
-    Val = n;
-    return StringRef();
-  }
-};
+void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
+                                   raw_ostream &Out) {
+  Out << Val;
+}
 
+StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
+  long long N;
+  if (getAsSignedInteger(Scalar, 0, N))
+    return "invalid number";
+  if ((N > INT32_MAX) || (N < INT32_MIN))
+    return "out of range number";
+  Val = N;
+  return StringRef();
+}
 
-template<>
-struct ScalarTraits<Hex16> {
-  static void output(const Hex16 &Val, void*, llvm::raw_ostream &Out) {
-    uint16_t Num = Val;
-    Out << format("0x%04X", Num);
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, Hex16 &Val) {
-    uint64_t n;
-    if ( getAsUnsignedInteger(Scalar, 0, n) )
-      return "invalid hex16 number";
-    if ( n > 0xFFFF )
-      return "out of range hex16 number";
-    Val = n;
-    return StringRef();
-  }
-};
-
-template<>
-struct ScalarTraits<Hex32> {
-  static void output(const Hex32 &Val, void*, llvm::raw_ostream &Out) {
-    uint32_t Num = Val;
-    Out << format("0x%08X", Num);
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, Hex32 &Val) {
-    uint64_t n;
-    if ( getAsUnsignedInteger(Scalar, 0, n) )
-      return "invalid hex32 number";
-    if ( n > 0xFFFFFFFFUL )
-      return "out of range hex32 number";
-    Val = n;
-    return StringRef();
-  }
-};
+void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
+                                   raw_ostream &Out) {
+  Out << Val;
+}
+
+StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
+  long long N;
+  if (getAsSignedInteger(Scalar, 0, N))
+    return "invalid number";
+  Val = N;
+  return StringRef();
+}
 
+void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
+  Out << format("%g", Val);
+}
 
-template<>
-struct ScalarTraits<Hex64> {
-  static void output(const Hex64 &Val, void*, llvm::raw_ostream &Out) {
-    uint64_t Num = Val;
-    Out << format("0x%016llX", Num);
-  }
-  static llvm::StringRef input(llvm::StringRef Scalar, void*, Hex64 &Val) {
-    uint64_t Num;
-    if ( getAsUnsignedInteger(Scalar, 0, Num) )
-      return "invalid hex64 number";
-    Val = Num;
-    return StringRef();
-  }
-};
+StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
+  SmallString<32> buff(Scalar.begin(), Scalar.end());
+  char *end;
+  Val = strtod(buff.c_str(), &end);
+  if (*end != '\0')
+    return "invalid floating point number";
+  return StringRef();
+}
 
+void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
+  Out << format("%g", Val);
+}
 
+StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
+  SmallString<32> buff(Scalar.begin(), Scalar.end());
+  char *end;
+  Val = strtod(buff.c_str(), &end);
+  if (*end != '\0')
+    return "invalid floating point number";
+  return StringRef();
+}
 
+void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
+  uint8_t Num = Val;
+  Out << format("0x%02X", Num);
+}
 
-// We want all the ScalarTrait specialized on built-in types
-// to be instantiated here.
-template <typename T>
-struct ForceUse {
-  ForceUse() : oproc(ScalarTraits<T>::output), iproc(ScalarTraits<T>::input) {}
-  void (*oproc)(const T &, void*, llvm::raw_ostream &);
-  llvm::StringRef (*iproc)(llvm::StringRef, void*, T &);
-};
+StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
+  unsigned long long n;
+  if (getAsUnsignedInteger(Scalar, 0, n))
+    return "invalid hex8 number";
+  if (n > 0xFF)
+    return "out of range hex8 number";
+  Val = n;
+  return StringRef();
+}
 
-static ForceUse<bool>            Dummy1;
-static ForceUse<llvm::StringRef> Dummy2;
-static ForceUse<uint8_t>         Dummy3;
-static ForceUse<uint16_t>        Dummy4;
-static ForceUse<uint32_t>        Dummy5;
-static ForceUse<uint64_t>        Dummy6;
-static ForceUse<int8_t>          Dummy7;
-static ForceUse<int16_t>         Dummy8;
-static ForceUse<int32_t>         Dummy9;
-static ForceUse<int64_t>         Dummy10;
-static ForceUse<float>           Dummy11;
-static ForceUse<double>          Dummy12;
-static ForceUse<Hex8>            Dummy13;
-static ForceUse<Hex16>           Dummy14;
-static ForceUse<Hex32>           Dummy15;
-static ForceUse<Hex64>           Dummy16;
+void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
+  uint16_t Num = Val;
+  Out << format("0x%04X", Num);
+}
 
+StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
+  unsigned long long n;
+  if (getAsUnsignedInteger(Scalar, 0, n))
+    return "invalid hex16 number";
+  if (n > 0xFFFF)
+    return "out of range hex16 number";
+  Val = n;
+  return StringRef();
+}
 
+void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
+  uint32_t Num = Val;
+  Out << format("0x%08X", Num);
+}
 
-} // namespace yaml
-} // namespace llvm
+StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
+  unsigned long long n;
+  if (getAsUnsignedInteger(Scalar, 0, n))
+    return "invalid hex32 number";
+  if (n > 0xFFFFFFFFUL)
+    return "out of range hex32 number";
+  Val = n;
+  return StringRef();
+}
 
+void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
+  uint64_t Num = Val;
+  Out << format("0x%016llX", Num);
+}
 
+StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
+  unsigned long long Num;
+  if (getAsUnsignedInteger(Scalar, 0, Num))
+    return "invalid hex64 number";
+  Val = Num;
+  return StringRef();
+}