|
25 | 25 | #include <cxx/ast_cursor.h> |
26 | 26 | #include <cxx/control.h> |
27 | 27 | #include <cxx/literals.h> |
| 28 | +#include <cxx/memory_layout.h> |
28 | 29 | #include <cxx/mlir/cxx_dialect.h> |
29 | 30 | #include <cxx/names.h> |
30 | 31 | #include <cxx/symbols.h> |
31 | 32 | #include <cxx/translation_unit.h> |
32 | 33 | #include <cxx/types.h> |
| 34 | + |
| 35 | +// |
33 | 36 | #include <mlir/Dialect/ControlFlow/IR/ControlFlowOps.h> |
34 | 37 | #include <mlir/Dialect/Func/IR/FuncOps.h> |
35 | 38 | #include <mlir/Dialect/SCF/IR/SCF.h> |
|
38 | 41 |
|
39 | 42 | namespace cxx { |
40 | 43 |
|
| 44 | +struct Codegen::ConvertType { |
| 45 | + Codegen& gen; |
| 46 | + |
| 47 | + [[nodiscard]] auto control() const { return gen.control(); } |
| 48 | + [[nodiscard]] auto memoryLayout() const { return control()->memoryLayout(); } |
| 49 | + |
| 50 | + auto getExprType() const -> mlir::Type { |
| 51 | + return gen.builder_.getType<mlir::cxx::ExprType>(); |
| 52 | + } |
| 53 | + |
| 54 | + auto getIntType(const Type* type, bool isSigned) -> mlir::Type { |
| 55 | + const auto width = memoryLayout()->sizeOf(type).value() * 8; |
| 56 | + return gen.builder_.getIntegerType(width, isSigned); |
| 57 | + } |
| 58 | + |
| 59 | + auto operator()(const VoidType* type) -> mlir::Type { |
| 60 | + return gen.builder_.getNoneType(); |
| 61 | + } |
| 62 | + |
| 63 | + auto operator()(const NullptrType* type) -> mlir::Type { |
| 64 | + return getExprType(); |
| 65 | + } |
| 66 | + |
| 67 | + auto operator()(const DecltypeAutoType* type) -> mlir::Type { |
| 68 | + return getExprType(); |
| 69 | + } |
| 70 | + |
| 71 | + auto operator()(const AutoType* type) -> mlir::Type { return getExprType(); } |
| 72 | + |
| 73 | + auto operator()(const BoolType* type) -> mlir::Type { |
| 74 | + return gen.builder_.getI1Type(); |
| 75 | + } |
| 76 | + |
| 77 | + auto operator()(const SignedCharType* type) -> mlir::Type { |
| 78 | + return getIntType(type, true); |
| 79 | + } |
| 80 | + |
| 81 | + auto operator()(const ShortIntType* type) -> mlir::Type { |
| 82 | + return getIntType(type, true); |
| 83 | + } |
| 84 | + |
| 85 | + auto operator()(const IntType* type) -> mlir::Type { |
| 86 | + return getIntType(type, true); |
| 87 | + } |
| 88 | + |
| 89 | + auto operator()(const LongIntType* type) -> mlir::Type { |
| 90 | + return getIntType(type, true); |
| 91 | + } |
| 92 | + |
| 93 | + auto operator()(const LongLongIntType* type) -> mlir::Type { |
| 94 | + return getIntType(type, true); |
| 95 | + } |
| 96 | + |
| 97 | + auto operator()(const Int128Type* type) -> mlir::Type { |
| 98 | + return getIntType(type, true); |
| 99 | + } |
| 100 | + |
| 101 | + auto operator()(const UnsignedCharType* type) -> mlir::Type { |
| 102 | + return getIntType(type, false); |
| 103 | + } |
| 104 | + |
| 105 | + auto operator()(const UnsignedShortIntType* type) -> mlir::Type { |
| 106 | + return getIntType(type, false); |
| 107 | + } |
| 108 | + |
| 109 | + auto operator()(const UnsignedIntType* type) -> mlir::Type { |
| 110 | + return getIntType(type, false); |
| 111 | + } |
| 112 | + |
| 113 | + auto operator()(const UnsignedLongIntType* type) -> mlir::Type { |
| 114 | + return getIntType(type, false); |
| 115 | + } |
| 116 | + |
| 117 | + auto operator()(const UnsignedLongLongIntType* type) -> mlir::Type { |
| 118 | + return getIntType(type, false); |
| 119 | + } |
| 120 | + |
| 121 | + auto operator()(const UnsignedInt128Type* type) -> mlir::Type { |
| 122 | + return getIntType(type, false); |
| 123 | + } |
| 124 | + |
| 125 | + auto operator()(const CharType* type) -> mlir::Type { return getExprType(); } |
| 126 | + |
| 127 | + auto operator()(const Char8Type* type) -> mlir::Type { return getExprType(); } |
| 128 | + |
| 129 | + auto operator()(const Char16Type* type) -> mlir::Type { |
| 130 | + return getExprType(); |
| 131 | + } |
| 132 | + |
| 133 | + auto operator()(const Char32Type* type) -> mlir::Type { |
| 134 | + return getExprType(); |
| 135 | + } |
| 136 | + |
| 137 | + auto operator()(const WideCharType* type) -> mlir::Type { |
| 138 | + return getExprType(); |
| 139 | + } |
| 140 | + |
| 141 | + auto operator()(const FloatType* type) -> mlir::Type { |
| 142 | + return gen.builder_.getF32Type(); |
| 143 | + } |
| 144 | + |
| 145 | + auto operator()(const DoubleType* type) -> mlir::Type { |
| 146 | + return gen.builder_.getF64Type(); |
| 147 | + } |
| 148 | + |
| 149 | + auto operator()(const LongDoubleType* type) -> mlir::Type { |
| 150 | + return getExprType(); |
| 151 | + } |
| 152 | + |
| 153 | + auto operator()(const QualType* type) -> mlir::Type { |
| 154 | + return gen.convertType(type->elementType()); |
| 155 | + } |
| 156 | + |
| 157 | + auto operator()(const BoundedArrayType* type) -> mlir::Type { |
| 158 | + return getExprType(); |
| 159 | + } |
| 160 | + |
| 161 | + auto operator()(const UnboundedArrayType* type) -> mlir::Type { |
| 162 | + return getExprType(); |
| 163 | + } |
| 164 | + |
| 165 | + auto operator()(const PointerType* type) -> mlir::Type { |
| 166 | + auto elementType = gen.convertType(type->elementType()); |
| 167 | + return gen.builder_.getType<mlir::cxx::PointerType>(elementType); |
| 168 | + } |
| 169 | + |
| 170 | + auto operator()(const LvalueReferenceType* type) -> mlir::Type { |
| 171 | + return getExprType(); |
| 172 | + } |
| 173 | + |
| 174 | + auto operator()(const RvalueReferenceType* type) -> mlir::Type { |
| 175 | + return getExprType(); |
| 176 | + } |
| 177 | + |
| 178 | + auto operator()(const FunctionType* type) -> mlir::Type { |
| 179 | + return getExprType(); |
| 180 | + } |
| 181 | + |
| 182 | + auto operator()(const ClassType* type) -> mlir::Type { return getExprType(); } |
| 183 | + |
| 184 | + auto operator()(const EnumType* type) -> mlir::Type { return getExprType(); } |
| 185 | + |
| 186 | + auto operator()(const ScopedEnumType* type) -> mlir::Type { |
| 187 | + return getExprType(); |
| 188 | + } |
| 189 | + |
| 190 | + auto operator()(const MemberObjectPointerType* type) -> mlir::Type { |
| 191 | + return getExprType(); |
| 192 | + } |
| 193 | + |
| 194 | + auto operator()(const MemberFunctionPointerType* type) -> mlir::Type { |
| 195 | + return getExprType(); |
| 196 | + } |
| 197 | + |
| 198 | + auto operator()(const NamespaceType* type) -> mlir::Type { |
| 199 | + return getExprType(); |
| 200 | + } |
| 201 | + |
| 202 | + auto operator()(const TypeParameterType* type) -> mlir::Type { |
| 203 | + return getExprType(); |
| 204 | + } |
| 205 | + |
| 206 | + auto operator()(const TemplateTypeParameterType* type) -> mlir::Type { |
| 207 | + return getExprType(); |
| 208 | + } |
| 209 | + |
| 210 | + auto operator()(const UnresolvedNameType* type) -> mlir::Type { |
| 211 | + return getExprType(); |
| 212 | + } |
| 213 | + |
| 214 | + auto operator()(const UnresolvedBoundedArrayType* type) -> mlir::Type { |
| 215 | + return getExprType(); |
| 216 | + } |
| 217 | + |
| 218 | + auto operator()(const UnresolvedUnderlyingType* type) -> mlir::Type { |
| 219 | + return getExprType(); |
| 220 | + } |
| 221 | + |
| 222 | + auto operator()(const OverloadSetType* type) -> mlir::Type { |
| 223 | + return getExprType(); |
| 224 | + } |
| 225 | + |
| 226 | + auto operator()(const BuiltinVaListType* type) -> mlir::Type { |
| 227 | + return getExprType(); |
| 228 | + } |
| 229 | + |
| 230 | + auto operator()(const Type* type) -> mlir::Type { |
| 231 | + return gen.builder_.getType<mlir::cxx::ExprType>(); |
| 232 | + } |
| 233 | +}; |
| 234 | + |
41 | 235 | struct Codegen::UnitVisitor { |
42 | 236 | Codegen& gen; |
43 | 237 |
|
@@ -664,6 +858,10 @@ auto Codegen::emitTodoExpr(SourceLocation location, std::string_view message) |
664 | 858 | return op; |
665 | 859 | } |
666 | 860 |
|
| 861 | +auto Codegen::convertType(const Type* type) -> mlir::Type { |
| 862 | + return visit(ConvertType{*this}, type); |
| 863 | +} |
| 864 | + |
667 | 865 | auto Codegen::operator()(UnitAST* ast) -> UnitResult { |
668 | 866 | if (ast) return visit(UnitVisitor{*this}, ast); |
669 | 867 | return {}; |
@@ -1201,7 +1399,7 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast) |
1201 | 1399 | std::vector<mlir::Type> resultTypes; |
1202 | 1400 |
|
1203 | 1401 | for (auto paramTy : functionType->parameterTypes()) { |
1204 | | - inputTypes.push_back(exprType); |
| 1402 | + inputTypes.push_back(gen.convertType(paramTy)); |
1205 | 1403 | } |
1206 | 1404 |
|
1207 | 1405 | if (!gen.control()->is_void(functionType->returnType())) { |
@@ -1619,10 +1817,15 @@ auto Codegen::ExpressionVisitor::operator()(BoolLiteralExpressionAST* ast) |
1619 | 1817 |
|
1620 | 1818 | auto Codegen::ExpressionVisitor::operator()(IntLiteralExpressionAST* ast) |
1621 | 1819 | -> ExpressionResult { |
| 1820 | + auto op = |
| 1821 | + gen.emitTodoExpr(ast->firstSourceLocation(), to_string(ast->kind())); |
| 1822 | + |
| 1823 | +#if false |
1622 | 1824 | auto loc = gen.getLocation(ast->literalLoc); |
1623 | 1825 |
|
1624 | 1826 | auto op = gen.builder_.create<mlir::cxx::IntLiteralOp>( |
1625 | 1827 | loc, ast->literal->integerValue()); |
| 1828 | +#endif |
1626 | 1829 |
|
1627 | 1830 | return {op}; |
1628 | 1831 | } |
|
0 commit comments