2019-01-28 23:38:17 +01:00
|
|
|
// Copyright (c) 2019, NVIDIA CORPORATION. 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.
|
|
|
|
|
|
|
|
#ifndef FORTRAN_EVALUATE_CONSTANT_H_
|
|
|
|
#define FORTRAN_EVALUATE_CONSTANT_H_
|
|
|
|
|
|
|
|
#include "type.h"
|
|
|
|
#include <ostream>
|
|
|
|
|
|
|
|
namespace Fortran::evaluate {
|
2019-02-01 22:37:49 +01:00
|
|
|
|
2019-01-28 23:38:17 +01:00
|
|
|
// Wraps a constant value in a class templated by its resolved type.
|
|
|
|
// N.B. Generic constants are represented by generic expressions
|
|
|
|
// (like Expr<SomeInteger> & Expr<SomeType>) wrapping the appropriate
|
|
|
|
// instantiations of Constant.
|
|
|
|
|
2019-02-07 21:05:27 +01:00
|
|
|
template<typename> class Constant;
|
|
|
|
|
|
|
|
template<typename RESULT> class ConstantBase {
|
2019-01-28 23:38:17 +01:00
|
|
|
public:
|
2019-02-07 21:05:27 +01:00
|
|
|
using Result = RESULT;
|
2019-01-28 23:38:17 +01:00
|
|
|
using Value = Scalar<Result>;
|
|
|
|
|
2019-02-07 21:05:27 +01:00
|
|
|
template<typename A> ConstantBase(const A &x) : values_{x} {}
|
2019-01-28 23:38:17 +01:00
|
|
|
template<typename A>
|
2019-02-07 21:05:27 +01:00
|
|
|
ConstantBase(std::enable_if_t<!std::is_reference_v<A>, A> &&x)
|
2019-01-28 23:38:17 +01:00
|
|
|
: values_{std::move(x)} {}
|
2019-02-07 21:05:27 +01:00
|
|
|
ConstantBase(std::vector<Value> &&x, std::vector<std::int64_t> &&s)
|
2019-01-28 23:38:17 +01:00
|
|
|
: values_(std::move(x)), shape_(std::move(s)) {}
|
2019-02-07 21:05:27 +01:00
|
|
|
~ConstantBase();
|
2019-01-28 23:38:17 +01:00
|
|
|
|
|
|
|
int Rank() const { return static_cast<int>(shape_.size()); }
|
2019-02-07 21:05:27 +01:00
|
|
|
bool operator==(const ConstantBase &that) const {
|
2019-01-28 23:38:17 +01:00
|
|
|
return shape_ == that.shape_ && values_ == that.values_;
|
|
|
|
}
|
2019-02-01 22:37:49 +01:00
|
|
|
bool empty() const { return values_.empty(); }
|
2019-01-28 23:38:17 +01:00
|
|
|
std::size_t size() const { return values_.size(); }
|
|
|
|
const std::vector<std::int64_t> &shape() const { return shape_; }
|
|
|
|
|
2019-02-01 22:37:49 +01:00
|
|
|
Value operator*() const {
|
2019-01-28 23:38:17 +01:00
|
|
|
CHECK(values_.size() == 1);
|
|
|
|
return values_.at(0);
|
|
|
|
}
|
|
|
|
|
2019-02-01 22:37:49 +01:00
|
|
|
// Apply 1-based subscripts
|
|
|
|
Value At(const std::vector<std::int64_t> &) const;
|
2019-01-28 23:38:17 +01:00
|
|
|
|
|
|
|
Constant<SubscriptInteger> SHAPE() const;
|
|
|
|
std::ostream &AsFortran(std::ostream &) const;
|
|
|
|
|
2019-02-07 21:05:27 +01:00
|
|
|
protected:
|
2019-01-28 23:38:17 +01:00
|
|
|
std::vector<Value> values_;
|
|
|
|
std::vector<std::int64_t> shape_;
|
2019-02-07 21:05:27 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
const Constant<Result> &AsConstant() const {
|
|
|
|
return *static_cast<const Constant<Result> *>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicType GetType() const { return AsConstant().GetType(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> class Constant : public ConstantBase<T> {
|
|
|
|
public:
|
|
|
|
using Result = T;
|
|
|
|
using ConstantBase<Result>::ConstantBase;
|
|
|
|
CLASS_BOILERPLATE(Constant)
|
|
|
|
static constexpr DynamicType GetType() { return Result::GetType(); }
|
2019-01-28 23:38:17 +01:00
|
|
|
};
|
|
|
|
|
2019-02-01 22:37:49 +01:00
|
|
|
template<int KIND>
|
2019-02-07 21:05:27 +01:00
|
|
|
class Constant<Type<TypeCategory::Character, KIND>>
|
|
|
|
: public ConstantBase<Type<TypeCategory::Character, KIND>> {
|
|
|
|
public:
|
|
|
|
using Result = Type<TypeCategory::Character, KIND>;
|
|
|
|
using ConstantBase<Result>::ConstantBase;
|
|
|
|
CLASS_BOILERPLATE(Constant)
|
|
|
|
static constexpr DynamicType GetType() { return Result::GetType(); }
|
|
|
|
std::int64_t LEN() const {
|
|
|
|
if (this->values_.empty()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return static_cast<std::int64_t>(this->values_.front().size());
|
|
|
|
}
|
2019-02-01 22:37:49 +01:00
|
|
|
}
|
2019-02-07 21:05:27 +01:00
|
|
|
// TODO pmk: make CHARACTER values contiguous (they're strings now)
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> class Constant<SomeDerived> : public ConstantBase<SomeDerived> {
|
|
|
|
public:
|
|
|
|
using Result = SomeDerived;
|
|
|
|
using Base = ConstantBase<Result>;
|
|
|
|
template<typename A>
|
|
|
|
Constant(const semantics::DerivedTypeSpec &spec, const A &x)
|
|
|
|
: Base{x}, spec_{&spec} {}
|
|
|
|
template<typename A>
|
|
|
|
Constant(const semantics::DerivedTypeSpec &spec,
|
|
|
|
std::enable_if_t<!std::is_reference_v<A>, A> &&x)
|
|
|
|
: Base{std::move(x)}, spec_{&spec} {}
|
|
|
|
Constant(const semantics::DerivedTypeSpec &, std::vector<Value> &&,
|
|
|
|
std::vector<std::int64_t> &&);
|
|
|
|
|
|
|
|
CLASS_BOILERPLATE(Constant)
|
|
|
|
DynamicType GetType() const {
|
|
|
|
return DynamicType{TypeCategory::Derived, 0, spec_};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const semantics::DerivedTypeSpec *spec_;
|
|
|
|
};
|
2019-02-01 22:37:49 +01:00
|
|
|
|
2019-02-07 21:05:27 +01:00
|
|
|
FOR_EACH_SPECIFIC_TYPE(extern template class ConstantBase)
|
2019-01-28 23:38:17 +01:00
|
|
|
FOR_EACH_INTRINSIC_KIND(extern template class Constant)
|
|
|
|
}
|
|
|
|
#endif // FORTRAN_EVALUATE_CONSTANT_H_
|