constant.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #include "toolchain/check/cpp/constant.h"
  5. #include "toolchain/check/cpp/import.h"
  6. #include "toolchain/check/cpp/location.h"
  7. #include "toolchain/check/cpp/type_mapping.h"
  8. #include "toolchain/check/eval.h"
  9. #include "toolchain/check/member_access.h"
  10. #include "toolchain/check/type.h"
  11. #include "toolchain/check/type_completion.h"
  12. #include "toolchain/diagnostics/format_providers.h"
  13. namespace Carbon::Check {
  14. static auto MapLValueToConstant(Context& context, SemIR::LocId loc_id,
  15. const clang::APValue& ap_value,
  16. clang::QualType type) -> SemIR::ConstantId {
  17. CARBON_CHECK(ap_value.isLValue(), "not an LValue");
  18. const auto* value_decl =
  19. ap_value.getLValueBase().get<const clang::ValueDecl*>();
  20. if (!ap_value.hasLValuePath()) {
  21. context.TODO(loc_id, "lvalue has no path");
  22. return SemIR::ErrorInst::ConstantId;
  23. }
  24. if (ap_value.isLValueOnePastTheEnd()) {
  25. context.TODO(loc_id, "one-past-the-end lvalue");
  26. return SemIR::ErrorInst::ConstantId;
  27. }
  28. auto key = SemIR::ClangDeclKey::ForNonFunctionDecl(
  29. // TODO: can this const_cast be avoided?
  30. const_cast<clang::ValueDecl*>(value_decl));
  31. auto inst_id = ImportCppDecl(context, loc_id, key);
  32. if (ap_value.getLValuePath().empty()) {
  33. return context.constant_values().Get(inst_id);
  34. }
  35. // Import the base type so that its fields can be accessed.
  36. auto var_storage = context.insts().GetAs<SemIR::VarStorage>(inst_id);
  37. // TODO: currently an error isn't reachable here because incomplete
  38. // array types can't be imported. Once that changes, switch to
  39. // `RequireCompleteType` and handle the error.
  40. CompleteTypeOrCheckFail(context, var_storage.type_id);
  41. clang::QualType qual_type = ap_value.getLValueBase().getType();
  42. for (const auto& entry : ap_value.getLValuePath()) {
  43. if (qual_type->isArrayType()) {
  44. context.TODO(loc_id, "lvalue path contains an array type");
  45. } else {
  46. const auto* decl =
  47. cast<clang::Decl>(entry.getAsBaseOrMember().getPointer());
  48. const auto* field_decl = dyn_cast<clang::FieldDecl>(decl);
  49. if (!field_decl) {
  50. context.TODO(loc_id, "lvalue path contains a base class subobject");
  51. return SemIR::ErrorInst::ConstantId;
  52. }
  53. auto field_inst_id =
  54. ImportCppDecl(context, loc_id,
  55. SemIR::ClangDeclKey::ForNonFunctionDecl(
  56. const_cast<clang::FieldDecl*>(field_decl)));
  57. if (field_inst_id == SemIR::ErrorInst::InstId) {
  58. context.TODO(loc_id,
  59. "unsupported field in lvalue path: " +
  60. ap_value.getAsString(context.ast_context(), type));
  61. return SemIR::ErrorInst::ConstantId;
  62. }
  63. const SemIR::FieldDecl& field_decl_inst =
  64. context.insts().GetAs<SemIR::FieldDecl>(field_inst_id);
  65. qual_type = field_decl->getType();
  66. inst_id = PerformMemberAccess(context, loc_id, inst_id,
  67. field_decl_inst.name_id);
  68. }
  69. }
  70. return context.constant_values().Get(inst_id);
  71. }
  72. auto MapAPValueToConstant(Context& context, SemIR::LocId loc_id,
  73. const clang::APValue& ap_value, clang::QualType type,
  74. bool is_lvalue) -> SemIR::ConstantId {
  75. SemIR::TypeId type_id = ImportCppType(context, loc_id, type).type_id;
  76. if (!type_id.has_value()) {
  77. return SemIR::ConstantId::NotConstant;
  78. }
  79. if (is_lvalue) {
  80. return MapLValueToConstant(context, loc_id, ap_value, type);
  81. } else if (type->isPointerType()) {
  82. auto const_id = MapLValueToConstant(context, loc_id, ap_value, type);
  83. auto inst_id = AddInst<SemIR::AddrOf>(
  84. context, loc_id,
  85. {.type_id = type_id,
  86. .lvalue_id = context.constant_values().GetInstId(const_id)});
  87. return context.constant_values().Get(inst_id);
  88. } else if (ap_value.isInt()) {
  89. if (type->isBooleanType()) {
  90. auto value = SemIR::BoolValue::From(!ap_value.getInt().isZero());
  91. return TryEvalInst(
  92. context, SemIR::BoolLiteral{.type_id = type_id, .value = value});
  93. } else {
  94. CARBON_CHECK(type->isIntegralOrEnumerationType());
  95. IntId int_id = context.ints().Add(ap_value.getInt());
  96. return TryEvalInst(context,
  97. SemIR::IntValue{.type_id = type_id, .int_id = int_id});
  98. }
  99. } else if (ap_value.isFloat()) {
  100. FloatId float_id = context.floats().Add(ap_value.getFloat());
  101. return TryEvalInst(
  102. context, SemIR::FloatValue{.type_id = type_id, .float_id = float_id});
  103. } else {
  104. // TODO: support other types.
  105. context.TODO(loc_id, "unsupported conversion to constant from APValue " +
  106. ap_value.getAsString(context.ast_context(), type));
  107. return SemIR::ErrorInst::ConstantId;
  108. }
  109. }
  110. static auto MapAPValueToConstantForConstexpr(Context& context,
  111. SemIR::LocId loc_id,
  112. const clang::APValue& ap_value,
  113. clang::QualType type)
  114. -> SemIR::ConstantId {
  115. bool is_lvalue = false;
  116. if (type->isReferenceType()) {
  117. is_lvalue = true;
  118. type = type.getNonReferenceType();
  119. }
  120. return MapAPValueToConstant(context, loc_id, ap_value, type, is_lvalue);
  121. }
  122. auto EvalCppVarDecl(Context& context, SemIR::LocId loc_id,
  123. const clang::VarDecl* var_decl, SemIR::TypeId type_id)
  124. -> SemIR::ConstantId {
  125. // If the C++ global is constant, map it to a Carbon constant.
  126. if (var_decl->isUsableInConstantExpressions(context.ast_context())) {
  127. if (const auto* ap_value = var_decl->getEvaluatedValue()) {
  128. auto clang_type = MapToCppType(context, type_id);
  129. if (clang_type.isNull()) {
  130. context.TODO(loc_id, "failed to map C++ type to Carbon");
  131. return SemIR::ErrorInst::ConstantId;
  132. }
  133. return MapAPValueToConstantForConstexpr(context, loc_id, *ap_value,
  134. clang_type);
  135. }
  136. }
  137. return SemIR::ConstantId::NotConstant;
  138. }
  139. auto MapConstantToAPValue(Context& context, SemIR::InstId const_inst_id,
  140. clang::QualType param_type)
  141. -> std::optional<clang::APValue> {
  142. if (param_type->isIntegerType()) {
  143. const bool is_signed = param_type->isSignedIntegerOrEnumerationType();
  144. if (auto int_value =
  145. context.insts().TryGetAs<SemIR::IntValue>(const_inst_id)) {
  146. const auto& ap_int = context.ints().Get(int_value->int_id);
  147. auto aps_int =
  148. llvm::APSInt(ap_int, !is_signed)
  149. .extOrTrunc(context.ast_context().getIntWidth(param_type));
  150. return clang::APValue(aps_int);
  151. } else if (auto bool_value = context.insts().TryGetAs<SemIR::BoolLiteral>(
  152. const_inst_id)) {
  153. llvm::APInt ap_int(context.ast_context().getIntWidth(param_type),
  154. bool_value->value.ToBool(), is_signed);
  155. auto aps_int =
  156. llvm::APSInt(ap_int, !is_signed)
  157. .extOrTrunc(context.ast_context().getIntWidth(param_type));
  158. return clang::APValue(aps_int);
  159. }
  160. } else if (param_type->isFloatingType()) {
  161. if (auto float_value =
  162. context.insts().TryGetAs<SemIR::FloatValue>(const_inst_id)) {
  163. const auto& ap_float = context.floats().Get(float_value->float_id);
  164. return clang::APValue(ap_float);
  165. }
  166. }
  167. // TODO: support additional parameter types.
  168. return std::nullopt;
  169. }
  170. static auto ConvertArgToExpr(Context& context, SemIR::InstId arg_inst_id,
  171. clang::QualType param_type) -> clang::Expr* {
  172. auto const_inst_id = context.constant_values().GetConstantInstId(arg_inst_id);
  173. if (!const_inst_id.has_value()) {
  174. return nullptr;
  175. }
  176. auto ap_value = MapConstantToAPValue(context, const_inst_id, param_type);
  177. if (!ap_value.has_value()) {
  178. return nullptr;
  179. }
  180. auto* opaque_value_expr = new (context.ast_context()) clang::OpaqueValueExpr(
  181. clang::SourceLocation(), param_type, clang::VK_PRValue);
  182. return clang::ConstantExpr::Create(context.ast_context(), opaque_value_expr,
  183. *ap_value);
  184. }
  185. auto EvalCppCall(Context& context, SemIR::LocId loc_id,
  186. SemIR::ClangDeclId clang_decl_id, SemIR::InstBlockId args_id)
  187. -> SemIR::ConstantId {
  188. const auto& args = context.inst_blocks().Get(args_id);
  189. auto* decl = context.clang_decls().Get(clang_decl_id).GetAsKey().decl;
  190. auto* function_decl = cast<clang::FunctionDecl>(decl);
  191. // Create expr for the function declaration.
  192. auto* decl_ref_expr = clang::DeclRefExpr::Create(
  193. context.ast_context(), /*QualifierLoc=*/clang::NestedNameSpecifierLoc(),
  194. /*TemplateKWLoc=*/clang::SourceLocation(), function_decl,
  195. /*RefersToEnclosingVariableOrCapture=*/false,
  196. /*NameLoc=*/GetCppLocation(context, loc_id), function_decl->getType(),
  197. clang::VK_LValue);
  198. // Cast to a function pointer type.
  199. auto function_ptr_type =
  200. context.ast_context().getPointerType(function_decl->getType());
  201. auto* implicit_cast_expr = clang::ImplicitCastExpr::Create(
  202. context.ast_context(), function_ptr_type,
  203. clang::CK_FunctionToPointerDecay, decl_ref_expr, nullptr,
  204. clang::VK_PRValue, clang::FPOptionsOverride());
  205. // Convert the arguments to exprs.
  206. clang::SmallVector<clang::Expr*> arg_exprs;
  207. for (auto [arg_inst_id, parm_var_decl] :
  208. llvm::zip(args, function_decl->parameters())) {
  209. if (auto* arg_expr =
  210. ConvertArgToExpr(context, arg_inst_id, parm_var_decl->getType())) {
  211. arg_exprs.push_back(arg_expr);
  212. } else {
  213. return SemIR::ConstantId::NotConstant;
  214. }
  215. }
  216. // Create an expr to call the function.
  217. auto* call_expr = clang::CallExpr::Create(
  218. context.ast_context(), implicit_cast_expr, arg_exprs,
  219. function_decl->getCallResultType(), clang::VK_PRValue,
  220. /*RParenLoc=*/GetCppLocation(context, loc_id),
  221. clang::FPOptionsOverride());
  222. // Evaluate the expr as a constant and map that to Carbon constant.
  223. clang::SmallVector<clang::PartialDiagnosticAt> notes;
  224. clang::Expr::EvalResult eval_result;
  225. eval_result.Diag = &notes;
  226. if (!call_expr->EvaluateAsConstantExpr(eval_result, context.ast_context())) {
  227. context.clang_sema().Diag(call_expr->getBeginLoc(),
  228. clang::diag::err_invalid_consteval_call)
  229. << function_decl << function_decl->isConsteval();
  230. for (const auto& note : notes) {
  231. context.clang_sema().Diag(note.first, note.second);
  232. }
  233. return SemIR::ErrorInst::ConstantId;
  234. }
  235. return MapAPValueToConstantForConstexpr(context, loc_id, eval_result.Val,
  236. function_decl->getCallResultType());
  237. }
  238. } // namespace Carbon::Check