Repository: carbon-language/carbon-lang
File: toolchain/check/cpp/import.cpp (C++)

Code before:
      context.name_scopes().Get(scope_id).inst_id());
  return class_decl.has_value() &&
         !context.types().IsComplete(
             context.classes().Get(class_decl->class_id).self_type_id);
}

// Maps a Clang constant expression to a Carbon constant. Currently supports
// only integer constants.
// TODO: Add support for the other constant types for which a C++ to Carbon type
// mapping exists.
static auto MapConstant(Context& context, SemIR::LocId loc_id,
                        clang::Expr* expr) -> SemIR::InstId {
  CARBON_CHECK(expr, "empty expression");

  SemIR::TypeId type_id = MapType(context, loc_id, expr->getType()).type_id;
  if (!type_id.has_value()) {
    context.TODO(
        loc_id, "Unsupported: constant type: " + expr->getType().getAsString());
    return SemIR::ErrorInst::InstId;
  }
  SemIR::InstId inst_id = SemIR::InstId::None;
  if (auto* integer_literal = dyn_cast<clang::IntegerLiteral>(expr)) {
    auto int_id =
        context.ints().Add(integer_literal->getValue().getSExtValue());
    inst_id = AddInstInNoBlock<SemIR::IntValue>(
        context, loc_id, {.type_id = type_id, .int_id = int_id});
  } else if (auto* float_literal = dyn_cast<clang::FloatingLiteral>(expr)) {
    auto float_id = context.floats().Add(float_literal->getValue());
    inst_id = AddInstInNoBlock<SemIR::FloatValue>(
        context, loc_id, {.type_id = type_id, .float_id = float_id});
  } else {
    context.TODO(
        loc_id, "Unsupported: constant type: " + expr->getType().getAsString());
    inst_id = SemIR::ErrorInst::InstId;
  }
  if (inst_id != SemIR::ErrorInst::InstId) {
    context.imports().push_back(inst_id);
  }
  return inst_id;
}

// Imports a macro definition into the scope. Currently supports only simple
// object-like macros that expand to a constant integer value.
// TODO: Add support for other macro types and non-integer literal values.
static auto ImportMacro(Context& context, SemIR::LocId loc_id,
                        SemIR::NameScopeId scope_id, SemIR::NameId name_id,
                        clang::MacroInfo* macro_info)
    -> SemIR::ScopeLookupResult {
  clang::Expr* macro_expr =
      TryEvaluateMacroToConstant(context, loc_id, name_id, macro_info);


Review comment (suggestion):
Per discussion, you want `MakeImportedLocIdAndInst` here, something like:

```suggestion
        context, MakeImportedLocIdAndInst(loc_id, {.type_id = type_id, .int_id = int_id}));
```

Code after:
  SemIR::InstId inst_id = SemIR::InstId::None;
  SemIR::ImportIRInstId imported_loc_id =
      AddImportIRInst(context.sem_ir(), expr->getExprLoc());

  if (auto* integer_literal = dyn_cast<clang::IntegerLiteral>(expr)) {
    IntId int_id =
        context.ints().Add(integer_literal->getValue().getSExtValue());
    inst_id = AddInstInNoBlock(
        context,
        MakeImportedLocIdAndInst<SemIR::IntValue>(
            context, imported_loc_id, {.type_id = type_id, .int_id = int_id}));
  } else if (auto* float_literal = dyn_cast<clang::FloatingLiteral>(expr)) {
    FloatId float_id = context.floats().Add(float_literal->getValue());
    inst_id = AddInstInNoBlock(context,
                               MakeImportedLocIdAndInst<SemIR::FloatValue>(
                                   context, imported_loc_id,
                                   {.type_id = type_id, .float_id = float_id}));
  } else {
    context.TODO(loc_id, llvm::formatv(
                             "Unsupported: C++ constant expression type: '{0}'",
                             expr->getType().getAsString()));
    return SemIR::ErrorInst::InstId;
  }

  context.imports().push_back(inst_id);
  return inst_id;
}

// Imports a macro definition into the scope. Currently supports only simple
// object-like macros that expand to a constant integer value.
// TODO: Add support for other macro types and non-integer literal values.
static auto ImportMacro(Context& context, SemIR::LocId loc_id,
                        SemIR::NameScopeId scope_id, SemIR::NameId name_id,
                        clang::MacroInfo* macro_info)
    -> SemIR::ScopeLookupResult {
  clang::Expr* macro_expr =
      TryEvaluateMacroToConstant(context, loc_id, name_id, macro_info);

  if (!macro_expr) {
    return SemIR::ScopeLookupResult::MakeNotFound();
  }

  auto inst_id = MapConstant(context, loc_id, macro_expr);
  if (inst_id == SemIR::ErrorInst::InstId) {
    return SemIR::ScopeLookupResult::MakeNotFound();
  }

  AddNameToScope(context, scope_id, name_id, SemIR::AccessKind::Public,
                 inst_id);
  return SemIR::ScopeLookupResult::MakeWrappedLookupResult(
      inst_id, SemIR::AccessKind::Public);