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