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-08-23 20:45:49 +02:00
|
|
|
#include "../common/fortran.h"
|
2018-06-18 20:03:43 +02:00
|
|
|
#include "../common/idioms.h"
|
2018-08-23 01:05:06 +02:00
|
|
|
#include "../parser/message.h"
|
2018-03-23 01:08:20 +01:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
2018-08-23 01:05:06 +02:00
|
|
|
#include <set>
|
2018-03-23 01:08:20 +01:00
|
|
|
#include <string>
|
|
|
|
|
2018-03-23 20:24:29 +01:00
|
|
|
namespace Fortran::semantics {
|
2018-03-23 01:08:20 +01:00
|
|
|
|
2018-08-23 01:05:06 +02:00
|
|
|
using namespace parser::literals;
|
|
|
|
|
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:
|
2018-12-11 23:03:55 +01:00
|
|
|
ENUM_CLASS(Kind, System, Global, Module, MainProgram, Subprogram, DerivedType,
|
|
|
|
Block, Forall)
|
2018-08-23 20:45:49 +02:00
|
|
|
using ImportKind = common::ImportKind;
|
2018-03-23 01:08:20 +01:00
|
|
|
|
2018-09-15 00:04:50 +02:00
|
|
|
// Create the Global scope -- the root of the scope tree
|
|
|
|
Scope() : Scope{*this, Kind::Global, nullptr} {}
|
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-08-23 01:05:06 +02:00
|
|
|
bool operator==(const Scope &that) const { return this == &that; }
|
|
|
|
bool operator!=(const Scope &that) const { return this != &that; }
|
|
|
|
|
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-09-07 18:06:27 +02:00
|
|
|
bool IsModule() const; // only module, not submodule
|
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-09-08 00:39:20 +02:00
|
|
|
size_type size() const { return symbols_.size(); }
|
2018-03-23 01:08:20 +01:00
|
|
|
|
2018-08-23 01:05:06 +02:00
|
|
|
// Look for symbol by name in this scope and host (depending on imports).
|
2018-11-16 21:43:08 +01:00
|
|
|
Symbol *FindSymbol(const SourceName &) const;
|
2018-08-23 01:05:06 +02: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))};
|
2018-08-03 01:21:27 +02:00
|
|
|
return symbols_.emplace(name, &symbol);
|
2018-06-20 01:06:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-08-03 01:21:27 +02:00
|
|
|
// For Module scope, maintain a mapping of all submodule scopes with this
|
2018-08-03 20:32:21 +02:00
|
|
|
// module as its ancestor module. AddSubmodule returns false if already there.
|
2018-08-03 01:21:27 +02:00
|
|
|
Scope *FindSubmodule(const SourceName &) const;
|
2018-08-23 20:24:12 +02:00
|
|
|
bool AddSubmodule(const SourceName &, Scope &);
|
2018-08-03 01:21:27 +02:00
|
|
|
|
2018-12-17 21:41:43 +01:00
|
|
|
DeclTypeSpec &MakeNumericType(TypeCategory, int kind);
|
|
|
|
DeclTypeSpec &MakeLogicalType(int kind);
|
|
|
|
DeclTypeSpec &MakeCharacterType(ParamValue &&length, int kind = 0);
|
2019-01-08 00:05:53 +01:00
|
|
|
DeclTypeSpec &MakeDerivedType(const Symbol &);
|
2018-12-17 21:41:43 +01:00
|
|
|
DeclTypeSpec &MakeTypeStarType();
|
|
|
|
DeclTypeSpec &MakeClassStarType();
|
2018-06-22 17:21:19 +02:00
|
|
|
|
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-08-03 01:21:27 +02:00
|
|
|
void set_chars(std::string &&chars) { chars_ = std::move(chars); }
|
2018-07-25 15:55:11 +02:00
|
|
|
|
2018-08-23 20:45:49 +02:00
|
|
|
ImportKind GetImportKind() const;
|
2018-08-23 01:05:06 +02:00
|
|
|
// Names appearing in IMPORT statements in this scope
|
|
|
|
std::set<SourceName> importNames() const { return importNames_; }
|
|
|
|
|
|
|
|
// Set the kind of imports from host into this scope.
|
|
|
|
// Return an error message for incompatible kinds.
|
2018-08-23 20:24:12 +02:00
|
|
|
std::optional<parser::MessageFixedText> SetImportKind(ImportKind);
|
2018-08-23 01:05:06 +02:00
|
|
|
|
2018-11-16 21:43:08 +01:00
|
|
|
void add_importName(const SourceName &);
|
2018-08-23 01:05:06 +02:00
|
|
|
|
2018-11-29 00:55:55 +01:00
|
|
|
// The range of the source of this and nested scopes.
|
|
|
|
const parser::CharBlock &sourceRange() const { return sourceRange_; }
|
|
|
|
void AddSourceRange(const parser::CharBlock &);
|
|
|
|
// Find the smallest scope under this one that contains source
|
|
|
|
const Scope *FindScope(const parser::CharBlock &) const;
|
|
|
|
|
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-11-29 00:55:55 +01:00
|
|
|
parser::CharBlock sourceRange_;
|
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-08-03 01:21:27 +02:00
|
|
|
std::map<SourceName, Scope *> submodules_;
|
2018-12-11 23:51:08 +01:00
|
|
|
std::list<DeclTypeSpec> declTypeSpecs_;
|
2018-12-14 23:04:15 +01:00
|
|
|
std::list<CharacterTypeSpec> characterTypeSpecs_;
|
2018-06-22 17:21:19 +02:00
|
|
|
std::list<DerivedTypeSpec> derivedTypeSpecs_;
|
2018-07-27 20:44:31 +02:00
|
|
|
std::string chars_;
|
2018-08-23 01:05:06 +02:00
|
|
|
std::optional<ImportKind> importKind_;
|
|
|
|
std::set<SourceName> importNames_;
|
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-08-23 01:05:06 +02:00
|
|
|
bool CanImport(const SourceName &) const;
|
2018-12-17 21:41:43 +01:00
|
|
|
DeclTypeSpec &MakeLengthlessType(const DeclTypeSpec &);
|
2018-08-23 01:05:06 +02:00
|
|
|
|
2018-03-23 01:08:20 +01:00
|
|
|
friend std::ostream &operator<<(std::ostream &, const Scope &);
|
|
|
|
};
|
2018-10-25 14:55:23 +02:00
|
|
|
}
|
2018-03-23 01:08:20 +01:00
|
|
|
#endif // FORTRAN_SEMANTICS_SCOPE_H_
|