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_FUNCTION_TYPE_HPP 00035 #define CFRONT_FUNCTION_TYPE_HPP 00036 00037 #include <vector> 00038 00039 namespace cfront { 00040 00041 typedef std::vector<Type*> ParameterTypes; 00042 00046 class FunctionType : public Type { 00047 public: 00048 FunctionType(Type* retType = NULL) 00049 : returnType_(retType), hasVararg_(false) {} 00050 FunctionType(const FunctionType& other) 00051 : Type(other), returnType_(other.returnType_), 00052 hasVararg_(other.hasVararg_) 00053 { 00054 this->parameterTypes_.resize(other.parameterTypes_.size()); 00055 std::copy(other.parameterTypes_.begin(), other.parameterTypes_.end(), 00056 this->parameterTypes_.begin()); 00057 } 00058 00059 virtual ~FunctionType() {} 00060 00061 virtual FunctionType* clone() const 00062 { 00063 return new FunctionType(*this); 00064 } 00065 00066 virtual bool isCompatibleType(const Type* other) const 00067 { 00068 const FunctionType* fo = dynamic_cast<const FunctionType*>(other); 00069 if (fo == NULL) return false; 00070 00071 if (!returnType_->isCompatibleType(fo->returnType_)) { 00072 return false; 00073 } 00074 00075 const ParameterTypes& otherParams = fo->parameterTypes_; 00076 ParameterTypes::const_iterator i, j; 00077 for (i = this->parameterTypes_.begin(), j = otherParams.begin(); 00078 i != parameterTypes_.end() || j != otherParams.end(); ++i, ++j) { 00079 if (!(*i)->isCompatibleType(*j)) { 00080 return false; 00081 } 00082 } 00083 return i == parameterTypes_.end() && j == otherParams.end(); 00084 } 00085 00086 virtual size_t getSize(const TargetInfo& targetInfo) const 00087 { 00088 return targetInfo.getSize(TargetInfo::POINTER); 00089 } 00090 00091 virtual size_t getAlign(const TargetInfo& targetInfo) const 00092 { 00093 return targetInfo.getAlign(TargetInfo::POINTER); 00094 } 00095 00096 const Type* getReturnType() const 00097 { 00098 return returnType_; 00099 } 00100 00101 const ParameterTypes& getParameterTypes() const 00102 { 00103 return parameterTypes_; 00104 } 00105 00106 void addParameterType(Type* t) 00107 { 00108 parameterTypes_.push_back(t); 00109 } 00110 00111 void setVararg(bool hasVararg) 00112 { 00113 hasVararg_ = hasVararg; 00114 } 00115 00116 bool hasVararg() const 00117 { 00118 return hasVararg_; 00119 } 00120 00121 void setReturnType(Type* t) 00122 { 00123 returnType_ = t; 00124 } 00125 00126 #ifdef PARSER_DEBUG 00127 virtual std::string toString() const 00128 { 00129 std::stringstream ss; 00130 ss << "FunctionType("; 00131 for (ParameterTypes::const_iterator i = parameterTypes_.begin(); 00132 i != parameterTypes_.end(); ++i) { 00133 ss << " [" << (*i)->toString() << "]"; 00134 } 00135 if (hasVararg_) { 00136 ss << " ..."; 00137 } 00138 ss << " ) -> [" << returnType_->toString() << "]"; 00139 return ss.str(); 00140 } 00141 #endif 00142 00143 protected: 00144 Type* returnType_; 00145 ParameterTypes parameterTypes_; 00146 bool hasVararg_; 00147 }; 00148 00149 } // namespace cfront 00150 00151 #endif 00152 00153 00154 /* 00155 * Local Variables: 00156 * mode: C++ 00157 * c-basic-offset: 4 00158 * indent-tabs-mode: nil 00159 * End: 00160 */