Exemplo n.º 1
0
static void maybeEmitDebugInfoForLocalTypeData(IRGenFunction &IGF,
                                               LocalTypeDataKey key,
                                               MetadataResponse value) {
  // FIXME: This check doesn't entirely behave correctly for non-transparent
  // functions that were inlined into transparent functions. Correct would be to
  // check which instruction requests the type metadata and see whether its
  // inlined function is transparent.
  auto * DS = IGF.getDebugScope();
  if (DS && DS->getInlinedFunction() &&
      DS->getInlinedFunction()->isTransparent())
    return;
  
  // Only for formal type metadata.
  if (key.Kind != LocalTypeDataKind::forFormalTypeMetadata())
    return;

  // Only for archetypes, and not for opened archetypes.
  auto type = dyn_cast<ArchetypeType>(key.Type);
  if (!type)
    return;
  if (type->getOpenedExistentialType())
    return;

  llvm::Value *data = value.getMetadata();

  // At -O0, create an alloca to keep the type alive.
  auto name = type->getFullName();
  if (!IGF.IGM.IRGen.Opts.shouldOptimize()) {
    auto alloca =
        IGF.createAlloca(data->getType(), IGF.IGM.getPointerAlignment(), name);
    IGF.Builder.CreateStore(data, alloca);
    data = alloca.getAddress();
  }

  // Only if debug info is enabled.
  if (!IGF.IGM.DebugInfo)
    return;

  // Emit debug info for the metadata.
  llvm::SmallString<8> AssocType;
  auto *oocTy = type->mapTypeOutOfContext().getPointer();
  {
    llvm::raw_svector_ostream OS(AssocType);
    while (auto *dependentMemberType = dyn_cast<DependentMemberType>(oocTy)) {
      OS << '.' << dependentMemberType->getName();
      oocTy = dependentMemberType->getBase().getPointer();
    }
  }
  auto *typeParam = cast<GenericTypeParamType>(oocTy);
  IGF.IGM.DebugInfo->emitTypeMetadata(IGF, data, typeParam->getDepth(),
                                      typeParam->getIndex(), AssocType);
}