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 30 class ProgramTree { 31 public: 32 using EntryStmtList = std::list<common::Reference<const parser::EntryStmt>>; 33 34 // Build the ProgramTree rooted at one of these program units. 35 static ProgramTree Build(const parser::ProgramUnit &); 36 static ProgramTree Build(const parser::MainProgram &); 37 static ProgramTree Build(const parser::FunctionSubprogram &); 38 static ProgramTree Build(const parser::SubroutineSubprogram &); 39 static ProgramTree Build(const parser::SeparateModuleSubprogram &); 40 static ProgramTree Build(const parser::Module &); 41 static ProgramTree Build(const parser::Submodule &); 42 static ProgramTree Build(const parser::BlockData &); 43 static ProgramTree Build(const parser::CompilerDirective &); 44 45 ENUM_CLASS(Kind, // kind of node 46 Program, Function, Subroutine, MpSubprogram, Module, Submodule, BlockData) 47 using Stmt = std::variant< // the statement that introduces the program unit 48 const parser::Statement<parser::ProgramStmt> *, 49 const parser::Statement<parser::FunctionStmt> *, 50 const parser::Statement<parser::SubroutineStmt> *, 51 const parser::Statement<parser::MpSubprogramStmt> *, 52 const parser::Statement<parser::ModuleStmt> *, 53 const parser::Statement<parser::SubmoduleStmt> *, 54 const parser::Statement<parser::BlockDataStmt> *>; 55 56 ProgramTree(const parser::Name &name, const parser::SpecificationPart &spec, 57 const parser::ExecutionPart *exec = nullptr) 58 : name_{name}, spec_{spec}, exec_{exec} {} 59 60 const parser::Name &name() const { return name_; } 61 Kind GetKind() const; 62 const Stmt &stmt() const { return stmt_; } 63 bool isSpecificationPartResolved() const { 64 return isSpecificationPartResolved_; 65 } 66 void set_isSpecificationPartResolved(bool yes = true) { 67 isSpecificationPartResolved_ = yes; 68 } 69 const parser::ParentIdentifier &GetParentId() const; // only for Submodule 70 const parser::SpecificationPart &spec() const { return spec_; } 71 const parser::ExecutionPart *exec() const { return exec_; } 72 std::list<ProgramTree> &children() { return children_; } 73 const std::list<ProgramTree> &children() const { return children_; } 74 const std::list<common::Reference<const parser::EntryStmt>> & 75 entryStmts() const { 76 return entryStmts_; 77 } 78 Symbol::Flag GetSubpFlag() const; 79 bool IsModule() const; // Module or Submodule 80 bool HasModulePrefix() const; // in function or subroutine stmt 81 Scope *scope() const { return scope_; } 82 void set_scope(Scope &); 83 void AddChild(ProgramTree &&); 84 void AddEntry(const parser::EntryStmt &); 85 86 template <typename T> 87 ProgramTree &set_stmt(const parser::Statement<T> &stmt) { 88 stmt_ = &stmt; 89 return *this; 90 } 91 template <typename T> 92 ProgramTree &set_endStmt(const parser::Statement<T> &stmt) { 93 endStmt_ = &stmt.source; 94 return *this; 95 } 96 97 private: 98 const parser::Name &name_; 99 Stmt stmt_{ 100 static_cast<const parser::Statement<parser::ProgramStmt> *>(nullptr)}; 101 const parser::SpecificationPart &spec_; 102 const parser::ExecutionPart *exec_{nullptr}; 103 std::list<ProgramTree> children_; 104 EntryStmtList entryStmts_; 105 Scope *scope_{nullptr}; 106 const parser::CharBlock *endStmt_{nullptr}; 107 bool isSpecificationPartResolved_{false}; 108 }; 109 110 } // namespace Fortran::semantics 111 #endif // FORTRAN_SEMANTICS_PROGRAM_TREE_H_ 112