/* ----------------------------------------------------------------------------- Copyright 2019-2020 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] #ifndef CATEGORY_SOURCE_HPP_ #define CATEGORY_SOURCE_HPP_ #include #include #include #include #include "types.hpp" #include "function.hpp" #include "cycle-check.hpp" #define BUILTIN_FAIL(e) { \ FAIL() << TypeValue::Call((e), Function_Formatted_formatted, \ ParamTuple(), ArgTuple()).Only()->AsString(); \ __builtin_unreachable(); \ } extern const S& Var_empty; S Box_Bool(bool value); S Box_String(const PrimString& value); S Box_Char(PrimChar value); S Box_Int(PrimInt value); S Box_Float(PrimFloat value); class TypeCategory { public: inline ReturnTuple Call(const CategoryFunction& label, const ParamTuple& params, const ValueTuple& args) { return Dispatch(label, params, args); } virtual std::string CategoryName() const = 0; ALWAYS_PERMANENT(TypeCategory) virtual ~TypeCategory() = default; protected: TypeCategory() = default; virtual ReturnTuple Dispatch(const CategoryFunction& label, const ParamTuple& params, const ValueTuple& args); }; class TypeInstance { public: inline ReturnTuple Call(const TypeFunction& label, ParamTuple params, const ValueTuple& args) { return Dispatch(label, params, args); } virtual std::string CategoryName() const = 0; virtual void BuildTypeName(std::ostream& output) const = 0; static std::string TypeName(const TypeInstance& type) { std::ostringstream output; type.BuildTypeName(output); return output.str(); } static S Reduce(TypeInstance& from, TypeInstance& to, S target) { TRACE_FUNCTION("reduce") return CanConvert(from, to)? target : Var_empty; } virtual bool TypeArgsForParent( const TypeCategory& category, std::vector& args) const { return false; } ALWAYS_PERMANENT(TypeInstance) virtual ~TypeInstance() = default; protected: TypeInstance() = default; virtual ReturnTuple Dispatch(const TypeFunction& label, const ParamTuple& params, const ValueTuple& args); virtual bool CanConvertFrom(const TypeInstance& from) const { return false; } static bool CanConvert(const TypeInstance& from, const TypeInstance& to); template static void TypeNameFrom(std::ostream& output, const TypeCategory& category, const Ts&... params) { std::vector params2{¶ms...}; output << category.CategoryName(); if (!params2.empty()) { output << "<"; bool first = true; for (const auto param : params2) { if (!first) output << ","; first = false; param->BuildTypeName(output); } output << ">"; } } enum class MergeType { SINGLE, UNION, INTERSECT, }; private: virtual MergeType InstanceMergeType() const { return MergeType::SINGLE; } virtual std::vector MergedTypes() const { return std::vector{this}; } static bool ExpandCheckLeft(const TypeInstance& from, const TypeInstance& to); static bool ExpandCheckRight(const TypeInstance& from, const TypeInstance& to); }; class TypeValue { public: inline static ReturnTuple Call(const S& target, const ValueFunction& label, const ParamTuple& params, const ValueTuple& args) { if (target == nullptr) { FAIL() << "Function called on null value"; } return target->Dispatch(target, label, params, args); } static bool Present(S target); static S Require(S target); static S Strong(W target); virtual bool AsBool() const; virtual const PrimString& AsString() const; virtual PrimChar AsChar() const; virtual PrimInt AsInt() const; virtual PrimFloat AsFloat() const; ALWAYS_PERMANENT(TypeValue) virtual ~TypeValue() = default; protected: TypeValue() = default; // NOTE: For some reason, making this private causes a segfault. virtual std::string CategoryName() const = 0; virtual bool Present() const; virtual ReturnTuple Dispatch(const S& self, const ValueFunction& label, const ParamTuple& params, const ValueTuple& args); }; template using InstanceMap = std::map::Type, R>; #endif // CATEGORY_SOURCE_HPP_