diff --git a/flang/.gitignore b/flang/.gitignore index 8a614a1aa83c..4c1751ec5a08 100644 --- a/flang/.gitignore +++ b/flang/.gitignore @@ -1,7 +1,7 @@ Debug Release tags -f18 +./f18 *.o *~ CMakeCache.txt diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt index 258511b39110..1d59b87b1019 100644 --- a/flang/CMakeLists.txt +++ b/flang/CMakeLists.txt @@ -9,14 +9,14 @@ set(CMAKE_BUILD_WITH_INSTALL_RPATH true) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++17") set(SOURCES - char-buffer.cc - f2018-demo.cc - idioms.cc - message.cc - parse-tree.cc - position.cc - preprocessor.cc - prescan.cc - source.cc + tools/f18/f18.cc + lib/parser/char-buffer.cc + lib/parser/idioms.cc + lib/parser/message.cc + lib/parser/parse-tree.cc + lib/parser/position.cc + lib/parser/preprocessor.cc + lib/parser/prescan.cc + lib/parser/source.cc ) add_executable(f18 ${SOURCES}) diff --git a/flang/C++style.md b/flang/documentation/C++style.md similarity index 100% rename from flang/C++style.md rename to flang/documentation/C++style.md diff --git a/flang/ParserCombinators.md b/flang/documentation/ParserCombinators.md similarity index 100% rename from flang/ParserCombinators.md rename to flang/documentation/ParserCombinators.md diff --git a/flang/f2018-grammar.txt b/flang/documentation/f2018-grammar.txt similarity index 100% rename from flang/f2018-grammar.txt rename to flang/documentation/f2018-grammar.txt diff --git a/flang/basic-parsers.h b/flang/lib/parser/basic-parsers.h similarity index 99% rename from flang/basic-parsers.h rename to flang/lib/parser/basic-parsers.h index de3263075625..7ac406e59eaa 100644 --- a/flang/basic-parsers.h +++ b/flang/lib/parser/basic-parsers.h @@ -26,6 +26,7 @@ #include namespace Fortran { +namespace parser { // fail("...") returns a parser that never succeeds. It reports an // error message at the current position. The result type is unused, @@ -1316,5 +1317,6 @@ constexpr struct GetPosition { } } getPosition; +} // namespace parser } // namespace Fortran #endif // FORTRAN_BASIC_PARSERS_H_ diff --git a/flang/char-buffer.cc b/flang/lib/parser/char-buffer.cc similarity index 96% rename from flang/char-buffer.cc rename to flang/lib/parser/char-buffer.cc index d6f903be0015..ed553d72f4b1 100644 --- a/flang/char-buffer.cc +++ b/flang/lib/parser/char-buffer.cc @@ -4,6 +4,7 @@ #include namespace Fortran { +namespace parser { char *CharBuffer::FreeSpace(size_t *n) { int offset{LastBlockOffset()}; @@ -45,4 +46,5 @@ void CharBuffer::CopyToContiguous(char *data) { } CHECK(to == data + bytes_); } +} // namespace parser } // namespace Fortran diff --git a/flang/char-buffer.h b/flang/lib/parser/char-buffer.h similarity index 98% rename from flang/char-buffer.h rename to flang/lib/parser/char-buffer.h index 70c5c0ec8b61..ba0c772cf350 100644 --- a/flang/char-buffer.h +++ b/flang/lib/parser/char-buffer.h @@ -10,6 +10,7 @@ #include namespace Fortran { +namespace parser { class CharBuffer { public: @@ -113,5 +114,6 @@ private: size_t bytes_{0}; bool lastBlockEmpty_{false}; }; +} // namespace parser } // namespace Fortran #endif // FORTRAN_CHAR_BUFFER_H_ diff --git a/flang/char-parsers.h b/flang/lib/parser/char-parsers.h similarity index 98% rename from flang/char-parsers.h rename to flang/lib/parser/char-parsers.h index c5cbdf55f9c0..cb5b6d58461a 100644 --- a/flang/char-parsers.h +++ b/flang/lib/parser/char-parsers.h @@ -9,6 +9,7 @@ #include namespace Fortran { +namespace parser { template struct ExactRaw { using resultType = char; @@ -107,5 +108,6 @@ private: constexpr RawStringMatch operator""_raw(const char str[], size_t n) { return RawStringMatch{str, n}; } +} // namespace parser } // namespace Fortran #endif // FORTRAN_CHAR_PARSERS_H_ diff --git a/flang/cooked-chars.h b/flang/lib/parser/cooked-chars.h similarity index 99% rename from flang/cooked-chars.h rename to flang/lib/parser/cooked-chars.h index 8dedf608382b..0a4ef359ae00 100644 --- a/flang/cooked-chars.h +++ b/flang/lib/parser/cooked-chars.h @@ -18,6 +18,7 @@ #include namespace Fortran { +namespace parser { constexpr struct FixedFormPadding { using resultType = char; @@ -210,5 +211,6 @@ static inline bool ConsumedAllInput(const ParseState &state) { } constexpr StatePredicateGuardParser consumedAllInput{ConsumedAllInput}; +} // namespace parser } // namespace Fortran #endif // FORTRAN_COOKED_CHARS_H_ diff --git a/flang/cooked-tokens.h b/flang/lib/parser/cooked-tokens.h similarity index 99% rename from flang/cooked-tokens.h rename to flang/lib/parser/cooked-tokens.h index 11b5d926d290..3c89b7d59745 100644 --- a/flang/cooked-tokens.h +++ b/flang/lib/parser/cooked-tokens.h @@ -18,6 +18,7 @@ #include namespace Fortran { +namespace parser { class CharPredicateGuardParser { public: @@ -430,5 +431,6 @@ template inline constexpr auto optionalBeforeColons(const PA &p) { return "," >> p / "::" || "::" >> construct{} || !","_tok >> construct{}; } +} // namespace parser } // namespace Fortran #endif // FORTRAN_COOKED_TOKENS_H_ diff --git a/flang/debug-parser.h b/flang/lib/parser/debug-parser.h similarity index 96% rename from flang/debug-parser.h rename to flang/lib/parser/debug-parser.h index b395f5bd6d65..769f7f75d750 100644 --- a/flang/debug-parser.h +++ b/flang/lib/parser/debug-parser.h @@ -12,6 +12,7 @@ #include namespace Fortran { +namespace parser { class DebugParser { public: @@ -34,5 +35,6 @@ private: constexpr DebugParser operator""_debug(const char str[], size_t n) { return DebugParser{str, n}; } +} // namespace parser } // namespace Fortran #endif // FORTRAN_DEBUG_PARSER_H_ diff --git a/flang/format-specification.h b/flang/lib/parser/format-specification.h similarity index 100% rename from flang/format-specification.h rename to flang/lib/parser/format-specification.h diff --git a/flang/grammar.h b/flang/lib/parser/grammar.h similarity index 99% rename from flang/grammar.h rename to flang/lib/parser/grammar.h index 928a79cadaca..634bbbf69993 100644 --- a/flang/grammar.h +++ b/flang/lib/parser/grammar.h @@ -24,6 +24,7 @@ #include namespace Fortran { +namespace parser { // The productions that follow are derived from the draft Fortran 2018 // standard, with some necessary modifications to remove left recursion @@ -3643,5 +3644,6 @@ TYPE_CONTEXT_PARSER("PAUSE statement", // is used only via scalar-int-variable // R1030 default-char-constant-expr -> default-char-expr // is only used via scalar-default-char-constant-expr +} // namespace parser } // namespace Fortran #endif // FORTRAN_GRAMMAR_H_ diff --git a/flang/idioms.cc b/flang/lib/parser/idioms.cc similarity index 91% rename from flang/idioms.cc rename to flang/lib/parser/idioms.cc index cfdda0382cd0..7ffa1524ea5f 100644 --- a/flang/idioms.cc +++ b/flang/lib/parser/idioms.cc @@ -4,6 +4,7 @@ #include namespace Fortran { +namespace parser { [[noreturn]] void die(const char *msg, ...) { va_list ap; @@ -18,4 +19,5 @@ namespace Fortran { std::ostream &operator<<(std::ostream &o, const std::monostate &) { return o << "(empty variant)"; } +} // namespace parser } // namespace Fortran diff --git a/flang/idioms.h b/flang/lib/parser/idioms.h similarity index 97% rename from flang/idioms.h rename to flang/lib/parser/idioms.h index 39c77e0ffc80..34a71cefc299 100644 --- a/flang/idioms.h +++ b/flang/lib/parser/idioms.h @@ -34,6 +34,7 @@ struct is_trivially_copy_constructible>> : false_type {}; using namespace std::string_literals; // enable "this is a std::string"s namespace Fortran { +namespace parser { // Helper templates for combining a list of lambdas into an anonymous // struct for use with std::visit() on a std::variant<> sum type. @@ -57,6 +58,7 @@ template visitors(LAMBDAS... x)->visitors; template bool operator!(const std::optional &x) { return !x.has_value(); } +} // namespace parser } // namespace Fortran // For switch statements without default: labels. @@ -73,6 +75,7 @@ template struct BadType : std::false_type {}; // Formatting namespace Fortran { +namespace parser { template std::ostream &operator<<(std::ostream &o, const std::optional &x) { if (x.has_value()) { @@ -116,5 +119,6 @@ std::ostream &operator<<(std::ostream &o, const std::variant &x) { return std::visit( [&o](const auto &y) -> std::ostream & { return o << y; }, x); } +} // namespace parser } // namespace Fortran #endif // FORTRAN_IDIOMS_H_ diff --git a/flang/indirection.h b/flang/lib/parser/indirection.h similarity index 97% rename from flang/indirection.h rename to flang/lib/parser/indirection.h index 34987fd1e169..707b10501218 100644 --- a/flang/indirection.h +++ b/flang/lib/parser/indirection.h @@ -11,6 +11,7 @@ #include namespace Fortran { +namespace parser { template class Indirection { public: @@ -54,5 +55,6 @@ template std::ostream &operator<<(std::ostream &o, const Indirection &x) { return o << *x; } +} // namespace parser } // namespace Fortran #endif // FORTRAN_INDIRECTION_H_ diff --git a/flang/message.cc b/flang/lib/parser/message.cc similarity index 92% rename from flang/message.cc rename to flang/lib/parser/message.cc index 55cdbf4ad524..d73752087b7b 100644 --- a/flang/message.cc +++ b/flang/lib/parser/message.cc @@ -1,6 +1,7 @@ #include "message.h" namespace Fortran { +namespace parser { std::ostream &operator<<(std::ostream &o, const Message &msg) { if (msg.context()) { @@ -21,4 +22,5 @@ std::ostream &operator<<(std::ostream &o, const Messages &ms) { } return o; } +} // namespace parser } // namespace Fortran diff --git a/flang/message.h b/flang/lib/parser/message.h similarity index 98% rename from flang/message.h rename to flang/lib/parser/message.h index 4456e97da025..76718673be17 100644 --- a/flang/message.h +++ b/flang/lib/parser/message.h @@ -13,6 +13,7 @@ #include namespace Fortran { +namespace parser { class Message; using MessageContext = std::shared_ptr; @@ -103,5 +104,6 @@ private: std::ostream &operator<<(std::ostream &, const Message &); std::ostream &operator<<(std::ostream &, const Messages &); +} // namespace parser } // namespace Fortran #endif // FORTRAN_MESSAGE_H_ diff --git a/flang/parse-state.h b/flang/lib/parser/parse-state.h similarity index 99% rename from flang/parse-state.h rename to flang/lib/parser/parse-state.h index 88e5735d34db..e07f802e6992 100644 --- a/flang/parse-state.h +++ b/flang/lib/parser/parse-state.h @@ -17,6 +17,7 @@ #include namespace Fortran { +namespace parser { class UserState; @@ -228,5 +229,6 @@ private: // reflected in the copy and move constructors defined at the top of this // class definition! }; +} // namespace parser } // namespace Fortran #endif // FORTRAN_PARSE_STATE_H_ diff --git a/flang/parse-tree.cc b/flang/lib/parser/parse-tree.cc similarity index 99% rename from flang/parse-tree.cc rename to flang/lib/parser/parse-tree.cc index a4d84eeed331..4497a622690c 100644 --- a/flang/parse-tree.cc +++ b/flang/lib/parser/parse-tree.cc @@ -4,6 +4,7 @@ #include namespace Fortran { +namespace parser { #define UNION_FORMATTER(TYPE) \ std::ostream &operator<<(std::ostream &o, const TYPE &x) { \ @@ -1085,4 +1086,5 @@ std::ostream &operator<<(std::ostream &o, const ProcedureStmt &x) { std::ostream &operator<<(std::ostream &o, const Suffix &x) { return o << "(Suffix " << x.binding << ' ' << x.resultName << ')'; } +} // namespace parser } // namespace Fortran diff --git a/flang/parse-tree.h b/flang/lib/parser/parse-tree.h similarity index 99% rename from flang/parse-tree.h rename to flang/lib/parser/parse-tree.h index 85b4e894ed09..34cecb5d0c57 100644 --- a/flang/parse-tree.h +++ b/flang/lib/parser/parse-tree.h @@ -83,6 +83,7 @@ } namespace Fortran { +namespace parser { // These are the unavoidable recursively-defined productions of Fortran. // Some references to the representations of their parses require @@ -3222,5 +3223,6 @@ std::ostream &operator<<(std::ostream &o, const LoopBounds &x) { return o << "(LoopBounds " << x.name << ' ' << x.lower << ' ' << x.upper << ' ' << x.step << ')'; } +} // namespace parser } // namespace Fortran #endif // FORTRAN_PARSE_TREE_H_ diff --git a/flang/position.cc b/flang/lib/parser/position.cc similarity index 83% rename from flang/position.cc rename to flang/lib/parser/position.cc index b398225b7131..beeef3cf2d5a 100644 --- a/flang/position.cc +++ b/flang/lib/parser/position.cc @@ -1,7 +1,9 @@ #include "position.h" namespace Fortran { +namespace parser { std::ostream &operator<<(std::ostream &o, const Position &x) { return o << "(at line " << x.lineNumber() << ", column " << x.column() << ')'; } +} // namespace parser } // namespace Fortran diff --git a/flang/position.h b/flang/lib/parser/position.h similarity index 97% rename from flang/position.h rename to flang/lib/parser/position.h index 17a24581e001..943ee57ad689 100644 --- a/flang/position.h +++ b/flang/lib/parser/position.h @@ -8,6 +8,7 @@ #include namespace Fortran { +namespace parser { class Position { public: @@ -70,5 +71,6 @@ private: }; std::ostream &operator<<(std::ostream &, const Position &); +} // namespace parser } // namespace Fortran #endif // FORTRAN_POSITION_H_ diff --git a/flang/preprocessor.cc b/flang/lib/parser/preprocessor.cc similarity index 99% rename from flang/preprocessor.cc rename to flang/lib/parser/preprocessor.cc index c5c0c77eeca6..3481a3fed1ef 100644 --- a/flang/preprocessor.cc +++ b/flang/lib/parser/preprocessor.cc @@ -13,6 +13,7 @@ #include namespace Fortran { +namespace parser { bool CharPointerWithLength::IsBlank() const { for (size_t j{0}; j < bytes_; ++j) { @@ -937,4 +938,5 @@ bool Preprocessor::IsIfPredicateTrue( } return result; } +} // namespace parser } // namespace Fortran diff --git a/flang/preprocessor.h b/flang/lib/parser/preprocessor.h similarity index 93% rename from flang/preprocessor.h rename to flang/lib/parser/preprocessor.h index 165f935a6e04..2896a8526cbc 100644 --- a/flang/preprocessor.h +++ b/flang/lib/parser/preprocessor.h @@ -19,6 +19,7 @@ #include namespace Fortran { +namespace parser { class CharBuffer; class Prescanner; @@ -50,11 +51,12 @@ private: const char *data_{nullptr}; size_t bytes_{0}; }; +} // namespace parser } // namespace Fortran // Specializations to enable std::unordered_map -template<> struct std::hash { - size_t operator()(const Fortran::CharPointerWithLength &x) const { +template<> struct std::hash { + size_t operator()(const Fortran::parser::CharPointerWithLength &x) const { size_t hash{0}, bytes{x.size()}; for (size_t j{0}; j < bytes; ++j) { hash = (hash * 31) ^ x[j]; @@ -63,9 +65,9 @@ template<> struct std::hash { } }; -template<> struct std::equal_to { - bool operator()(const Fortran::CharPointerWithLength &x, - const Fortran::CharPointerWithLength &y) const { +template<> struct std::equal_to { + bool operator()(const Fortran::parser::CharPointerWithLength &x, + const Fortran::parser::CharPointerWithLength &y) const { return x.size() == y.size() && std::memcmp(static_cast(&x[0]), static_cast(&y[0]), x.size()) == 0; @@ -73,6 +75,7 @@ template<> struct std::equal_to { }; namespace Fortran { +namespace parser { // Buffers a contiguous sequence of characters that has been partitioned into // a sequence of preprocessing tokens. @@ -199,5 +202,6 @@ private: std::unordered_map definitions_; std::stack ifStack_; }; +} // namespace parser } // namespace Fortran #endif // FORTRAN_PREPROCESSOR_H_ diff --git a/flang/prescan.cc b/flang/lib/parser/prescan.cc similarity index 99% rename from flang/prescan.cc rename to flang/lib/parser/prescan.cc index 7b84472e471a..efe4a58de38e 100644 --- a/flang/prescan.cc +++ b/flang/lib/parser/prescan.cc @@ -8,6 +8,7 @@ #include namespace Fortran { +namespace parser { CharBuffer Prescanner::Prescan(const SourceFile &source) { sourceFile_ = &source; @@ -490,4 +491,5 @@ void Prescanner::PayNewlineDebt(CharBuffer *out) { out->Put('\n'); } } +} // namespace parser } // namespace Fortran diff --git a/flang/prescan.h b/flang/lib/parser/prescan.h similarity index 98% rename from flang/prescan.h rename to flang/lib/parser/prescan.h index b28737cfba9c..aac809832c0c 100644 --- a/flang/prescan.h +++ b/flang/lib/parser/prescan.h @@ -20,6 +20,7 @@ #include namespace Fortran { +namespace parser { class Prescanner { public: @@ -109,5 +110,6 @@ private: int delimiterNesting_{0}; Preprocessor preprocessor_; }; +} // namespace parser } // namespace Fortran #endif // FORTRAN_PRESCAN_H_ diff --git a/flang/source.cc b/flang/lib/parser/source.cc similarity index 98% rename from flang/source.cc rename to flang/lib/parser/source.cc index 4850155ce11a..635f44a28f42 100644 --- a/flang/source.cc +++ b/flang/lib/parser/source.cc @@ -14,6 +14,7 @@ // TODO: Port to Windows &c. namespace Fortran { +namespace parser { SourceFile::~SourceFile() { Close(); } @@ -122,4 +123,5 @@ void SourceFile::Close() { } path_.clear(); } +} // namespace parser } // namespace Fortran diff --git a/flang/source.h b/flang/lib/parser/source.h similarity index 95% rename from flang/source.h rename to flang/lib/parser/source.h index e6d575497b1d..c26bd8c53f4c 100644 --- a/flang/source.h +++ b/flang/lib/parser/source.h @@ -9,6 +9,7 @@ #include namespace Fortran { +namespace parser { class SourceFile { public: @@ -27,5 +28,6 @@ private: const char *content_{nullptr}; size_t bytes_{0}; }; +} // namespace parser } // namespace Fortran #endif // FORTRAN_SOURCE_H_ diff --git a/flang/user-state.h b/flang/lib/parser/user-state.h similarity index 94% rename from flang/user-state.h rename to flang/lib/parser/user-state.h index b1867842c493..972d432b32a9 100644 --- a/flang/user-state.h +++ b/flang/lib/parser/user-state.h @@ -10,6 +10,7 @@ #include namespace Fortran { +namespace parser { class UserState { public: using Label = std::uint64_t; @@ -35,5 +36,6 @@ private: std::unordered_set