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 "symbol.h"
13#include "flang/Parser/parse-tree.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
26namespace Fortran::semantics {
27
28class Scope;
29class SemanticsContext;
30
32public:
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(
41 static std::optional<ProgramTree> Build(
43 static std::optional<ProgramTree> Build(
45 static std::optional<ProgramTree> Build(
47 static std::optional<ProgramTree> Build(
49 static std::optional<ProgramTree> Build(
51 static std::optional<ProgramTree> Build(
53 static std::optional<ProgramTree> Build(
55 static std::optional<ProgramTree> Build(
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
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
117private:
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_
Definition: char-block.h:28
Definition: program-tree.h:31
Definition: scope.h:58
Definition: semantics.h:67
Definition: parse-tree.h:3027
Definition: parse-tree.h:3353
Definition: parse-tree.h:3332
Definition: parse-tree.h:3298
Definition: parse-tree.h:3040
Definition: parse-tree.h:1319
Definition: parse-tree.h:2936
Definition: parse-tree.h:2971
Definition: parse-tree.h:580
Definition: parse-tree.h:4909
Definition: parse-tree.h:2996
Definition: parse-tree.h:564
Definition: parse-tree.h:3324
Definition: parse-tree.h:445
Definition: parse-tree.h:355
Definition: parse-tree.h:3013
Definition: parse-tree.h:3308