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