2018-05-01 21:50:34 +02: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-23 01:08:20 +01:00
|
|
|
#ifndef FORTRAN_SEMANTICS_SCOPE_H_
|
|
|
|
#define FORTRAN_SEMANTICS_SCOPE_H_
|
|
|
|
|
|
|
|
#include "attr.h"
|
|
|
|
#include "symbol.h"
|
2018-06-18 20:03:43 +02:00
|
|
|
#include "../common/idioms.h"
|
2018-03-23 01:08:20 +01:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2018-03-23 20:24:29 +01:00
|
|
|
namespace Fortran::semantics {
|
2018-03-23 01:08:20 +01:00
|
|
|
|
|
|
|
class Scope {
|
2018-06-20 01:06:41 +02:00
|
|
|
using mapType = std::map<SourceName, Symbol *>;
|
2018-04-09 20:53:20 +02:00
|
|
|
|
2018-03-23 01:08:20 +01:00
|
|
|
public:
|
|
|
|
// root of the scope tree; contains intrinsics:
|
2018-06-22 17:21:19 +02:00
|
|
|
static Scope systemScope;
|
2018-03-23 01:08:20 +01:00
|
|
|
static Scope globalScope; // contains program-units
|
|
|
|
|
2018-06-22 17:21:19 +02:00
|
|
|
ENUM_CLASS(Kind, System, Global, Module, MainProgram, Subprogram, DerivedType)
|
2018-03-23 01:08:20 +01:00
|
|
|
|
2018-06-22 17:21:19 +02:00
|
|
|
Scope(Scope &parent, Kind kind, Symbol *symbol)
|
|
|
|
: parent_{parent}, kind_{kind}, symbol_{symbol} {
|
|
|
|
if (symbol) {
|
|
|
|
symbol->set_scope(this);
|
|
|
|
}
|
|
|
|
}
|
2018-03-23 01:08:20 +01:00
|
|
|
|
2018-06-22 17:21:19 +02:00
|
|
|
Scope &parent() {
|
|
|
|
CHECK(kind_ != Kind::System);
|
|
|
|
return parent_;
|
|
|
|
}
|
2018-03-23 01:08:20 +01:00
|
|
|
const Scope &parent() const {
|
|
|
|
CHECK(kind_ != Kind::System);
|
|
|
|
return parent_;
|
|
|
|
}
|
|
|
|
Kind kind() const { return kind_; }
|
2018-05-14 22:51:13 +02:00
|
|
|
Symbol *symbol() { return symbol_; }
|
2018-04-19 00:06:35 +02:00
|
|
|
const Symbol *symbol() const { return symbol_; }
|
2018-03-23 01:08:20 +01:00
|
|
|
|
2018-05-04 00:57:56 +02:00
|
|
|
const SourceName &name() const {
|
|
|
|
CHECK(symbol_); // must only be called for Scopes known to have a symbol
|
|
|
|
return symbol_->name();
|
|
|
|
}
|
|
|
|
|
2018-03-23 01:08:20 +01:00
|
|
|
/// Make a scope nested in this one
|
2018-05-14 22:51:13 +02:00
|
|
|
Scope &MakeScope(Kind kind, Symbol *symbol = nullptr);
|
2018-03-23 01:08:20 +01:00
|
|
|
|
2018-04-11 22:11:42 +02:00
|
|
|
using size_type = mapType::size_type;
|
|
|
|
using iterator = mapType::iterator;
|
|
|
|
using const_iterator = mapType::const_iterator;
|
2018-03-23 01:08:20 +01:00
|
|
|
|
2018-04-09 19:07:36 +02:00
|
|
|
iterator begin() { return symbols_.begin(); }
|
|
|
|
iterator end() { return symbols_.end(); }
|
|
|
|
const_iterator begin() const { return symbols_.begin(); }
|
|
|
|
const_iterator end() const { return symbols_.end(); }
|
2018-04-09 20:53:20 +02:00
|
|
|
const_iterator cbegin() const { return symbols_.cbegin(); }
|
|
|
|
const_iterator cend() const { return symbols_.cend(); }
|
2018-04-09 19:07:36 +02:00
|
|
|
|
2018-06-22 17:21:19 +02:00
|
|
|
iterator find(const SourceName &name);
|
|
|
|
const_iterator find(const SourceName &name) const;
|
|
|
|
size_type erase(const SourceName &);
|
2018-03-23 01:08:20 +01:00
|
|
|
|
2018-04-09 19:07:36 +02:00
|
|
|
/// Make a Symbol with unknown details.
|
|
|
|
std::pair<iterator, bool> try_emplace(
|
2018-04-11 22:11:42 +02:00
|
|
|
const SourceName &name, Attrs attrs = Attrs()) {
|
2018-04-09 19:07:36 +02:00
|
|
|
return try_emplace(name, attrs, UnknownDetails());
|
|
|
|
}
|
2018-03-23 01:08:20 +01:00
|
|
|
/// Make a Symbol with provided details.
|
2018-03-23 20:24:29 +01:00
|
|
|
template<typename D>
|
2018-04-11 22:11:42 +02:00
|
|
|
std::pair<iterator, bool> try_emplace(const SourceName &name, D &&details) {
|
2018-04-09 19:07:36 +02:00
|
|
|
return try_emplace(name, Attrs(), details);
|
2018-03-23 01:08:20 +01:00
|
|
|
}
|
2018-04-09 19:07:36 +02:00
|
|
|
/// Make a Symbol with attrs and details
|
2018-03-23 01:08:20 +01:00
|
|
|
template<typename D>
|
2018-04-09 19:07:36 +02:00
|
|
|
std::pair<iterator, bool> try_emplace(
|
2018-04-11 22:11:42 +02:00
|
|
|
const SourceName &name, Attrs attrs, D &&details) {
|
2018-06-20 01:06:41 +02:00
|
|
|
Symbol &symbol{MakeSymbol(name, attrs, std::move(details))};
|
|
|
|
return symbols_.insert(std::make_pair(name, &symbol));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Make a Symbol but don't add it to the scope.
|
|
|
|
template<typename D>
|
|
|
|
Symbol &MakeSymbol(const SourceName &name, Attrs attrs, D &&details) {
|
|
|
|
return allSymbols.Make(*this, name, attrs, std::move(details));
|
2018-03-23 20:24:29 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 00:06:35 +02:00
|
|
|
std::list<Scope> &children() { return children_; }
|
|
|
|
const std::list<Scope> &children() const { return children_; }
|
|
|
|
|
2018-06-22 17:21:19 +02:00
|
|
|
DerivedTypeSpec &MakeDerivedTypeSpec(const SourceName &);
|
|
|
|
|
2018-07-26 23:33:41 +02:00
|
|
|
// For modules read from module files, this is the stream of characters
|
|
|
|
// that are referenced by SourceName objects.
|
2018-07-27 17:12:37 +02:00
|
|
|
void set_chars(std::vector<char> chars) {
|
2018-07-26 23:33:41 +02:00
|
|
|
chars_ = std::move(chars);
|
2018-07-25 15:55:11 +02:00
|
|
|
}
|
|
|
|
|
2018-03-23 01:08:20 +01:00
|
|
|
private:
|
2018-06-22 17:21:19 +02:00
|
|
|
Scope &parent_;
|
2018-03-23 01:08:20 +01:00
|
|
|
const Kind kind_;
|
2018-05-14 22:51:13 +02:00
|
|
|
Symbol *const symbol_;
|
2018-03-23 01:08:20 +01:00
|
|
|
std::list<Scope> children_;
|
2018-04-11 22:11:42 +02:00
|
|
|
mapType symbols_;
|
2018-06-22 17:21:19 +02:00
|
|
|
std::list<DerivedTypeSpec> derivedTypeSpecs_;
|
2018-07-27 17:12:37 +02:00
|
|
|
std::vector<char> chars_;
|
2018-03-23 01:08:20 +01:00
|
|
|
|
2018-06-20 01:06:41 +02:00
|
|
|
// Storage for all Symbols. Every Symbol is in allSymbols and every Symbol*
|
|
|
|
// or Symbol& points to one in there.
|
|
|
|
static Symbols<1024> allSymbols;
|
|
|
|
|
2018-03-23 01:08:20 +01:00
|
|
|
friend std::ostream &operator<<(std::ostream &, const Scope &);
|
|
|
|
};
|
|
|
|
|
2018-03-23 20:24:29 +01:00
|
|
|
} // namespace Fortran::semantics
|
2018-03-23 01:08:20 +01:00
|
|
|
#endif // FORTRAN_SEMANTICS_SCOPE_H_
|