blob: 8211a61f55cbd9d1df583b31507b2bdfe6377f02 [file] [edit]
//===--- TBAAUtils.cpp - Shared TBAA queries ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/CodeGenUtils/TBAAUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
namespace clang::CodeGenUtils {
bool typeHasMayAlias(QualType QTy) {
// Tagged types have declarations, and therefore may have attributes.
if (auto *TD = QTy->getAsTagDecl())
if (TD->hasAttr<MayAliasAttr>())
return true;
// Also look for may_alias as a declaration attribute on a typedef.
// FIXME: We should follow GCC and model may_alias as a type attribute
// rather than as a declaration attribute.
while (auto *TT = QTy->getAs<TypedefType>()) {
if (TT->getDecl()->hasAttr<MayAliasAttr>())
return true;
QTy = TT->desugar();
}
// Also consider an array type as may_alias when its element type (at
// any level) is marked as such.
if (auto *ArrayTy = QTy->getAsArrayTypeUnsafe())
if (typeHasMayAlias(ArrayTy->getElementType()))
return true;
return false;
}
bool isValidTBAABaseType(QualType QTy) {
if (const auto *RD = QTy->getAsRecordDecl()) {
// Incomplete types are not valid base access types.
if (!RD->isCompleteDefinition())
return false;
if (RD->hasFlexibleArrayMember())
return false;
// RD can be struct, union, class, interface or enum.
// For now, we only handle struct and class.
if (RD->isStruct() || RD->isClass())
return true;
}
return false;
}
QualType getTBAASignedCounterpart(const ASTContext &Ctx,
const BuiltinType *BTy) {
// Unsigned types can alias their corresponding signed types.
switch (BTy->getKind()) {
case BuiltinType::UShort:
return Ctx.ShortTy;
case BuiltinType::UInt:
return Ctx.IntTy;
case BuiltinType::ULong:
return Ctx.LongTy;
case BuiltinType::ULongLong:
return Ctx.LongLongTy;
case BuiltinType::UInt128:
return Ctx.Int128Ty;
case BuiltinType::UShortFract:
return Ctx.ShortFractTy;
case BuiltinType::UFract:
return Ctx.FractTy;
case BuiltinType::ULongFract:
return Ctx.LongFractTy;
case BuiltinType::SatUShortFract:
return Ctx.SatShortFractTy;
case BuiltinType::SatUFract:
return Ctx.SatFractTy;
case BuiltinType::SatULongFract:
return Ctx.SatLongFractTy;
case BuiltinType::UShortAccum:
return Ctx.ShortAccumTy;
case BuiltinType::UAccum:
return Ctx.AccumTy;
case BuiltinType::ULongAccum:
return Ctx.LongAccumTy;
case BuiltinType::SatUShortAccum:
return Ctx.SatShortAccumTy;
case BuiltinType::SatUAccum:
return Ctx.SatAccumTy;
case BuiltinType::SatULongAccum:
return Ctx.SatLongAccumTy;
default:
return QualType();
}
}
} // namespace clang::CodeGenUtils