// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. // // 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. #pragma once #include "paddle/ap/include/axpr/adt.h" #include "paddle/ap/include/axpr/attr_map.h" #include "paddle/ap/include/axpr/bool.h" #include "paddle/ap/include/axpr/builtin_func_type.h" #include "paddle/ap/include/axpr/builtin_high_order_func_type.h" #include "paddle/ap/include/axpr/builtin_serializable_attr_map.h" #include "paddle/ap/include/axpr/builtin_symbol.h" #include "paddle/ap/include/axpr/class_instance.h" #include "paddle/ap/include/axpr/closure.h" #include "paddle/ap/include/axpr/continuation.h" #include "paddle/ap/include/axpr/data_type.h" #include "paddle/ap/include/axpr/data_value.h" #include "paddle/ap/include/axpr/environment.h" #include "paddle/ap/include/axpr/error.h" #include "paddle/ap/include/axpr/float.h" #include "paddle/ap/include/axpr/function.h" #include "paddle/ap/include/axpr/int.h" #include "paddle/ap/include/axpr/list.h" #include "paddle/ap/include/axpr/method.h" #include "paddle/ap/include/axpr/mutable_list.h" #include "paddle/ap/include/axpr/mutable_ordered_dict.h" #include "paddle/ap/include/axpr/nothing.h" #include "paddle/ap/include/axpr/ordered_dict.h" #include "paddle/ap/include/axpr/packed_args.h" #include "paddle/ap/include/axpr/pointer_type.h" #include "paddle/ap/include/axpr/pointer_value.h" #include "paddle/ap/include/axpr/serializable_list.h" #include "paddle/ap/include/axpr/serializable_value.h" #include "paddle/ap/include/axpr/starred.h" #include "paddle/ap/include/axpr/string.h" #include "paddle/ap/include/axpr/type.h" #include "paddle/ap/include/axpr/type_util.h" namespace ap::axpr { using adt::Nothing; template using ValueBase = std::variant, adt::List, MutableList, AttrMap, AttrMap, OrderedDict, MutableOrderedDict, BuiltinClassInstance, ClassInstance, PackedArgs, Starred, Function, Closure, Continuation, Method, builtin_symbol::Symbol, BuiltinFuncType, BuiltinHighOrderFuncType>, Nothing, bool, int64_t, double, std::string, DataType, DataValue, PointerType, PointerValue, adt::List, adt::List, MutableList, AttrMap, AttrMap, OrderedDict, MutableOrderedDict, BuiltinClassInstance, ClassInstance, PackedArgs, Starred, Function, Closure, Continuation, Method, builtin_symbol::Symbol, BuiltinFuncType, BuiltinHighOrderFuncType>; template ValueT GetType(const ValueT& value) { return value.Match( [](const BuiltinClassInstance& impl) -> ValueT { return impl.type; }, [](const ClassInstance& impl) -> ValueT { return impl->type; }, [](const auto& impl) -> ValueT { using T = std::decay_t; return TypeImpl{}; }); } template adt::Result::TypeT> CastToType(const ValueT& value) { ADT_LET_CONST_REF(type, value.template TryGet::TypeT>()); return type; } template adt::Result TryGetTypeImpl(const ValueT& value) { ADT_LET_CONST_REF(type, CastToType(value)); ADT_LET_CONST_REF(type_impl, type.template TryGet()); return type_impl; } template adt::Result TryGetBuiltinClassInstance(const ValueT& val) { ADT_LET_CONST_REF(instance, val.template TryGet>()); ADT_LET_CONST_REF(ret, instance.template TryGet()); return ret; } template adt::Result Get(const ValueT& val) { using TypeT = typename TypeTrait::TypeT; if constexpr (ValueT::template IsMyAlternative()) { return val.template TryGet(); } else if constexpr (TypeT::template IsMyAlternative()) { return TryGetTypeImpl(val); } else { return TryGetBuiltinClassInstance(val); } } template adt::Result CastableTo(const ValueT& val) { using TypeT = typename TypeTrait::TypeT; if constexpr (ValueT::template IsMyAlternative()) { return val.template Has(); } else if constexpr (TypeT::template IsMyAlternative()) { ADT_LET_CONST_REF(type, CastToType(val)); return type.template Has(); } else { ADT_LET_CONST_REF(instance, val.template TryGet>()); return instance.template Has(); } } struct Value : public ValueBase { using ValueBase::ValueBase; ADT_DEFINE_VARIANT_METHODS(ValueBase); static axpr::AttrMap GetExportedTypes() { return axpr::GetObjectTypeName2Type(); } template adt::Result CastTo() const { return axpr::Get(*this); } template bool CastableTo() const { const auto& ret = axpr::CastableTo(*this); return ret.HasOkValue() ? ret.GetOkValue() : false; } }; } // namespace ap::axpr