<div dir="ltr"><div class="gmail_default" style>Hey Nick, I just wanted to mention that YamlIO seems to be leaking memory:</div><div class="gmail_default" style><br></div><div class="gmail_default" style><a href="http://lab.llvm.org:8011/builders/llvm-x86_64-linux-vg_leak/builds/140">http://lab.llvm.org:8011/builders/llvm-x86_64-linux-vg_leak/builds/140</a><br>
</div><div class="gmail_default" style><br></div><div class="gmail_default" style>There are other (unrelated) failures on that bot, the yamlio ones look like actual leaks to me?</div></div><div class="gmail_extra"><br><br>
<div class="gmail_quote">On Wed, Dec 12, 2012 at 12:46 PM, Nick Kledzik <span dir="ltr"><<a href="mailto:kledzik@apple.com" target="_blank">kledzik@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: kledzik<br>
Date: Wed Dec 12 14:46:15 2012<br>
New Revision: 170019<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=170019&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=170019&view=rev</a><br>
Log:<br>
Initial implementation of a utility for converting native data<br>
structures to and from YAML using traits.  The first client will<br>
be the test suite of lld.  The documentation will show up at:<br>
<br>
   <a href="http://llvm.org/docs/YamlIO.html" target="_blank">http://llvm.org/docs/YamlIO.html</a><br>
<br>
<br>
<br>
Added:<br>
    llvm/trunk/docs/YamlIO.rst<br>
    llvm/trunk/include/llvm/Support/YAMLTraits.h<br>
    llvm/trunk/lib/Support/YAMLTraits.cpp<br>
    llvm/trunk/unittests/Support/YAMLIOTest.cpp<br>
Modified:<br>
    llvm/trunk/docs/userguides.rst<br>
    llvm/trunk/lib/Support/CMakeLists.txt<br>
    llvm/trunk/unittests/Support/CMakeLists.txt<br>
<br>
Added: llvm/trunk/docs/YamlIO.rst<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/YamlIO.rst?rev=170019&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/YamlIO.rst?rev=170019&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/docs/YamlIO.rst (added)<br>
+++ llvm/trunk/docs/YamlIO.rst Wed Dec 12 14:46:15 2012<br>
@@ -0,0 +1,862 @@<br>
+.. _yamlio:<br>
+<br>
+=====================<br>
+YAML I/O<br>
+=====================<br>
+<br>
+.. contents::<br>
+   :local:<br>
+<br>
+Introduction to YAML<br>
+====================<br>
+<br>
+YAML is a human readable data serialization language.  The full YAML language<br>
+spec can be read at `<a href="http://yaml.org" target="_blank">yaml.org</a><br>
+<<a href="http://www.yaml.org/spec/1.2/spec.html#Introduction" target="_blank">http://www.yaml.org/spec/1.2/spec.html#Introduction</a>>`_.  The simplest form of<br>
+yaml is just "scalars", "mappings", and "sequences".  A scalar is any number<br>
+or string.  The pound/hash symbol (#) begins a comment line.   A mapping is<br>
+a set of key-value pairs where the key ends with a colon.  For example:<br>
+<br>
+.. code-block:: yaml<br>
+<br>
+     # a mapping<br>
+     name:      Tom<br>
+     hat-size:  7<br>
+<br>
+A sequence is a list of items where each item starts with a leading dash ('-').<br>
+For example:<br>
+<br>
+.. code-block:: yaml<br>
+<br>
+     # a sequence<br>
+     - x86<br>
+     - x86_64<br>
+     - PowerPC<br>
+<br>
+You can combine mappings and sequences by indenting.  For example a sequence<br>
+of mappings in which one of the mapping values is itself a sequence:<br>
+<br>
+.. code-block:: yaml<br>
+<br>
+     # a sequence of mappings with one key's value being a sequence<br>
+     - name:      Tom<br>
+       cpus:<br>
+        - x86<br>
+        - x86_64<br>
+     - name:      Bob<br>
+       cpus:<br>
+        - x86<br>
+     - name:      Dan<br>
+       cpus:<br>
+        - PowerPC<br>
+        - x86<br>
+<br>
+Sometime sequences are known to be short and the one entry per line is too<br>
+verbose, so YAML offers an alternate syntax for sequences called a "Flow<br>
+Sequence" in which you put comma separated sequence elements into square<br>
+brackets.  The above example could then be simplified to :<br>
+<br>
+<br>
+.. code-block:: yaml<br>
+<br>
+     # a sequence of mappings with one key's value being a flow sequence<br>
+     - name:      Tom<br>
+       cpus:      [ x86, x86_64 ]<br>
+     - name:      Bob<br>
+       cpus:      [ x86 ]<br>
+     - name:      Dan<br>
+       cpus:      [ PowerPC, x86 ]<br>
+<br>
+<br>
+Introduction to YAML I/O<br>
+========================<br>
+<br>
+The use of indenting makes the YAML easy for a human to read and understand,<br>
+but having a program read and write YAML involves a lot of tedious details.<br>
+The YAML I/O library structures and simplifies reading and writing YAML<br>
+documents.<br>
+<br>
+YAML I/O assumes you have some "native" data structures which you want to be<br>
+able to dump as YAML and recreate from YAML.  The first step is to try<br>
+writing example YAML for your data structures. You may find after looking at<br>
+possible YAML representations that a direct mapping of your data structures<br>
+to YAML is not very readable.  Often the fields are not in the order that<br>
+a human would find readable.  Or the same information is replicated in multiple<br>
+locations, making it hard for a human to write such YAML correctly.<br>
+<br>
+In relational database theory there is a design step called normalization in<br>
+which you reorganize fields and tables.  The same considerations need to<br>
+go into the design of your YAML encoding.  But, you may not want to change<br>
+your exisiting native data structures.  Therefore, when writing out YAML<br>
+there may be a normalization step, and when reading YAML there would be a<br>
+corresponding denormalization step.<br>
+<br>
+YAML I/O uses a non-invasive, traits based design.  YAML I/O defines some<br>
+abstract base templates.  You specialize those templates on your data types.<br>
+For instance, if you have an eumerated type FooBar you could specialize<br>
+ScalarEnumerationTraits on that type and define the enumeration() method:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::ScalarEnumerationTraits;<br>
+    using llvm::yaml::IO;<br>
+<br>
+    template <><br>
+    struct ScalarEnumerationTraits<FooBar> {<br>
+      static void enumeration(IO &io, FooBar &value) {<br>
+      ...<br>
+      }<br>
+    };<br>
+<br>
+<br>
+As with all YAML I/O template specializations, the ScalarEnumerationTraits is used for<br>
+both reading and writing YAML. That is, the mapping between in-memory enum<br>
+values and the YAML string representation is only in place.<br>
+This assures that the code for writing and parsing of YAML stays in sync.<br>
+<br>
+To specify a YAML mappings, you define a specialization on<br>
+llvm::yaml::MapppingTraits.<br>
+If your native data structure happens to be a struct that is already normalized,<br>
+then the specialization is simple.  For example:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::MapppingTraits;<br>
+    using llvm::yaml::IO;<br>
+<br>
+    template <><br>
+    struct MapppingTraits<Person> {<br>
+      static void mapping(IO &io, Person &info) {<br>
+        io.mapRequired("name",         <a href="http://info.name" target="_blank">info.name</a>);<br>
+        io.mapOptional("hat-size",     info.hatSize);<br>
+      }<br>
+    };<br>
+<br>
+<br>
+A YAML sequence is automatically infered if you data type has begin()/end()<br>
+iterators and a push_back() method.  Therefore any of the STL containers<br>
+(such as std::vector<>) will automatically translate to YAML sequences.<br>
+<br>
+Once you have defined specializations for your data types, you can<br>
+programmatically use YAML I/O to write a YAML document:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::Output;<br>
+<br>
+    Person tom;<br>
+    <a href="http://tom.name" target="_blank">tom.name</a> = "Tom";<br>
+    tom.hatSize = 8;<br>
+    Person dan;<br>
+    <a href="http://dan.name" target="_blank">dan.name</a> = "Dan";<br>
+    dan.hatSize = 7;<br>
+    std::vector<Person> persons;<br>
+    persons.push_back(tom);<br>
+    persons.push_back(dan);<br>
+<br>
+    Output yout(llvm::outs());<br>
+    yout << persons;<br>
+<br>
+This would write the following:<br>
+<br>
+.. code-block:: yaml<br>
+<br>
+     - name:      Tom<br>
+       hat-size:  8<br>
+     - name:      Dan<br>
+       hat-size:  7<br>
+<br>
+And you can also read such YAML documents with the following code:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::Input;<br>
+<br>
+    typedef std::vector<Person> PersonList;<br>
+    std::vector<PersonList> docs;<br>
+<br>
+    Input yin(document.getBuffer());<br>
+    yin >> docs;<br>
+<br>
+    if ( yin.error() )<br>
+      return;<br>
+<br>
+    // Process read document<br>
+    for ( PersonList &pl : docs ) {<br>
+      for ( Person &person : pl ) {<br>
+        cout << "name=" << <a href="http://person.name" target="_blank">person.name</a>;<br>
+      }<br>
+    }<br>
+<br>
+One other feature of YAML is the ability to define multiple documents in a<br>
+single file.  That is why reading YAML produces a vector of your document type.<br>
+<br>
+<br>
+<br>
+Error Handling<br>
+==============<br>
+<br>
+When parsing a YAML document, if the input does not match your schema (as<br>
+expressed in your XxxTraits<> specializations).  YAML I/O<br>
+will print out an error message and your Input object's error() method will<br>
+return true. For instance the following document:<br>
+<br>
+.. code-block:: yaml<br>
+<br>
+     - name:      Tom<br>
+       shoe-size: 12<br>
+     - name:      Dan<br>
+       hat-size:  7<br>
+<br>
+Has a key (shoe-size) that is not defined in the schema.  YAML I/O will<br>
+automatically generate this error:<br>
+<br>
+.. code-block:: yaml<br>
+<br>
+    YAML:2:2: error: unknown key 'shoe-size'<br>
+      shoe-size:       12<br>
+      ^~~~~~~~~<br>
+<br>
+Similar errors are produced for other input not conforming to the schema.<br>
+<br>
+<br>
+Scalars<br>
+=======<br>
+<br>
+YAML scalars are just strings (i.e. not a sequence or mapping).  The YAML I/O<br>
+library provides support for translating between YAML scalars and specific<br>
+C++ types.<br>
+<br>
+<br>
+Built-in types<br>
+--------------<br>
+The following types have built-in support in YAML I/O:<br>
+<br>
+* bool<br>
+* float<br>
+* double<br>
+* StringRef<br>
+* int64_t<br>
+* int32_t<br>
+* int16_t<br>
+* int8_t<br>
+* uint64_t<br>
+* uint32_t<br>
+* uint16_t<br>
+* uint8_t<br>
+<br>
+That is, you can use those types in fields of MapppingTraits or as element type<br>
+in sequence.  When reading, YAML I/O will validate that the string found<br>
+is convertible to that type and error out if not.<br>
+<br>
+<br>
+Unique types<br>
+------------<br>
+Given that YAML I/O is trait based, the selection of how to convert your data<br>
+to YAML is based on the type of your data.  But in C++ type matching, typedefs<br>
+do not generate unique type names.  That means if you have two typedefs of<br>
+unsigned int, to YAML I/O both types look exactly like unsigned int.  To<br>
+facilitate make unique type names, YAML I/O provides a macro which is used<br>
+like a typedef on built-in types, but expands to create a class with conversion<br>
+operators to and from the base type.  For example:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    LLVM_YAML_STRONG_TYPEDEF(uint32_t, MyFooFlags)<br>
+    LLVM_YAML_STRONG_TYPEDEF(uint32_t, MyBarFlags)<br>
+<br>
+This generates two classes MyFooFlags and MyBarFlags which you can use in your<br>
+native data structures instead of uint32_t. They are implicitly<br>
+converted to and from uint32_t.  The point of creating these unique types<br>
+is that you can now specify traits on them to get different YAML conversions.<br>
+<br>
+Hex types<br>
+---------<br>
+An example use of a unique type is that YAML I/O provides fixed sized unsigned<br>
+integers that are written with YAML I/O as hexadecimal instead of the decimal<br>
+format used by the built-in integer types:<br>
+<br>
+* Hex64<br>
+* Hex32<br>
+* Hex16<br>
+* Hex8<br>
+<br>
+You can use llvm::yaml::Hex32 instead of uint32_t and the only different will<br>
+be that when YAML I/O writes out that type it will be formatted in hexadecimal.<br>
+<br>
+<br>
+ScalarEnumerationTraits<br>
+-----------------------<br>
+YAML I/O supports translating between in-memory enumerations and a set of string<br>
+values in YAML documents. This is done by specializing ScalarEnumerationTraits<><br>
+on your enumeration type and define a enumeration() method.<br>
+For instance, suppose you had an enumeration of CPUs and a struct with it as<br>
+a field:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    enum CPUs {<br>
+      cpu_x86_64  = 5,<br>
+      cpu_x86     = 7,<br>
+      cpu_PowerPC = 8<br>
+    };<br>
+<br>
+    struct Info {<br>
+      CPUs      cpu;<br>
+      uint32_t  flags;<br>
+    };<br>
+<br>
+To support reading and writing of this enumeration, you can define a<br>
+ScalarEnumerationTraits specialization on CPUs, which can then be used<br>
+as a field type:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::ScalarEnumerationTraits;<br>
+    using llvm::yaml::MapppingTraits;<br>
+    using llvm::yaml::IO;<br>
+<br>
+    template <><br>
+    struct ScalarEnumerationTraits<CPUs> {<br>
+      static void enumeration(IO &io, CPUs &value) {<br>
+        io.enumCase(value, "x86_64",  cpu_x86_64);<br>
+        io.enumCase(value, "x86",     cpu_x86);<br>
+        io.enumCase(value, "PowerPC", cpu_PowerPC);<br>
+      }<br>
+    };<br>
+<br>
+    template <><br>
+    struct MapppingTraits<Info> {<br>
+      static void mapping(IO &io, Info &info) {<br>
+        io.mapRequired("cpu",       info.cpu);<br>
+        io.mapOptional("flags",     info.flags, 0);<br>
+      }<br>
+    };<br>
+<br>
+When reading YAML, if the string found does not match any of the the strings<br>
+specified by enumCase() methods, an error is automatically generated.<br>
+When writing YAML, if the value being written does not match any of the values<br>
+specified by the enumCase() methods, a runtime assertion is triggered.<br>
+<br>
+<br>
+BitValue<br>
+--------<br>
+Another common data structure in C++ is a field where each bit has a unique<br>
+meaning.  This is often used in a "flags" field.  YAML I/O has support for<br>
+converting such fields to a flow sequence.   For instance suppose you<br>
+had the following bit flags defined:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    enum {<br>
+      flagsPointy = 1<br>
+      flagsHollow = 2<br>
+      flagsFlat   = 4<br>
+      flagsRound  = 8<br>
+    };<br>
+<br>
+    LLVM_YAML_UNIQUE_TYPE(MyFlags, uint32_t)<br>
+<br>
+To support reading and writing of MyFlags, you specialize ScalarBitSetTraits<><br>
+on MyFlags and provide the bit values and their names.<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::ScalarBitSetTraits;<br>
+    using llvm::yaml::MapppingTraits;<br>
+    using llvm::yaml::IO;<br>
+<br>
+    template <><br>
+    struct ScalarBitSetTraits<MyFlags> {<br>
+      static void bitset(IO &io, MyFlags &value) {<br>
+        io.bitSetCase(value, "hollow",  flagHollow);<br>
+        io.bitSetCase(value, "flat",    flagFlat);<br>
+        io.bitSetCase(value, "round",   flagRound);<br>
+        io.bitSetCase(value, "pointy",  flagPointy);<br>
+      }<br>
+    };<br>
+<br>
+    struct Info {<br>
+      StringRef   name;<br>
+      MyFlags     flags;<br>
+    };<br>
+<br>
+    template <><br>
+    struct MapppingTraits<Info> {<br>
+      static void mapping(IO &io, Info& info) {<br>
+        io.mapRequired("name",  <a href="http://info.name" target="_blank">info.name</a>);<br>
+        io.mapRequired("flags", info.flags);<br>
+       }<br>
+    };<br>
+<br>
+With the above, YAML I/O (when writing) will test mask each value in the<br>
+bitset trait against the flags field, and each that matches will<br>
+cause the corresponding string to be added to the flow sequence.  The opposite<br>
+is done when reading and any unknown string values will result in a error. With<br>
+the above schema, a same valid YAML document is:<br>
+<br>
+.. code-block:: yaml<br>
+<br>
+    name:    Tom<br>
+    flags:   [ pointy, flat ]<br>
+<br>
+<br>
+Custom Scalar<br>
+-------------<br>
+Sometimes for readability a scalar needs to be formatted in a custom way. For<br>
+instance your internal data structure may use a integer for time (seconds since<br>
+some epoch), but in YAML it would be much nicer to express that integer in<br>
+some time format (e.g. 4-May-2012 10:30pm).  YAML I/O has a way to support<br>
+custom formatting and parsing of scalar types by specializing ScalarTraits<> on<br>
+your data type.  When writing, YAML I/O will provide the native type and<br>
+your specialization must create a temporary llvm::StringRef.  When reading,<br>
+YAML I/O will provide a llvm::StringRef of scalar and your specialization<br>
+must convert that to your native data type.  An outline of a custom scalar type<br>
+looks like:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::ScalarTraits;<br>
+    using llvm::yaml::IO;<br>
+<br>
+    template <><br>
+    struct ScalarTraits<MyCustomType> {<br>
+      static void output(const T &value, llvm::raw_ostream &out) {<br>
+        out << value;  // do custom formatting here<br>
+      }<br>
+      static StringRef input(StringRef scalar, T &value) {<br>
+        // do custom parsing here.  Return the empty string on success,<br>
+        // or an error message on failure.<br>
+        return StringRef();<br>
+      }<br>
+    };<br>
+<br>
+<br>
+Mappings<br>
+========<br>
+<br>
+To be translated to or from a YAML mapping for your type T you must specialize<br>
+llvm::yaml::MapppingTraits on T and implement the "void mapping(IO &io, T&)"<br>
+method. If your native data structures use pointers to a class everywhere,<br>
+you can specialize on the class pointer.  Examples:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::MapppingTraits;<br>
+    using llvm::yaml::IO;<br>
+<br>
+    // Example of struct Foo which is used by value<br>
+    template <><br>
+    struct MapppingTraits<Foo> {<br>
+      static void mapping(IO &io, Foo &foo) {<br>
+        io.mapOptional("size",      foo.size);<br>
+      ...<br>
+      }<br>
+    };<br>
+<br>
+    // Example of struct Bar which is natively always a pointer<br>
+    template <><br>
+    struct MapppingTraits<Bar*> {<br>
+      static void mapping(IO &io, Bar *&bar) {<br>
+        io.mapOptional("size",    bar->size);<br>
+      ...<br>
+      }<br>
+    };<br>
+<br>
+<br>
+No Normalization<br>
+----------------<br>
+<br>
+The mapping() method is responsible, if needed, for normalizing and<br>
+denormalizing. In a simple case where the native data structure requires no<br>
+normalization, the mapping method just uses mapOptional() or mapRequired() to<br>
+bind the struct's fields to YAML key names.  For example:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::MapppingTraits;<br>
+    using llvm::yaml::IO;<br>
+<br>
+    template <><br>
+    struct MapppingTraits<Person> {<br>
+      static void mapping(IO &io, Person &info) {<br>
+        io.mapRequired("name",         <a href="http://info.name" target="_blank">info.name</a>);<br>
+        io.mapOptional("hat-size",     info.hatSize);<br>
+      }<br>
+    };<br>
+<br>
+<br>
+Normalization<br>
+----------------<br>
+<br>
+When [de]normalization is required, the mapping() method needs a way to access<br>
+normalized values as fields. To help with this, there is<br>
+a template MappingNormalization<> which you can then use to automatically<br>
+do the normalization and denormalization.  The template is used to create<br>
+a local variable in your mapping() method which contains the normalized keys.<br>
+<br>
+Suppose you have native data type<br>
+Polar which specifies a position in polar coordinates (distance, angle):<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    struct Polar {<br>
+      float distance;<br>
+      float angle;<br>
+    };<br>
+<br>
+but you've decided the normalized YAML for should be in x,y coordinates. That<br>
+is, you want the yaml to look like:<br>
+<br>
+.. code-block:: yaml<br>
+<br>
+    x:   10.3<br>
+    y:   -4.7<br>
+<br>
+You can support this by defining a MapppingTraits that normalizes the polar<br>
+coordinates to x,y coordinates when writing YAML and denormalizes x,y<br>
+coordindates into polar when reading YAML.<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::MapppingTraits;<br>
+    using llvm::yaml::IO;<br>
+<br>
+    template <><br>
+    struct MapppingTraits<Polar> {<br>
+<br>
+      class NormalizedPolar {<br>
+      public:<br>
+        NormalizedPolar(IO &io)<br>
+          : x(0.0), y(0.0) {<br>
+        }<br>
+        NormalizedPolar(IO &, Polar &polar)<br>
+          : x(polar.distance * cos(polar.angle)),<br>
+            y(polar.distance * sin(polar.angle)) {<br>
+        }<br>
+        Polar denormalize(IO &) {<br>
+          return Polar(sqrt(x*x+y*y, arctan(x,y));<br>
+        }<br>
+<br>
+        float        x;<br>
+        float        y;<br>
+      };<br>
+<br>
+      static void mapping(IO &io, Polar &polar) {<br>
+        MappingNormalization<NormalizedPolar, Polar> keys(io, polar);<br>
+<br>
+        io.mapRequired("x",    keys->x);<br>
+        io.mapRequired("y",    keys->y);<br>
+      }<br>
+    };<br>
+<br>
+When writing YAML, the local variable "keys" will be a stack allocated<br>
+instance of NormalizedPolar, constructed from the suppled polar object which<br>
+initializes it x and y fields.  The mapRequired() methods then write out the x<br>
+and y values as key/value pairs.<br>
+<br>
+When reading YAML, the local variable "keys" will be a stack allocated instance<br>
+of NormalizedPolar, constructed by the empty constructor.  The mapRequired<br>
+methods will find the matching key in the YAML document and fill in the x and y<br>
+fields of the NormalizedPolar object keys. At the end of the mapping() method<br>
+when the local keys variable goes out of scope, the denormalize() method will<br>
+automatically be called to convert the read values back to polar coordinates,<br>
+and then assigned back to the second parameter to mapping().<br>
+<br>
+In some cases, the normalized class may be a subclass of the native type and<br>
+could be returned by the denormalize() method, except that the temporary<br>
+normalized instance is stack allocated.  In these cases, the utility template<br>
+MappingNormalizationHeap<> can be used instead.  It just like<br>
+MappingNormalization<> except that it heap allocates the normalized object<br>
+when reading YAML.  It never destroyes the normalized object.  The denormalize()<br>
+method can this return "this".<br>
+<br>
+<br>
+Default values<br>
+--------------<br>
+Within a mapping() method, calls to io.mapRequired() mean that that key is<br>
+required to exist when parsing YAML documents, otherwise YAML I/O will issue an<br>
+error.<br>
+<br>
+On the other hand, keys registered with io.mapOptional() are allowed to not<br>
+exist in the YAML document being read.  So what value is put in the field<br>
+for those optional keys?<br>
+There are two steps to how those optional fields are filled in. First, the<br>
+second parameter to the mapping() method is a reference to a native class.  That<br>
+native class must have a default constructor.  Whatever value the default<br>
+constructor initially sets for an optional field will be that field's value.<br>
+Second, the mapOptional() method has an optional third parameter.  If provided<br>
+it is the value that mapOptional() should set that field to if the YAML document<br>
+does not have that key.<br>
+<br>
+There is one important difference between those two ways (default constructor<br>
+and third parameter to mapOptional). When YAML I/O generates a YAML document,<br>
+if the mapOptional() third parameter is used, if the actual value being written<br>
+is the same as (using ==) the default value, then that key/value is not written.<br>
+<br>
+<br>
+Order of Keys<br>
+--------------<br>
+<br>
+When writing out a YAML document, the keys are written in the order that the<br>
+calls to mapRequired()/mapOptional() are made in the mapping() method. This<br>
+gives you a chance to write the fields in an order that a human reader of<br>
+the YAML document would find natural.  This may be different that the order<br>
+of the fields in the native class.<br>
+<br>
+When reading in a YAML document, the keys in the document can be in any order,<br>
+but they are processed in the order that the calls to mapRequired()/mapOptional()<br>
+are made in the mapping() method.  That enables some interesting<br>
+functionality.  For instance, if the first field bound is the cpu and the second<br>
+field bound is flags, and the flags are cpu specific, you can programmatically<br>
+switch how the flags are converted to and from YAML based on the cpu.<br>
+This works for both reading and writing. For example:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::MapppingTraits;<br>
+    using llvm::yaml::IO;<br>
+<br>
+    struct Info {<br>
+      CPUs        cpu;<br>
+      uint32_t    flags;<br>
+    };<br>
+<br>
+    template <><br>
+    struct MapppingTraits<Info> {<br>
+      static void mapping(IO &io, Info &info) {<br>
+        io.mapRequired("cpu",       info.cpu);<br>
+        // flags must come after cpu for this to work when reading yaml<br>
+        if ( info.cpu == cpu_x86_64 )<br>
+          io.mapRequired("flags",  *(My86_64Flags*)info.flags);<br>
+        else<br>
+          io.mapRequired("flags",  *(My86Flags*)info.flags);<br>
+     }<br>
+    };<br>
+<br>
+<br>
+Sequence<br>
+========<br>
+<br>
+To be translated to or from a YAML sequence for your type T you must specialize<br>
+llvm::yaml::SequenceTraits on T and implement two methods:<br>
+“size_t size(IO &io, T&)† and â€œT::value_type& element(IO &io, T&, size_t indx)†.<br>
+For example:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+  template <><br>
+  struct SequenceTraits<MySeq> {<br>
+    static size_t size(IO &io, MySeq &list) { ... }<br>
+    static MySeqEl element(IO &io, MySeq &list, size_t index) { ... }<br>
+  };<br>
+<br>
+The size() method returns how many elements are currently in your sequence.<br>
+The element() method returns a reference to the i'th element in the sequence.<br>
+When parsing YAML, the element() method may be called with an index one bigger<br>
+than the current size.  Your element() method should allocate space for one<br>
+more element (using default constructor if element is a C++ object) and returns<br>
+a reference to that new allocated space.<br>
+<br>
+<br>
+Flow Sequence<br>
+-------------<br>
+A YAML "flow sequence" is a sequence that when written to YAML it uses the<br>
+inline notation (e.g [ foo, bar ] ).  To specify that a sequence type should<br>
+be written in YAML as a flow sequence, your SequenceTraits specialization should<br>
+add "static const bool flow = true;".  For instance:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+  template <><br>
+  struct SequenceTraits<MyList> {<br>
+    static size_t size(IO &io, MyList &list) { ... }<br>
+    static MyListEl element(IO &io, MyList &list, size_t index) { ... }<br>
+<br>
+    // The existence of this member causes YAML I/O to use a flow sequence<br>
+    static const bool flow = true;<br>
+  };<br>
+<br>
+With the above, if you used MyList as the data type in your native data<br>
+strucutures, then then when converted to YAML, a flow sequence of integers<br>
+will be used (e.g. [ 10, -3, 4 ]).<br>
+<br>
+<br>
+Utility Macros<br>
+--------------<br>
+Since a common source of sequences is std::vector<>, YAML I/O provids macros:<br>
+LLVM_YAML_IS_SEQUENCE_VECTOR() and LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR() which<br>
+can be used to easily specify SequenceTraits<> on a std::vector type.  YAML<br>
+I/O does not partial specialize SequenceTraits on std::vector<> because that<br>
+would force all vectors to be sequences.  An example use of the macros:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+  std::vector<MyType1>;<br>
+  std::vector<MyType2>;<br>
+  LLVM_YAML_IS_SEQUENCE_VECTOR(MyType1)<br>
+  LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyType2)<br>
+<br>
+<br>
+<br>
+Document List<br>
+=============<br>
+<br>
+YAML allows you to define multiple "documents" in a single YAML file.  Each<br>
+new document starts with a left aligned "---" token.  The end of all documents<br>
+is denoted with a left aligned "..." token.  Many users of YAML will never<br>
+have need for multiple documents.  The top level node in their YAML schema<br>
+will be a mapping or sequence. For those cases, the following is not needed.<br>
+But for cases where you do want multiple documents, you can specify a<br>
+trait for you document list type.  The trait has the same methods as<br>
+SequenceTraits but is named DocumentListTraits.  For example:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+  template <><br>
+  struct DocumentListTraits<MyDocList> {<br>
+    static size_t size(IO &io, MyDocList &list) { ... }<br>
+    static MyDocType element(IO &io, MyDocList &list, size_t index) { ... }<br>
+  };<br>
+<br>
+<br>
+User Context Data<br>
+=================<br>
+When an llvm::yaml::Input or llvm::yaml::Output object is created their<br>
+constructors take an optional "context" parameter.  This is a pointer to<br>
+whatever state information you might need.<br>
+<br>
+For instance, in a previous example we showed how the conversion type for a<br>
+flags field could be determined at runtime based on the value of another field<br>
+in the mapping. But what if an inner mapping needs to know some field value<br>
+of an outer mapping?  That is where the "context" parameter comes in. You<br>
+can set values in the context in the outer map's mapping() method and<br>
+retrieve those values in the inner map's mapping() method.<br>
+<br>
+The context value is just a void*.  All your traits which use the context<br>
+and operate on your native data types, need to agree what the context value<br>
+actually is.  It could be a pointer to an object or struct which your various<br>
+traits use to shared context sensitive information.<br>
+<br>
+<br>
+Output<br>
+======<br>
+<br>
+The llvm::yaml::Output class is used to generate a YAML document from your<br>
+in-memory data structures, using traits defined on your data types.<br>
+To instantiate an Output object you need an llvm::raw_ostream, and optionally<br>
+a context pointer:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+      class Output : public IO {<br>
+      public:<br>
+        Output(llvm::raw_ostream &, void *context=NULL);<br>
+<br>
+Once you have an Output object, you can use the C++ stream operator on it<br>
+to write your native data as YAML. One thing to recall is that a YAML file<br>
+can contain multiple "documents".  If the top level data structure you are<br>
+streaming as YAML is a mapping, scalar, or sequence, then Output assumes you<br>
+are generating one document and wraps the mapping output<br>
+with  "``---``" and trailing "``...``".<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::Output;<br>
+<br>
+    void dumpMyMapDoc(const MyMapType &info) {<br>
+      Output yout(llvm::outs());<br>
+      yout << info;<br>
+    }<br>
+<br>
+The above could produce output like:<br>
+<br>
+.. code-block:: yaml<br>
+<br>
+     ---<br>
+     name:      Tom<br>
+     hat-size:  7<br>
+     ...<br>
+<br>
+On the other hand, if the top level data structure you are streaming as YAML<br>
+has a DocumentListTraits specialization, then Output walks through each element<br>
+of your DocumentList and generates a "---" before the start of each element<br>
+and ends with a "...".<br>
+<br>
+.. code-block:: c++<br>
+<br>
+    using llvm::yaml::Output;<br>
+<br>
+    void dumpMyMapDoc(const MyDocListType &docList) {<br>
+      Output yout(llvm::outs());<br>
+      yout << docList;<br>
+    }<br>
+<br>
+The above could produce output like:<br>
+<br>
+.. code-block:: yaml<br>
+<br>
+     ---<br>
+     name:      Tom<br>
+     hat-size:  7<br>
+     ---<br>
+     name:      Tom<br>
+     shoe-size:  11<br>
+     ...<br>
+<br>
+Input<br>
+=====<br>
+<br>
+The llvm::yaml::Input class is used to parse YAML document(s) into your native<br>
+data structures. To instantiate an Input<br>
+object you need a StringRef to the entire YAML file, and optionally a context<br>
+pointer:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+      class Input : public IO {<br>
+      public:<br>
+        Input(StringRef inputContent, void *context=NULL);<br>
+<br>
+Once you have an Input object, you can use the C++ stream operator to read<br>
+the document(s).  If you expect there might be multiple YAML documents in<br>
+one file, you'll need to specialize DocumentListTraits on a list of your<br>
+document type and stream in that document list type.  Otherwise you can<br>
+just stream in the document type.  Also, you can check if there was<br>
+any syntax errors in the YAML be calling the error() method on the Input<br>
+object.  For example:<br>
+<br>
+.. code-block:: c++<br>
+<br>
+     // Reading a single document<br>
+     using llvm::yaml::Input;<br>
+<br>
+     Input yin(mb.getBuffer());<br>
+<br>
+     // Parse the YAML file<br>
+     MyDocType theDoc;<br>
+     yin >> theDoc;<br>
+<br>
+     // Check for error<br>
+     if ( yin.error() )<br>
+       return;<br>
+<br>
+<br>
+.. code-block:: c++<br>
+<br>
+     // Reading multiple documents in one file<br>
+     using llvm::yaml::Input;<br>
+<br>
+     LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(std::vector<MyDocType>)<br>
+<br>
+     Input yin(mb.getBuffer());<br>
+<br>
+     // Parse the YAML file<br>
+     std::vector<MyDocType> theDocList;<br>
+     yin >> theDocList;<br>
+<br>
+     // Check for error<br>
+     if ( yin.error() )<br>
+       return;<br>
+<br>
+<br>
<br>
Modified: llvm/trunk/docs/userguides.rst<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/userguides.rst?rev=170019&r1=170018&r2=170019&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/userguides.rst?rev=170019&r1=170018&r2=170019&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/docs/userguides.rst (original)<br>
+++ llvm/trunk/docs/userguides.rst Wed Dec 12 14:46:15 2012<br>
@@ -24,6 +24,7 @@<br>
    tutorial/index<br>
    ReleaseNotes<br>
    Passes<br>
+   YamlIO<br>
<br>
 * :ref:`getting_started`<br>
<br>
@@ -100,6 +101,10 @@<br>
<br>
    Instructions for adding new builder to LLVM buildbot master.<br>
<br>
+* :ref:`yamlio`<br>
+<br>
+   A reference guide for using LLVM's YAML I/O library.<br>
+<br>
 * **IRC** -- You can probably find help on the unofficial LLVM IRC.<br>
<br>
    We often are on <a href="http://irc.oftc.net" target="_blank">irc.oftc.net</a> in the #llvm channel.  If you are using the<br>
<br>
Added: llvm/trunk/include/llvm/Support/YAMLTraits.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/YAMLTraits.h?rev=170019&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/YAMLTraits.h?rev=170019&view=auto</a><br>

==============================================================================<br>
--- llvm/trunk/include/llvm/Support/YAMLTraits.h (added)<br>
+++ llvm/trunk/include/llvm/Support/YAMLTraits.h Wed Dec 12 14:46:15 2012<br>
@@ -0,0 +1,1114 @@<br>
+//===- llvm/Supporrt/YAMLTraits.h -------------------------------*- C++ -*-===//<br>
+//<br>
+//                             The LLVM Linker<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_YAML_TRAITS_H_<br>
+#define LLVM_YAML_TRAITS_H_<br>
+<br>
+<br>
+#include "llvm/ADT/DenseMap.h"<br>
+#include "llvm/ADT/DenseMapInfo.h"<br>
+#include "llvm/ADT/SmallVector.h"<br>
+#include "llvm/ADT/StringExtras.h"<br>
+#include "llvm/ADT/StringRef.h"<br>
+#include "llvm/ADT/StringSwitch.h"<br>
+#include "llvm/ADT/Twine.h"<br>
+#include "llvm/Support/Compiler.h"<br>
+#include "llvm/Support/SourceMgr.h"<br>
+#include "llvm/Support/system_error.h"<br>
+#include "llvm/Support/type_traits.h"<br>
+#include "llvm/Support/YAMLParser.h"<br>
+#include "llvm/Support/raw_ostream.h"<br>
+<br>
+<br>
+namespace llvm {<br>
+namespace yaml {<br>
+<br>
+<br>
+/// This class should be specialized by any type that needs to be converted<br>
+/// to/from a YAML mapping.  For example:<br>
+///<br>
+///     struct ScalarBitSetTraits<MyStruct> {<br>
+///       static void mapping(IO &io, MyStruct &s) {<br>
+///         io.mapRequired("name", <a href="http://s.name" target="_blank">s.name</a>);<br>
+///         io.mapRequired("size", s.size);<br>
+///         io.mapOptional("age",  s.age);<br>
+///       }<br>
+///     };<br>
+template<class T><br>
+struct MappingTraits {<br>
+  // Must provide:<br>
+  // static void mapping(IO &io, T &fields);<br>
+};<br>
+<br>
+<br>
+/// This class should be specialized by any integral type that converts<br>
+/// to/from a YAML scalar where there is a one-to-one mapping between<br>
+/// in-memory values and a string in YAML.  For example:<br>
+///<br>
+///     struct ScalarEnumerationTraits<Colors> {<br>
+///         static void enumeration(IO &io, Colors &value) {<br>
+///           io.enumCase(value, "red",   cRed);<br>
+///           io.enumCase(value, "blue",  cBlue);<br>
+///           io.enumCase(value, "green", cGreen);<br>
+///         }<br>
+///       };<br>
+template<typename T><br>
+struct ScalarEnumerationTraits {<br>
+  // Must provide:<br>
+  // static void enumeration(IO &io, T &value);<br>
+};<br>
+<br>
+<br>
+/// This class should be specialized by any integer type that is a union<br>
+/// of bit values and the YAML representation is a flow sequence of<br>
+/// strings.  For example:<br>
+///<br>
+///      struct ScalarBitSetTraits<MyFlags> {<br>
+///        static void bitset(IO &io, MyFlags &value) {<br>
+///          io.bitSetCase(value, "big",   flagBig);<br>
+///          io.bitSetCase(value, "flat",  flagFlat);<br>
+///          io.bitSetCase(value, "round", flagRound);<br>
+///        }<br>
+///      };<br>
+template<typename T><br>
+struct ScalarBitSetTraits {<br>
+  // Must provide:<br>
+  // static void bitset(IO &io, T &value);<br>
+};<br>
+<br>
+<br>
+/// This class should be specialized by type that requires custom conversion<br>
+/// to/from a yaml scalar.  For example:<br>
+///<br>
+///    template<><br>
+///    struct ScalarTraits<MyType> {<br>
+///      static void output(const MyType &val, void*, llvm::raw_ostream &out) {<br>
+///        // stream out custom formatting<br>
+///        out << llvm::format("%x", val);<br>
+///      }<br>
+///      static StringRef input(StringRef scalar, void*, MyType &value) {<br>
+///        // parse scalar and set `value`<br>
+///        // return empty string on success, or error string<br>
+///        return StringRef();<br>
+///      }<br>
+///    };<br>
+template<typename T><br>
+struct ScalarTraits {<br>
+  // Must provide:<br>
+  //<br>
+  // Function to write the value as a string:<br>
+  //static void output(const T &value, void *ctxt, llvm::raw_ostream &out);<br>
+  //<br>
+  // Function to convert a string to a value.  Returns the empty<br>
+  // StringRef on success or an error string if string is malformed:<br>
+  //static StringRef input(StringRef scalar, void *ctxt, T &value);<br>
+};<br>
+<br>
+<br>
+/// This class should be specialized by any type that needs to be converted<br>
+/// to/from a YAML sequence.  For example:<br>
+///<br>
+///    template<><br>
+///    struct SequenceTraits< std::vector<MyType> > {<br>
+///      static size_t size(IO &io, std::vector<MyType> &seq) {<br>
+///        return seq.size();<br>
+///      }<br>
+///      static MyType& element(IO &, std::vector<MyType> &seq, size_t index) {<br>
+///        if ( index >= seq.size() )<br>
+///          seq.resize(index+1);<br>
+///        return seq[index];<br>
+///      }<br>
+///    };<br>
+template<typename T><br>
+struct SequenceTraits {<br>
+  // Must provide:<br>
+  // static size_t size(IO &io, T &seq);<br>
+  // static T::value_type& element(IO &io, T &seq, size_t index);<br>
+  //<br>
+  // The following is option and will cause generated YAML to use<br>
+  // a flow sequence (e.g. [a,b,c]).<br>
+  // static const bool flow = true;<br>
+};<br>
+<br>
+<br>
+/// This class should be specialized by any type that needs to be converted<br>
+/// to/from a list of YAML documents.<br>
+template<typename T><br>
+struct DocumentListTraits {<br>
+  // Must provide:<br>
+  // static size_t size(IO &io, T &seq);<br>
+  // static T::value_type& element(IO &io, T &seq, size_t index);<br>
+};<br>
+<br>
+<br>
+// Only used by compiler if both template types are the same<br>
+template <typename T, T><br>
+struct SameType;<br>
+<br>
+// Only used for better diagnostics of missing traits<br>
+template <typename T><br>
+struct MissingTrait;<br>
+<br>
+<br>
+<br>
+// Test if ScalarEnumerationTraits<T> is defined on type T.<br>
+template <class T><br>
+struct has_ScalarEnumerationTraits<br>
+{<br>
+  typedef void (*Signature_enumeration)(class IO&, T&);<br>
+<br>
+  template <typename U><br>
+  static char test(SameType<Signature_enumeration, &U::enumeration>*);<br>
+<br>
+  template <typename U><br>
+  static double test(...);<br>
+<br>
+public:<br>
+  static bool const value = (sizeof(test<ScalarEnumerationTraits<T> >(0)) == 1);<br>
+};<br>
+<br>
+<br>
+// Test if ScalarBitSetTraits<T> is defined on type T.<br>
+template <class T><br>
+struct has_ScalarBitSetTraits<br>
+{<br>
+  typedef void (*Signature_bitset)(class IO&, T&);<br>
+<br>
+  template <typename U><br>
+  static char test(SameType<Signature_bitset, &U::bitset>*);<br>
+<br>
+  template <typename U><br>
+  static double test(...);<br>
+<br>
+public:<br>
+  static bool const value = (sizeof(test<ScalarBitSetTraits<T> >(0)) == 1);<br>
+};<br>
+<br>
+<br>
+// Test if ScalarTraits<T> is defined on type T.<br>
+template <class T><br>
+struct has_ScalarTraits<br>
+{<br>
+  typedef llvm::StringRef (*Signature_input)(llvm::StringRef, void*, T&);<br>
+  typedef void (*Signature_output)(const T&, void*, llvm::raw_ostream&);<br>
+<br>
+  template <typename U><br>
+  static char test(SameType<Signature_input, &U::input>*,<br>
+                   SameType<Signature_output, &U::output>*);<br>
+<br>
+  template <typename U><br>
+  static double test(...);<br>
+<br>
+public:<br>
+  static bool const value = (sizeof(test<ScalarTraits<T> >(0,0)) == 1);<br>
+};<br>
+<br>
+<br>
+// Test if MappingTraits<T> is defined on type T.<br>
+template <class T><br>
+struct has_MappingTraits<br>
+{<br>
+  typedef void (*Signature_mapping)(class IO&, T&);<br>
+<br>
+  template <typename U><br>
+  static char test(SameType<Signature_mapping, &U::mapping>*);<br>
+<br>
+  template <typename U><br>
+  static double test(...);<br>
+<br>
+public:<br>
+  static bool const value = (sizeof(test<MappingTraits<T> >(0)) == 1);<br>
+};<br>
+<br>
+<br>
+// Test if SequenceTraits<T> is defined on type T<br>
+// and SequenceTraits<T>::flow is *not* defined.<br>
+template <class T><br>
+struct has_SequenceTraits<br>
+{<br>
+  typedef size_t (*Signature_size)(class IO&, T&);<br>
+<br>
+  template <typename U><br>
+  static char test(SameType<Signature_size, &U::size>*);<br>
+<br>
+  template <typename U><br>
+  static double test(...);<br>
+<br>
+  template <typename U> static<br>
+  char flowtest( char[sizeof(&U::flow)] ) ;<br>
+<br>
+  template <typename U><br>
+  static double flowtest(...);<br>
+<br>
+public:<br>
+  static bool const value =  (sizeof(test<SequenceTraits<T> >(0)) == 1)<br>
+                          && (sizeof(flowtest<T>(0)) != 1);<br>
+};<br>
+<br>
+<br>
+// Test if SequenceTraits<T> is defined on type T<br>
+// and SequenceTraits<T>::flow is defined.<br>
+template <class T><br>
+struct has_FlowSequenceTraits<br>
+{<br>
+  typedef size_t (*Signature_size)(class IO&, T&);<br>
+<br>
+  template <typename U><br>
+  static char test(SameType<Signature_size, &U::size>*);<br>
+<br>
+  template <typename U><br>
+  static double test(...);<br>
+<br>
+  template <typename U> static<br>
+  char flowtest( char[sizeof(&U::flow)] ) ;<br>
+<br>
+  template <typename U><br>
+  static double flowtest(...);<br>
+<br>
+public:<br>
+  static bool const value =  (sizeof(test<SequenceTraits<T> >(0)) == 1)<br>
+                          && (sizeof(flowtest<T>(0)) == 1);<br>
+};<br>
+<br>
+<br>
+// Test if DocumentListTraits<T> is defined on type T<br>
+template <class T><br>
+struct has_DocumentListTraits<br>
+{<br>
+  typedef size_t (*Signature_size)(class IO&, T&);<br>
+<br>
+  template <typename U><br>
+  static char test(SameType<Signature_size, &U::size>*);<br>
+<br>
+  template <typename U><br>
+  static double test(...);<br>
+<br>
+public:<br>
+  static bool const value =  (sizeof(test<DocumentListTraits<T> >(0)) == 1);<br>
+};<br>
+<br>
+<br>
+<br>
+<br>
+template<typename T><br>
+struct missingTraits : public  llvm::integral_constant<bool,<br>
+                                         !has_ScalarEnumerationTraits<T>::value<br>
+                                      && !has_ScalarBitSetTraits<T>::value<br>
+                                      && !has_ScalarTraits<T>::value<br>
+                                      && !has_MappingTraits<T>::value<br>
+                                      && !has_SequenceTraits<T>::value<br>
+                                      && !has_FlowSequenceTraits<T>::value<br>
+                                      && !has_DocumentListTraits<T>::value >  {};<br>
+<br>
+<br>
+// Base class for Input and Output.<br>
+class IO {<br>
+public:<br>
+<br>
+  IO(void *Ctxt=NULL);<br>
+  virtual ~IO();<br>
+<br>
+  virtual bool outputting() = 0;<br>
+<br>
+  virtual unsigned beginSequence() = 0;<br>
+  virtual bool preflightElement(unsigned, void *&) = 0;<br>
+  virtual void postflightElement(void*) = 0;<br>
+  virtual void endSequence() = 0;<br>
+<br>
+  virtual unsigned beginFlowSequence() = 0;<br>
+  virtual bool preflightFlowElement(unsigned, void *&) = 0;<br>
+  virtual void postflightFlowElement(void*) = 0;<br>
+  virtual void endFlowSequence() = 0;<br>
+<br>
+  virtual void beginMapping() = 0;<br>
+  virtual void endMapping() = 0;<br>
+  virtual bool preflightKey(const char*, bool, bool, bool &, void *&) = 0;<br>
+  virtual void postflightKey(void*) = 0;<br>
+<br>
+  virtual void beginEnumScalar() = 0;<br>
+  virtual bool matchEnumScalar(const char*, bool) = 0;<br>
+  virtual void endEnumScalar() = 0;<br>
+<br>
+  virtual bool beginBitSetScalar(bool &) = 0;<br>
+  virtual bool bitSetMatch(const char*, bool) = 0;<br>
+  virtual void endBitSetScalar() = 0;<br>
+<br>
+  virtual void scalarString(StringRef &) = 0;<br>
+<br>
+  virtual void setError(const Twine &) = 0;<br>
+<br>
+  template <typename T><br>
+  void enumCase(T &Val, const char* Str, const T ConstVal) {<br>
+    if ( matchEnumScalar(Str, (Val == ConstVal)) ) {<br>
+      Val = ConstVal;<br>
+    }<br>
+  }<br>
+<br>
+  // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF<br>
+  template <typename T><br>
+  void enumCase(T &Val, const char* Str, const uint32_t ConstVal) {<br>
+    if ( matchEnumScalar(Str, (Val == static_cast<T>(ConstVal))) ) {<br>
+      Val = ConstVal;<br>
+    }<br>
+  }<br>
+<br>
+  template <typename T><br>
+  void bitSetCase(T &Val, const char* Str, const T ConstVal) {<br>
+    if ( bitSetMatch(Str, ((Val & ConstVal) == ConstVal)) ) {<br>
+      Val = Val | ConstVal;<br>
+    }<br>
+  }<br>
+<br>
+  // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF<br>
+  template <typename T><br>
+  void bitSetCase(T &Val, const char* Str, const uint32_t ConstVal) {<br>
+    if ( bitSetMatch(Str, ((Val & ConstVal) == ConstVal)) ) {<br>
+      Val = Val | ConstVal;<br>
+    }<br>
+  }<br>
+<br>
+  void *getContext();<br>
+  void setContext(void *);<br>
+<br>
+  template <typename T><br>
+  void mapRequired(const char* Key, T& Val) {<br>
+    this->processKey(Key, Val, true);<br>
+  }<br>
+<br>
+  template <typename T><br>
+  typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type<br>
+  mapOptional(const char* Key, T& Val) {<br>
+    // omit key/value instead of outputting empty sequence<br>
+    if ( this->outputting() && !(Val.begin() != Val.end()) )<br>
+      return;<br>
+    this->processKey(Key, Val, false);<br>
+  }<br>
+<br>
+  template <typename T><br>
+  typename llvm::enable_if_c<!has_SequenceTraits<T>::value,void>::type<br>
+  mapOptional(const char* Key, T& Val) {<br>
+    this->processKey(Key, Val, false);<br>
+  }<br>
+<br>
+  template <typename T><br>
+  void mapOptional(const char* Key, T& Val, const T& Default) {<br>
+    this->processKeyWithDefault(Key, Val, Default, false);<br>
+  }<br>
+<br>
+<br>
+private:<br>
+  template <typename T><br>
+  void processKeyWithDefault(const char *Key, T &Val, const T& DefaultValue,<br>
+                                                                bool Required) {<br>
+    void *SaveInfo;<br>
+    bool UseDefault;<br>
+    const bool sameAsDefault = (Val == DefaultValue);<br>
+    if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault,<br>
+                                                                  SaveInfo) ) {<br>
+      yamlize(*this, Val, Required);<br>
+      this->postflightKey(SaveInfo);<br>
+    }<br>
+    else {<br>
+      if ( UseDefault )<br>
+        Val = DefaultValue;<br>
+    }<br>
+  }<br>
+<br>
+  template <typename T><br>
+  void processKey(const char *Key, T &Val, bool Required) {<br>
+    void *SaveInfo;<br>
+    bool UseDefault;<br>
+    if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {<br>
+      yamlize(*this, Val, Required);<br>
+      this->postflightKey(SaveInfo);<br>
+    }<br>
+  }<br>
+<br>
+private:<br>
+  void  *Ctxt;<br>
+};<br>
+<br>
+<br>
+<br>
+template<typename T><br>
+typename llvm::enable_if_c<has_ScalarEnumerationTraits<T>::value,void>::type<br>
+yamlize(IO &io, T &Val, bool) {<br>
+  io.beginEnumScalar();<br>
+  ScalarEnumerationTraits<T>::enumeration(io, Val);<br>
+  io.endEnumScalar();<br>
+}<br>
+<br>
+template<typename T><br>
+typename llvm::enable_if_c<has_ScalarBitSetTraits<T>::value,void>::type<br>
+yamlize(IO &io, T &Val, bool) {<br>
+  bool DoClear;<br>
+  if ( io.beginBitSetScalar(DoClear) ) {<br>
+    if ( DoClear )<br>
+      Val = static_cast<T>(0);<br>
+    ScalarBitSetTraits<T>::bitset(io, Val);<br>
+    io.endBitSetScalar();<br>
+  }<br>
+}<br>
+<br>
+<br>
+template<typename T><br>
+typename llvm::enable_if_c<has_ScalarTraits<T>::value,void>::type<br>
+yamlize(IO &io, T &Val, bool) {<br>
+  if ( io.outputting() ) {<br>
+    std::string Storage;<br>
+    llvm::raw_string_ostream Buffer(Storage);<br>
+    ScalarTraits<T>::output(Val, io.getContext(), Buffer);<br>
+    StringRef Str = Buffer.str();<br>
+    io.scalarString(Str);<br>
+  }<br>
+  else {<br>
+    StringRef Str;<br>
+    io.scalarString(Str);<br>
+    StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val);<br>
+    if ( !Result.empty() ) {<br>
+      io.setError(llvm::Twine(Result));<br>
+    }<br>
+  }<br>
+}<br>
+<br>
+<br>
+template<typename T><br>
+typename llvm::enable_if_c<has_MappingTraits<T>::value, void>::type<br>
+yamlize(IO &io, T &Val, bool) {<br>
+  io.beginMapping();<br>
+  MappingTraits<T>::mapping(io, Val);<br>
+  io.endMapping();<br>
+}<br>
+<br>
+#ifndef BUILDING_YAMLIO<br>
+template<typename T><br>
+typename llvm::enable_if_c<missingTraits<T>::value, void>::type<br>
+yamlize(IO &io, T &Val, bool) {<br>
+  char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];<br>
+}<br>
+#endif<br>
+<br>
+template<typename T><br>
+typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type<br>
+yamlize(IO &io, T &Seq, bool) {<br>
+  unsigned incount = io.beginSequence();<br>
+  unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incount;<br>
+  for(unsigned i=0; i < count; ++i) {<br>
+    void *SaveInfo;<br>
+    if ( io.preflightElement(i, SaveInfo) ) {<br>
+      yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);<br>
+      io.postflightElement(SaveInfo);<br>
+    }<br>
+  }<br>
+  io.endSequence();<br>
+}<br>
+<br>
+template<typename T><br>
+typename llvm::enable_if_c<has_FlowSequenceTraits<T>::value,void>::type<br>
+yamlize(IO &io, T &Seq, bool) {<br>
+  unsigned incount = io.beginFlowSequence();<br>
+  unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incount;<br>
+  for(unsigned i=0; i < count; ++i) {<br>
+    void *SaveInfo;<br>
+    if ( io.preflightFlowElement(i, SaveInfo) ) {<br>
+      yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);<br>
+      io.postflightFlowElement(SaveInfo);<br>
+    }<br>
+  }<br>
+  io.endFlowSequence();<br>
+}<br>
+<br>
+<br>
+<br>
+// Clients of YAML I/O only see declaration of the traits for built-in<br>
+// types.  The implementation is in the LLVM Support library.  Without<br>
+// this #ifdef, every client would get a copy of the implementation of<br>
+// these traits.<br>
+#ifndef BUILDING_YAMLIO<br>
+template<><br>
+struct ScalarTraits<bool> {<br>
+  static void output(const bool &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, bool &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<StringRef> {<br>
+  static void output(const StringRef &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, StringRef &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<uint8_t> {<br>
+  static void output(const uint8_t &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, uint8_t &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<uint16_t> {<br>
+  static void output(const uint16_t &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, uint16_t &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<uint32_t> {<br>
+  static void output(const uint32_t &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, uint32_t &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<uint64_t> {<br>
+  static void output(const uint64_t &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, uint64_t &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<int8_t> {<br>
+  static void output(const int8_t &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, int8_t &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<int16_t> {<br>
+  static void output(const int16_t &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, int16_t &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<int32_t> {<br>
+  static void output(const int32_t &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, int32_t &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<int64_t> {<br>
+  static void output(const int64_t &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, int64_t &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<float> {<br>
+  static void output(const float &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, float &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<double> {<br>
+  static void output(const double &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, double &);<br>
+};<br>
+#endif<br>
+<br>
+<br>
+<br>
+// Utility for use within MappingTraits<>::mapping() method<br>
+// to [de]normalize an object for use with YAML conversion.<br>
+template <typename TNorm, typename TFinal><br>
+struct MappingNormalization {<br>
+  MappingNormalization(IO &i_o, TFinal &Obj)<br>
+      : io(i_o), BufPtr(NULL), Result(Obj) {<br>
+    if ( io.outputting() ) {<br>
+      BufPtr = new (&Buffer) TNorm(io, Obj);<br>
+    }<br>
+    else {<br>
+      BufPtr = new (&Buffer) TNorm(io);<br>
+    }<br>
+  }<br>
+<br>
+  ~MappingNormalization() {<br>
+    if ( ! io.outputting() ) {<br>
+      Result = BufPtr->denormalize(io);<br>
+    }<br>
+    BufPtr->~TNorm();<br>
+  }<br>
+<br>
+  TNorm* operator->() { return BufPtr; }<br>
+<br>
+private:<br>
+  typedef typename llvm::AlignedCharArrayUnion<TNorm> Storage;<br>
+<br>
+  Storage       Buffer;<br>
+  IO           &io;<br>
+  TNorm        *BufPtr;<br>
+  TFinal       &Result;<br>
+};<br>
+<br>
+<br>
+<br>
+// Utility for use within MappingTraits<>::mapping() method<br>
+// to [de]normalize an object for use with YAML conversion.<br>
+template <typename TNorm, typename TFinal><br>
+struct MappingNormalizationHeap {<br>
+  MappingNormalizationHeap(IO &i_o, TFinal &Obj)<br>
+    : io(i_o), BufPtr(NULL), Result(Obj) {<br>
+    if ( io.outputting() ) {<br>
+      BufPtr = new (&Buffer) TNorm(io, Obj);<br>
+    }<br>
+    else {<br>
+      BufPtr = new TNorm(io);<br>
+    }<br>
+  }<br>
+<br>
+  ~MappingNormalizationHeap() {<br>
+    if ( io.outputting() ) {<br>
+      BufPtr->~TNorm();<br>
+    }<br>
+    else {<br>
+      Result = BufPtr->denormalize(io);<br>
+    }<br>
+  }<br>
+<br>
+  TNorm* operator->() { return BufPtr; }<br>
+<br>
+private:<br>
+  typedef typename llvm::AlignedCharArrayUnion<TNorm> Storage;<br>
+<br>
+  Storage       Buffer;<br>
+  IO           &io;<br>
+  TNorm        *BufPtr;<br>
+  TFinal       &Result;<br>
+};<br>
+<br>
+<br>
+<br>
+///<br>
+/// The Input class is used to parse a yaml document into in-memory structs<br>
+/// and vectors.<br>
+///<br>
+/// It works by using YAMLParser to do a syntax parse of the entire yaml<br>
+/// document, then the Input class builds a graph of HNodes which wraps<br>
+/// each yaml Node.  The extra layer is buffering.  The low level yaml<br>
+/// parser only lets you look at each node once.  The buffering layer lets<br>
+/// you search and interate multiple times.  This is necessary because<br>
+/// the mapRequired() method calls may not be in the same order<br>
+/// as the keys in the document.<br>
+///<br>
+class Input : public IO {<br>
+public:<br>
+  // Construct a yaml Input object from a StringRef and optional user-data.<br>
+  Input(StringRef InputContent, void *Ctxt=NULL);<br>
+<br>
+  // Check if there was an syntax or semantic error during parsing.<br>
+  llvm::error_code error();<br>
+<br>
+  // To set alternate error reporting.<br>
+  void setDiagHandler(llvm::SourceMgr::DiagHandlerTy Handler, void *Ctxt = 0);<br>
+<br>
+private:<br>
+  virtual bool outputting();<br>
+  virtual void beginMapping();<br>
+  virtual void endMapping();<br>
+  virtual bool preflightKey(const char *, bool, bool, bool &, void *&);<br>
+  virtual void postflightKey(void *);<br>
+  virtual unsigned beginSequence();<br>
+  virtual void endSequence();<br>
+  virtual bool preflightElement(unsigned index, void *&);<br>
+  virtual void postflightElement(void *);<br>
+  virtual unsigned beginFlowSequence();<br>
+  virtual bool preflightFlowElement(unsigned , void *&);<br>
+  virtual void postflightFlowElement(void *);<br>
+  virtual void endFlowSequence();<br>
+  virtual void beginEnumScalar();<br>
+  virtual bool matchEnumScalar(const char*, bool);<br>
+  virtual void endEnumScalar();<br>
+  virtual bool beginBitSetScalar(bool &);<br>
+  virtual bool bitSetMatch(const char *, bool );<br>
+  virtual void endBitSetScalar();<br>
+  virtual void scalarString(StringRef &);<br>
+  virtual void setError(const Twine &message);<br>
+<br>
+  class HNode {<br>
+  public:<br>
+    HNode(Node *n) : _node(n) { }<br>
+    static inline bool classof(const HNode *) { return true; }<br>
+<br>
+    Node *_node;<br>
+  };<br>
+<br>
+  class EmptyHNode : public HNode {<br>
+  public:<br>
+    EmptyHNode(Node *n) : HNode(n) { }<br>
+    static inline bool classof(const HNode *n) {<br>
+      return NullNode::classof(n->_node);<br>
+    }<br>
+    static inline bool classof(const EmptyHNode *) { return true; }<br>
+  };<br>
+<br>
+  class ScalarHNode : public HNode {<br>
+  public:<br>
+    ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }<br>
+<br>
+    StringRef value() const { return _value; }<br>
+<br>
+    static inline bool classof(const HNode *n) {<br>
+      return ScalarNode::classof(n->_node);<br>
+    }<br>
+    static inline bool classof(const ScalarHNode *) { return true; }<br>
+  protected:<br>
+    StringRef _value;<br>
+  };<br>
+<br>
+  class MapHNode : public HNode {<br>
+  public:<br>
+    MapHNode(Node *n) : HNode(n) { }<br>
+<br>
+    static inline bool classof(const HNode *n) {<br>
+      return MappingNode::classof(n->_node);<br>
+    }<br>
+    static inline bool classof(const MapHNode *) { return true; }<br>
+<br>
+    struct StrMappingInfo {<br>
+      static StringRef getEmptyKey() { return StringRef(); }<br>
+      static StringRef getTombstoneKey() { return StringRef(" ", 0); }<br>
+      static unsigned getHashValue(StringRef const val) {<br>
+                                                return llvm::HashString(val); }<br>
+      static bool isEqual(StringRef const lhs,<br>
+                          StringRef const rhs) { return lhs.equals(rhs); }<br>
+    };<br>
+    typedef llvm::DenseMap<StringRef, HNode*, StrMappingInfo> NameToNode;<br>
+<br>
+    bool isValidKey(StringRef key);<br>
+<br>
+    NameToNode                        Mapping;<br>
+    llvm::SmallVector<const char*, 6> ValidKeys;<br>
+  };<br>
+<br>
+  class SequenceHNode : public HNode {<br>
+  public:<br>
+    SequenceHNode(Node *n) : HNode(n) { }<br>
+<br>
+    static inline bool classof(const HNode *n) {<br>
+      return SequenceNode::classof(n->_node);<br>
+    }<br>
+    static inline bool classof(const SequenceHNode *) { return true; }<br>
+<br>
+    std::vector<HNode*> Entries;<br>
+  };<br>
+<br>
+  Input::HNode *createHNodes(Node *node);<br>
+  void setError(HNode *hnode, const Twine &message);<br>
+  void setError(Node *node, const Twine &message);<br>
+<br>
+<br>
+public:<br>
+  // These are only used by operator>>. They could be private<br>
+  // if those templated things could be made friends.<br>
+  bool setCurrentDocument();<br>
+  void nextDocument();<br>
+<br>
+private:<br>
+  llvm::yaml::Stream              *Strm;<br>
+  llvm::SourceMgr                  SrcMgr;<br>
+  llvm::error_code                 EC;<br>
+  llvm::BumpPtrAllocator           Allocator;<br>
+  llvm::yaml::document_iterator    DocIterator;<br>
+  std::vector<bool>                BitValuesUsed;<br>
+  HNode                           *CurrentNode;<br>
+  bool                             ScalarMatchFound;<br>
+};<br>
+<br>
+<br>
+<br>
+<br>
+///<br>
+/// The Output class is used to generate a yaml document from in-memory structs<br>
+/// and vectors.<br>
+///<br>
+class Output : public IO {<br>
+public:<br>
+  Output(llvm::raw_ostream &, void *Ctxt=NULL);<br>
+  virtual ~Output();<br>
+<br>
+  virtual bool outputting();<br>
+  virtual void beginMapping();<br>
+  virtual void endMapping();<br>
+  virtual bool preflightKey(const char *key, bool, bool, bool &, void *&);<br>
+  virtual void postflightKey(void *);<br>
+  virtual unsigned beginSequence();<br>
+  virtual void endSequence();<br>
+  virtual bool preflightElement(unsigned, void *&);<br>
+  virtual void postflightElement(void *);<br>
+  virtual unsigned beginFlowSequence();<br>
+  virtual bool preflightFlowElement(unsigned, void *&);<br>
+  virtual void postflightFlowElement(void *);<br>
+  virtual void endFlowSequence();<br>
+  virtual void beginEnumScalar();<br>
+  virtual bool matchEnumScalar(const char*, bool);<br>
+  virtual void endEnumScalar();<br>
+  virtual bool beginBitSetScalar(bool &);<br>
+  virtual bool bitSetMatch(const char *, bool );<br>
+  virtual void endBitSetScalar();<br>
+  virtual void scalarString(StringRef &);<br>
+  virtual void setError(const Twine &message);<br>
+<br>
+public:<br>
+  // These are only used by operator<<. They could be private<br>
+  // if that templated operator could be made a friend.<br>
+  void beginDocuments();<br>
+  bool preflightDocument(unsigned);<br>
+  void postflightDocument();<br>
+  void endDocuments();<br>
+<br>
+private:<br>
+  void output(StringRef s);<br>
+  void outputUpToEndOfLine(StringRef s);<br>
+  void newLineCheck();<br>
+  void outputNewLine();<br>
+  void paddedKey(StringRef key);<br>
+<br>
+  enum InState { inSeq, inFlowSeq, inMapFirstKey, inMapOtherKey };<br>
+<br>
+  llvm::raw_ostream       &Out;<br>
+  SmallVector<InState, 8>  StateStack;<br>
+  int                      Column;<br>
+  int                      ColumnAtFlowStart;<br>
+  bool                     NeedBitValueComma;<br>
+  bool                     NeedFlowSequenceComma;<br>
+  bool                     EnumerationMatchFound;<br>
+  bool                     NeedsNewLine;<br>
+};<br>
+<br>
+<br>
+<br>
+<br>
+/// YAML I/O does conversion based on types. But often native data types<br>
+/// are just a typedef of built in intergral types (e.g. int).  But the C++<br>
+/// type matching system sees through the typedef and all the typedefed types<br>
+/// look like a built in type. This will cause the generic YAML I/O conversion<br>
+/// to be used. To provide better control over the YAML conversion, you can<br>
+/// use this macro instead of typedef.  It will create a class with one field<br>
+/// and automatic conversion operators to and from the base type.<br>
+/// Based on BOOST_STRONG_TYPEDEF<br>
+#define LLVM_YAML_STRONG_TYPEDEF(_base, _type)                                 \<br>
+    struct _type {                                                             \<br>
+        _type() { }                                                            \<br>
+        _type(const _base v) : value(v) { }                                    \<br>
+        _type(const _type &v) : value(v.value) {}                              \<br>
+        _type &operator=(const _type &rhs) { value = rhs.value; return *this; }\<br>
+        _type &operator=(const _base &rhs) { value = rhs; return *this; }      \<br>
+        operator const _base & () const { return value; }                      \<br>
+        bool operator==(const _type &rhs) const { return value == rhs.value; } \<br>
+        bool operator==(const _base &rhs) const { return value == rhs; }       \<br>
+        bool operator<(const _type &rhs) const { return value < rhs.value; }   \<br>
+        _base value;                                                           \<br>
+    };<br>
+<br>
+<br>
+<br>
+///<br>
+/// Use these types instead of uintXX_t in any mapping to have<br>
+/// its yaml output formatted as hexadecimal.<br>
+///<br>
+LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)<br>
+LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)<br>
+LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)<br>
+LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)<br>
+<br>
+<br>
+// Clients of YAML I/O only see declaration of the traits for Hex*<br>
+// types.  The implementation is in the LLVM Support library.  Without<br>
+// this #ifdef, every client would get a copy of the implementation of<br>
+// these traits.<br>
+#ifndef BUILDING_YAMLIO<br>
+template<><br>
+struct ScalarTraits<Hex8> {<br>
+  static void output(const Hex8 &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, Hex8 &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<Hex16> {<br>
+  static void output(const Hex16 &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, Hex16 &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<Hex32> {<br>
+  static void output(const Hex32 &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, Hex32 &);<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<Hex64> {<br>
+  static void output(const Hex64 &, void*, llvm::raw_ostream &);<br>
+  static llvm::StringRef input(llvm::StringRef , void*, Hex64 &);<br>
+};<br>
+#endif<br>
+<br>
+<br>
+// Define non-member operator>> so that Input can stream in a document list.<br>
+template <typename T><br>
+inline<br>
+typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Input &>::type<br>
+operator>>(Input &yin, T &docList) {<br>
+  int i = 0;<br>
+  while ( yin.setCurrentDocument() ) {<br>
+    yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true);<br>
+    if ( yin.error() )<br>
+      return yin;<br>
+    yin.nextDocument();<br>
+    ++i;<br>
+  }<br>
+  return yin;<br>
+}<br>
+<br>
+// Define non-member operator>> so that Input can stream in a map as a document.<br>
+template <typename T><br>
+inline<br>
+typename llvm::enable_if_c<has_MappingTraits<T>::value,Input &>::type<br>
+operator>>(Input &yin, T &docMap) {<br>
+  yin.setCurrentDocument();<br>
+  yamlize(yin, docMap, true);<br>
+  return yin;<br>
+}<br>
+<br>
+// Define non-member operator>> so that Input can stream in a sequence as<br>
+// a document.<br>
+template <typename T><br>
+inline<br>
+typename llvm::enable_if_c<has_SequenceTraits<T>::value,Input &>::type<br>
+operator>>(Input &yin, T &docSeq) {<br>
+  yin.setCurrentDocument();<br>
+  yamlize(yin, docSeq, true);<br>
+  return yin;<br>
+}<br>
+<br>
+#ifndef BUILDING_YAMLIO<br>
+// Provide better error message about types missing a trait specialization<br>
+template <typename T><br>
+inline<br>
+typename llvm::enable_if_c<missingTraits<T>::value,Input &>::type<br>
+operator>>(Input &yin, T &docSeq) {<br>
+  char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];<br>
+  return yin;<br>
+}<br>
+#endif<br>
+<br>
+<br>
+// Define non-member operator<< so that Output can stream out document list.<br>
+template <typename T><br>
+inline<br>
+typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Output &>::type<br>
+operator<<(Output &yout, T &docList) {<br>
+  yout.beginDocuments();<br>
+  const size_t count = DocumentListTraits<T>::size(yout, docList);<br>
+  for(size_t i=0; i < count; ++i) {<br>
+    if ( yout.preflightDocument(i) ) {<br>
+      yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true);<br>
+      yout.postflightDocument();<br>
+    }<br>
+  }<br>
+  yout.endDocuments();<br>
+  return yout;<br>
+}<br>
+<br>
+// Define non-member operator<< so that Output can stream out a map.<br>
+template <typename T><br>
+inline<br>
+typename llvm::enable_if_c<has_MappingTraits<T>::value,Output &>::type<br>
+operator<<(Output &yout, T &map) {<br>
+  yout.beginDocuments();<br>
+  if ( yout.preflightDocument(0) ) {<br>
+    yamlize(yout, map, true);<br>
+    yout.postflightDocument();<br>
+  }<br>
+  yout.endDocuments();<br>
+  return yout;<br>
+}<br>
+<br>
+// Define non-member operator<< so that Output can stream out a sequence.<br>
+template <typename T><br>
+inline<br>
+typename llvm::enable_if_c<has_SequenceTraits<T>::value,Output &>::type<br>
+operator<<(Output &yout, T &seq) {<br>
+  yout.beginDocuments();<br>
+  if ( yout.preflightDocument(0) ) {<br>
+    yamlize(yout, seq, true);<br>
+    yout.postflightDocument();<br>
+  }<br>
+  yout.endDocuments();<br>
+  return yout;<br>
+}<br>
+<br>
+#ifndef BUILDING_YAMLIO<br>
+// Provide better error message about types missing a trait specialization<br>
+template <typename T><br>
+inline<br>
+typename llvm::enable_if_c<missingTraits<T>::value,Output &>::type<br>
+operator<<(Output &yout, T &seq) {<br>
+  char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];<br>
+  return yout;<br>
+}<br>
+#endif<br>
+<br>
+<br>
+} // namespace yaml<br>
+} // namespace llvm<br>
+<br>
+<br>
+/// Utility for declaring that a std::vector of a particular type<br>
+/// should be considered a YAML sequence.<br>
+#define LLVM_YAML_IS_SEQUENCE_VECTOR(_type)                                 \<br>
+  namespace llvm {                                                          \<br>
+  namespace yaml {                                                          \<br>
+    template<>                                                              \<br>
+    struct SequenceTraits< std::vector<_type> > {                           \<br>
+      static size_t size(IO &io, std::vector<_type> &seq) {                 \<br>
+        return seq.size();                                                  \<br>
+      }                                                                     \<br>
+      static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\<br>
+        if ( index >= seq.size() )                                          \<br>
+          seq.resize(index+1);                                              \<br>
+        return seq[index];                                                  \<br>
+      }                                                                     \<br>
+    };                                                                      \<br>
+  }                                                                         \<br>
+  }<br>
+<br>
+/// Utility for declaring that a std::vector of a particular type<br>
+/// should be considered a YAML flow sequence.<br>
+#define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(_type)                            \<br>
+  namespace llvm {                                                          \<br>
+  namespace yaml {                                                          \<br>
+    template<>                                                              \<br>
+    struct SequenceTraits< std::vector<_type> > {                           \<br>
+      static size_t size(IO &io, std::vector<_type> &seq) {                 \<br>
+        return seq.size();                                                  \<br>
+      }                                                                     \<br>
+      static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\<br>
+        if ( index >= seq.size() )                                          \<br>
+          seq.resize(index+1);                                              \<br>
+        return seq[index];                                                  \<br>
+      }                                                                     \<br>
+      static const bool flow = true;                                        \<br>
+    };                                                                      \<br>
+  }                                                                         \<br>
+  }<br>
+<br>
+/// Utility for declaring that a std::vector of a particular type<br>
+/// should be considered a YAML document list.<br>
+#define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type)                            \<br>
+  namespace llvm {                                                          \<br>
+  namespace yaml {                                                          \<br>
+    template<>                                                              \<br>
+    struct DocumentListTraits< std::vector<_type> > {                       \<br>
+      static size_t size(IO &io, std::vector<_type> &seq) {                 \<br>
+        return seq.size();                                                  \<br>
+      }                                                                     \<br>
+      static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\<br>
+        if ( index >= seq.size() )                                          \<br>
+          seq.resize(index+1);                                              \<br>
+        return seq[index];                                                  \<br>
+      }                                                                     \<br>
+    };                                                                      \<br>
+  }                                                                         \<br>
+  }<br>
+<br>
+<br>
+<br>
+#endif // LLVM_YAML_TRAITS_H_<br>
<br>
Modified: llvm/trunk/lib/Support/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CMakeLists.txt?rev=170019&r1=170018&r2=170019&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CMakeLists.txt?rev=170019&r1=170018&r2=170019&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/lib/Support/CMakeLists.txt (original)<br>
+++ llvm/trunk/lib/Support/CMakeLists.txt Wed Dec 12 14:46:15 2012<br>
@@ -50,6 +50,7 @@<br>
   Triple.cpp<br>
   Twine.cpp<br>
   YAMLParser.cpp<br>
+  YAMLTraits.cpp<br>
   raw_os_ostream.cpp<br>
   raw_ostream.cpp<br>
   regcomp.c<br>
<br>
Added: llvm/trunk/lib/Support/YAMLTraits.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/YAMLTraits.cpp?rev=170019&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/YAMLTraits.cpp?rev=170019&view=auto</a><br>

==============================================================================<br>
--- llvm/trunk/lib/Support/YAMLTraits.cpp (added)<br>
+++ llvm/trunk/lib/Support/YAMLTraits.cpp Wed Dec 12 14:46:15 2012<br>
@@ -0,0 +1,881 @@<br>
+//===- lib/Support/YAMLTraits.cpp -----------------------------------------===//<br>
+//<br>
+//                             The LLVM Linker<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#define BUILDING_YAMLIO<br>
+#include "llvm/Support/YAMLTraits.h"<br>
+<br>
+#include "llvm/ADT/Twine.h"<br>
+#include "llvm/Support/Casting.h"<br>
+#include "llvm/Support/ErrorHandling.h"<br>
+#include "llvm/Support/format.h"<br>
+#include "llvm/Support/raw_ostream.h"<br>
+#include "llvm/Support/YAMLParser.h"<br>
+<br>
+#include <cstring><br>
+<br>
+namespace llvm {<br>
+namespace yaml {<br>
+<br>
+<br>
+<br>
+//===----------------------------------------------------------------------===//<br>
+//  IO<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+IO::IO(void *Context) : Ctxt(Context) {<br>
+}<br>
+<br>
+IO::~IO() {<br>
+}<br>
+<br>
+void *IO::getContext() {<br>
+  return Ctxt;<br>
+}<br>
+<br>
+void IO::setContext(void *Context) {<br>
+  Ctxt = Context;<br>
+}<br>
+<br>
+<br>
+//===----------------------------------------------------------------------===//<br>
+//  Input<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+Input::Input(StringRef InputContent, void *Ctxt)<br>
+    : IO(Ctxt), CurrentNode(NULL) {<br>
+  Strm = new Stream(InputContent, SrcMgr);<br>
+  DocIterator = Strm->begin();<br>
+}<br>
+<br>
+<br>
+llvm::error_code Input::error() {<br>
+  return EC;<br>
+}<br>
+<br>
+void Input::setDiagHandler(llvm::SourceMgr::DiagHandlerTy Handler, void *Ctxt) {<br>
+  SrcMgr.setDiagHandler(Handler, Ctxt);<br>
+}<br>
+<br>
+bool Input::outputting() {<br>
+  return false;<br>
+}<br>
+<br>
+bool Input::setCurrentDocument() {<br>
+  if ( DocIterator != Strm->end() ) {<br>
+    Node *N = DocIterator->getRoot();<br>
+    if (llvm::isa<NullNode>(N)) {<br>
+      // Empty files are allowed and ignored<br>
+      ++DocIterator;<br>
+      return setCurrentDocument();<br>
+    }<br>
+    CurrentNode = this->createHNodes(N);<br>
+    return true;<br>
+  }<br>
+  return false;<br>
+}<br>
+<br>
+void Input::nextDocument() {<br>
+  ++DocIterator;<br>
+}<br>
+<br>
+void Input::beginMapping() {<br>
+  if ( EC )<br>
+    return;<br>
+  MapHNode *MN = llvm::dyn_cast<MapHNode>(CurrentNode);<br>
+  if ( MN ) {<br>
+    MN->ValidKeys.clear();<br>
+  }<br>
+}<br>
+<br>
+bool Input::preflightKey(const char *Key, bool Required, bool,<br>
+                                          bool &UseDefault, void *&SaveInfo) {<br>
+  UseDefault = false;<br>
+  if ( EC )<br>
+    return false;<br>
+  MapHNode *MN = llvm::dyn_cast<MapHNode>(CurrentNode);<br>
+  if ( !MN ) {<br>
+    setError(CurrentNode, "not a mapping");<br>
+    return false;<br>
+  }<br>
+  MN->ValidKeys.push_back(Key);<br>
+  HNode *Value = MN->Mapping[Key];<br>
+  if ( !Value ) {<br>
+    if ( Required )<br>
+      setError(CurrentNode, Twine("missing required key '") + Key + "'");<br>
+    else<br>
+      UseDefault = true;<br>
+   return false;<br>
+  }<br>
+  SaveInfo = CurrentNode;<br>
+  CurrentNode = Value;<br>
+  return true;<br>
+}<br>
+<br>
+void Input::postflightKey(void *saveInfo) {<br>
+  CurrentNode = reinterpret_cast<HNode*>(saveInfo);<br>
+}<br>
+<br>
+void Input::endMapping() {<br>
+  if ( EC )<br>
+    return;<br>
+  MapHNode *MN = llvm::dyn_cast<MapHNode>(CurrentNode);<br>
+  if ( !MN )<br>
+    return;<br>
+  for (MapHNode::NameToNode::iterator i=MN->Mapping.begin(),<br>
+                                        End=MN->Mapping.end(); i != End; ++i) {<br>
+    if ( ! MN->isValidKey(i->first) ) {<br>
+       setError(i->second, Twine("unknown key '") + i->first + "'" );<br>
+      break;<br>
+    }<br>
+  }<br>
+}<br>
+<br>
+<br>
+unsigned Input::beginSequence() {<br>
+  if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {<br>
+    return SQ->Entries.size();<br>
+  }<br>
+  return 0;<br>
+}<br>
+void Input::endSequence() {<br>
+}<br>
+bool Input::preflightElement(unsigned Index, void *&SaveInfo) {<br>
+  if ( EC )<br>
+    return false;<br>
+  if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {<br>
+    SaveInfo = CurrentNode;<br>
+    CurrentNode = SQ->Entries[Index];<br>
+    return true;<br>
+  }<br>
+  return false;<br>
+}<br>
+void Input::postflightElement(void *SaveInfo) {<br>
+  CurrentNode = reinterpret_cast<HNode*>(SaveInfo);<br>
+}<br>
+<br>
+unsigned Input::beginFlowSequence() {<br>
+   if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {<br>
+    return SQ->Entries.size();<br>
+  }<br>
+  return 0;<br>
+}<br>
+bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {<br>
+  if ( EC )<br>
+    return false;<br>
+  if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {<br>
+    SaveInfo = CurrentNode;<br>
+    CurrentNode = SQ->Entries[index];<br>
+    return true;<br>
+  }<br>
+  return false;<br>
+}<br>
+void Input::postflightFlowElement(void *SaveInfo) {<br>
+  CurrentNode = reinterpret_cast<HNode*>(SaveInfo);<br>
+}<br>
+void Input::endFlowSequence() {<br>
+}<br>
+<br>
+<br>
+void Input::beginEnumScalar() {<br>
+  ScalarMatchFound = false;<br>
+}<br>
+<br>
+bool Input::matchEnumScalar(const char *Str, bool) {<br>
+  if ( ScalarMatchFound )<br>
+    return false;<br>
+  if ( ScalarHNode *SN = llvm::dyn_cast<ScalarHNode>(CurrentNode) ) {<br>
+    if ( SN->value().equals(Str) ) {<br>
+      ScalarMatchFound = true;<br>
+      return true;<br>
+    }<br>
+  }<br>
+  return false;<br>
+}<br>
+<br>
+void Input::endEnumScalar() {<br>
+  if ( !ScalarMatchFound ) {<br>
+    setError(CurrentNode, "unknown enumerated scalar");<br>
+  }<br>
+}<br>
+<br>
+<br>
+<br>
+bool Input::beginBitSetScalar(bool &DoClear) {<br>
+  BitValuesUsed.clear();<br>
+  if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {<br>
+    BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);<br>
+  }<br>
+  else {<br>
+    setError(CurrentNode, "expected sequence of bit values");<br>
+  }<br>
+  DoClear = true;<br>
+  return true;<br>
+}<br>
+<br>
+bool Input::bitSetMatch(const char *Str, bool) {<br>
+  if ( EC )<br>
+    return false;<br>
+  if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {<br>
+    unsigned Index = 0;<br>
+    for (std::vector<HNode*>::iterator i=SQ->Entries.begin(),<br>
+                                       End=SQ->Entries.end(); i != End; ++i) {<br>
+      if ( ScalarHNode *SN = llvm::dyn_cast<ScalarHNode>(*i) ) {<br>
+        if ( SN->value().equals(Str) ) {<br>
+          BitValuesUsed[Index] = true;<br>
+          return true;<br>
+        }<br>
+      }<br>
+      else {<br>
+        setError(CurrentNode, "unexpected scalar in sequence of bit values");<br>
+      }<br>
+      ++Index;<br>
+    }<br>
+  }<br>
+  else {<br>
+    setError(CurrentNode, "expected sequence of bit values");<br>
+  }<br>
+  return false;<br>
+}<br>
+<br>
+void Input::endBitSetScalar() {<br>
+  if ( EC )<br>
+    return;<br>
+  if ( SequenceHNode *SQ = llvm::dyn_cast<SequenceHNode>(CurrentNode) ) {<br>
+    assert(BitValuesUsed.size() == SQ->Entries.size());<br>
+    for ( unsigned i=0; i < SQ->Entries.size(); ++i ) {<br>
+      if ( !BitValuesUsed[i] ) {<br>
+        setError(SQ->Entries[i], "unknown bit value");<br>
+        return;<br>
+      }<br>
+    }<br>
+  }<br>
+}<br>
+<br>
+<br>
+void Input::scalarString(StringRef &S) {<br>
+  if ( ScalarHNode *SN = llvm::dyn_cast<ScalarHNode>(CurrentNode) ) {<br>
+    S = SN->value();<br>
+  }<br>
+  else {<br>
+    setError(CurrentNode, "unexpected scalar");<br>
+  }<br>
+}<br>
+<br>
+void Input::setError(HNode *hnode, const Twine &message) {<br>
+  this->setError(hnode->_node, message);<br>
+}<br>
+<br>
+void Input::setError(Node *node, const Twine &message) {<br>
+  Strm->printError(node, message);<br>
+  EC = make_error_code(errc::invalid_argument);<br>
+}<br>
+<br>
+Input::HNode *Input::createHNodes(Node *N) {<br>
+  llvm::SmallString<128> StringStorage;<br>
+  if ( ScalarNode *SN = llvm::dyn_cast<ScalarNode>(N) ) {<br>
+    StringRef KeyStr = SN->getValue(StringStorage);<br>
+    if ( !StringStorage.empty() ) {<br>
+      // Copy string to permanent storage<br>
+      unsigned Len = StringStorage.size();<br>
+      char* Buf = Allocator.Allocate<char>(Len);<br>
+      memcpy(Buf, &StringStorage[0], Len);<br>
+      KeyStr = StringRef(Buf, Len);<br>
+    }<br>
+    return new (Allocator) ScalarHNode(N, KeyStr);<br>
+  }<br>
+  else if ( SequenceNode *SQ = llvm::dyn_cast<SequenceNode>(N) ) {<br>
+    SequenceHNode *SQHNode = new (Allocator) SequenceHNode(N);<br>
+    for (SequenceNode::iterator i=SQ->begin(),End=SQ->end(); i != End; ++i ) {<br>
+      HNode *Entry = this->createHNodes(i);<br>
+      if ( EC )<br>
+        break;<br>
+      SQHNode->Entries.push_back(Entry);<br>
+    }<br>
+    return SQHNode;<br>
+  }<br>
+  else if ( MappingNode *Map = llvm::dyn_cast<MappingNode>(N) ) {<br>
+    MapHNode *mapHNode = new (Allocator) MapHNode(N);<br>
+    for (MappingNode::iterator i=Map->begin(), End=Map->end(); i != End; ++i ) {<br>
+      ScalarNode *KeyScalar = llvm::dyn_cast<ScalarNode>(i->getKey());<br>
+      StringStorage.clear();<br>
+      llvm::StringRef KeyStr = KeyScalar->getValue(StringStorage);<br>
+      if ( !StringStorage.empty() ) {<br>
+        // Copy string to permanent storage<br>
+        unsigned Len = StringStorage.size();<br>
+        char* Buf = Allocator.Allocate<char>(Len);<br>
+        memcpy(Buf, &StringStorage[0], Len);<br>
+        KeyStr = StringRef(Buf, Len);<br>
+      }<br>
+     HNode *ValueHNode = this->createHNodes(i->getValue());<br>
+      if ( EC )<br>
+        break;<br>
+      mapHNode->Mapping[KeyStr] = ValueHNode;<br>
+    }<br>
+    return mapHNode;<br>
+  }<br>
+  else if ( llvm::isa<NullNode>(N) ) {<br>
+    return new (Allocator) EmptyHNode(N);<br>
+  }<br>
+  else {<br>
+    setError(N, "unknown node kind");<br>
+    return NULL;<br>
+  }<br>
+}<br>
+<br>
+<br>
+bool Input::MapHNode::isValidKey(StringRef Key) {<br>
+  for (SmallVector<const char*, 6>::iterator i=ValidKeys.begin(),<br>
+                                  End=ValidKeys.end(); i != End; ++i) {<br>
+    if ( Key.equals(*i) )<br>
+      return true;<br>
+  }<br>
+  return false;<br>
+}<br>
+<br>
+void Input::setError(const Twine &Message) {<br>
+  this->setError(CurrentNode, Message);<br>
+}<br>
+<br>
+<br>
+//===----------------------------------------------------------------------===//<br>
+//  Output<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+Output::Output(llvm::raw_ostream &yout, void *context)<br>
+    : IO(context), Out(yout), Column(0), ColumnAtFlowStart(0),<br>
+       NeedBitValueComma(false), NeedFlowSequenceComma(false),<br>
+       EnumerationMatchFound(false), NeedsNewLine(false) {<br>
+}<br>
+<br>
+Output::~Output() {<br>
+}<br>
+<br>
+bool Output::outputting() {<br>
+  return true;<br>
+}<br>
+<br>
+void Output::beginMapping() {<br>
+  StateStack.push_back(inMapFirstKey);<br>
+  NeedsNewLine = true;<br>
+}<br>
+<br>
+void Output::endMapping() {<br>
+  StateStack.pop_back();<br>
+}<br>
+<br>
+<br>
+bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,<br>
+                                                bool &UseDefault, void *&) {<br>
+  UseDefault = false;<br>
+  if ( Required || !SameAsDefault ) {<br>
+    this->newLineCheck();<br>
+    this->paddedKey(Key);<br>
+    return true;<br>
+  }<br>
+  return false;<br>
+}<br>
+<br>
+void Output::postflightKey(void*) {<br>
+  if ( StateStack.back() == inMapFirstKey ) {<br>
+    StateStack.pop_back();<br>
+    StateStack.push_back(inMapOtherKey);<br>
+  }<br>
+}<br>
+<br>
+void Output::beginDocuments() {<br>
+  this->outputUpToEndOfLine("---");<br>
+}<br>
+<br>
+bool Output::preflightDocument(unsigned index) {<br>
+  if ( index > 0 )<br>
+    this->outputUpToEndOfLine("\n---");<br>
+  return true;<br>
+}<br>
+<br>
+void Output::postflightDocument() {<br>
+}<br>
+<br>
+void Output::endDocuments() {<br>
+  output("\n...\n");<br>
+}<br>
+<br>
+unsigned Output::beginSequence() {<br>
+  StateStack.push_back(inSeq);<br>
+  NeedsNewLine = true;<br>
+  return 0;<br>
+}<br>
+void Output::endSequence() {<br>
+  StateStack.pop_back();<br>
+}<br>
+bool Output::preflightElement(unsigned , void *&) {<br>
+  return true;<br>
+}<br>
+void Output::postflightElement(void*) {<br>
+}<br>
+<br>
+unsigned Output::beginFlowSequence() {<br>
+  this->newLineCheck();<br>
+  StateStack.push_back(inFlowSeq);<br>
+  ColumnAtFlowStart = Column;<br>
+  output("[ ");<br>
+  NeedFlowSequenceComma = false;<br>
+  return 0;<br>
+}<br>
+void Output::endFlowSequence() {<br>
+  StateStack.pop_back();<br>
+  this->outputUpToEndOfLine(" ]");<br>
+}<br>
+bool Output::preflightFlowElement(unsigned , void *&) {<br>
+  if ( NeedFlowSequenceComma )<br>
+    output(", ");<br>
+  if ( Column > 70 ) {<br>
+    output("\n");<br>
+    for(int  i=0; i < ColumnAtFlowStart; ++i)<br>
+      output(" ");<br>
+    Column = ColumnAtFlowStart;<br>
+    output("  ");<br>
+  }<br>
+  return true;<br>
+}<br>
+void Output::postflightFlowElement(void*) {<br>
+  NeedFlowSequenceComma = true;<br>
+}<br>
+<br>
+<br>
+<br>
+void Output::beginEnumScalar() {<br>
+  EnumerationMatchFound = false;<br>
+}<br>
+<br>
+bool Output::matchEnumScalar(const char *Str, bool Match) {<br>
+  if ( Match && !EnumerationMatchFound ) {<br>
+    this->newLineCheck();<br>
+    this->outputUpToEndOfLine(Str);<br>
+    EnumerationMatchFound = true;<br>
+  }<br>
+  return false;<br>
+}<br>
+<br>
+void Output::endEnumScalar() {<br>
+  if ( !EnumerationMatchFound )<br>
+    llvm_unreachable("bad runtime enum value");<br>
+}<br>
+<br>
+<br>
+<br>
+bool Output::beginBitSetScalar(bool &DoClear) {<br>
+  this->newLineCheck();<br>
+  output("[ ");<br>
+  NeedBitValueComma = false;<br>
+  DoClear = false;<br>
+  return true;<br>
+}<br>
+<br>
+bool Output::bitSetMatch(const char *Str, bool Matches) {<br>
+ if ( Matches ) {<br>
+    if ( NeedBitValueComma )<br>
+      output(", ");<br>
+    this->output(Str);<br>
+    NeedBitValueComma = true;<br>
+  }<br>
+  return false;<br>
+}<br>
+<br>
+void Output::endBitSetScalar() {<br>
+  this->outputUpToEndOfLine(" ]");<br>
+}<br>
+<br>
+void Output::scalarString(StringRef &S) {<br>
+  this->newLineCheck();<br>
+  if (S.find('\n') == StringRef::npos) {<br>
+    // No embedded new-line chars, just print string.<br>
+    this->outputUpToEndOfLine(S);<br>
+    return;<br>
+  }<br>
+  unsigned i = 0;<br>
+  unsigned j = 0;<br>
+  unsigned End = S.size();<br>
+  output("'"); // Starting single quote.<br>
+  const char *Base = S.data();<br>
+  while (j < End) {<br>
+    // Escape a single quote by doubling it.<br>
+    if (S[j] == '\'') {<br>
+      output(StringRef(&Base[i], j - i + 1));<br>
+      output("'");<br>
+      i = j + 1;<br>
+    }<br>
+    ++j;<br>
+  }<br>
+  output(StringRef(&Base[i], j - i));<br>
+  this->outputUpToEndOfLine("'"); // Ending single quote.<br>
+}<br>
+<br>
+void Output::setError(const Twine &message) {<br>
+}<br>
+<br>
+<br>
+void Output::output(StringRef s) {<br>
+  Column += s.size();<br>
+  Out << s;<br>
+}<br>
+<br>
+void Output::outputUpToEndOfLine(StringRef s) {<br>
+  this->output(s);<br>
+  if ( StateStack.back() != inFlowSeq )<br>
+    NeedsNewLine = true;<br>
+}<br>
+<br>
+void Output::outputNewLine() {<br>
+  Out << "\n";<br>
+  Column = 0;<br>
+}<br>
+<br>
+// if seq at top, indent as if map, then add "- "<br>
+// if seq in middle, use "- " if firstKey, else use "  "<br>
+//<br>
+<br>
+void Output::newLineCheck() {<br>
+  if ( ! NeedsNewLine )<br>
+    return;<br>
+  NeedsNewLine = false;<br>
+<br>
+  this->outputNewLine();<br>
+<br>
+  assert(StateStack.size() > 0);<br>
+  unsigned Indent = StateStack.size() - 1;<br>
+  bool OutputDash = false;<br>
+<br>
+  if ( StateStack.back() == inSeq ) {<br>
+    OutputDash = true;<br>
+  }<br>
+  else if ( (StateStack.size() > 1)<br>
+            && (StateStack.back() == inMapFirstKey)<br>
+            && (StateStack[StateStack.size()-2] == inSeq) ) {<br>
+    --Indent;<br>
+    OutputDash = true;<br>
+  }<br>
+<br>
+  for (unsigned i=0; i < Indent; ++i) {<br>
+    output("  ");<br>
+  }<br>
+  if ( OutputDash ) {<br>
+    output("- ");<br>
+  }<br>
+<br>
+}<br>
+<br>
+void Output::paddedKey(StringRef key) {<br>
+  output(key);<br>
+  output(":");<br>
+  const char *spaces = "                ";<br>
+  if ( key.size() < strlen(spaces) )<br>
+    output(&spaces[key.size()]);<br>
+  else<br>
+    output(" ");<br>
+}<br>
+<br>
+//===----------------------------------------------------------------------===//<br>
+//  traits for built-in types<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+template<><br>
+struct ScalarTraits<bool> {<br>
+  static void output(const bool &Val, void*, llvm::raw_ostream &Out) {<br>
+    Out << ( Val ? "true" : "false");<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, bool &Val) {<br>
+    if ( Scalar.equals("true") ) {<br>
+      Val = true;<br>
+      return StringRef();<br>
+    }<br>
+    else if ( Scalar.equals("false") ) {<br>
+      Val = false;<br>
+      return StringRef();<br>
+    }<br>
+    return "invalid boolean";<br>
+  }<br>
+};<br>
+<br>
+<br>
+template<><br>
+struct ScalarTraits<StringRef> {<br>
+  static void output(const StringRef &Val, void*, llvm::raw_ostream &Out) {<br>
+    Out << Val;<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, StringRef &Val){<br>
+    Val = Scalar;<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+<br>
+template<><br>
+struct ScalarTraits<uint8_t> {<br>
+  static void output(const uint8_t &Val, void*, llvm::raw_ostream &Out) {<br>
+    // use temp uin32_t because ostream thinks uint8_t is a character<br>
+    uint32_t Num = Val;<br>
+    Out << Num;<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, uint8_t &Val) {<br>
+    uint64_t n;<br>
+    if ( getAsUnsignedInteger(Scalar, 0, n) )<br>
+      return "invalid number";<br>
+    if ( n > 0xFF )<br>
+      return "out of range number";<br>
+    Val = n;<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+<br>
+template<><br>
+struct ScalarTraits<uint16_t> {<br>
+  static void output(const uint16_t &Val, void*, llvm::raw_ostream &Out) {<br>
+    Out << Val;<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, uint16_t &Val) {<br>
+    uint64_t n;<br>
+    if ( getAsUnsignedInteger(Scalar, 0, n) )<br>
+      return "invalid number";<br>
+    if ( n > 0xFFFF )<br>
+      return "out of range number";<br>
+    Val = n;<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<uint32_t> {<br>
+  static void output(const uint32_t &Val, void*, llvm::raw_ostream &Out) {<br>
+    Out << Val;<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, uint32_t &Val) {<br>
+    uint64_t n;<br>
+    if ( getAsUnsignedInteger(Scalar, 0, n) )<br>
+      return "invalid number";<br>
+    if ( n > 0xFFFFFFFFUL )<br>
+      return "out of range number";<br>
+    Val = n;<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+<br>
+template<><br>
+struct ScalarTraits<uint64_t> {<br>
+  static void output(const uint64_t &Val, void*, llvm::raw_ostream &Out) {<br>
+    Out << Val;<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, uint64_t &Val) {<br>
+    if ( getAsUnsignedInteger(Scalar, 0, Val) )<br>
+      return "invalid number";<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+<br>
+template<><br>
+struct ScalarTraits<int8_t> {<br>
+  static void output(const int8_t &Val, void*, llvm::raw_ostream &Out) {<br>
+    // use temp in32_t because ostream thinks int8_t is a character<br>
+    int32_t Num = Val;<br>
+    Out << Num;<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, int8_t &Val) {<br>
+    int64_t n;<br>
+    if ( getAsSignedInteger(Scalar, 0, n) )<br>
+      return "invalid number";<br>
+    if ( (n > 127) || (n < -128) )<br>
+      return "out of range number";<br>
+    Val = n;<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+<br>
+template<><br>
+struct ScalarTraits<int16_t> {<br>
+  static void output(const int16_t &Val, void*, llvm::raw_ostream &Out) {<br>
+    Out << Val;<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, int16_t &Val) {<br>
+    int64_t n;<br>
+    if ( getAsSignedInteger(Scalar, 0, n) )<br>
+      return "invalid number";<br>
+    if ( (n > INT16_MAX) || (n < INT16_MIN) )<br>
+      return "out of range number";<br>
+    Val = n;<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+<br>
+template<><br>
+struct ScalarTraits<int32_t> {<br>
+  static void output(const int32_t &Val, void*, llvm::raw_ostream &Out) {<br>
+    Out << Val;<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, int32_t &Val) {<br>
+    int64_t n;<br>
+    if ( getAsSignedInteger(Scalar, 0, n) )<br>
+      return "invalid number";<br>
+    if ( (n > INT32_MAX) || (n < INT32_MIN) )<br>
+      return "out of range number";<br>
+    Val = n;<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<int64_t> {<br>
+  static void output(const int64_t &Val, void*, llvm::raw_ostream &Out) {<br>
+    Out << Val;<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, int64_t &Val) {<br>
+    if ( getAsSignedInteger(Scalar, 0, Val) )<br>
+      return "invalid number";<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<double> {<br>
+  static void output(const double &Val, void*, llvm::raw_ostream &Out) {<br>
+  Out << format("%g", Val);<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, double &Val) {<br>
+    SmallString<32> buff(Scalar.begin(), Scalar.end());<br>
+    char *end;<br>
+    Val = strtod(buff.c_str(), &end);<br>
+    if ( *end != '\0' )<br>
+      return "invalid floating point number";<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<float> {<br>
+  static void output(const float &Val, void*, llvm::raw_ostream &Out) {<br>
+  Out << format("%g", Val);<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, float &Val) {<br>
+    SmallString<32> buff(Scalar.begin(), Scalar.end());<br>
+    char *end;<br>
+    Val = strtod(buff.c_str(), &end);<br>
+    if ( *end != '\0' )<br>
+      return "invalid floating point number";<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+<br>
+<br>
+template<><br>
+struct ScalarTraits<Hex8> {<br>
+  static void output(const Hex8 &Val, void*, llvm::raw_ostream &Out) {<br>
+    uint8_t Num = Val;<br>
+    Out << format("0x%02X", Num);<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, Hex8 &Val) {<br>
+    uint64_t n;<br>
+    if ( getAsUnsignedInteger(Scalar, 0, n) )<br>
+      return "invalid hex8 number";<br>
+    if ( n > 0xFF )<br>
+      return "out of range hex8 number";<br>
+    Val = n;<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+<br>
+template<><br>
+struct ScalarTraits<Hex16> {<br>
+  static void output(const Hex16 &Val, void*, llvm::raw_ostream &Out) {<br>
+    uint16_t Num = Val;<br>
+    Out << format("0x%04X", Num);<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, Hex16 &Val) {<br>
+    uint64_t n;<br>
+    if ( getAsUnsignedInteger(Scalar, 0, n) )<br>
+      return "invalid hex16 number";<br>
+    if ( n > 0xFFFF )<br>
+      return "out of range hex16 number";<br>
+    Val = n;<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+template<><br>
+struct ScalarTraits<Hex32> {<br>
+  static void output(const Hex32 &Val, void*, llvm::raw_ostream &Out) {<br>
+    uint32_t Num = Val;<br>
+    Out << format("0x%08X", Num);<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, Hex32 &Val) {<br>
+    uint64_t n;<br>
+    if ( getAsUnsignedInteger(Scalar, 0, n) )<br>
+      return "invalid hex32 number";<br>
+    if ( n > 0xFFFFFFFFUL )<br>
+      return "out of range hex32 number";<br>
+    Val = n;<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+<br>
+template<><br>
+struct ScalarTraits<Hex64> {<br>
+  static void output(const Hex64 &Val, void*, llvm::raw_ostream &Out) {<br>
+    uint64_t Num = Val;<br>
+    Out << format("0x%016llX", Num);<br>
+  }<br>
+  static llvm::StringRef input(llvm::StringRef Scalar, void*, Hex64 &Val) {<br>
+    uint64_t Num;<br>
+    if ( getAsUnsignedInteger(Scalar, 0, Num) )<br>
+      return "invalid hex64 number";<br>
+    Val = Num;<br>
+    return StringRef();<br>
+  }<br>
+};<br>
+<br>
+<br>
+<br>
+<br>
+// We want all the ScalarTrait specialized on built-in types<br>
+// to be instantiated here.<br>
+template <typename T><br>
+struct ForceUse {<br>
+  ForceUse() : oproc(ScalarTraits<T>::output), iproc(ScalarTraits<T>::input) {}<br>
+  void (*oproc)(const T &, void*, llvm::raw_ostream &);<br>
+  llvm::StringRef (*iproc)(llvm::StringRef, void*, T &);<br>
+};<br>
+<br>
+static ForceUse<bool>            Dummy1;<br>
+static ForceUse<llvm::StringRef> Dummy2;<br>
+static ForceUse<uint8_t>         Dummy3;<br>
+static ForceUse<uint16_t>        Dummy4;<br>
+static ForceUse<uint32_t>        Dummy5;<br>
+static ForceUse<uint64_t>        Dummy6;<br>
+static ForceUse<int8_t>          Dummy7;<br>
+static ForceUse<int16_t>         Dummy8;<br>
+static ForceUse<int32_t>         Dummy9;<br>
+static ForceUse<int64_t>         Dummy10;<br>
+static ForceUse<float>           Dummy11;<br>
+static ForceUse<double>          Dummy12;<br>
+static ForceUse<Hex8>            Dummy13;<br>
+static ForceUse<Hex16>           Dummy14;<br>
+static ForceUse<Hex32>           Dummy15;<br>
+static ForceUse<Hex64>           Dummy16;<br>
+<br>
+<br>
+<br>
+} // namespace yaml<br>
+} // namespace llvm<br>
+<br>
+<br>
<br>
Modified: llvm/trunk/unittests/Support/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CMakeLists.txt?rev=170019&r1=170018&r2=170019&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CMakeLists.txt?rev=170019&r1=170018&r2=170019&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/unittests/Support/CMakeLists.txt (original)<br>
+++ llvm/trunk/unittests/Support/CMakeLists.txt Wed Dec 12 14:46:15 2012<br>
@@ -24,6 +24,7 @@<br>
   SwapByteOrderTest.cpp<br>
   TimeValue.cpp<br>
   ValueHandleTest.cpp<br>
+  YAMLIOTest.cpp<br>
   YAMLParserTest.cpp<br>
   formatted_raw_ostream_test.cpp<br>
   raw_ostream_test.cpp<br>
<br>
Added: llvm/trunk/unittests/Support/YAMLIOTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/YAMLIOTest.cpp?rev=170019&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/YAMLIOTest.cpp?rev=170019&view=auto</a><br>

==============================================================================<br>
--- llvm/trunk/unittests/Support/YAMLIOTest.cpp (added)<br>
+++ llvm/trunk/unittests/Support/YAMLIOTest.cpp Wed Dec 12 14:46:15 2012<br>
@@ -0,0 +1,1288 @@<br>
+//===- unittest/Support/YAMLIOTest.cpp ------------------------------------===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/ADT/SmallString.h"<br>
+#include "llvm/ADT/Twine.h"<br>
+#include "llvm/Support/Casting.h"<br>
+#include "llvm/Support/Format.h"<br>
+#include "llvm/Support/YAMLTraits.h"<br>
+#include "gtest/gtest.h"<br>
+<br>
+<br>
+using llvm::yaml::Input;<br>
+using llvm::yaml::Output;<br>
+using llvm::yaml::IO;<br>
+using llvm::yaml::MappingTraits;<br>
+using llvm::yaml::MappingNormalization;<br>
+using llvm::yaml::ScalarTraits;<br>
+using llvm::yaml::Hex8;<br>
+using llvm::yaml::Hex16;<br>
+using llvm::yaml::Hex32;<br>
+using llvm::yaml::Hex64;<br>
+<br>
+<br>
+//===----------------------------------------------------------------------===//<br>
+//  Test MappingTraits<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+struct FooBar {<br>
+  int foo;<br>
+  int bar;<br>
+};<br>
+typedef std::vector<FooBar> FooBarSequence;<br>
+<br>
+LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)<br>
+<br>
+<br>
+namespace llvm {<br>
+namespace yaml {<br>
+  template <><br>
+  struct MappingTraits<FooBar> {<br>
+    static void mapping(IO &io, FooBar& fb) {<br>
+      io.mapRequired("foo",    fb.foo);<br>
+      io.mapRequired("bar",    fb.bar);<br>
+    }<br>
+  };<br>
+}<br>
+}<br>
+<br>
+<br>
+//<br>
+// Test the reading of a yaml mapping<br>
+//<br>
+TEST(YAMLIO, TestMapRead) {<br>
+  FooBar doc;<br>
+  Input yin("---\nfoo:  3\nbar:  5\n...\n");<br>
+  yin >> doc;<br>
+<br>
+  EXPECT_FALSE(yin.error());<br>
+  EXPECT_EQ(doc.foo, 3);<br>
+  EXPECT_EQ(doc.bar,5);<br>
+}<br>
+<br>
+<br>
+//<br>
+// Test the reading of a yaml sequence of mappings<br>
+//<br>
+TEST(YAMLIO, TestSequenceMapRead) {<br>
+  FooBarSequence seq;<br>
+  Input yin("---\n - foo:  3\n   bar:  5\n - foo:  7\n   bar:  9\n...\n");<br>
+  yin >> seq;<br>
+<br>
+  EXPECT_FALSE(yin.error());<br>
+  EXPECT_EQ(seq.size(), 2UL);<br>
+  FooBar& map1 = seq[0];<br>
+  FooBar& map2 = seq[1];<br>
+  EXPECT_EQ(map1.foo, 3);<br>
+  EXPECT_EQ(map1.bar, 5);<br>
+  EXPECT_EQ(map2.foo, 7);<br>
+  EXPECT_EQ(map2.bar, 9);<br>
+}<br>
+<br>
+<br>
+//<br>
+// Test writing then reading back a sequence of mappings<br>
+//<br>
+TEST(YAMLIO, TestSequenceMapWriteAndRead) {<br>
+  std::string intermediate;<br>
+  {<br>
+    FooBar entry1;<br>
+    entry1.foo = 10;<br>
+    entry1.bar = -3;<br>
+    FooBar entry2;<br>
+    entry2.foo = 257;<br>
+    entry2.bar = 0;<br>
+    FooBarSequence seq;<br>
+    seq.push_back(entry1);<br>
+    seq.push_back(entry2);<br>
+<br>
+    llvm::raw_string_ostream ostr(intermediate);<br>
+    Output yout(ostr);<br>
+    yout << seq;<br>
+  }<br>
+<br>
+  {<br>
+    Input yin(intermediate);<br>
+    FooBarSequence seq2;<br>
+    yin >> seq2;<br>
+<br>
+    EXPECT_FALSE(yin.error());<br>
+    EXPECT_EQ(seq2.size(), 2UL);<br>
+    FooBar& map1 = seq2[0];<br>
+    FooBar& map2 = seq2[1];<br>
+    EXPECT_EQ(map1.foo, 10);<br>
+    EXPECT_EQ(map1.bar, -3);<br>
+    EXPECT_EQ(map2.foo, 257);<br>
+    EXPECT_EQ(map2.bar, 0);<br>
+  }<br>
+}<br>
+<br>
+<br>
+//===----------------------------------------------------------------------===//<br>
+//  Test built-in types<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+struct BuiltInTypes {<br>
+  llvm::StringRef str;<br>
+  uint64_t        u64;<br>
+  uint32_t        u32;<br>
+  uint16_t        u16;<br>
+  uint8_t         u8;<br>
+  bool            b;<br>
+  int64_t         s64;<br>
+  int32_t         s32;<br>
+  int16_t         s16;<br>
+  int8_t          s8;<br>
+  float           f;<br>
+  double          d;<br>
+  Hex8            h8;<br>
+  Hex16           h16;<br>
+  Hex32           h32;<br>
+  Hex64           h64;<br>
+};<br>
+<br>
+namespace llvm {<br>
+namespace yaml {<br>
+  template <><br>
+  struct MappingTraits<BuiltInTypes> {<br>
+    static void mapping(IO &io, BuiltInTypes& bt) {<br>
+      io.mapRequired("str",      bt.str);<br>
+      io.mapRequired("u64",      bt.u64);<br>
+      io.mapRequired("u32",      bt.u32);<br>
+      io.mapRequired("u16",      bt.u16);<br>
+      io.mapRequired("u8",       bt.u8);<br>
+      io.mapRequired("b",        bt.b);<br>
+      io.mapRequired("s64",      bt.s64);<br>
+      io.mapRequired("s32",      bt.s32);<br>
+      io.mapRequired("s16",      bt.s16);<br>
+      io.mapRequired("s8",       bt.s8);<br>
+      io.mapRequired("f",        bt.f);<br>
+      io.mapRequired("d",        bt.d);<br>
+      io.mapRequired("h8",       bt.h8);<br>
+      io.mapRequired("h16",      bt.h16);<br>
+      io.mapRequired("h32",      bt.h32);<br>
+      io.mapRequired("h64",      bt.h64);<br>
+    }<br>
+  };<br>
+}<br>
+}<br>
+<br>
+<br>
+//<br>
+// Test the reading of all built-in scalar conversions<br>
+//<br>
+TEST(YAMLIO, TestReadBuiltInTypes) {<br>
+  BuiltInTypes map;<br>
+  Input yin("---\n"<br>
+            "str:      hello there\n"<br>
+            "u64:      5000000000\n"<br>
+            "u32:      4000000000\n"<br>
+            "u16:      65000\n"<br>
+            "u8:       255\n"<br>
+            "b:        false\n"<br>
+            "s64:      -5000000000\n"<br>
+            "s32:      -2000000000\n"<br>
+            "s16:      -32000\n"<br>
+            "s8:       -127\n"<br>
+            "f:        137.125\n"<br>
+            "d:        -2.8625\n"<br>
+            "h8:       0xFF\n"<br>
+            "h16:      0x8765\n"<br>
+            "h32:      0xFEDCBA98\n"<br>
+            "h64:      0xFEDCBA9876543210\n"<br>
+           "...\n");<br>
+  yin >> map;<br>
+<br>
+  EXPECT_FALSE(yin.error());<br>
+  EXPECT_TRUE(map.str.equals("hello there"));<br>
+  EXPECT_EQ(map.u64, 5000000000ULL);<br>
+  EXPECT_EQ(map.u32, 4000000000);<br>
+  EXPECT_EQ(map.u16, 65000);<br>
+  EXPECT_EQ(map.u8,  255);<br>
+  EXPECT_EQ(map.b,   false);<br>
+  EXPECT_EQ(map.s64, -5000000000LL);<br>
+  EXPECT_EQ(map.s32, -2000000000L);<br>
+  EXPECT_EQ(map.s16, -32000);<br>
+  EXPECT_EQ(map.s8,  -127);<br>
+  EXPECT_EQ(map.f,   137.125);<br>
+  EXPECT_EQ(map.d,   -2.8625);<br>
+  EXPECT_EQ(map.h8,  Hex8(255));<br>
+  EXPECT_EQ(map.h16, Hex16(0x8765));<br>
+  EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));<br>
+  EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));<br>
+}<br>
+<br>
+<br>
+//<br>
+// Test writing then reading back all built-in scalar types<br>
+//<br>
+TEST(YAMLIO, TestReadWriteBuiltInTypes) {<br>
+  std::string intermediate;<br>
+  {<br>
+    BuiltInTypes map;<br>
+    map.str = "one two";<br>
+    map.u64 = 6000000000;<br>
+    map.u32 = 3000000000;<br>
+    map.u16 = 50000;<br>
+    map.u8  = 254;<br>
+    map.b   = true;<br>
+    map.s64 = -6000000000;<br>
+    map.s32 = -2000000000;<br>
+    map.s16 = -32000;<br>
+    map.s8  = -128;<br>
+    map.f   = 3.25;<br>
+    map.d   = -2.8625;<br>
+    map.h8  = 254;<br>
+    map.h16 = 50000;<br>
+    map.h32 = 3000000000;<br>
+    map.h64 = 6000000000LL;<br>
+<br>
+    llvm::raw_string_ostream ostr(intermediate);<br>
+    Output yout(ostr);<br>
+    yout << map;<br>
+  }<br>
+<br>
+  {<br>
+    Input yin(intermediate);<br>
+    BuiltInTypes map;<br>
+    yin >> map;<br>
+<br>
+    EXPECT_FALSE(yin.error());<br>
+    EXPECT_TRUE(map.str.equals("one two"));<br>
+    EXPECT_EQ(map.u64,      6000000000ULL);<br>
+    EXPECT_EQ(map.u32,      3000000000UL);<br>
+    EXPECT_EQ(map.u16,      50000);<br>
+    EXPECT_EQ(map.u8,       254);<br>
+    EXPECT_EQ(map.b,        true);<br>
+    EXPECT_EQ(map.s64,      -6000000000LL);<br>
+    EXPECT_EQ(map.s32,      -2000000000L);<br>
+    EXPECT_EQ(map.s16,      -32000);<br>
+    EXPECT_EQ(map.s8,       -128);<br>
+    EXPECT_EQ(map.f,        3.25);<br>
+    EXPECT_EQ(map.d,        -2.8625);<br>
+    EXPECT_EQ(map.h8,       Hex8(254));<br>
+    EXPECT_EQ(map.h16,      Hex16(50000));<br>
+    EXPECT_EQ(map.h32,      Hex32(3000000000));<br>
+    EXPECT_EQ(map.h64,      Hex64(6000000000LL));<br>
+  }<br>
+}<br>
+<br>
+<br>
+<br>
+//===----------------------------------------------------------------------===//<br>
+//  Test ScalarEnumerationTraits<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+enum Colors {<br>
+    cRed,<br>
+    cBlue,<br>
+    cGreen,<br>
+    cYellow<br>
+};<br>
+<br>
+struct ColorMap {<br>
+  Colors      c1;<br>
+  Colors      c2;<br>
+  Colors      c3;<br>
+  Colors      c4;<br>
+  Colors      c5;<br>
+  Colors      c6;<br>
+};<br>
+<br>
+namespace llvm {<br>
+namespace yaml {<br>
+  template <><br>
+  struct ScalarEnumerationTraits<Colors> {<br>
+    static void enumeration(IO &io, Colors &value) {<br>
+      io.enumCase(value, "red",   cRed);<br>
+      io.enumCase(value, "blue",  cBlue);<br>
+      io.enumCase(value, "green", cGreen);<br>
+      io.enumCase(value, "yellow",cYellow);<br>
+    }<br>
+  };<br>
+  template <><br>
+  struct MappingTraits<ColorMap> {<br>
+    static void mapping(IO &io, ColorMap& c) {<br>
+      io.mapRequired("c1", c.c1);<br>
+      io.mapRequired("c2", c.c2);<br>
+      io.mapRequired("c3", c.c3);<br>
+      io.mapOptional("c4", c.c4, cBlue);   // supplies default<br>
+      io.mapOptional("c5", c.c5, cYellow); // supplies default<br>
+      io.mapOptional("c6", c.c6, cRed);    // supplies default<br>
+    }<br>
+  };<br>
+}<br>
+}<br>
+<br>
+<br>
+//<br>
+// Test reading enumerated scalars<br>
+//<br>
+TEST(YAMLIO, TestEnumRead) {<br>
+  ColorMap map;<br>
+  Input yin("---\n"<br>
+            "c1:  blue\n"<br>
+            "c2:  red\n"<br>
+            "c3:  green\n"<br>
+            "c5:  yellow\n"<br>
+            "...\n");<br>
+  yin >> map;<br>
+<br>
+  EXPECT_FALSE(yin.error());<br>
+  EXPECT_EQ(cBlue,  map.c1);<br>
+  EXPECT_EQ(cRed,   map.c2);<br>
+  EXPECT_EQ(cGreen, map.c3);<br>
+  EXPECT_EQ(cBlue,  map.c4);  // tests default<br>
+  EXPECT_EQ(cYellow,map.c5);  // tests overridden<br>
+  EXPECT_EQ(cRed,   map.c6);  // tests default<br>
+}<br>
+<br>
+<br>
+<br>
+//===----------------------------------------------------------------------===//<br>
+//  Test ScalarBitSetTraits<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+enum MyFlags {<br>
+  flagNone    = 0,<br>
+  flagBig     = 1 << 0,<br>
+  flagFlat    = 1 << 1,<br>
+  flagRound   = 1 << 2,<br>
+  flagPointy  = 1 << 3<br>
+};<br>
+inline MyFlags operator|(MyFlags a, MyFlags b) {<br>
+  return static_cast<MyFlags>(<br>
+                      static_cast<uint32_t>(a) | static_cast<uint32_t>(b));<br>
+}<br>
+<br>
+struct FlagsMap {<br>
+  MyFlags     f1;<br>
+  MyFlags     f2;<br>
+  MyFlags     f3;<br>
+  MyFlags     f4;<br>
+};<br>
+<br>
+<br>
+namespace llvm {<br>
+namespace yaml {<br>
+  template <><br>
+  struct ScalarBitSetTraits<MyFlags> {<br>
+    static void bitset(IO &io, MyFlags &value) {<br>
+      io.bitSetCase(value, "big",   flagBig);<br>
+      io.bitSetCase(value, "flat",  flagFlat);<br>
+      io.bitSetCase(value, "round", flagRound);<br>
+      io.bitSetCase(value, "pointy",flagPointy);<br>
+    }<br>
+  };<br>
+  template <><br>
+  struct MappingTraits<FlagsMap> {<br>
+    static void mapping(IO &io, FlagsMap& c) {<br>
+      io.mapRequired("f1", c.f1);<br>
+      io.mapRequired("f2", c.f2);<br>
+      io.mapRequired("f3", c.f3);<br>
+      io.mapOptional("f4", c.f4, MyFlags(flagRound));<br>
+     }<br>
+  };<br>
+}<br>
+}<br>
+<br>
+<br>
+//<br>
+// Test reading flow sequence representing bit-mask values<br>
+//<br>
+TEST(YAMLIO, TestFlagsRead) {<br>
+  FlagsMap map;<br>
+  Input yin("---\n"<br>
+            "f1:  [ big ]\n"<br>
+            "f2:  [ round, flat ]\n"<br>
+            "f3:  []\n"<br>
+            "...\n");<br>
+  yin >> map;<br>
+<br>
+  EXPECT_FALSE(yin.error());<br>
+  EXPECT_EQ(flagBig,              map.f1);<br>
+  EXPECT_EQ(flagRound|flagFlat,   map.f2);<br>
+  EXPECT_EQ(flagNone,             map.f3);  // check empty set<br>
+  EXPECT_EQ(flagRound,            map.f4);  // check optional key<br>
+}<br>
+<br>
+<br>
+//<br>
+// Test writing then reading back bit-mask values<br>
+//<br>
+TEST(YAMLIO, TestReadWriteFlags) {<br>
+  std::string intermediate;<br>
+  {<br>
+    FlagsMap map;<br>
+    map.f1 = flagBig;<br>
+    map.f2 = flagRound | flagFlat;<br>
+    map.f3 = flagNone;<br>
+    map.f4 = flagNone;<br>
+<br>
+    llvm::raw_string_ostream ostr(intermediate);<br>
+    Output yout(ostr);<br>
+    yout << map;<br>
+  }<br>
+<br>
+  {<br>
+    Input yin(intermediate);<br>
+    FlagsMap map2;<br>
+    yin >> map2;<br>
+<br>
+    EXPECT_FALSE(yin.error());<br>
+    EXPECT_EQ(flagBig,              map2.f1);<br>
+    EXPECT_EQ(flagRound|flagFlat,   map2.f2);<br>
+    EXPECT_EQ(flagNone,             map2.f3);<br>
+    //EXPECT_EQ(flagRound,       ...<br><br>[Message clipped]  </blockquote></div><br></div>