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 <string>
14 
15 namespace Fortran::parser {
16 class CharBlock;
17 class Message;
18 class MessageFixedText;
19 } // namespace Fortran::parser
20 
21 namespace llvm {
22 class raw_ostream;
23 }
24 
25 namespace Fortran::semantics {
26 
27 using SourceName = parser::CharBlock;
28 class Symbol;
29 class Scope;
30 class SemanticsContext;
31 
32 class ModFileWriter {
33 public:
34   ModFileWriter(SemanticsContext &context) : context_{context} {}
35   bool WriteAll();
36 
37 private:
38   SemanticsContext &context_;
39   // Buffer to use with raw_string_ostream
40   std::string usesBuf_;
41   std::string useExtraAttrsBuf_;
42   std::string declsBuf_;
43   std::string containsBuf_;
44 
45   llvm::raw_string_ostream uses_{usesBuf_};
46   llvm::raw_string_ostream useExtraAttrs_{
47       useExtraAttrsBuf_}; // attrs added to used entity
48   llvm::raw_string_ostream decls_{declsBuf_};
49   llvm::raw_string_ostream contains_{containsBuf_};
50 
51   void WriteAll(const Scope &);
52   void WriteOne(const Scope &);
53   void Write(const Symbol &);
54   std::string GetAsString(const Symbol &);
55   void PutSymbols(const Scope &);
56   void PutSymbol(llvm::raw_ostream &, const Symbol &);
57   void PutDerivedType(const Symbol &);
58   void PutSubprogram(const Symbol &);
59   void PutGeneric(const Symbol &);
60   void PutUse(const Symbol &);
61   void PutUseExtraAttr(Attr, const Symbol &, const Symbol &);
62 };
63 
64 class ModFileReader {
65 public:
66   // directories specifies where to search for module files
67   ModFileReader(SemanticsContext &context) : context_{context} {}
68   // Find and read the module file for a module or submodule.
69   // If ancestor is specified, look for a submodule of that module.
70   // Return the Scope for that module/submodule or nullptr on error.
71   Scope *Read(const SourceName &, Scope *ancestor = nullptr);
72 
73 private:
74   SemanticsContext &context_;
75 
76   parser::Message &Say(const SourceName &, const std::string &,
77       parser::MessageFixedText &&, const std::string &);
78 };
79 
80 } // namespace Fortran::semantics
81 #endif
82