athena/atdna/main.cpp

1277 lines
56 KiB
C++
Raw Normal View History

2019-09-04 19:18:26 +00:00
#include <cstdint>
2017-12-29 07:55:42 +00:00
#include <cstdio>
2019-09-04 19:18:26 +00:00
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/DeclCXX.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Basic/Version.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendAction.h>
#include <clang/Frontend/Utils.h>
#include <clang/Sema/Sema.h>
#include <clang/Tooling/Tooling.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/Format.h>
2015-08-03 01:42:47 +00:00
using namespace std::literals;
2015-08-03 01:42:47 +00:00
static unsigned AthenaError = 0;
2016-03-04 23:00:12 +00:00
#define ATHENA_DNA_BASETYPE "struct athena::io::DNA"
2015-08-03 01:42:47 +00:00
#ifndef INSTALL_PREFIX
2018-12-08 05:18:17 +00:00
#define INSTALL_PREFIX / usr / local
2015-08-03 01:42:47 +00:00
#endif
#define XSTR(s) STR(s)
#define STR(s) #s
static llvm::cl::opt<bool> Help("h", llvm::cl::desc("Alias for -help"), llvm::cl::Hidden);
static llvm::cl::opt<bool> Verbose("v", llvm::cl::desc("verbose mode"));
2015-08-03 01:42:47 +00:00
static llvm::cl::OptionCategory ATDNAFormatCategory("atdna options");
2018-12-08 05:18:17 +00:00
static llvm::cl::opt<std::string> OutputFilename("o", llvm::cl::desc("Specify output filename"),
llvm::cl::value_desc("filename"), llvm::cl::Prefix);
2015-08-03 01:42:47 +00:00
2018-12-08 05:18:17 +00:00
static llvm::cl::opt<bool> FExceptions("fexceptions", llvm::cl::desc("Enable C++ Exceptions"));
static llvm::cl::opt<bool> FMSCompat("fms-compatibility", llvm::cl::desc("Enable MS header compatibility"));
static llvm::cl::opt<std::string>
FMSCompatVersion("fms-compatibility-version",
llvm::cl::desc("Specify MS compatibility version (18.00 for VS2013, 19.00 for VS2015)"));
2015-08-03 01:42:47 +00:00
2018-12-08 05:18:17 +00:00
static llvm::cl::list<std::string> InputFilenames(llvm::cl::Positional, llvm::cl::desc("<Input files>"),
llvm::cl::ZeroOrMore);
2015-08-03 01:42:47 +00:00
2018-12-08 05:18:17 +00:00
static llvm::cl::list<std::string> IncludeSearchPaths("I", llvm::cl::desc("Header search path"), llvm::cl::Prefix);
2015-08-03 01:42:47 +00:00
2018-12-08 05:18:17 +00:00
static llvm::cl::list<std::string> SystemIncludeSearchPaths("isystem", llvm::cl::desc("System Header search path"));
2015-08-03 01:42:47 +00:00
static llvm::cl::list<std::string> StandardCXXLibISystem("stdlib++-isystem", llvm::cl::desc("Standard C++ library search path"));
2018-12-08 05:18:17 +00:00
static llvm::cl::opt<std::string> StandardCXXLib("stdlib", llvm::cl::desc("Standard C++ library"));
static llvm::cl::opt<bool> DepFile("MD", llvm::cl::desc("Make Dependency file"));
2018-12-08 05:18:17 +00:00
static llvm::cl::opt<std::string> DepFileOut("MF", llvm::cl::desc("Dependency file out path"));
2018-12-08 05:18:17 +00:00
static llvm::cl::list<std::string> DepFileTargets("MT", llvm::cl::desc("Dependency file targets"));
2018-12-08 05:18:17 +00:00
static llvm::cl::list<std::string> SystemIncRoot("isysroot", llvm::cl::desc("System include root"));
2018-12-08 05:18:17 +00:00
static llvm::cl::list<std::string> PreprocessorDefines("D", llvm::cl::desc("Preprocessor define"), llvm::cl::Prefix);
2015-08-03 01:42:47 +00:00
static llvm::cl::opt<bool> EmitIncludes("emit-includes",
llvm::cl::desc("Emit DNA for included files (not just main file)"));
/* LLVM 3.7 changed the stream type */
#if LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 7)
using StreamOut = llvm::raw_pwrite_stream;
#else
using StreamOut = llvm::raw_fd_ostream;
#endif
2018-12-08 05:18:17 +00:00
class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
clang::ASTContext& context;
StreamOut& fileOut;
2015-08-03 01:42:47 +00:00
2018-12-08 05:18:17 +00:00
bool isDNARecord(const clang::CXXRecordDecl* record, std::string& baseDNA) {
for (const clang::CXXBaseSpecifier& base : record->bases()) {
const clang::QualType qtp = base.getType().getCanonicalType();
if (!qtp.getAsString().compare(0, sizeof(ATHENA_DNA_BASETYPE) - 1, ATHENA_DNA_BASETYPE))
return true;
}
for (const clang::CXXBaseSpecifier& base : record->bases()) {
clang::QualType qtp = base.getType().getCanonicalType();
const clang::Type* tp = qtp.getTypePtrOrNull();
if (tp) {
const clang::CXXRecordDecl* rDecl = tp->getAsCXXRecordDecl();
if (rDecl) {
if (isDNARecord(rDecl, baseDNA)) {
bool hasRead = false;
bool hasWrite = false;
for (const clang::CXXMethodDecl* method : rDecl->methods()) {
std::string compName = method->getDeclName().getAsString();
2019-10-01 07:10:47 +00:00
if (compName == "read")
2018-12-08 05:18:17 +00:00
hasRead = true;
2019-10-01 07:10:47 +00:00
else if (compName == "write")
2018-12-08 05:18:17 +00:00
hasWrite = true;
2015-10-18 04:05:35 +00:00
}
2018-12-08 05:18:17 +00:00
if (hasRead && hasWrite) {
std::string templateStmt;
GetNestedTypeName(rDecl, templateStmt, baseDNA);
2015-10-18 04:05:35 +00:00
}
2018-12-08 05:18:17 +00:00
return true;
}
2015-10-18 04:05:35 +00:00
}
2018-12-08 05:18:17 +00:00
}
2015-10-18 04:05:35 +00:00
}
2018-12-08 05:18:17 +00:00
return false;
}
2015-10-18 04:05:35 +00:00
2018-12-08 05:18:17 +00:00
int64_t GetSizeValue(const clang::Type* theType, unsigned width) {
if (theType->isEnumeralType()) {
2019-01-29 08:24:10 +00:00
const auto* eType = static_cast<const clang::EnumType*>(theType);
2018-12-08 05:18:17 +00:00
clang::EnumDecl* eDecl = eType->getDecl();
theType = eDecl->getIntegerType().getCanonicalType().getTypePtr();
2015-08-20 22:23:19 +00:00
2019-01-29 08:24:10 +00:00
const auto* bType = static_cast<const clang::BuiltinType*>(theType);
2018-12-08 05:18:17 +00:00
if (bType->isBooleanType()) {
return 1;
} else if (bType->isUnsignedInteger() || bType->isSignedInteger()) {
return width / 8;
}
} else if (theType->isBuiltinType()) {
2019-01-29 08:24:10 +00:00
const auto* bType = static_cast<const clang::BuiltinType*>(theType);
2018-12-08 05:18:17 +00:00
if (bType->isBooleanType()) {
return 1;
} else if (bType->isUnsignedInteger() || bType->isSignedInteger() || bType->isFloatingPoint()) {
return width / 8;
}
} else if (theType->isRecordType()) {
const clang::CXXRecordDecl* rDecl = theType->getAsCXXRecordDecl();
for (const clang::FieldDecl* field : rDecl->fields()) {
2019-10-01 07:10:47 +00:00
if (field->getName() == "clangVec") {
2019-01-29 08:24:10 +00:00
const auto* vType = static_cast<const clang::VectorType*>(field->getType().getTypePtr());
2018-12-08 05:18:17 +00:00
if (vType->isVectorType()) {
2019-01-29 08:24:10 +00:00
const auto* eType = static_cast<const clang::BuiltinType*>(vType->getElementType().getTypePtr());
const uint64_t typeWidth = context.getTypeInfo(eType).Width;
if (!eType->isBuiltinType() || !eType->isFloatingPoint() || (typeWidth != 32 && typeWidth != 64))
2018-12-08 05:18:17 +00:00
continue;
if (vType->getNumElements() == 2) {
return typeWidth / 8 * 2;
2018-12-08 05:18:17 +00:00
} else if (vType->getNumElements() == 3) {
return typeWidth / 8 * 3;
2018-12-08 05:18:17 +00:00
} else if (vType->getNumElements() == 4) {
return typeWidth / 8 * 4;
2015-08-03 01:42:47 +00:00
}
2018-12-08 05:18:17 +00:00
}
}
2018-12-08 05:18:17 +00:00
}
}
2018-12-08 05:18:17 +00:00
return 0;
}
static std::string GetFieldString(const std::string& fieldName) {
#if 0
2018-12-08 05:18:17 +00:00
size_t underscorePos = fieldName.find('_');
std::string idString = fieldName;
if (underscorePos != std::string::npos && underscorePos != fieldName.size() - 1)
idString.assign(fieldName.begin() + underscorePos + 1, fieldName.end());
return idString;
#endif
return fieldName;
2018-12-08 05:18:17 +00:00
}
static std::string GetPropIdExpr(const clang::FieldDecl* field, const std::string& fieldName) {
std::string fieldStr = GetFieldString(fieldName);
std::string propIdExpr = "\""s.append(fieldStr).append("\"sv");
2018-12-08 05:18:17 +00:00
for (clang::Attr* attr : field->attrs()) {
if (clang::AnnotateAttr* annot = clang::dyn_cast_or_null<clang::AnnotateAttr>(attr)) {
llvm::StringRef textRef = annot->getAnnotation();
if (textRef.startswith_lower("rcrc32=")) {
unsigned long num = strtoul(textRef.data() + 7, nullptr, 16);
std::string tmpS;
llvm::raw_string_ostream s(tmpS);
2019-10-01 07:10:47 +00:00
s << llvm::format("\"%s\"sv, 0x%08X", fieldStr.c_str(), num);
2018-12-08 05:18:17 +00:00
propIdExpr = s.str();
break;
}
}
}
2018-12-08 05:18:17 +00:00
return propIdExpr;
}
static std::string GetOpString(const std::string& fieldName, const std::string& propIdExpr,
const std::string& endianExpr) {
return "<Op, "s.append(endianExpr)
.append(">(athena::io::PropId(")
.append(propIdExpr)
.append("), ")
.append(fieldName)
.append(", s)");
2018-12-08 05:18:17 +00:00
}
static std::string GetOpString(const std::string& fieldName, const std::string& propIdExpr) {
return "<Op>(athena::io::PropId("s.append(propIdExpr).append("), ").append(fieldName).append(", s)");
2018-12-08 05:18:17 +00:00
}
static std::string GetVectorOpString(const std::string& fieldName, const std::string& propIdExpr,
const std::string& sizeExpr, const std::string& endianExpr) {
return "<Op, "s.append(endianExpr)
.append(">(athena::io::PropId(")
.append(propIdExpr)
.append("), ")
.append(fieldName)
.append(", ")
.append(sizeExpr)
.append(", s)");
2018-12-08 05:18:17 +00:00
}
static std::string GetVectorOpString(const std::string& fieldName, const std::string& propIdExpr,
const std::string& sizeExpr) {
return "<Op>(athena::io::PropId("s.append(propIdExpr)
.append("), ")
.append(fieldName)
.append(", ")
.append(sizeExpr)
.append(", s)");
2018-12-08 05:18:17 +00:00
}
void RecurseNestedTypeName(const clang::DeclContext* decl, std::string& templateStmt, std::string& qualType) {
2018-12-08 05:18:17 +00:00
if (!decl)
return;
RecurseNestedTypeName(decl->getParent(), templateStmt, qualType);
if (const clang::CXXRecordDecl* rec = clang::dyn_cast_or_null<clang::CXXRecordDecl>(decl)) {
if (!qualType.empty())
qualType += "::";
qualType += rec->getName();
const clang::ClassTemplateSpecializationDecl* cts =
clang::dyn_cast_or_null<clang::ClassTemplateSpecializationDecl>(rec);
if (cts && cts->isExplicitSpecialization()) {
qualType += '<';
bool needsComma = false;
for (auto& arg : cts->getTemplateArgs().asArray()) {
if (needsComma)
qualType += ", ";
llvm::raw_string_ostream OS(qualType);
arg.print(context.getPrintingPolicy(), OS);
needsComma = true;
}
qualType += '>';
} else if (const clang::ClassTemplateDecl* ct = rec->getDescribedClassTemplate()) {
2018-12-08 05:18:17 +00:00
templateStmt += "template <";
qualType += '<';
bool needsComma = false;
for (const clang::NamedDecl* parm : *ct->getTemplateParameters()) {
if (const clang::TemplateTypeParmDecl* tpParm = clang::dyn_cast_or_null<clang::TemplateTypeParmDecl>(parm)) {
if (needsComma) {
templateStmt += ", ";
qualType += ", ";
2015-08-03 01:42:47 +00:00
}
templateStmt += "class "s.append(tpParm->getName().str());
2018-12-08 05:18:17 +00:00
qualType += tpParm->getName();
needsComma = true;
} else if (const clang::NonTypeTemplateParmDecl* nonTypeParm =
clang::dyn_cast_or_null<clang::NonTypeTemplateParmDecl>(parm)) {
if (needsComma) {
templateStmt += ", ";
qualType += ", ";
}
templateStmt += nonTypeParm->getType().getAsString(context.getPrintingPolicy()).append(1, ' ').append(nonTypeParm->getName().str());
2018-12-08 05:18:17 +00:00
qualType += nonTypeParm->getName();
needsComma = true;
}
2015-08-03 01:42:47 +00:00
}
2018-12-08 05:18:17 +00:00
templateStmt += ">\n";
qualType += '>';
}
} else if (const clang::NamedDecl* namedDecl = clang::dyn_cast_or_null<clang::NamedDecl>(decl)) {
if (!qualType.empty())
qualType += "::";
qualType += namedDecl->getName();
2015-08-03 01:42:47 +00:00
}
2018-12-08 05:18:17 +00:00
}
void GetNestedTypeName(const clang::DeclContext* decl, std::string& templateStmt, std::string& qualType) {
2018-12-08 05:18:17 +00:00
templateStmt.clear();
qualType.clear();
RecurseNestedTypeName(decl, templateStmt, qualType);
}
void RecurseNestedTypeSpecializations(const clang::DeclContext* decl,
std::vector<std::pair<std::string, int>>& specializations) {
2018-12-08 05:18:17 +00:00
if (!decl) {
specializations.emplace_back();
return;
}
2015-08-20 22:23:19 +00:00
2018-12-08 05:18:17 +00:00
std::vector<std::pair<std::string, int>> parentSpecializations;
RecurseNestedTypeSpecializations(decl->getParent(), parentSpecializations);
bool foundSpecializations = false;
if (const clang::CXXRecordDecl* rec = clang::dyn_cast_or_null<clang::CXXRecordDecl>(decl)) {
const clang::ClassTemplateSpecializationDecl* cts =
clang::dyn_cast_or_null<clang::ClassTemplateSpecializationDecl>(rec);
if (cts && cts->isExplicitSpecialization()) {
for (const auto& parent : parentSpecializations) {
if (parent.first.empty()) {
specializations.emplace_back(std::string(rec->getName().str()).append(1, '<'), 0);
} else {
auto specialization = std::string(parent.first).append("::").append(rec->getName().str()).append(1, '<');
specializations.emplace_back(std::move(specialization), parent.second);
}
bool needsComma = false;
for (auto& arg : cts->getTemplateArgs().asArray()) {
if (needsComma)
specializations.back().first += ", ";
llvm::raw_string_ostream OS(specializations.back().first);
arg.print(context.getPrintingPolicy(), OS);
needsComma = true;
}
specializations.back().first += '>';
}
foundSpecializations = true;
} else if (const clang::ClassTemplateDecl* ct = rec->getDescribedClassTemplate()) {
2018-12-08 05:18:17 +00:00
int numParms = 0;
for (const clang::NamedDecl* parm : *ct->getTemplateParameters())
if (clang::dyn_cast_or_null<clang::TemplateTypeParmDecl>(parm) ||
clang::dyn_cast_or_null<clang::NonTypeTemplateParmDecl>(parm))
++numParms;
for (clang::Attr* attr : rec->attrs()) {
if (clang::AnnotateAttr* annot = clang::dyn_cast_or_null<clang::AnnotateAttr>(attr)) {
llvm::StringRef textRef = annot->getAnnotation();
if (textRef.startswith_lower("specparms=")) {
llvm::SmallVector<llvm::StringRef, 16> specParms;
textRef.substr(10).split(specParms, ',');
int numTuples = int(specParms.size()) / numParms;
for (const auto& parent : parentSpecializations) {
for (int i = 0; i < numTuples; ++i) {
if (parent.first.empty()) {
specializations.emplace_back(std::string(rec->getName().str()).append(1, '<'), 1);
} else {
auto specialization =
std::string(parent.first).append("::").append(rec->getName().str()).append(1, '<');
specializations.emplace_back(std::move(specialization), parent.second + 1);
}
2018-12-08 05:18:17 +00:00
bool needsComma = false;
for (auto it = specParms.begin() + i * numParms;
it != specParms.end() && it != specParms.begin() + (i + 1) * numParms; ++it) {
2019-01-29 08:24:10 +00:00
llvm::StringRef trimmed = it->trim();
2018-12-08 05:18:17 +00:00
if (needsComma)
specializations.back().first += ", ";
specializations.back().first += trimmed;
needsComma = true;
}
specializations.back().first += '>';
2015-08-03 01:42:47 +00:00
}
2018-12-08 05:18:17 +00:00
}
foundSpecializations = true;
break;
2015-08-03 01:42:47 +00:00
}
2018-12-08 05:18:17 +00:00
}
2015-08-03 01:42:47 +00:00
}
2018-12-08 05:18:17 +00:00
}
}
if (!foundSpecializations) {
2018-12-08 05:18:17 +00:00
for (const auto& parent : parentSpecializations) {
if (const clang::NamedDecl* namedDecl = clang::dyn_cast_or_null<clang::NamedDecl>(decl)) {
if (parent.first.empty()) {
specializations.emplace_back(namedDecl->getName().str(), parent.second);
} else {
specializations.emplace_back(std::string(parent.first).append("::").append(namedDecl->getName().str()),
parent.second);
}
} else {
2018-12-08 05:18:17 +00:00
specializations.push_back(parent);
}
2018-12-08 05:18:17 +00:00
}
}
2018-12-08 05:18:17 +00:00
}
std::vector<std::pair<std::string, int>> GetNestedTypeSpecializations(const clang::DeclContext* decl) {
2018-12-08 05:18:17 +00:00
std::vector<std::pair<std::string, int>> ret;
RecurseNestedTypeSpecializations(decl, ret);
return ret;
}
void emitEnumerateFunc(clang::CXXRecordDecl* decl, const std::string& baseDNA) {
std::string templateStmt;
std::string qualTypeStr;
GetNestedTypeName(decl, templateStmt, qualTypeStr);
fileOut << templateStmt;
2018-12-13 07:38:14 +00:00
fileOut << "template <class Op>\nvoid " << qualTypeStr << "::Enumerate(typename Op::StreamT& s) {\n";
2018-12-08 05:18:17 +00:00
if (baseDNA.size())
2018-12-13 07:38:14 +00:00
fileOut << " " << baseDNA << "::Enumerate<Op>(s);\n";
2018-12-08 05:18:17 +00:00
enum class NodeType { Do, DoSeek, DoAlign };
struct OutputNode {
NodeType m_type = NodeType::Do;
std::string m_fieldName;
std::string m_ioOp;
bool m_squelched = false;
OutputNode(NodeType type, std::string fieldName, std::string ioOp, bool squelched)
: m_type(type), m_fieldName(std::move(fieldName)), m_ioOp(std::move(ioOp)), m_squelched(squelched) {}
2018-12-08 05:18:17 +00:00
};
std::vector<OutputNode> outputNodes;
for (const clang::FieldDecl* field : decl->fields()) {
clang::QualType qualType = field->getType();
const clang::Type* regType = qualType.getTypePtrOrNull();
if (!regType || regType->getTypeClass() == clang::Type::TemplateTypeParm)
continue;
while (regType->getTypeClass() == clang::Type::Elaborated || regType->getTypeClass() == clang::Type::Typedef)
regType = regType->getUnqualifiedDesugaredType();
/* Resolve constant array */
while (regType->getTypeClass() == clang::Type::ConstantArray) {
2019-01-29 08:24:10 +00:00
const auto* caType = static_cast<const clang::ConstantArrayType*>(regType);
2018-12-08 05:18:17 +00:00
qualType = caType->getElementType();
regType = qualType.getTypePtrOrNull();
if (regType->getTypeClass() == clang::Type::Elaborated)
regType = regType->getUnqualifiedDesugaredType();
}
std::string fieldName = field->getName().str();
2018-12-08 05:18:17 +00:00
std::string propIdExpr = GetPropIdExpr(field, fieldName);
if (regType->getTypeClass() == clang::Type::TemplateSpecialization) {
2019-01-29 08:24:10 +00:00
const auto* tsType = static_cast<const clang::TemplateSpecializationType*>(regType);
2018-12-08 05:18:17 +00:00
const clang::TemplateDecl* tsDecl = tsType->getTemplateName().getAsTemplateDecl();
const clang::TemplateParameterList* classParms = tsDecl->getTemplateParameters();
2019-10-01 07:10:47 +00:00
if (tsDecl->getName() == "Value") {
2018-12-08 05:18:17 +00:00
llvm::APSInt endian(64, -1);
std::string endianExprStr;
bool defaultEndian = true;
/* FIXME: The is the following code required? */
#if 0
2018-12-08 05:18:17 +00:00
if (classParms->size() >= 2) {
const clang::NamedDecl* endianParm = classParms->getParam(1);
if (endianParm->getKind() == clang::Decl::NonTypeTemplateParm) {
2019-01-29 08:24:10 +00:00
const auto* nttParm = static_cast<const clang::NonTypeTemplateParmDecl*>(endianParm);
2018-12-08 05:18:17 +00:00
llvm::raw_string_ostream strStream(endianExprStr);
nttParm->print(strStream, context.getPrintingPolicy());
2015-10-18 04:05:35 +00:00
}
2018-12-08 05:18:17 +00:00
}
#endif
2018-12-08 05:18:17 +00:00
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Expression) {
const clang::Expr* expr = arg.getAsExpr();
llvm::raw_string_ostream strStream(endianExprStr);
expr->printPretty(strStream, nullptr, context.getPrintingPolicy());
defaultEndian = false;
}
}
std::string ioOp;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Type) {
if (defaultEndian) {
2018-12-08 05:18:17 +00:00
ioOp = GetOpString(fieldName, propIdExpr);
} else {
2018-12-08 05:18:17 +00:00
ioOp = GetOpString(fieldName, propIdExpr, endianExprStr);
}
2018-12-08 05:18:17 +00:00
}
}
if (ioOp.empty()) {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
diag.AddString("Unable to use type '"s.append(tsDecl->getName().str()).append("' with Athena"));
2018-12-08 05:18:17 +00:00
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
continue;
}
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), std::move(ioOp), false);
2019-10-01 07:10:47 +00:00
} else if (tsDecl->getName() == "Vector") {
2018-12-08 05:18:17 +00:00
llvm::APSInt endian(64, -1);
std::string endianExprStr;
bool defaultEndian = true;
if (classParms->size() >= 3) {
const clang::NamedDecl* endianParm = classParms->getParam(2);
if (endianParm->getKind() == clang::Decl::NonTypeTemplateParm) {
2019-01-29 08:24:10 +00:00
const auto* nttParm = static_cast<const clang::NonTypeTemplateParmDecl*>(endianParm);
2018-12-08 05:18:17 +00:00
llvm::raw_string_ostream strStream(endianExprStr);
nttParm->print(strStream, context.getPrintingPolicy());
}
}
std::string sizeExpr;
size_t idx = 0;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Expression) {
const clang::Expr* expr = arg.getAsExpr()->IgnoreImpCasts();
if (idx == 1) {
2019-01-29 08:24:10 +00:00
const auto* uExpr = static_cast<const clang::UnaryExprOrTypeTraitExpr*>(expr);
2018-12-08 05:18:17 +00:00
if (uExpr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
uExpr->getKind() == clang::UETT_SizeOf) {
const clang::Expr* argExpr = uExpr->getArgumentExpr();
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
2019-01-29 08:24:10 +00:00
argExpr = static_cast<const clang::ParenExpr*>(argExpr)->getSubExpr();
2018-12-08 05:18:17 +00:00
if (argExpr->getStmtClass() == clang::Stmt::DeclRefExprClass) {
2019-01-29 08:24:10 +00:00
const auto* drExpr = static_cast<const clang::DeclRefExpr*>(argExpr);
2018-12-08 05:18:17 +00:00
std::string testName = drExpr->getFoundDecl()->getNameAsString();
for (auto i = outputNodes.rbegin(); i != outputNodes.rend(); ++i) {
if (i->m_fieldName == testName) {
i->m_squelched = true;
break;
}
2015-10-18 04:05:35 +00:00
}
2018-12-08 05:18:17 +00:00
}
llvm::raw_string_ostream strStream2(sizeExpr);
argExpr->printPretty(strStream2, nullptr, context.getPrintingPolicy());
}
2018-12-08 05:18:17 +00:00
} else if (idx == 2) {
defaultEndian = false;
llvm::raw_string_ostream strStream(endianExprStr);
expr->printPretty(strStream, nullptr, context.getPrintingPolicy());
}
}
++idx;
}
clang::QualType templateType;
std::string ioOp;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Type) {
templateType = arg.getAsType().getCanonicalType();
if (defaultEndian) {
2018-12-08 05:18:17 +00:00
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExpr);
} else {
2018-12-08 05:18:17 +00:00
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExpr, endianExprStr);
}
2018-12-08 05:18:17 +00:00
}
}
if (ioOp.empty()) {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
diag.AddString("Unable to use type '"s.append(templateType.getAsString()).append("' with Athena"));
2018-12-08 05:18:17 +00:00
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
continue;
}
if (sizeExpr.empty()) {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
2018-12-08 05:18:17 +00:00
diag.AddString("Unable to use count variable with Athena");
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
continue;
}
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), std::move(ioOp), false);
2019-10-01 07:10:47 +00:00
} else if (tsDecl->getName() == "Buffer") {
2018-12-08 05:18:17 +00:00
const clang::Expr* sizeExpr = nullptr;
std::string sizeExprStr;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Expression) {
const auto* uExpr =
static_cast<const clang::UnaryExprOrTypeTraitExpr*>(arg.getAsExpr()->IgnoreImpCasts());
2018-12-08 05:18:17 +00:00
if (uExpr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
uExpr->getKind() == clang::UETT_SizeOf) {
const clang::Expr* argExpr = uExpr->getArgumentExpr();
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
2019-01-29 08:24:10 +00:00
argExpr = static_cast<const clang::ParenExpr*>(argExpr)->getSubExpr();
2018-12-08 05:18:17 +00:00
sizeExpr = argExpr;
llvm::raw_string_ostream strStream(sizeExprStr);
argExpr->printPretty(strStream, nullptr, context.getPrintingPolicy());
}
}
}
if (sizeExprStr.empty()) {
if (sizeExpr) {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(sizeExpr->getExprLoc(), AthenaError);
2018-12-08 05:18:17 +00:00
diag.AddString("Unable to use size variable with Athena");
diag.AddSourceRange(clang::CharSourceRange(sizeExpr->getSourceRange(), true));
} else {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
2018-12-08 05:18:17 +00:00
diag.AddString("Unable to use size variable with Athena");
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
}
continue;
}
std::string ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExprStr);
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), std::move(ioOp), false);
2019-10-01 07:10:47 +00:00
} else if (tsDecl->getName() == "String") {
2018-12-08 05:18:17 +00:00
std::string sizeExprStr;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Expression) {
const clang::Expr* expr = arg.getAsExpr()->IgnoreImpCasts();
2019-01-29 08:24:10 +00:00
const auto* uExpr = static_cast<const clang::UnaryExprOrTypeTraitExpr*>(expr);
2018-12-08 05:18:17 +00:00
llvm::APSInt sizeLiteral;
if (expr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
uExpr->getKind() == clang::UETT_SizeOf) {
const clang::Expr* argExpr = uExpr->getArgumentExpr();
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
2019-01-29 08:24:10 +00:00
argExpr = static_cast<const clang::ParenExpr*>(argExpr)->getSubExpr();
2018-12-08 05:18:17 +00:00
llvm::raw_string_ostream strStream(sizeExprStr);
argExpr->printPretty(strStream, nullptr, context.getPrintingPolicy());
} else if (expr->isIntegerConstantExpr(sizeLiteral, context)) {
sizeExprStr = sizeLiteral.toString(10);
}
}
}
std::string ioOp;
if (!sizeExprStr.empty()) {
2018-12-08 05:18:17 +00:00
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExprStr);
} else {
2018-12-08 05:18:17 +00:00
ioOp = GetOpString(fieldName, propIdExpr);
}
2018-12-08 05:18:17 +00:00
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), std::move(ioOp), false);
2019-10-01 07:10:47 +00:00
} else if (tsDecl->getName() == "WString") {
2018-12-08 05:18:17 +00:00
llvm::APSInt endian(64, -1);
std::string endianExprStr;
bool defaultEndian = true;
if (classParms->size() >= 2) {
const clang::NamedDecl* endianParm = classParms->getParam(1);
if (endianParm->getKind() == clang::Decl::NonTypeTemplateParm) {
2019-01-29 08:24:10 +00:00
const auto* nttParm = static_cast<const clang::NonTypeTemplateParmDecl*>(endianParm);
2018-12-08 05:18:17 +00:00
llvm::raw_string_ostream strStream(endianExprStr);
nttParm->print(strStream, context.getPrintingPolicy());
}
}
std::string sizeExprStr;
size_t idx = 0;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Expression) {
const clang::Expr* expr = arg.getAsExpr()->IgnoreImpCasts();
if (idx == 0) {
llvm::APSInt sizeLiteral;
2019-01-29 08:24:10 +00:00
const auto* uExpr = static_cast<const clang::UnaryExprOrTypeTraitExpr*>(expr);
2018-12-08 05:18:17 +00:00
if (expr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
uExpr->getKind() == clang::UETT_SizeOf) {
const clang::Expr* argExpr = uExpr->getArgumentExpr();
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
2019-01-29 08:24:10 +00:00
argExpr = static_cast<const clang::ParenExpr*>(argExpr)->getSubExpr();
2018-12-08 05:18:17 +00:00
llvm::raw_string_ostream strStream2(sizeExprStr);
argExpr->printPretty(strStream2, nullptr, context.getPrintingPolicy());
} else if (expr->isIntegerConstantExpr(sizeLiteral, context)) {
sizeExprStr = sizeLiteral.toString(10);
2015-10-18 04:05:35 +00:00
}
2018-12-08 05:18:17 +00:00
} else if (idx == 1) {
defaultEndian = false;
llvm::raw_string_ostream strStream(endianExprStr);
expr->printPretty(strStream, nullptr, context.getPrintingPolicy());
}
}
++idx;
}
std::string ioOp;
if (!sizeExprStr.empty()) {
if (defaultEndian) {
2018-12-08 05:18:17 +00:00
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExprStr);
} else {
2018-12-08 05:18:17 +00:00
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExprStr, endianExprStr);
}
2018-12-08 05:18:17 +00:00
} else {
if (defaultEndian) {
2018-12-08 05:18:17 +00:00
ioOp = GetOpString(fieldName, propIdExpr);
} else {
2018-12-08 05:18:17 +00:00
ioOp = GetOpString(fieldName, propIdExpr, endianExprStr);
}
2018-12-08 05:18:17 +00:00
}
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), std::move(ioOp), false);
2019-10-01 07:10:47 +00:00
} else if (tsDecl->getName() == "Seek") {
2018-12-08 05:18:17 +00:00
size_t idx = 0;
std::string offsetExprStr;
llvm::APSInt direction(64, false);
2018-12-08 05:18:17 +00:00
const clang::Expr* directionExpr = nullptr;
bool bad = false;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Expression) {
const clang::Expr* expr = arg.getAsExpr()->IgnoreImpCasts();
if (!idx) {
2019-01-29 08:24:10 +00:00
const auto* uExpr = static_cast<const clang::UnaryExprOrTypeTraitExpr*>(expr);
2018-12-08 05:18:17 +00:00
llvm::APSInt offsetLiteral;
if (expr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
uExpr->getKind() == clang::UETT_SizeOf) {
const clang::Expr* argExpr = uExpr->getArgumentExpr();
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
2019-01-29 08:24:10 +00:00
argExpr = static_cast<const clang::ParenExpr*>(argExpr)->getSubExpr();
2018-12-08 05:18:17 +00:00
llvm::raw_string_ostream strStream(offsetExprStr);
argExpr->printPretty(strStream, nullptr, context.getPrintingPolicy());
} else if (expr->isIntegerConstantExpr(offsetLiteral, context)) {
offsetExprStr = offsetLiteral.toString(10);
2015-10-18 04:05:35 +00:00
}
2018-12-08 05:18:17 +00:00
} else {
clang::APValue result;
if (expr->isCXX11ConstantExpr(context, &result)) {
directionExpr = expr;
direction = result.getInt();
} else {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(expr->getExprLoc(), AthenaError);
2018-12-08 05:18:17 +00:00
diag.AddString("Unable to use non-constant direction expression in Athena");
diag.AddSourceRange(clang::CharSourceRange(expr->getSourceRange(), true));
bad = true;
break;
2018-07-16 07:42:14 +00:00
}
2018-12-08 05:18:17 +00:00
}
}
2018-12-08 05:18:17 +00:00
++idx;
}
if (bad)
continue;
int64_t directionVal = direction.getSExtValue();
if (directionVal < 0 || directionVal > 2) {
if (directionExpr) {
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(directionExpr->getExprLoc(), AthenaError);
2018-12-08 05:18:17 +00:00
diag.AddString("Direction parameter must be 'Begin', 'Current', or 'End'");
diag.AddSourceRange(clang::CharSourceRange(directionExpr->getSourceRange(), true));
} else {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
2018-12-08 05:18:17 +00:00
diag.AddString("Direction parameter must be 'Begin', 'Current', or 'End'");
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
2015-10-18 04:05:35 +00:00
}
2018-12-08 05:18:17 +00:00
continue;
}
if (directionVal == 0) {
outputNodes.emplace_back(NodeType::DoSeek, std::move(fieldName),
"<Op>("s.append(offsetExprStr).append(", athena::SeekOrigin::Begin, s)"), false);
} else if (directionVal == 1) {
outputNodes.emplace_back(NodeType::DoSeek, std::move(fieldName),
"<Op>("s.append(offsetExprStr).append(", athena::SeekOrigin::Current, s)"), false);
} else if (directionVal == 2) {
outputNodes.emplace_back(NodeType::DoSeek, std::move(fieldName),
"<Op>("s.append(offsetExprStr).append(", athena::SeekOrigin::End, s)"), false);
}
2019-10-01 07:10:47 +00:00
} else if (tsDecl->getName() == "Align") {
2018-12-08 05:18:17 +00:00
llvm::APSInt align(64, 0);
bool bad = false;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Expression) {
const clang::Expr* expr = arg.getAsExpr();
if (!expr->isIntegerConstantExpr(align, context)) {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(expr->getExprLoc(), AthenaError);
2018-12-08 05:18:17 +00:00
diag.AddString("Unable to use non-constant align expression in Athena");
diag.AddSourceRange(clang::CharSourceRange(expr->getSourceRange(), true));
bad = true;
break;
2018-12-08 05:18:17 +00:00
}
}
2018-12-08 05:18:17 +00:00
}
if (bad)
continue;
const int64_t alignVal = align.getSExtValue();
2018-12-08 05:18:17 +00:00
if (alignVal) {
outputNodes.emplace_back(NodeType::DoAlign, std::move(fieldName),
"<Op>("s.append(align.toString(10, true)).append(", s)"), false);
2018-12-08 05:18:17 +00:00
}
} else {
const clang::NamedDecl* nd = tsDecl->getTemplatedDecl();
if (const clang::CXXRecordDecl* rd = clang::dyn_cast_or_null<clang::CXXRecordDecl>(nd)) {
std::string baseDNA2;
if (isDNARecord(rd, baseDNA2)) {
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), GetOpString(fieldName, propIdExpr), false);
}
2018-12-08 05:18:17 +00:00
}
2015-10-18 04:05:35 +00:00
}
} else if (regType->getTypeClass() == clang::Type::Record) {
2018-12-08 05:18:17 +00:00
const clang::CXXRecordDecl* cxxRDecl = regType->getAsCXXRecordDecl();
std::string baseDNA2;
if (cxxRDecl && isDNARecord(cxxRDecl, baseDNA2)) {
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), GetOpString(fieldName, propIdExpr), false);
}
2018-12-08 05:18:17 +00:00
}
2015-10-18 04:05:35 +00:00
}
2018-12-08 05:18:17 +00:00
for (const OutputNode& node : outputNodes) {
switch (node.m_type) {
case NodeType::Do:
if (node.m_squelched)
2018-12-13 07:38:14 +00:00
fileOut << " DoSize" << node.m_ioOp << ";\n";
2018-12-08 05:18:17 +00:00
else
2018-12-13 07:38:14 +00:00
fileOut << " Do" << node.m_ioOp << ";\n";
2018-12-08 05:18:17 +00:00
break;
case NodeType::DoSeek:
2018-12-13 07:38:14 +00:00
fileOut << " DoSeek" << node.m_ioOp << ";\n";
2018-12-08 05:18:17 +00:00
break;
case NodeType::DoAlign:
2018-12-13 07:38:14 +00:00
fileOut << " DoAlign" << node.m_ioOp << ";\n";
2018-12-08 05:18:17 +00:00
break;
}
}
2018-12-08 05:18:17 +00:00
fileOut << "}\n\n";
}
void emitLookupFunc(clang::CXXRecordDecl* decl, const std::string& baseDNA) {
std::string templateStmt;
std::string qualTypeStr;
GetNestedTypeName(decl, templateStmt, qualTypeStr);
fileOut << templateStmt;
2018-12-13 07:38:14 +00:00
fileOut << "template <class Op>\nbool " << qualTypeStr << "::Lookup(uint64_t hash, typename Op::StreamT& s) {\n";
2018-12-08 05:18:17 +00:00
if (baseDNA.size())
2018-12-13 07:38:14 +00:00
fileOut << " if (" << baseDNA << "::Lookup<Op>(hash, s))\n"
<< " return true;\n";
2018-12-08 05:18:17 +00:00
2018-12-13 07:38:14 +00:00
fileOut << " switch (hash) {\n";
2018-12-08 05:18:17 +00:00
for (const clang::FieldDecl* field : decl->fields()) {
clang::QualType qualType = field->getType();
const clang::Type* regType = qualType.getTypePtrOrNull();
if (!regType || regType->getTypeClass() == clang::Type::TemplateTypeParm)
continue;
while (regType->getTypeClass() == clang::Type::Elaborated || regType->getTypeClass() == clang::Type::Typedef)
regType = regType->getUnqualifiedDesugaredType();
/* Resolve constant array */
while (regType->getTypeClass() == clang::Type::ConstantArray) {
2019-01-29 08:24:10 +00:00
const auto* caType = static_cast<const clang::ConstantArrayType*>(regType);
2018-12-08 05:18:17 +00:00
qualType = caType->getElementType();
regType = qualType.getTypePtrOrNull();
if (regType->getTypeClass() == clang::Type::Elaborated)
regType = regType->getUnqualifiedDesugaredType();
}
std::string fieldName = field->getName().str();
2018-12-08 05:18:17 +00:00
std::string propIdExpr = GetPropIdExpr(field, fieldName);
if (regType->getTypeClass() == clang::Type::TemplateSpecialization) {
2019-01-29 08:24:10 +00:00
const auto* tsType = static_cast<const clang::TemplateSpecializationType*>(regType);
2018-12-08 05:18:17 +00:00
const clang::TemplateDecl* tsDecl = tsType->getTemplateName().getAsTemplateDecl();
const clang::TemplateParameterList* classParms = tsDecl->getTemplateParameters();
2019-10-01 07:10:47 +00:00
if (tsDecl->getName() == "Value") {
2018-12-08 05:18:17 +00:00
llvm::APSInt endian(64, -1);
std::string endianExprStr;
bool defaultEndian = true;
if (classParms->size() >= 2) {
const clang::NamedDecl* endianParm = classParms->getParam(1);
if (endianParm->getKind() == clang::Decl::NonTypeTemplateParm) {
2019-01-29 08:24:10 +00:00
const auto* nttParm = static_cast<const clang::NonTypeTemplateParmDecl*>(endianParm);
2018-12-08 05:18:17 +00:00
llvm::raw_string_ostream strStream(endianExprStr);
nttParm->print(strStream, context.getPrintingPolicy());
}
}
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Expression) {
const clang::Expr* expr = arg.getAsExpr();
llvm::raw_string_ostream strStream(endianExprStr);
expr->printPretty(strStream, nullptr, context.getPrintingPolicy());
defaultEndian = false;
}
}
std::string ioOp;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Type) {
if (defaultEndian)
ioOp = GetOpString(fieldName, propIdExpr);
else
ioOp = GetOpString(fieldName, propIdExpr, endianExprStr);
}
}
if (ioOp.empty()) {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
diag.AddString("Unable to use type '"s.append(tsDecl->getName().str()).append("' with Athena"));
2018-12-08 05:18:17 +00:00
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
continue;
}
2018-12-13 07:38:14 +00:00
fileOut << " AT_PROP_CASE(" << propIdExpr << "):\n"
<< " Do" << ioOp << ";\n"
<< " return true;\n";
2018-12-08 05:18:17 +00:00
2019-10-01 07:10:47 +00:00
} else if (tsDecl->getName() == "Vector") {
2018-12-08 05:18:17 +00:00
llvm::APSInt endian(64, -1);
std::string endianExprStr;
bool defaultEndian = true;
if (classParms->size() >= 3) {
const clang::NamedDecl* endianParm = classParms->getParam(2);
if (endianParm->getKind() == clang::Decl::NonTypeTemplateParm) {
2019-01-29 08:24:10 +00:00
const auto* nttParm = static_cast<const clang::NonTypeTemplateParmDecl*>(endianParm);
2018-12-08 05:18:17 +00:00
llvm::raw_string_ostream strStream(endianExprStr);
nttParm->print(strStream, context.getPrintingPolicy());
}
}
std::string sizeExpr;
size_t idx = 0;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Expression) {
const clang::Expr* expr = arg.getAsExpr()->IgnoreImpCasts();
if (idx == 1) {
2019-01-29 08:24:10 +00:00
const auto* uExpr = static_cast<const clang::UnaryExprOrTypeTraitExpr*>(expr);
2018-12-08 05:18:17 +00:00
if (uExpr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
uExpr->getKind() == clang::UETT_SizeOf) {
const clang::Expr* argExpr = uExpr->getArgumentExpr();
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
2019-01-29 08:24:10 +00:00
argExpr = static_cast<const clang::ParenExpr*>(argExpr)->getSubExpr();
2018-12-08 05:18:17 +00:00
llvm::raw_string_ostream strStream2(sizeExpr);
argExpr->printPretty(strStream2, nullptr, context.getPrintingPolicy());
2018-07-16 07:42:14 +00:00
}
2018-12-08 05:18:17 +00:00
} else if (idx == 2) {
llvm::raw_string_ostream strStream(endianExprStr);
expr->printPretty(strStream, nullptr, context.getPrintingPolicy());
defaultEndian = false;
}
}
2018-12-08 05:18:17 +00:00
++idx;
}
clang::QualType templateType;
std::string ioOp;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Type) {
templateType = arg.getAsType().getCanonicalType();
if (defaultEndian)
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExpr);
else
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExpr, endianExprStr);
}
}
if (ioOp.empty()) {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
diag.AddString("Unable to use type '"s.append(templateType.getAsString()).append("' with Athena"));
2018-12-08 05:18:17 +00:00
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
continue;
}
if (sizeExpr.empty()) {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
2018-12-08 05:18:17 +00:00
diag.AddString("Unable to use count variable with Athena");
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
continue;
}
2018-12-13 07:38:14 +00:00
fileOut << " AT_PROP_CASE(" << propIdExpr << "):\n"
<< " Do" << ioOp << ";\n"
<< " return true;\n";
2019-10-01 07:10:47 +00:00
} else if (tsDecl->getName() == "Buffer") {
2018-12-08 05:18:17 +00:00
const clang::Expr* sizeExpr = nullptr;
std::string sizeExprStr;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Expression) {
const auto* uExpr =
static_cast<const clang::UnaryExprOrTypeTraitExpr*>(arg.getAsExpr()->IgnoreImpCasts());
2018-12-08 05:18:17 +00:00
if (uExpr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
uExpr->getKind() == clang::UETT_SizeOf) {
const clang::Expr* argExpr = uExpr->getArgumentExpr();
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
2019-01-29 08:24:10 +00:00
argExpr = static_cast<const clang::ParenExpr*>(argExpr)->getSubExpr();
2018-12-08 05:18:17 +00:00
sizeExpr = argExpr;
llvm::raw_string_ostream strStream(sizeExprStr);
argExpr->printPretty(strStream, nullptr, context.getPrintingPolicy());
}
}
}
if (sizeExprStr.empty()) {
if (sizeExpr) {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(sizeExpr->getExprLoc(), AthenaError);
2018-12-08 05:18:17 +00:00
diag.AddString("Unable to use size variable with Athena");
diag.AddSourceRange(clang::CharSourceRange(sizeExpr->getSourceRange(), true));
} else {
2019-01-29 08:24:10 +00:00
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
2018-12-08 05:18:17 +00:00
diag.AddString("Unable to use size variable with Athena");
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
}
continue;
}
std::string ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExprStr);
2018-12-13 07:38:14 +00:00
fileOut << " AT_PROP_CASE(" << propIdExpr << "):\n"
<< " Do" << ioOp << ";\n"
<< " return true;\n";
2019-10-01 07:10:47 +00:00
} else if (tsDecl->getName() == "String") {
2018-12-08 05:18:17 +00:00
std::string sizeExprStr;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Expression) {
const clang::Expr* expr = arg.getAsExpr()->IgnoreImpCasts();
2019-01-29 08:24:10 +00:00
const auto* uExpr = static_cast<const clang::UnaryExprOrTypeTraitExpr*>(expr);
2018-12-08 05:18:17 +00:00
llvm::APSInt sizeLiteral;
if (expr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
uExpr->getKind() == clang::UETT_SizeOf) {
const clang::Expr* argExpr = uExpr->getArgumentExpr();
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
2019-01-29 08:24:10 +00:00
argExpr = static_cast<const clang::ParenExpr*>(argExpr)->getSubExpr();
2018-12-08 05:18:17 +00:00
llvm::raw_string_ostream strStream(sizeExprStr);
argExpr->printPretty(strStream, nullptr, context.getPrintingPolicy());
} else if (expr->isIntegerConstantExpr(sizeLiteral, context)) {
sizeExprStr = sizeLiteral.toString(10);
}
}
}
std::string ioOp;
if (!sizeExprStr.empty())
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExprStr);
else
ioOp = GetOpString(fieldName, propIdExpr);
2018-12-13 07:38:14 +00:00
fileOut << " AT_PROP_CASE(" << propIdExpr << "):\n"
<< " Do" << ioOp << ";\n"
<< " return true;\n";
2019-10-01 07:10:47 +00:00
} else if (tsDecl->getName() == "WString") {
2018-12-08 05:18:17 +00:00
llvm::APSInt endian(64, -1);
std::string endianExprStr;
bool defaultEndian = true;
if (classParms->size() >= 2) {
const clang::NamedDecl* endianParm = classParms->getParam(1);
if (endianParm->getKind() == clang::Decl::NonTypeTemplateParm) {
2019-01-29 08:24:10 +00:00
const auto* nttParm = static_cast<const clang::NonTypeTemplateParmDecl*>(endianParm);
2018-12-08 05:18:17 +00:00
llvm::raw_string_ostream strStream(endianExprStr);
nttParm->print(strStream, context.getPrintingPolicy());
}
}
std::string sizeExprStr;
size_t idx = 0;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Expression) {
const clang::Expr* expr = arg.getAsExpr()->IgnoreImpCasts();
if (idx == 0) {
llvm::APSInt sizeLiteral;
2019-01-29 08:24:10 +00:00
const auto* uExpr = static_cast<const clang::UnaryExprOrTypeTraitExpr*>(expr);
2018-12-08 05:18:17 +00:00
if (expr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
uExpr->getKind() == clang::UETT_SizeOf) {
const clang::Expr* argExpr = uExpr->getArgumentExpr();
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
2019-01-29 08:24:10 +00:00
argExpr = static_cast<const clang::ParenExpr*>(argExpr)->getSubExpr();
2018-12-08 05:18:17 +00:00
llvm::raw_string_ostream strStream2(sizeExprStr);
argExpr->printPretty(strStream2, nullptr, context.getPrintingPolicy());
} else if (expr->isIntegerConstantExpr(sizeLiteral, context)) {
sizeExprStr = sizeLiteral.toString(10);
2015-08-03 01:42:47 +00:00
}
2018-12-08 05:18:17 +00:00
} else if (idx == 1) {
llvm::raw_string_ostream strStream(endianExprStr);
expr->printPretty(strStream, nullptr, context.getPrintingPolicy());
defaultEndian = false;
}
}
++idx;
}
std::string ioOp;
if (!sizeExprStr.empty()) {
if (defaultEndian)
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExprStr);
else
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExprStr, endianExprStr);
} else {
if (defaultEndian)
ioOp = GetOpString(fieldName, propIdExpr);
else
ioOp = GetOpString(fieldName, propIdExpr, endianExprStr);
}
2018-12-13 07:38:14 +00:00
fileOut << " AT_PROP_CASE(" << propIdExpr << "):\n"
<< " Do" << ioOp << ";\n"
<< " return true;\n";
2018-12-08 05:18:17 +00:00
} else {
const clang::NamedDecl* nd = tsDecl->getTemplatedDecl();
if (const clang::CXXRecordDecl* rd = clang::dyn_cast_or_null<clang::CXXRecordDecl>(nd)) {
std::string baseDNA2;
if (isDNARecord(rd, baseDNA2)) {
2018-12-13 07:38:14 +00:00
fileOut << " AT_PROP_CASE(" << propIdExpr << "):\n"
<< " Do" << GetOpString(fieldName, propIdExpr) << ";\n"
<< " return true;\n";
2015-08-03 01:42:47 +00:00
}
2018-12-08 05:18:17 +00:00
}
2015-08-03 01:42:47 +00:00
}
} else if (regType->getTypeClass() == clang::Type::Record) {
2018-12-08 05:18:17 +00:00
const clang::CXXRecordDecl* cxxRDecl = regType->getAsCXXRecordDecl();
std::string baseDNA2;
if (cxxRDecl && isDNARecord(cxxRDecl, baseDNA2)) {
2018-12-13 07:38:14 +00:00
fileOut << " AT_PROP_CASE(" << propIdExpr << "):\n"
<< " Do" << GetOpString(fieldName, propIdExpr) << ";\n"
<< " return true;\n";
2018-12-08 05:18:17 +00:00
}
}
}
2015-08-03 01:42:47 +00:00
2018-12-13 07:38:14 +00:00
fileOut << " default:\n return false;\n }\n}\n\n";
2018-12-08 05:18:17 +00:00
}
2018-12-08 05:18:17 +00:00
public:
explicit ATDNAEmitVisitor(clang::ASTContext& ctxin, StreamOut& fo) : context(ctxin), fileOut(fo) {}
bool VisitCXXRecordDecl(clang::CXXRecordDecl* decl) {
if (!EmitIncludes && !context.getSourceManager().isInMainFile(decl->getLocation()))
return true;
if (decl->isInvalidDecl() || !decl->hasDefinition() || !decl->isCompleteDefinition())
return true;
if (!decl->getNumBases())
return true;
/* First ensure this inherits from struct athena::io::DNA */
std::string baseDNA;
if (!isDNARecord(decl, baseDNA))
return true;
/* Determine if is is a YAML DNA */
bool isYamlDNA = false;
for (const clang::CXXMethodDecl* method : decl->methods())
if (method->getDeclName().isIdentifier() && (method->getName() == "read" || method->getName() == "write") &&
2018-12-08 05:18:17 +00:00
method->getNumParams() == 1 &&
method->getParamDecl(0)->getType().getAsString() == "athena::io::YAMLDocReader &") {
isYamlDNA = true;
break;
}
/* Determine if this is a regular DNA or PropDNA */
bool isPropDNA = false;
for (const clang::Decl* d : decl->decls())
if (const clang::FunctionTemplateDecl* m = clang::dyn_cast_or_null<clang::FunctionTemplateDecl>(d))
2019-10-01 07:10:47 +00:00
if (m->getDeclName().isIdentifier() && m->getName() == "Lookup") {
2018-12-08 05:18:17 +00:00
isPropDNA = true;
break;
}
2018-12-08 05:18:17 +00:00
/* Make sure there isn't Delete meta type */
for (const clang::FieldDecl* field : decl->fields()) {
clang::QualType qualType = field->getType().getCanonicalType();
const clang::Type* regType = qualType.getTypePtrOrNull();
if (regType) {
const clang::CXXRecordDecl* rDecl = regType->getAsCXXRecordDecl();
if (rDecl) {
2019-10-01 07:10:47 +00:00
if (rDecl->getName() == "Delete") {
2018-12-08 05:18:17 +00:00
const clang::CXXRecordDecl* rParentDecl = llvm::dyn_cast_or_null<clang::CXXRecordDecl>(rDecl->getParent());
if (rParentDecl) {
std::string parentCheck = rParentDecl->getTypeForDecl()->getCanonicalTypeInternal().getAsString();
if (!parentCheck.compare(0, sizeof(ATHENA_DNA_BASETYPE) - 1, ATHENA_DNA_BASETYPE))
return true;
2015-08-03 01:42:47 +00:00
}
2018-12-08 05:18:17 +00:00
}
2015-08-03 01:42:47 +00:00
}
2018-12-08 05:18:17 +00:00
}
}
2015-08-03 01:42:47 +00:00
2018-12-08 05:18:17 +00:00
std::vector<std::pair<std::string, int>> specializations = GetNestedTypeSpecializations(decl);
emitEnumerateFunc(decl, baseDNA);
if (isPropDNA) {
emitLookupFunc(decl, baseDNA);
for (const auto& specialization : specializations)
fileOut << "AT_SPECIALIZE_PROPDNA(" << specialization.first << ")\n";
} else if (isYamlDNA) {
for (const auto& specialization : specializations)
fileOut << "AT_SPECIALIZE_DNA_YAML(" << specialization.first << ")\n";
} else {
for (const auto& specialization : specializations)
fileOut << "AT_SPECIALIZE_DNA(" << specialization.first << ")\n";
}
fileOut << "\n\n";
2015-08-03 01:42:47 +00:00
2018-12-08 05:18:17 +00:00
for (const auto& specialization : specializations) {
for (int i = 0; i < specialization.second; ++i)
fileOut << "template <>\n";
2019-10-01 07:10:47 +00:00
fileOut << "std::string_view " << specialization.first << "::DNAType() {\n return \"" << specialization.first
<< "\"sv;\n}\n";
2015-08-03 01:42:47 +00:00
}
2018-12-08 05:18:17 +00:00
fileOut << "\n\n";
return true;
}
2015-08-03 01:42:47 +00:00
};
2018-12-08 05:18:17 +00:00
class ATDNAConsumer : public clang::ASTConsumer {
std::unique_ptr<StreamOut> fileOut;
StreamOut& fileOutOld;
ATDNAEmitVisitor emitVisitor;
2015-08-03 01:42:47 +00:00
public:
2018-12-08 05:18:17 +00:00
explicit ATDNAConsumer(clang::ASTContext& context, std::unique_ptr<StreamOut>&& fo, StreamOut* foOld)
: fileOut(std::move(fo)), fileOutOld(*foOld), emitVisitor(context, *foOld) {}
void HandleTranslationUnit(clang::ASTContext& context) override {
/* Write file head */
fileOutOld << "/* Auto generated atdna implementation */\n"
"#include \"athena/DNAOp.hpp\"\n";
for (const std::string& inputf : InputFilenames)
fileOutOld << "#include \"" << inputf << "\"\n";
fileOutOld << "\n";
/* Emit file */
emitVisitor.TraverseDecl(context.getTranslationUnitDecl());
}
2015-08-03 01:42:47 +00:00
};
2018-12-08 05:18:17 +00:00
class ATDNAAction : public clang::ASTFrontendAction {
/* Used by LLVM 3.9+; client owns stream */
std::unique_ptr<StreamOut> MakeStreamOut(std::unique_ptr<StreamOut>&& so, StreamOut*& outPtr) {
outPtr = so.get();
return std::move(so);
}
2018-12-08 05:18:17 +00:00
/* Used by previous versions of LLVM; CompilerInstance owns stream */
std::unique_ptr<StreamOut> MakeStreamOut(StreamOut* so, StreamOut*& outPtr) {
outPtr = so;
return {};
}
#if LLVM_VERSION_MAJOR >= 9
2019-10-19 01:31:35 +00:00
llvm::Optional<clang::DependencyFileGenerator> TheDependencyFileGenerator;
#else
2018-12-08 05:18:17 +00:00
std::unique_ptr<clang::DependencyFileGenerator> TheDependencyFileGenerator;
#endif
2015-08-03 01:42:47 +00:00
public:
2018-12-08 05:18:17 +00:00
explicit ATDNAAction() = default;
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance& compiler,
llvm::StringRef /*filename*/) override {
clang::DependencyOutputOptions DepOpts;
DepOpts.OutputFile = DepFileOut.getValue();
DepOpts.Targets = DepFileTargets;
#if LLVM_VERSION_MAJOR >= 9
2019-10-19 01:31:35 +00:00
if (!DepOpts.OutputFile.empty()) {
TheDependencyFileGenerator.emplace(DepOpts);
TheDependencyFileGenerator->attachToPreprocessor(compiler.getPreprocessor());
}
#else
2018-12-08 05:18:17 +00:00
if (!DepOpts.OutputFile.empty())
TheDependencyFileGenerator.reset(
clang::DependencyFileGenerator::CreateAndAttachToPreprocessor(compiler.getPreprocessor(), DepOpts));
#endif
2018-12-08 05:18:17 +00:00
std::unique_ptr<StreamOut> fileout;
StreamOut* fileoutOld;
if (OutputFilename.size())
fileout = MakeStreamOut(compiler.createOutputFile(OutputFilename, false, true, "", "", true), fileoutOld);
else
fileout = MakeStreamOut(compiler.createDefaultOutputFile(false, "a", "cpp"), fileoutOld);
AthenaError =
compiler.getASTContext().getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Error, "Athena error: %0");
return std::unique_ptr<clang::ASTConsumer>(
new ATDNAConsumer(compiler.getASTContext(), std::move(fileout), fileoutOld));
}
2015-08-03 01:42:47 +00:00
};
2018-12-08 05:18:17 +00:00
int main(int argc, const char** argv) {
llvm::cl::ParseCommandLineOptions(argc, argv, "Athena DNA Generator");
if (Help)
llvm::cl::PrintHelpMessage();
2015-08-03 01:42:47 +00:00
std::vector<std::string> args = {
XSTR(ATDNA_ARGV0),
"-fsyntax-only",
"-std=c++2a",
2021-04-08 20:18:20 +00:00
#if __x86_64__
"-mno-sse",
#endif
"-D__atdna__=1",
"-Wno-expansion-to-defined",
"-Wno-nullability-completeness",
"-Werror=shadow-field",
"-I" XSTR(INSTALL_PREFIX) "/include/Athena",
};
for (int a = 1; a < argc; ++a) {
args.emplace_back(argv[a]);
}
2018-12-08 05:18:17 +00:00
llvm::IntrusiveRefCntPtr<clang::FileManager> fman(new clang::FileManager(clang::FileSystemOptions()));
2020-04-22 10:36:08 +00:00
#if LLVM_VERSION_MAJOR >= 10
clang::tooling::ToolInvocation TI(std::move(args), std::make_unique<ATDNAAction>(), fman.get());
#else
2018-12-08 05:18:17 +00:00
ATDNAAction* action = new ATDNAAction();
clang::tooling::ToolInvocation TI(std::move(args), action, fman.get());
2020-04-22 10:36:08 +00:00
#endif
2018-12-08 05:18:17 +00:00
if (!TI.run())
return 1;
return 0;
2015-08-03 01:42:47 +00:00
}