[Target] Use range-based for loops (NFC)
[llvm-project.git] / flang / lib / Semantics / mod-file.h
blob5be117153dd4d1d46288d4694f735dfe44ae7a1e
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 //===----------------------------------------------------------------------===//
9 #ifndef FORTRAN_SEMANTICS_MOD_FILE_H_
10 #define FORTRAN_SEMANTICS_MOD_FILE_H_
12 #include "flang/Semantics/attr.h"
13 #include "flang/Semantics/symbol.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include <string>
17 namespace Fortran::parser {
18 class CharBlock;
19 class Message;
20 class MessageFixedText;
21 } // namespace Fortran::parser
23 namespace llvm {
24 class raw_ostream;
27 namespace Fortran::semantics {
29 using SourceName = parser::CharBlock;
30 class Symbol;
31 class Scope;
32 class SemanticsContext;
34 class ModFileWriter {
35 public:
36 explicit ModFileWriter(SemanticsContext &context) : context_{context} {}
37 bool WriteAll();
39 private:
40 SemanticsContext &context_;
41 // Buffer to use with raw_string_ostream
42 std::string usesBuf_;
43 std::string useExtraAttrsBuf_;
44 std::string declsBuf_;
45 std::string containsBuf_;
46 // Tracks nested DEC structures and fields of that type
47 UnorderedSymbolSet emittedDECStructures_, emittedDECFields_;
49 llvm::raw_string_ostream uses_{usesBuf_};
50 llvm::raw_string_ostream useExtraAttrs_{
51 useExtraAttrsBuf_}; // attrs added to used entity
52 llvm::raw_string_ostream decls_{declsBuf_};
53 llvm::raw_string_ostream contains_{containsBuf_};
54 bool isSubmodule_{false};
55 std::map<const Symbol *, SourceName> renamings_;
57 void WriteAll(const Scope &);
58 void WriteOne(const Scope &);
59 void Write(const Symbol &);
60 std::string GetAsString(const Symbol &);
61 void PrepareRenamings(const Scope &);
62 void PutSymbols(const Scope &);
63 // Returns true if a derived type with bindings and "contains" was emitted
64 bool PutComponents(const Symbol &);
65 void PutSymbol(llvm::raw_ostream &, const Symbol &);
66 void PutEntity(llvm::raw_ostream &, const Symbol &);
67 void PutEntity(
68 llvm::raw_ostream &, const Symbol &, std::function<void()>, Attrs);
69 void PutObjectEntity(llvm::raw_ostream &, const Symbol &);
70 void PutProcEntity(llvm::raw_ostream &, const Symbol &);
71 void PutDerivedType(const Symbol &, const Scope * = nullptr);
72 void PutDECStructure(const Symbol &, const Scope * = nullptr);
73 void PutTypeParam(llvm::raw_ostream &, const Symbol &);
74 void PutSubprogram(const Symbol &);
75 void PutGeneric(const Symbol &);
76 void PutUse(const Symbol &);
77 void PutUseExtraAttr(Attr, const Symbol &, const Symbol &);
78 llvm::raw_ostream &PutAttrs(llvm::raw_ostream &, Attrs,
79 const std::string * = nullptr, bool = false, std::string before = ","s,
80 std::string after = ""s) const;
81 void PutDirective(llvm::raw_ostream &, const Symbol &);
84 class ModFileReader {
85 public:
86 // directories specifies where to search for module files
87 ModFileReader(SemanticsContext &context) : context_{context} {}
88 // Find and read the module file for a module or submodule.
89 // If ancestor is specified, look for a submodule of that module.
90 // Return the Scope for that module/submodule or nullptr on error.
91 Scope *Read(const SourceName &, std::optional<bool> isIntrinsic,
92 Scope *ancestor, bool silent = false);
94 private:
95 SemanticsContext &context_;
97 parser::Message &Say(const SourceName &, const std::string &,
98 parser::MessageFixedText &&, const std::string &);
101 } // namespace Fortran::semantics
102 #endif