[llvm-commits] [hlvm] r38065 - in /hlvm/trunk: hlvm/AST/AST.cpp hlvm/AST/AST.h hlvm/AST/ContainerType.cpp hlvm/AST/ContainerType.h hlvm/AST/Node.h hlvm/AST/Operator.h hlvm/AST/Type.cpp hlvm/AST/Type.h hlvm/Reader/XML/HLVM.rng hlvm/Reader/XML/XMLReader.cpp hlvm/Writer/XML/XMLWriter.cpp test/xml2xml/enumeration.hlx

Reid Spencer reid at x10sys.com
Sat Jul 7 16:59:29 PDT 2007


Author: reid
Date: Sat Jul  7 18:59:29 2007
New Revision: 38065

URL: http://llvm.org/viewvc/llvm-project?rev=38065&view=rev
Log:
Add support for enumerations.

Added:
    hlvm/trunk/test/xml2xml/enumeration.hlx
Modified:
    hlvm/trunk/hlvm/AST/AST.cpp
    hlvm/trunk/hlvm/AST/AST.h
    hlvm/trunk/hlvm/AST/ContainerType.cpp
    hlvm/trunk/hlvm/AST/ContainerType.h
    hlvm/trunk/hlvm/AST/Node.h
    hlvm/trunk/hlvm/AST/Operator.h
    hlvm/trunk/hlvm/AST/Type.cpp
    hlvm/trunk/hlvm/AST/Type.h
    hlvm/trunk/hlvm/Reader/XML/HLVM.rng
    hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp
    hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp

Modified: hlvm/trunk/hlvm/AST/AST.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/AST.cpp?rev=38065&r1=38064&r2=38065&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/AST.cpp (original)
+++ hlvm/trunk/hlvm/AST/AST.cpp Sat Jul  7 18:59:29 2007
@@ -168,6 +168,18 @@
   return result;
 }
 
+EnumerationType* 
+AST::new_EnumerationType(
+  const Locator&loc, 
+  const std::string& id )
+{
+  EnumerationType* result = new EnumerationType();
+  result->setLocator(loc);
+  result->setName(id);
+  static_cast<ASTImpl*>(this)->addType(result);
+  return result;
+}
+
 RealType* 
 AST::new_RealType(
   const Locator&loc,

Modified: hlvm/trunk/hlvm/AST/AST.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/AST.h?rev=38065&r1=38064&r2=38065&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/AST.h (original)
+++ hlvm/trunk/hlvm/AST/AST.h Sat Jul  7 18:59:29 2007
@@ -63,6 +63,7 @@
   class StructureType;
   class SignatureType;
   class OpaqueType;
+  class EnumerationType;
 
   /// This class is used to hold or contain an Abstract Syntax Tree. It provides
   /// those aspects of the tree that are not part of the tree itself.
@@ -121,6 +122,10 @@
         int64_t min,            ///< The minimum value accepted in range
         int64_t max             ///< The maximum value accepted in range
       );
+      EnumerationType* new_EnumerationType(
+        const Locator&loc,      ///< The locator of the declaration
+        const std::string& id   ///< The name of the atom
+      );
       RealType* new_RealType(
         const Locator&loc,      ///< The locator of the declaration
         const std::string& id,  ///< The name of the atom

Modified: hlvm/trunk/hlvm/AST/ContainerType.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/ContainerType.cpp?rev=38065&r1=38064&r2=38065&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/ContainerType.cpp (original)
+++ hlvm/trunk/hlvm/AST/ContainerType.cpp Sat Jul  7 18:59:29 2007
@@ -56,7 +56,7 @@
 }
 
 const char* 
-ContainerType::getPrimitiveName()
+ContainerType::getPrimitiveName() const
 {
   return 0;
 }

Modified: hlvm/trunk/hlvm/AST/ContainerType.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/ContainerType.h?rev=38065&r1=38064&r2=38065&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/ContainerType.h (original)
+++ hlvm/trunk/hlvm/AST/ContainerType.h Sat Jul  7 18:59:29 2007
@@ -59,7 +59,7 @@
     /// @name Accessors
     /// @{
     public:
-      virtual const char* getPrimitiveName();
+      virtual const char* getPrimitiveName() const;
       // Methods to support type inquiry via is, cast, dyn_cast
       static inline bool classof(const ContainerType*) { return true; }
       static inline bool classof(const Type* T) { return T->isContainerType(); }

Modified: hlvm/trunk/hlvm/AST/Node.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/Node.h?rev=38065&r1=38064&r2=38065&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/Node.h (original)
+++ hlvm/trunk/hlvm/AST/Node.h Sat Jul  7 18:59:29 2007
@@ -51,6 +51,7 @@
     OctetTypeID,        ///< The Octet Type (8 bits uninterpreted)
     IntegerTypeID,      ///< The Integer Type (A number of bits of integer data)
     RangeTypeID,        ///< The Range Type (A Range of Integer Values)
+    EnumerationTypeID,  ///< The Enumeration Type (set of enumerated ids)
     RealTypeID,         ///< The Real Number Type (Any Real Number)
     RationalTypeID,     ///< The Rational Number Type (p/q type number)
     StringTypeID,       ///< The String Type (Array of UTF-16 chars + length)

Modified: hlvm/trunk/hlvm/AST/Operator.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/Operator.h?rev=38065&r1=38064&r2=38065&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/Operator.h (original)
+++ hlvm/trunk/hlvm/AST/Operator.h Sat Jul  7 18:59:29 2007
@@ -57,6 +57,10 @@
     /// @name Accessors
     /// @{
     public:
+      bool isNilaryOperator();
+      bool isUnaryOperator();
+      bool isBinaryOperator();
+      bool isTernaryOperator();
       static inline bool classof(const Operator*) { return true; }
       static inline bool classof(const Node* N) { return N->isOperator(); }
 

Modified: hlvm/trunk/hlvm/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/Type.cpp?rev=38065&r1=38064&r2=38065&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/Type.cpp (original)
+++ hlvm/trunk/hlvm/AST/Type.cpp Sat Jul  7 18:59:29 2007
@@ -43,7 +43,7 @@
 }
 
 const char*
-Type::getPrimitiveName()
+Type::getPrimitiveName() const
 {
   return 0;
 }
@@ -53,7 +53,7 @@
 }
 
 const char* 
-AnyType::getPrimitiveName()
+AnyType::getPrimitiveName() const
 {
   return "any";
 }
@@ -63,7 +63,7 @@
 }
 
 const char* 
-BooleanType::getPrimitiveName()
+BooleanType::getPrimitiveName() const
 {
   return "bool";
 }
@@ -73,7 +73,7 @@
 }
 
 const char* 
-CharacterType::getPrimitiveName()
+CharacterType::getPrimitiveName() const
 {
   return "char";
 }
@@ -83,7 +83,7 @@
 }
 
 const char* 
-IntegerType::getPrimitiveName()
+IntegerType::getPrimitiveName() const
 {
   if (numBits > 128)
     return 0;
@@ -119,7 +119,7 @@
 }
 
 const char* 
-OctetType::getPrimitiveName()
+OctetType::getPrimitiveName() const
 {
   return "octet";
 }
@@ -129,7 +129,7 @@
 }
 
 const char* 
-RangeType::getPrimitiveName()
+RangeType::getPrimitiveName() const
 {
   if (min < 0) {
     if (min >= 0 && max <= 255U)
@@ -159,12 +159,24 @@
   return 0;
 }
 
+EnumerationType::~EnumerationType()
+{
+}
+
+const char* 
+EnumerationType::getPrimitiveName() const
+{
+  if (size() < 4294967295U)
+    return "u32";
+  return 0;
+}
+
 RealType::~RealType()
 {
 }
 
 const char* 
-RealType::getPrimitiveName()
+RealType::getPrimitiveName() const
 {
   switch (mantissa) {
     case 23:
@@ -198,7 +210,7 @@
 }
 
 const char* 
-VoidType::getPrimitiveName()
+VoidType::getPrimitiveName() const
 {
   return "void";
 }
@@ -220,7 +232,7 @@
 }
 
 const char*
-AliasType::getPrimitiveName()
+AliasType::getPrimitiveName() const
 {
   return type->getPrimitiveName();
 }

Modified: hlvm/trunk/hlvm/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/Type.h?rev=38065&r1=38064&r2=38065&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/Type.h (original)
+++ hlvm/trunk/hlvm/AST/Type.h Sat Jul  7 18:59:29 2007
@@ -54,8 +54,8 @@
     /// @name Accessors
     /// @{
     public:
-      virtual const char* getPrimitiveName();
-      bool isPrimitive() { return getPrimitiveName() != 0; }
+      virtual const char* getPrimitiveName() const;
+      bool isPrimitive() const { return getPrimitiveName() != 0; }
 
     /// @}
     /// @name Type Identification
@@ -116,7 +116,8 @@
     /// @name Accessors
     /// @{
     public:
-      virtual const char* getPrimitiveName();
+      virtual const char* getPrimitiveName() const;
+
       // Methods to support type inquiry via is, cast, dyn_cast
       static inline bool classof(const AnyType*) { return true; }
       static inline bool classof(const Type* T) { return T->isAnyType(); }
@@ -137,7 +138,7 @@
     /// @name Accessors
     /// @{
     public:
-      virtual const char* getPrimitiveName();
+      virtual const char* getPrimitiveName() const;
       // Methods to support type inquiry via is, cast, dyn_cast
       static inline bool classof(const BooleanType*) { return true; }
       static inline bool classof(const Type* T) { return T->isBooleanType(); }
@@ -158,7 +159,7 @@
     /// @name Accessors
     /// @{
     public:
-      virtual const char* getPrimitiveName();
+      virtual const char* getPrimitiveName() const;
       // Methods to support type inquiry via is, cast, dyn_cast
       static inline bool classof(const CharacterType*) { return true; }
       static inline bool classof(const Type* T) { return T->isCharacterType(); }
@@ -180,7 +181,7 @@
     /// @name Accessors
     /// @{
     public:
-      virtual const char* getPrimitiveName();
+      virtual const char* getPrimitiveName() const;
       // Methods to support type inquiry via is, cast, dyn_cast
       static inline bool classof(const OctetType*) { return true; }
       static inline bool classof(const Type* T) { return T->isOctetType(); }
@@ -201,7 +202,7 @@
     /// @name Accessors
     /// @{
     public:
-      virtual const char* getPrimitiveName();
+      virtual const char* getPrimitiveName() const;
       // Methods to support type inquiry via is, cast, dyn_cast
       static inline bool classof(const VoidType*) { return true; }
       static inline bool classof(const Type* T) { return T->isVoidType(); }
@@ -228,7 +229,7 @@
     /// @name Accessors
     /// @{
     public:
-      virtual const char* getPrimitiveName();
+      virtual const char* getPrimitiveName() const;
 
       /// Return the number of bits
       uint64_t getBits()  const { return numBits; }
@@ -242,7 +243,7 @@
       static inline bool classof(const Node* T) { return T->is(IntegerTypeID); }
 
     /// @}
-    /// @name Accessors
+    /// @name Mutators
     /// @{
     public:
       /// Set the number of bits for this integer type
@@ -278,7 +279,7 @@
     /// @name Accessors
     /// @{
     public:
-      virtual const char* getPrimitiveName();
+      virtual const char* getPrimitiveName() const;
       /// Get min value of range
       int64_t getMin() { return min; }
 
@@ -291,7 +292,7 @@
       static inline bool classof(const Node* T) { return T->is(RangeTypeID); }
 
     /// @}
-    /// @name Accessors
+    /// @name Mutators
     /// @{
     public:
       /// Set min value of range
@@ -310,6 +311,68 @@
     friend class AST;
   };
 
+  /// This class represents an enumeration of things. Although represented by
+  /// an integer type, enumerations have no value. They only have a collation
+  /// order. 
+  class EnumerationType : public Type
+  {
+    /// @name Types
+    /// @{
+    public:
+      typedef std::vector<std::string> EnumeratorList;
+      typedef EnumeratorList::iterator iterator;
+      typedef EnumeratorList::const_iterator const_iterator;
+
+    /// @}
+    /// @name Constructors
+    /// @{
+    protected:
+      EnumerationType() : Type(EnumerationTypeID), enumerators() {}
+    public:
+      virtual ~EnumerationType();
+
+    /// @}
+    /// @name Accessors
+    /// @{
+    public:
+      virtual const char* getPrimitiveName() const;
+      // Methods to support type inquiry via is, cast, dyn_cast
+      static inline bool classof(const EnumerationType*) { return true; }
+      static inline bool classof(const Type* T) 
+        { return T->is(EnumerationTypeID); }
+      static inline bool classof(const Node* T) 
+        { return T->is(EnumerationTypeID); }
+
+    /// @}
+    /// @name Mutators
+    /// @{
+    public:
+      void addEnumerator(const std::string& en) { enumerators.push_back(en); }
+
+    /// @}
+    /// @name Iterators
+    /// @{
+    public:
+      iterator          begin()       { return enumerators.begin(); }
+      const_iterator    begin() const { return enumerators.begin(); }
+      iterator          end  ()       { return enumerators.end(); }
+      const_iterator    end  () const { return enumerators.end(); }
+      size_t            size () const { return enumerators.size(); }
+      bool              empty() const { return enumerators.empty(); }
+      std::string       front()       { return enumerators.front(); }
+      const std::string front() const { return enumerators.front(); }
+      std::string       back()        { return enumerators.back(); }
+      const std::string back()  const { return enumerators.back(); }
+
+    /// @}
+    /// @name Data
+    /// @{
+    protected:
+      EnumeratorList enumerators; ///< The list of the enumerators
+    /// @}
+    friend class AST;
+  };
+
   /// This class represents all HLVM real number types. The precision and 
   /// mantissa are specified as a number of decimal digits to be provided as a
   /// minimum.  HLVM will use the machine's natural floating point 
@@ -329,7 +392,7 @@
     /// @name Accessors
     /// @{
     public:
-      virtual const char* getPrimitiveName();
+      virtual const char* getPrimitiveName() const;
       /// Get the mantissa bits
       uint32_t getMantissa() { return mantissa; }
 
@@ -472,7 +535,7 @@
       static inline bool classof(const Node* T) { return T->is(VectorTypeID); }
 
     /// @}
-    /// @name Accessors
+    /// @name Mutators
     /// @{
     public:
       /// Set the type of the vector's elements.
@@ -506,7 +569,7 @@
     /// @name Accessors
     /// @{
     public:
-      virtual const char* getPrimitiveName();
+      virtual const char* getPrimitiveName() const;
       // Get the name for the type
       const std::string&  getName() const { return name; }
       

Modified: hlvm/trunk/hlvm/Reader/XML/HLVM.rng
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Reader/XML/HLVM.rng?rev=38065&r1=38064&r2=38065&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Reader/XML/HLVM.rng (original)
+++ hlvm/trunk/hlvm/Reader/XML/HLVM.rng Sat Jul  7 18:59:29 2007
@@ -166,17 +166,17 @@
     </optional>
     <zeroOrMore>
       <choice>
-        <ref name="Bundle.elem"/>
-        <ref name="Function.elem"/>
         <ref name="Alias.elem"/>
+        <ref name="Array.elem"/>
         <ref name="Atom.elem"/>
+        <ref name="Bundle.elem"/>
         <ref name="Enumeration.elem"/>
+        <ref name="Function.elem"/>
         <ref name="Pointer.elem"/>
-        <ref name="Array.elem"/>
-        <ref name="Vector.elem"/>
         <ref name="Structure.elem"/>
         <ref name="Signature.elem"/>
         <ref name="Variable.elem"/>
+        <ref name="Vector.elem"/>
       </choice>
     </zeroOrMore>
   </define>
@@ -284,12 +284,13 @@
   </define>
 
   <define name="Enumeration.elem">
-    <element name="enum">
+    <element name="enumeration">
       <ref name="Named_Element.pat"/>
       <oneOrMore>
-        <element name="value">
-          <ref name="Named_Element.pat"/>
-          <ref name="IntegerLiteral.pat"/>
+        <element name="enumerator">
+          <attribute name="id">
+            <ref name="Unprefixed_Identifier.type"/>
+          </attribute>
         </element>
       </oneOrMore>
     </element>

Modified: hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp?rev=38065&r1=38064&r2=38065&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp (original)
+++ hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp Sat Jul  7 18:59:29 2007
@@ -86,17 +86,18 @@
   inline void handleValidationError(xmlErrorPtr error);
 
   void parseTree();
+  AST::AliasType*parseAlias(xmlNodePtr& cur);
+  AST::Type*     parseArray(xmlNodePtr& cur);
+  AST::Type*     parseAtom(xmlNodePtr& cur);
   AST::Bundle*   parseBundle(xmlNodePtr& cur);
+  AST::Type*     parseEnumeration(xmlNodePtr& cur);
   AST::Function* parseFunction(xmlNodePtr& cur);
   AST::Import*   parseImport(xmlNodePtr& cur);
-  AST::Variable* parseVariable(xmlNodePtr& cur);
-  AST::AliasType*parseAlias(xmlNodePtr& cur);
-  AST::Type*     parseAtom(xmlNodePtr& cur);
   AST::Type*     parsePointer(xmlNodePtr& cur);
-  AST::Type*     parseArray(xmlNodePtr& cur);
-  AST::Type*     parseVector(xmlNodePtr& cur);
   AST::Type*     parseStructure(xmlNodePtr& cur);
   AST::Type*     parseSignature(xmlNodePtr& cur);
+  AST::Variable* parseVariable(xmlNodePtr& cur);
+  AST::Type*     parseVector(xmlNodePtr& cur);
 private:
 };
 
@@ -325,6 +326,23 @@
   assert(!"Atom definition element expected");
 }
 
+AST::Type*
+XMLReaderImpl::parseEnumeration(xmlNodePtr& cur)
+{
+  assert(getToken(cur->name)==TKN_enumeration);
+  AST::Locator loc(cur->line,0,&ast->getSystemID());
+  std::string name = getAttribute(cur,"name");
+  AST::EnumerationType* en = ast->new_EnumerationType(loc,name);
+  xmlNodePtr child = cur->children;
+  while (child && skipBlanks(child) && child->type == XML_ELEMENT_NODE) {
+    assert(getToken(child->name) == TKN_enumerator);
+    std::string id = getAttribute(child,"id");
+    en->addEnumerator(id);
+    child = child->next;
+  }
+  return en;
+}
+
 AST::Type*     
 XMLReaderImpl::parsePointer(xmlNodePtr& cur)
 {
@@ -392,7 +410,7 @@
     sig->setIsVarArgs(recognize_boolean(varargs));
   xmlNodePtr child = cur->children;
   while (child && skipBlanks(child) && child->type == XML_ELEMENT_NODE) {
-    assert(getToken(child->name) == TKN_arg && "Structure only has fields");
+    assert(getToken(child->name) == TKN_arg && "Signature only has args");
     std::string name = getAttribute(child,"name");
     std::string type = getAttribute(child,"type");
     AST::AliasType* nt = ast->new_AliasType(loc,name,ast->resolveType(type));
@@ -432,6 +450,7 @@
       case TKN_function : n = parseFunction(child); break;
       case TKN_alias    : n = parseAlias(child); break;
       case TKN_atom     : n = parseAtom(child); break;
+      case TKN_enumeration: n = parseEnumeration(child); break;
       case TKN_pointer  : n = parsePointer(child); break;
       case TKN_array    : n = parseArray(child); break;
       case TKN_vector   : n = parseVector(child); break;

Modified: hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp?rev=38065&r1=38064&r2=38065&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp (original)
+++ hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp Sat Jul  7 18:59:29 2007
@@ -99,6 +99,7 @@
   inline void put(AST::CharacterType* t);
   inline void put(AST::IntegerType* t);
   inline void put(AST::RangeType* t);
+  inline void put(AST::EnumerationType* t);
   inline void put(AST::RealType* t);
   inline void put(AST::OctetType* t);
   inline void put(AST::VoidType* t);
@@ -194,6 +195,26 @@
 void
 XMLWriterImpl::put(AST::RangeType* t)
 {
+  startElement("range");
+  writeAttribute("name",t->getName());
+  writeAttribute("min",t->getMin());
+  writeAttribute("max",t->getMax());
+  endElement();
+}
+
+void 
+XMLWriterImpl::put(AST::EnumerationType* t)
+{
+  startElement("enumeration");
+  writeAttribute("name",t->getName());
+  for (AST::EnumerationType::const_iterator I = t->begin(), E = t->end(); 
+       I != E; ++I)
+  {
+    startElement("enumerator");
+    writeAttribute("id",*I);
+    endElement();
+  }
+  endElement();
 }
 
 void
@@ -324,6 +345,7 @@
       case AST::CharacterTypeID:    put(cast<AST::CharacterType>(*I)); break;
       case AST::IntegerTypeID:      put(cast<AST::IntegerType>(*I)); break;
       case AST::RangeTypeID:        put(cast<AST::RangeType>(*I)); break;
+      case AST::EnumerationTypeID:  put(cast<AST::EnumerationType>(*I)); break;
       case AST::RealTypeID:         put(cast<AST::RealType>(*I)); break;
       case AST::OctetTypeID:        put(cast<AST::OctetType>(*I)); break;
       case AST::VoidTypeID:         put(cast<AST::VoidType>(*I)); break;

Added: hlvm/trunk/test/xml2xml/enumeration.hlx
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/test/xml2xml/enumeration.hlx?rev=38065&view=auto

==============================================================================
--- hlvm/trunk/test/xml2xml/enumeration.hlx (added)
+++ hlvm/trunk/test/xml2xml/enumeration.hlx Sat Jul  7 18:59:29 2007
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<hlvm xmlns="http://hlvm.org/src/hlvm/Reader/XML/HLVM.rng">
+  <bundle pubid="bundle">
+    <enumeration name="anEnumeration">
+      <enumerator id="one"/>
+      <enumerator id="two"/>
+      <enumerator id="three"/>
+      <enumerator id="four"/>
+    </enumeration>
+  </bundle>
+</hlvm>





More information about the llvm-commits mailing list