2018-05-01 12:50:34 -07:00
|
|
|
// Copyright (c) 2018, 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.
|
|
|
|
|
2018-03-22 17:08:20 -07:00
|
|
|
#include "symbol.h"
|
|
|
|
#include "scope.h"
|
2018-04-12 12:23:20 -07:00
|
|
|
#include "../parser/idioms.h"
|
2018-03-22 17:08:20 -07:00
|
|
|
#include <memory>
|
|
|
|
|
2018-03-23 12:24:29 -07:00
|
|
|
namespace Fortran::semantics {
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
void EntityDetails::set_type(const DeclTypeSpec &type) {
|
|
|
|
CHECK(!type_);
|
|
|
|
type_ = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityDetails::set_shape(const ArraySpec &shape) {
|
|
|
|
CHECK(shape_.empty());
|
|
|
|
for (const auto &shapeSpec : shape) {
|
|
|
|
shape_.push_back(shapeSpec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:16:42 -07:00
|
|
|
// The name of the kind of details for this symbol.
|
|
|
|
// This is primarily for debugging.
|
|
|
|
const std::string Symbol::GetDetailsName() const {
|
|
|
|
return std::visit(
|
|
|
|
parser::visitors{
|
|
|
|
[&](const UnknownDetails &x) { return "Unknown"; },
|
|
|
|
[&](const MainProgramDetails &x) { return "MainProgram"; },
|
|
|
|
[&](const ModuleDetails &x) { return "Module"; },
|
|
|
|
[&](const SubprogramDetails &x) { return "Subprogram"; },
|
|
|
|
[&](const EntityDetails &x) { return "Entity"; },
|
|
|
|
},
|
|
|
|
details_);
|
|
|
|
}
|
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
std::ostream &operator<<(std::ostream &os, const EntityDetails &x) {
|
|
|
|
if (x.type()) {
|
|
|
|
os << " type: " << *x.type();
|
|
|
|
}
|
|
|
|
if (!x.shape().empty()) {
|
|
|
|
os << " shape:";
|
|
|
|
for (const auto &s : x.shape()) {
|
|
|
|
os << ' ' << s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:16:42 -07:00
|
|
|
static std::ostream &DumpType(std::ostream &os, const Symbol &symbol) {
|
|
|
|
if (const auto *details = symbol.detailsIf<EntityDetails>()) {
|
|
|
|
if (details->type()) {
|
|
|
|
os << *details->type() << ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2018-03-22 17:08:20 -07:00
|
|
|
std::ostream &operator<<(std::ostream &os, const Symbol &sym) {
|
2018-04-17 14:16:42 -07:00
|
|
|
os << sym.name().ToString();
|
2018-03-22 17:08:20 -07:00
|
|
|
if (!sym.attrs().empty()) {
|
|
|
|
os << ", " << sym.attrs();
|
|
|
|
}
|
2018-04-18 15:06:35 -07:00
|
|
|
os << ": " << sym.GetDetailsName();
|
2018-03-22 17:08:20 -07:00
|
|
|
std::visit(
|
|
|
|
parser::visitors{
|
2018-04-18 15:06:35 -07:00
|
|
|
[&](const UnknownDetails &x) {},
|
|
|
|
[&](const MainProgramDetails &x) {},
|
|
|
|
[&](const ModuleDetails &x) {},
|
2018-03-22 17:08:20 -07:00
|
|
|
[&](const SubprogramDetails &x) {
|
2018-04-18 15:06:35 -07:00
|
|
|
os << " (";
|
2018-03-22 17:08:20 -07:00
|
|
|
int n = 0;
|
2018-04-17 14:16:42 -07:00
|
|
|
for (const auto &dummy : x.dummyArgs()) {
|
2018-03-22 17:08:20 -07:00
|
|
|
if (n++ > 0) os << ", ";
|
2018-04-17 14:16:42 -07:00
|
|
|
DumpType(os, *dummy);
|
|
|
|
os << dummy->name().ToString();
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
|
|
|
os << ')';
|
2018-04-17 14:16:42 -07:00
|
|
|
if (x.isFunction()) {
|
|
|
|
os << " result(";
|
|
|
|
DumpType(os, x.result());
|
|
|
|
os << x.result().name().ToString() << ')';
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
|
|
|
},
|
2018-04-18 15:06:35 -07:00
|
|
|
[&](const EntityDetails &x) { os << x; },
|
2018-03-22 17:08:20 -07:00
|
|
|
},
|
|
|
|
sym.details_);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2018-03-23 12:24:29 -07:00
|
|
|
} // namespace Fortran::semantics
|