FLANG
mod-file.h
1//===-- lib/Semantics/mod-file.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_MOD_FILE_H_
10#define FORTRAN_SEMANTICS_MOD_FILE_H_
11
12#include "flang/Semantics/attr.h"
13#include "flang/Semantics/symbol.h"
14#include "llvm/Support/raw_ostream.h"
15#include <string>
16
17namespace Fortran::parser {
18class CharBlock;
19class Message;
21} // namespace Fortran::parser
22
23namespace llvm {
24class raw_ostream;
25}
26
27namespace Fortran::semantics {
28
29using SourceName = parser::CharBlock;
30class Symbol;
31class Scope;
33
34class ModFileWriter {
35public:
36 explicit ModFileWriter(SemanticsContext &context) : context_{context} {}
37 bool WriteAll();
38 void WriteClosure(llvm::raw_ostream &, const Symbol &,
39 UnorderedSymbolSet &nonIntrinsicModulesWritten);
40 ModFileWriter &set_hermeticModuleFileOutput(bool yes = true) {
41 hermeticModuleFileOutput_ = yes;
42 return *this;
43 }
44
45private:
46 SemanticsContext &context_;
47 // Buffers to use with raw_string_ostream
48 std::string needsBuf_;
49 std::string usesBuf_;
50 std::string useExtraAttrsBuf_;
51 std::string declsBuf_;
52 std::string containsBuf_;
53 // Tracks nested DEC structures and fields of that type
54 UnorderedSymbolSet emittedDECStructures_, emittedDECFields_;
55 UnorderedSymbolSet usedNonIntrinsicModules_;
56 // Modules already re-exported by a plain USE for an operator-less declare
57 // reduction, so the USE is written once even when several such reductions
58 // come from the same module.
59 UnorderedSymbolSet reexportedReductionModules_;
60
61 llvm::raw_string_ostream needs_{needsBuf_};
62 llvm::raw_string_ostream uses_{usesBuf_};
63 llvm::raw_string_ostream useExtraAttrs_{
64 useExtraAttrsBuf_}; // attrs added to used entity
65 llvm::raw_string_ostream decls_{declsBuf_};
66 llvm::raw_string_ostream contains_{containsBuf_};
67 bool isSubmodule_{false};
68 bool hermeticModuleFileOutput_{false};
69
70 void WriteAll(const Scope &);
71 void WriteOne(const Scope &);
72 void Write(const Symbol &);
73 std::string GetAsString(const Symbol &);
74 void PrepareRenamings(const Scope &);
75 void PutSymbols(const Scope &, UnorderedSymbolSet *hermetic);
76 // Returns true if a derived type with bindings and "contains" was emitted
77 bool PutComponents(const Symbol &);
78 void PutSymbol(llvm::raw_ostream &, const Symbol &);
79 void PutEntity(llvm::raw_ostream &, const Symbol &);
80 void PutEntity(
81 llvm::raw_ostream &, const Symbol &, std::function<void()>, Attrs);
82 void PutObjectEntity(llvm::raw_ostream &, const Symbol &);
83 void PutProcEntity(llvm::raw_ostream &, const Symbol &);
84 void PutDerivedType(const Symbol &, const Scope * = nullptr);
85 void PutDECStructure(const Symbol &, const Scope * = nullptr);
86 void PutTypeParam(llvm::raw_ostream &, const Symbol &);
87 void PutUserReduction(llvm::raw_ostream &, const Symbol &);
88 void PutSubprogram(const Symbol &);
89 void PutGeneric(const Symbol &);
90 void PutUse(const Symbol &);
91 void PutUseExtraAttr(Attr, const Symbol &, const Symbol &);
92 llvm::raw_ostream &PutAttrs(llvm::raw_ostream &, Attrs,
93 const std::string * = nullptr, bool = false, std::string before = ","s,
94 std::string after = ""s) const;
95 void PutDirective(llvm::raw_ostream &, const Symbol &);
96};
97
98class ModFileReader {
99public:
100 ModFileReader(SemanticsContext &context) : context_{context} {}
101 // Find and read the module file for a module or submodule.
102 // If ancestor is specified, look for a submodule of that module.
103 // Return the Scope for that module/submodule or nullptr on error.
104 Scope *Read(SourceName, std::optional<bool> isIntrinsic, Scope *ancestor,
105 bool silent);
106
107private:
108 SemanticsContext &context_;
109
110 parser::Message &Say(const char *verb, SourceName, const std::string &,
111 parser::MessageFixedText &&, const std::string &);
112};
113
114} // namespace Fortran::semantics
115#endif
Definition char-block.h:26
Definition message.h:56
Definition message.h:200
Definition scope.h:68
Definition semantics.h:67
Definition symbol.h:893
Definition check-expression.h:19