output_segment.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_MIGRATE_CPP_OUTPUT_SEGMENT_H_
  5. #define CARBON_MIGRATE_CPP_OUTPUT_SEGMENT_H_
  6. #include <string>
  7. #include <type_traits>
  8. #include <utility>
  9. #include <variant>
  10. #include "clang/AST/ASTTypeTraits.h"
  11. #include "common/check.h"
  12. namespace Carbon {
  13. namespace Internal {
  14. // Checks that the type T is an acceptable node type from which an
  15. // OutputSegment can be constructed. We intentionally do not want to support
  16. // `clang::Type` because we support traversing through `clang::TypeLoc`
  17. // instead. However, most other types we intend to support as they become
  18. // necessary.
  19. template <typename T>
  20. concept IsSupportedClangASTNodeType = std::convertible_to<T*, clang::Stmt*> ||
  21. std::convertible_to<T*, clang::Decl*>;
  22. } // namespace Internal
  23. // Represents a segment of the output string. `OutputSegment`s come in two
  24. // flavors: Text and Node. A text segment holds string text that should be used
  25. // to be added to the output. A node segment holds a node in Clang's AST and
  26. // indicates that the output associated to that node should be the output
  27. // segment that the `RewriteBuilder` (defined below) has attached to that AST
  28. // node.
  29. //
  30. // For example, the output for a binary operator node corresponding to the C++
  31. // code snippet `f() + 3 * 5`, would be the sequence of three output segments:
  32. //
  33. // {Node(lhs), Text(" + "), Node(rhs)}
  34. //
  35. // The left-hand side and right-hand side can then be queried recursively to
  36. // determine what their output should be.
  37. class OutputSegment {
  38. public:
  39. // Creates a text-based `OutputSegment`.
  40. explicit OutputSegment(std::string content) : content_(std::move(content)) {}
  41. explicit OutputSegment(llvm::StringRef content) : content_(content.str()) {}
  42. explicit OutputSegment(const char* content) : content_(content) {}
  43. // Creates a node-based `OutputSegment` from `node`.
  44. explicit OutputSegment(const clang::DynTypedNode& node) : content_(node) {}
  45. template <typename T>
  46. requires Internal::IsSupportedClangASTNodeType<T>
  47. explicit OutputSegment(const T* node);
  48. // Creates a TypeLoc-based `OutputSegment` from `type_loc`.
  49. explicit OutputSegment(clang::TypeLoc type_loc)
  50. : content_(PassThroughQualifiedTypeLoc(type_loc)) {}
  51. private:
  52. friend struct OutputWriter;
  53. template <typename T>
  54. auto AssertNotNull(T* ptr) -> T& {
  55. CARBON_CHECK(ptr != nullptr);
  56. return *ptr;
  57. }
  58. // Traversals for TypeLocs have some sharp corners. In particular,
  59. // QualifiedTypeLocs are silently passed through to their unqualified part.
  60. // This means that when constructing output segments we also need to match
  61. // this behavior.
  62. static auto PassThroughQualifiedTypeLoc(clang::TypeLoc type_loc)
  63. -> clang::TypeLoc {
  64. auto qtl = type_loc.getAs<clang::QualifiedTypeLoc>();
  65. return qtl.isNull() ? type_loc : qtl.getUnqualifiedLoc();
  66. }
  67. std::variant<std::string, clang::DynTypedNode, clang::TypeLoc> content_;
  68. };
  69. template <typename T>
  70. requires Internal::IsSupportedClangASTNodeType<T>
  71. OutputSegment::OutputSegment(const T* node)
  72. : content_(clang::DynTypedNode::create(AssertNotNull(node))) {}
  73. } // namespace Carbon
  74. #endif // CARBON_MIGRATE_CPP_OUTPUT_SEGMENT_H_