| 1 |
//===-- lib/Semantics/program-tree.h ----------------------------*- C++ -*-===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#ifndef FORTRAN_SEMANTICS_PROGRAM_TREE_H_ |
| 10 |
#define FORTRAN_SEMANTICS_PROGRAM_TREE_H_ |
| 11 |
|
| 12 |
#include "flang/Parser/parse-tree.h" |
| 13 |
#include "flang/Semantics/symbol.h" |
| 14 |
#include <list> |
| 15 |
#include <variant> |
| 16 |
|
| 17 |
// A ProgramTree represents a tree of program units and their contained |
| 18 |
// subprograms. The root nodes represent: main program, function, subroutine, |
| 19 |
// module subprogram, module, or submodule. |
| 20 |
// Each node of the tree consists of: |
| 21 |
// - the statement that introduces the program unit |
| 22 |
// - the specification part |
| 23 |
// - the execution part if applicable (not for module or submodule) |
| 24 |
// - a child node for each contained subprogram |
| 25 |
|
| 26 |
namespace Fortran::semantics { |
| 27 |
|
| 28 |
class Scope; |
| 29 |
class SemanticsContext; |
| 30 |
|
| 31 |
class ProgramTree { |
| 32 |
public: |
| 33 |
using EntryStmtList = std::list<common::Reference<const parser::EntryStmt>>; |
| 34 |
using GenericSpecList = |
| 35 |
std::list<common::Reference<const parser::GenericSpec>>; |
| 36 |
|
| 37 |
// Build the ProgramTree rooted at one of these program units. |
| 38 |
static ProgramTree Build(const parser::ProgramUnit &, SemanticsContext &); |
| 39 |
static std::optional<ProgramTree> Build( |
| 40 |
const parser::MainProgram &, SemanticsContext &); |
| 41 |
static std::optional<ProgramTree> Build( |
| 42 |
const parser::FunctionSubprogram &, SemanticsContext &); |
| 43 |
static std::optional<ProgramTree> Build( |
| 44 |
const parser::SubroutineSubprogram &, SemanticsContext &); |
| 45 |
static std::optional<ProgramTree> Build( |
| 46 |
const parser::SeparateModuleSubprogram &, SemanticsContext &); |
| 47 |
static std::optional<ProgramTree> Build( |
| 48 |
const parser::Module &, SemanticsContext &); |
| 49 |
static std::optional<ProgramTree> Build( |
| 50 |
const parser::Submodule &, SemanticsContext &); |
| 51 |
static std::optional<ProgramTree> Build( |
| 52 |
const parser::BlockData &, SemanticsContext &); |
| 53 |
static std::optional<ProgramTree> Build( |
| 54 |
const parser::CompilerDirective &, SemanticsContext &); |
| 55 |
static std::optional<ProgramTree> Build( |
| 56 |
const parser::OpenACCRoutineConstruct &, SemanticsContext &); |
| 57 |
|
| 58 |
ENUM_CLASS(Kind, // kind of node |
| 59 |
Program, Function, Subroutine, MpSubprogram, Module, Submodule, BlockData) |
| 60 |
using Stmt = std::variant< // the statement that introduces the program unit |
| 61 |
const parser::Statement<parser::ProgramStmt> *, |
| 62 |
const parser::Statement<parser::FunctionStmt> *, |
| 63 |
const parser::Statement<parser::SubroutineStmt> *, |
| 64 |
const parser::Statement<parser::MpSubprogramStmt> *, |
| 65 |
const parser::Statement<parser::ModuleStmt> *, |
| 66 |
const parser::Statement<parser::SubmoduleStmt> *, |
| 67 |
const parser::Statement<parser::BlockDataStmt> *>; |
| 68 |
|
| 69 |
ProgramTree(const parser::Name &name, const parser::SpecificationPart &spec, |
| 70 |
const parser::ExecutionPart *exec = nullptr) |
| 71 |
: name_{name}, spec_{spec}, exec_{exec} {} |
| 72 |
|
| 73 |
const parser::Name &name() const { return name_; } |
| 74 |
Kind GetKind() const; |
| 75 |
const Stmt &stmt() const { return stmt_; } |
| 76 |
bool isSpecificationPartResolved() const { |
| 77 |
return isSpecificationPartResolved_; |
| 78 |
} |
| 79 |
void set_isSpecificationPartResolved(bool yes = true) { |
| 80 |
isSpecificationPartResolved_ = yes; |
| 81 |
} |
| 82 |
const parser::ParentIdentifier &GetParentId() const; // only for Submodule |
| 83 |
const parser::SpecificationPart &spec() const { return spec_; } |
| 84 |
const parser::ExecutionPart *exec() const { return exec_; } |
| 85 |
std::list<ProgramTree> &children() { return children_; } |
| 86 |
const std::list<ProgramTree> &children() const { return children_; } |
| 87 |
const EntryStmtList &entryStmts() const { return entryStmts_; } |
| 88 |
const GenericSpecList &genericSpecs() const { return genericSpecs_; } |
| 89 |
|
| 90 |
Symbol::Flag GetSubpFlag() const; |
| 91 |
bool IsModule() const; // Module or Submodule |
| 92 |
bool HasModulePrefix() const; // in function or subroutine stmt |
| 93 |
Scope *scope() const { return scope_; } |
| 94 |
void set_scope(Scope &); |
| 95 |
const parser::LanguageBindingSpec *bindingSpec() const { |
| 96 |
return bindingSpec_; |
| 97 |
} |
| 98 |
ProgramTree &set_bindingSpec(const parser::LanguageBindingSpec *spec) { |
| 99 |
bindingSpec_ = spec; |
| 100 |
return *this; |
| 101 |
} |
| 102 |
void AddChild(ProgramTree &&); |
| 103 |
void AddEntry(const parser::EntryStmt &); |
| 104 |
void AddGeneric(const parser::GenericSpec &); |
| 105 |
|
| 106 |
template <typename T> |
| 107 |
ProgramTree &set_stmt(const parser::Statement<T> &stmt) { |
| 108 |
stmt_ = &stmt; |
| 109 |
return *this; |
| 110 |
} |
| 111 |
template <typename T> |
| 112 |
ProgramTree &set_endStmt(const parser::Statement<T> &stmt) { |
| 113 |
endStmt_ = &stmt.source; |
| 114 |
return *this; |
| 115 |
} |
| 116 |
|
| 117 |
private: |
| 118 |
const parser::Name &name_; |
| 119 |
Stmt stmt_{ |
| 120 |
static_cast<const parser::Statement<parser::ProgramStmt> *>(nullptr)}; |
| 121 |
const parser::SpecificationPart &spec_; |
| 122 |
const parser::ExecutionPart *exec_{nullptr}; |
| 123 |
std::list<ProgramTree> children_; |
| 124 |
EntryStmtList entryStmts_; |
| 125 |
GenericSpecList genericSpecs_; |
| 126 |
Scope *scope_{nullptr}; |
| 127 |
const parser::CharBlock *endStmt_{nullptr}; |
| 128 |
bool isSpecificationPartResolved_{false}; |
| 129 |
const parser::LanguageBindingSpec *bindingSpec_{nullptr}; |
| 130 |
}; |
| 131 |
|
| 132 |
} // namespace Fortran::semantics |
| 133 |
#endif // FORTRAN_SEMANTICS_PROGRAM_TREE_H_ |