FLANG

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