00001 /* 00002 Copyright (c) 2009, National Institute of Advanced Industrial Science 00003 and Technology. All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are 00007 met: 00008 00009 * Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 00012 * Redistributions in binary form must reproduce the above copyright 00013 notice, this list of conditions and the following disclaimer in 00014 the documentation and/or other materials provided with the 00015 distribution. 00016 00017 * Neither the name of Fixstars Corporation nor the names of its 00018 contributors may be used to endorse or promote products derived 00019 from this software without specific prior written permission. 00020 00021 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00024 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00025 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00027 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00028 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00029 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00030 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00031 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 */ 00033 00034 #ifndef CFRONT_ENUM_TYPE_H 00035 #define CFRONT_ENUM_TYPE_H 00036 00037 #include <vector> 00038 00039 namespace cfront { 00040 00041 struct EnumConstant { 00042 std::string name; 00043 ConstantValue value; 00044 EnumConstant(const std::string& n, const ConstantValue& v) 00045 : name(n), value(v) {} 00046 }; 00047 00048 typedef std::vector<EnumConstant> EnumConstants; 00049 00050 00051 class EnumDefinition : public Type::Definition { 00052 public: 00053 explicit EnumDefinition(const std::string& name) : name_(name) {} 00054 00055 const EnumConstants& getConsts() const 00056 { 00057 return consts_; 00058 } 00059 00060 const std::string& getName() const 00061 { 00062 return name_; 00063 } 00064 00065 void addConst(const std::string& name, const ConstantValue& value) 00066 { 00067 consts_.push_back(EnumConstant(name, value)); 00068 } 00069 00070 private: 00071 EnumConstants consts_; 00072 std::string name_; 00073 }; 00074 00075 00079 class EnumType : public Type { 00080 public: 00081 explicit EnumType(EnumDefinition* def, QualFlags qf = 0) 00082 : definition_(def), qualFlags_(qf) {} 00083 EnumType(const EnumType& other) 00084 : Type(other), definition_(other.definition_), 00085 qualFlags_(other.qualFlags_) {} 00086 00087 virtual ~EnumType() {} 00088 00089 virtual EnumType* clone() const 00090 { 00091 return new EnumType(*this); 00092 } 00093 00094 virtual bool isCompatibleType(const Type* other) const 00095 { 00096 const EnumType* eo = dynamic_cast<const EnumType*>(other); 00097 if (eo == NULL) return false; 00098 return this->definition_ == eo->getDefinition(); 00099 } 00100 00101 virtual bool isArithmeticType() const 00102 { 00103 return true; 00104 } 00105 00106 virtual bool isIntegerType() const 00107 { 00108 return true; 00109 } 00110 00111 virtual size_t getSize(const TargetInfo& targetInfo) const 00112 { 00113 return targetInfo.getSize(TargetInfo::ENUM); 00114 } 00115 00116 virtual size_t getAlign(const TargetInfo& targetInfo) const 00117 { 00118 return targetInfo.getAlign(TargetInfo::ENUM); 00119 } 00120 00121 EnumDefinition* getDefinition() const 00122 { 00123 return definition_; 00124 } 00125 00126 QualFlags getQualFlags() const 00127 { 00128 return qualFlags_; 00129 } 00130 00131 void setQualFlags(QualFlags qf) 00132 { 00133 qualFlags_ = qf; 00134 } 00135 00136 #ifdef PARSER_DEBUG 00137 virtual std::string toString() const 00138 { 00139 std::stringstream ss; 00140 ss << "EnumType[ " << definition_->getName() << "]"; 00141 return ss.str(); 00142 } 00143 #endif 00144 00145 00146 protected: 00147 EnumDefinition* definition_; 00148 QualFlags qualFlags_; 00149 }; 00150 00151 } // namespace cfront 00152 00153 #endif 00154 00155 00156 /* 00157 * Local Variables: 00158 * mode: C++ 00159 * c-basic-offset: 4 00160 * indent-tabs-mode: nil 00161 * End: 00162 */