2015-06-19 02:55:05 +00:00
|
|
|
#include <stdio.h>
|
2015-06-15 02:49:02 +00:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Sema/Sema.h"
|
|
|
|
#include "clang/AST/RecordLayout.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
2015-06-18 07:32:11 +00:00
|
|
|
#include "clang/AST/TypeLoc.h"
|
2015-06-17 00:25:48 +00:00
|
|
|
#include "clang/Basic/Version.h"
|
2015-06-16 02:29:53 +00:00
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2015-06-15 02:49:02 +00:00
|
|
|
|
2015-06-17 01:05:43 +00:00
|
|
|
#ifndef INSTALL_PREFIX
|
|
|
|
#define INSTALL_PREFIX /usr/local
|
2015-06-17 00:57:06 +00:00
|
|
|
#endif
|
|
|
|
#define XSTR(s) STR(s)
|
|
|
|
#define STR(s) #s
|
|
|
|
|
2015-06-16 02:29:53 +00:00
|
|
|
static llvm::cl::opt<bool> Help("h", llvm::cl::desc("Alias for -help"), llvm::cl::Hidden);
|
|
|
|
|
|
|
|
static llvm::cl::OptionCategory ATDNAFormatCategory("atdna options");
|
|
|
|
|
|
|
|
static llvm::cl::opt<std::string> OutputFilename("o",
|
|
|
|
llvm::cl::desc("Specify output filename"),
|
|
|
|
llvm::cl::value_desc("filename"),
|
|
|
|
llvm::cl::Prefix);
|
2015-06-17 00:25:48 +00:00
|
|
|
|
2015-06-16 02:29:53 +00:00
|
|
|
static llvm::cl::list<std::string> InputFilenames(llvm::cl::Positional,
|
|
|
|
llvm::cl::desc("<Input files>"),
|
|
|
|
llvm::cl::OneOrMore);
|
|
|
|
|
2015-06-18 07:32:11 +00:00
|
|
|
static llvm::cl::list<std::string> IncludeSearchPaths("I",
|
|
|
|
llvm::cl::desc("Header search path"),
|
|
|
|
llvm::cl::Prefix);
|
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
static unsigned AthenaError = 0;
|
|
|
|
#define ATHENA_DNA_BASETYPE "struct Athena::io::DNA"
|
|
|
|
|
2015-06-17 00:25:48 +00:00
|
|
|
class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor>
|
2015-06-15 02:49:02 +00:00
|
|
|
{
|
|
|
|
clang::ASTContext& context;
|
2015-06-17 00:25:48 +00:00
|
|
|
llvm::raw_fd_ostream& fileOut;
|
2015-06-15 02:49:02 +00:00
|
|
|
|
2015-06-17 00:25:48 +00:00
|
|
|
std::string GetOpString(const clang::Type* theType, unsigned width,
|
2015-06-18 07:32:11 +00:00
|
|
|
const std::string& fieldName, bool writerPass,
|
|
|
|
bool& isDNAType)
|
2015-06-15 02:49:02 +00:00
|
|
|
{
|
2015-06-18 07:32:11 +00:00
|
|
|
isDNAType = false;
|
2015-06-17 00:25:48 +00:00
|
|
|
if (writerPass)
|
2015-06-15 02:49:02 +00:00
|
|
|
{
|
2015-06-17 00:25:48 +00:00
|
|
|
if (theType->isBuiltinType())
|
2015-06-15 02:49:02 +00:00
|
|
|
{
|
2015-06-17 00:25:48 +00:00
|
|
|
const clang::BuiltinType* bType = (clang::BuiltinType*)theType;
|
2015-06-18 07:32:11 +00:00
|
|
|
if (bType->isBooleanType())
|
|
|
|
{
|
2015-06-18 07:37:04 +00:00
|
|
|
return "writer.writeBool(" + fieldName + ");";
|
2015-06-18 07:32:11 +00:00
|
|
|
}
|
|
|
|
else if (bType->isUnsignedInteger())
|
2015-06-16 02:29:53 +00:00
|
|
|
{
|
2015-06-17 00:25:48 +00:00
|
|
|
if (width == 8)
|
|
|
|
return "writer.writeUByte(" + fieldName + ");";
|
|
|
|
else if (width == 16)
|
|
|
|
return "writer.writeUint16(" + fieldName + ");";
|
|
|
|
else if (width == 32)
|
|
|
|
return "writer.writeUint32(" + fieldName + ");";
|
|
|
|
else if (width == 64)
|
|
|
|
return "writer.writeUint64(" + fieldName + ");";
|
|
|
|
}
|
|
|
|
else if (bType->isSignedInteger())
|
|
|
|
{
|
|
|
|
if (width == 8)
|
|
|
|
return "writer.writeByte(" + fieldName + ");";
|
|
|
|
else if (width == 16)
|
|
|
|
return "writer.writeInt16(" + fieldName + ");";
|
|
|
|
else if (width == 32)
|
|
|
|
return "writer.writeInt32(" + fieldName + ");";
|
|
|
|
else if (width == 64)
|
|
|
|
return "writer.writeInt64(" + fieldName + ");";
|
|
|
|
}
|
|
|
|
else if (bType->isFloatingPoint())
|
|
|
|
{
|
|
|
|
if (width == 32)
|
|
|
|
return "writer.writeFloat(" + fieldName + ");";
|
|
|
|
else if (width == 64)
|
|
|
|
return "writer.writeDouble(" + fieldName + ");";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (theType->isRecordType())
|
|
|
|
{
|
2015-06-18 07:32:11 +00:00
|
|
|
const clang::CXXRecordDecl* rDecl = theType->getAsCXXRecordDecl();
|
2015-06-17 00:25:48 +00:00
|
|
|
for (const clang::FieldDecl* field : rDecl->fields())
|
|
|
|
{
|
|
|
|
if (!field->getNameAsString().compare("clangVec"))
|
2015-06-16 02:29:53 +00:00
|
|
|
{
|
2015-06-17 00:25:48 +00:00
|
|
|
const clang::VectorType* vType = (clang::VectorType*)field->getType().getTypePtr();
|
|
|
|
if (vType->isVectorType())
|
2015-06-16 02:29:53 +00:00
|
|
|
{
|
2015-06-17 00:25:48 +00:00
|
|
|
const clang::BuiltinType* eType = (clang::BuiltinType*)vType->getElementType().getTypePtr();
|
|
|
|
if (!eType->isBuiltinType() || !eType->isFloatingPoint() ||
|
|
|
|
context.getTypeInfo(eType).Width != 32)
|
|
|
|
continue;
|
|
|
|
if (vType->getNumElements() == 3)
|
|
|
|
return "writer.writeVec3f(" + fieldName + ");";
|
|
|
|
else if (vType->getNumElements() == 4)
|
|
|
|
return "writer.writeVec4f(" + fieldName + ");";
|
2015-06-16 02:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-18 07:32:11 +00:00
|
|
|
for (const clang::CXXBaseSpecifier& base : rDecl->bases())
|
2015-06-19 02:55:05 +00:00
|
|
|
if (!base.getType().getCanonicalType().getAsString().compare(0, sizeof(ATHENA_DNA_BASETYPE)-1, ATHENA_DNA_BASETYPE))
|
2015-06-18 07:32:11 +00:00
|
|
|
{
|
|
|
|
isDNAType = true;
|
|
|
|
return "write(writer);";
|
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-17 00:25:48 +00:00
|
|
|
if (theType->isBuiltinType())
|
2015-06-16 07:11:56 +00:00
|
|
|
{
|
2015-06-17 00:25:48 +00:00
|
|
|
const clang::BuiltinType* bType = (clang::BuiltinType*)theType;
|
2015-06-18 07:32:11 +00:00
|
|
|
if (bType->isBooleanType())
|
|
|
|
{
|
|
|
|
return "reader.readBool()";
|
|
|
|
}
|
|
|
|
else if (bType->isUnsignedInteger())
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
|
|
|
if (width == 8)
|
|
|
|
return "reader.readUByte()";
|
|
|
|
else if (width == 16)
|
|
|
|
return "reader.readUint16()";
|
|
|
|
else if (width == 32)
|
|
|
|
return "reader.readUint32()";
|
|
|
|
else if (width == 64)
|
|
|
|
return "reader.readUint64()";
|
|
|
|
}
|
|
|
|
else if (bType->isSignedInteger())
|
|
|
|
{
|
|
|
|
if (width == 8)
|
|
|
|
return "reader.readByte()";
|
|
|
|
else if (width == 16)
|
|
|
|
return "reader.readInt16()";
|
|
|
|
else if (width == 32)
|
|
|
|
return "reader.readInt32()";
|
|
|
|
else if (width == 64)
|
|
|
|
return "reader.readInt64()";
|
|
|
|
}
|
|
|
|
else if (bType->isFloatingPoint())
|
|
|
|
{
|
|
|
|
if (width == 32)
|
|
|
|
return "reader.readFloat()";
|
|
|
|
else if (width == 64)
|
|
|
|
return "reader.readDouble()";
|
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
}
|
2015-06-17 00:25:48 +00:00
|
|
|
else if (theType->isRecordType())
|
2015-06-16 07:11:56 +00:00
|
|
|
{
|
2015-06-18 07:32:11 +00:00
|
|
|
const clang::CXXRecordDecl* rDecl = theType->getAsCXXRecordDecl();
|
2015-06-17 00:25:48 +00:00
|
|
|
for (const clang::FieldDecl* field : rDecl->fields())
|
|
|
|
{
|
|
|
|
if (!field->getNameAsString().compare("clangVec"))
|
|
|
|
{
|
|
|
|
const clang::VectorType* vType = (clang::VectorType*)field->getType().getTypePtr();
|
|
|
|
if (vType->isVectorType())
|
|
|
|
{
|
|
|
|
const clang::BuiltinType* eType = (clang::BuiltinType*)vType->getElementType().getTypePtr();
|
|
|
|
if (!eType->isBuiltinType() || !eType->isFloatingPoint() ||
|
|
|
|
context.getTypeInfo(eType).Width != 32)
|
|
|
|
continue;
|
|
|
|
if (vType->getNumElements() == 3)
|
|
|
|
return "reader.readVec3f()";
|
|
|
|
else if (vType->getNumElements() == 4)
|
|
|
|
return "reader.readVec4f()";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-18 07:32:11 +00:00
|
|
|
for (const clang::CXXBaseSpecifier& base : rDecl->bases())
|
2015-06-19 02:55:05 +00:00
|
|
|
if (!base.getType().getCanonicalType().getAsString().compare(0, sizeof(ATHENA_DNA_BASETYPE)-1, ATHENA_DNA_BASETYPE))
|
2015-06-18 07:32:11 +00:00
|
|
|
{
|
|
|
|
isDNAType = true;
|
|
|
|
return "read(reader);";
|
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ATDNAEmitVisitor(clang::ASTContext& ctxin,
|
2015-06-17 00:25:48 +00:00
|
|
|
llvm::raw_fd_ostream& fo)
|
|
|
|
: context(ctxin), fileOut(fo) {}
|
2015-06-16 07:11:56 +00:00
|
|
|
|
|
|
|
bool VisitCXXRecordDecl(clang::CXXRecordDecl* decl)
|
|
|
|
{
|
|
|
|
if (decl->isInvalidDecl() || !decl->hasDefinition())
|
2015-06-15 02:49:02 +00:00
|
|
|
return true;
|
|
|
|
|
2015-06-16 07:11:56 +00:00
|
|
|
if (!decl->getNumBases())
|
|
|
|
return true;
|
2015-06-16 02:29:53 +00:00
|
|
|
|
2015-06-16 07:11:56 +00:00
|
|
|
/* First ensure this inherits from struct Athena::io::DNA */
|
|
|
|
bool foundDNA = false;
|
|
|
|
for (const clang::CXXBaseSpecifier& base : decl->bases())
|
2015-06-15 02:49:02 +00:00
|
|
|
{
|
2015-06-16 07:11:56 +00:00
|
|
|
clang::QualType canonType = base.getType().getCanonicalType();
|
2015-06-19 02:55:05 +00:00
|
|
|
if (!canonType.getAsString().compare(0, sizeof(ATHENA_DNA_BASETYPE)-1, ATHENA_DNA_BASETYPE))
|
2015-06-15 02:49:02 +00:00
|
|
|
{
|
2015-06-16 07:11:56 +00:00
|
|
|
foundDNA = true;
|
|
|
|
break;
|
2015-06-15 02:49:02 +00:00
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
}
|
|
|
|
if (!foundDNA)
|
|
|
|
return true;
|
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
/* Two passes - read then write */
|
2015-06-16 07:11:56 +00:00
|
|
|
for (int p=0 ; p<2 ; ++p)
|
|
|
|
{
|
|
|
|
if (p)
|
2015-06-19 02:55:05 +00:00
|
|
|
fileOut << "void " << decl->getQualifiedNameAsString() << "::write(Athena::io::IStreamWriter& writer) const\n{\n";
|
2015-06-16 07:11:56 +00:00
|
|
|
else
|
2015-06-19 02:55:05 +00:00
|
|
|
fileOut << "void " << decl->getQualifiedNameAsString() << "::read(Athena::io::IStreamReader& reader)\n{\n";
|
2015-06-17 00:25:48 +00:00
|
|
|
int currentEndian = -1;
|
2015-06-16 07:11:56 +00:00
|
|
|
|
|
|
|
for (const clang::FieldDecl* field : decl->fields())
|
2015-06-15 02:49:02 +00:00
|
|
|
{
|
2015-06-16 07:11:56 +00:00
|
|
|
clang::QualType qualType = field->getType();
|
|
|
|
clang::TypeInfo regTypeInfo = context.getTypeInfo(qualType);
|
|
|
|
const clang::Type* regType = qualType.getTypePtrOrNull();
|
2015-06-17 00:25:48 +00:00
|
|
|
if (regType->getTypeClass() == clang::Type::Elaborated)
|
|
|
|
regType = regType->getUnqualifiedDesugaredType();
|
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
/* Resolve constant array */
|
|
|
|
size_t arraySize = 1;
|
|
|
|
bool isArray = false;
|
|
|
|
if (regType->getTypeClass() == clang::Type::ConstantArray)
|
2015-06-16 02:29:53 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
isArray = true;
|
|
|
|
const clang::ConstantArrayType* caType = (clang::ConstantArrayType*)regType;
|
|
|
|
arraySize = caType->getSize().getZExtValue();
|
|
|
|
qualType = caType->getElementType();
|
|
|
|
regTypeInfo = context.getTypeInfo(qualType);
|
|
|
|
regType = qualType.getTypePtrOrNull();
|
|
|
|
if (regType->getTypeClass() == clang::Type::Elaborated)
|
|
|
|
regType = regType->getUnqualifiedDesugaredType();
|
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
for (int e=0 ; e<arraySize ; ++e)
|
|
|
|
{
|
|
|
|
std::string fieldName;
|
|
|
|
if (isArray)
|
2015-06-15 02:49:02 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
char subscript[16];
|
|
|
|
snprintf(subscript, 16, "[%d]", e);
|
|
|
|
fieldName = field->getNameAsString() + subscript;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fieldName = field->getNameAsString();
|
|
|
|
|
|
|
|
if (regType->getTypeClass() == clang::Type::TemplateSpecialization)
|
|
|
|
{
|
|
|
|
const clang::TemplateSpecializationType* tsType = (const clang::TemplateSpecializationType*)regType;
|
|
|
|
const clang::TemplateDecl* tsDecl = tsType->getTemplateName().getAsTemplateDecl();
|
|
|
|
const clang::TemplateParameterList* classParms = tsDecl->getTemplateParameters();
|
|
|
|
|
|
|
|
if (!tsDecl->getNameAsString().compare("Value"))
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
llvm::APSInt endian(64, -1);
|
|
|
|
const clang::Expr* endianExpr = nullptr;
|
|
|
|
if (classParms->size() >= 2)
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
const clang::NamedDecl* endianParm = classParms->getParam(1);
|
|
|
|
if (endianParm->getKind() == clang::Decl::NonTypeTemplateParm)
|
|
|
|
{
|
|
|
|
const clang::NonTypeTemplateParmDecl* nttParm = (clang::NonTypeTemplateParmDecl*)endianParm;
|
|
|
|
const clang::Expr* defArg = nttParm->getDefaultArgument();
|
|
|
|
endianExpr = defArg;
|
|
|
|
if (!defArg->isIntegerConstantExpr(endian, context))
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(defArg->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Endian value must be 'BigEndian' or 'LittleEndian'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(defArg->getSourceRange(), true));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2015-06-17 00:25:48 +00:00
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
clang::QualType templateType;
|
|
|
|
std::string ioOp;
|
|
|
|
bool isDNAType = false;
|
|
|
|
const clang::TemplateArgument* typeArg = nullptr;
|
|
|
|
for (const clang::TemplateArgument& arg : *tsType)
|
2015-06-16 07:11:56 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
if (arg.getKind() == clang::TemplateArgument::Type)
|
|
|
|
{
|
|
|
|
typeArg = &arg;
|
|
|
|
templateType = arg.getAsType().getCanonicalType();
|
|
|
|
const clang::Type* type = arg.getAsType().getCanonicalType().getTypePtr();
|
|
|
|
ioOp = GetOpString(type, regTypeInfo.Width, fieldName, p, isDNAType);
|
|
|
|
}
|
|
|
|
else if (arg.getKind() == clang::TemplateArgument::Expression)
|
|
|
|
{
|
|
|
|
const clang::Expr* expr = arg.getAsExpr();
|
|
|
|
endianExpr = expr;
|
|
|
|
if (expr->isIntegerConstantExpr(endian, context))
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(expr->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Endian value must be 'BigEndian' or 'LittleEndian'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(expr->getSourceRange(), true));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
}
|
2015-06-19 02:55:05 +00:00
|
|
|
|
|
|
|
int endianVal = endian.getSExtValue();
|
|
|
|
if (endianVal != 0 && endianVal != 1)
|
2015-06-16 07:11:56 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
if (endianExpr)
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(endianExpr->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Endian value must be 'BigEndian' or 'LittleEndian'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(endianExpr->getSourceRange(), true));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Endian value must be 'BigEndian' or 'LittleEndian'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
|
|
|
|
}
|
|
|
|
continue;
|
2015-06-16 07:11:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
if (ioOp.empty())
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Unable to use type '" + tsDecl->getNameAsString() + "' with Athena");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
if (currentEndian != endianVal)
|
|
|
|
{
|
|
|
|
if (endianVal == 0)
|
|
|
|
fileOut << (p ? " writer.setEndian(Athena::LittleEndian);\n" : " reader.setEndian(Athena::LittleEndian);\n");
|
|
|
|
else if (endianVal == 1)
|
|
|
|
fileOut << (p ? " writer.setEndian(Athena::BigEndian);\n" : " reader.setEndian(Athena::BigEndian);\n");
|
|
|
|
currentEndian = endianVal;
|
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
fileOut << " /* " << fieldName << " */\n";
|
|
|
|
if (!p)
|
|
|
|
fileOut << " " << fieldName << " = " << ioOp << ";\n";
|
|
|
|
else
|
|
|
|
fileOut << " " << ioOp << "\n";
|
|
|
|
}
|
|
|
|
else if (!tsDecl->getNameAsString().compare("Vector"))
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
llvm::APSInt endian(64, -1);
|
|
|
|
const clang::Expr* endianExpr = nullptr;
|
|
|
|
if (classParms->size() >= 3)
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
const clang::NamedDecl* endianParm = classParms->getParam(2);
|
|
|
|
if (endianParm->getKind() == clang::Decl::NonTypeTemplateParm)
|
|
|
|
{
|
|
|
|
const clang::NonTypeTemplateParmDecl* nttParm = (clang::NonTypeTemplateParmDecl*)endianParm;
|
|
|
|
const clang::Expr* defArg = nttParm->getDefaultArgument();
|
|
|
|
endianExpr = defArg;
|
|
|
|
if (!defArg->isIntegerConstantExpr(endian, context))
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(defArg->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Endian value must be 'BigEndian' or 'LittleEndian'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(defArg->getSourceRange(), true));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clang::QualType templateType;
|
|
|
|
std::string ioOp;
|
|
|
|
bool isDNAType = false;
|
|
|
|
std::string sizeExpr;
|
|
|
|
const clang::TemplateArgument* typeArg = nullptr;
|
|
|
|
const clang::TemplateArgument* sizeArg = nullptr;
|
|
|
|
size_t idx = 0;
|
|
|
|
bool bad = false;
|
|
|
|
for (const clang::TemplateArgument& arg : *tsType)
|
|
|
|
{
|
|
|
|
if (arg.getKind() == clang::TemplateArgument::Type)
|
|
|
|
{
|
|
|
|
typeArg = &arg;
|
|
|
|
templateType = arg.getAsType().getCanonicalType();
|
|
|
|
clang::TypeInfo typeInfo = context.getTypeInfo(templateType);
|
|
|
|
static const std::string elemStr = "elem";
|
|
|
|
ioOp = GetOpString(templateType.getTypePtr(), typeInfo.Width, elemStr, p, isDNAType);
|
|
|
|
}
|
|
|
|
else if (arg.getKind() == clang::TemplateArgument::Expression)
|
|
|
|
{
|
|
|
|
const clang::Expr* expr = arg.getAsExpr();
|
|
|
|
if (idx == 1)
|
|
|
|
{
|
|
|
|
sizeArg = &arg;
|
|
|
|
const clang::UnaryExprOrTypeTraitExpr* uExpr = (clang::UnaryExprOrTypeTraitExpr*)expr;
|
|
|
|
if (uExpr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
|
|
|
|
uExpr->getKind() == clang::UETT_SizeOf)
|
|
|
|
{
|
|
|
|
const clang::Expr* argExpr = uExpr->getArgumentExpr();
|
|
|
|
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
|
|
|
|
argExpr = ((clang::ParenExpr*)argExpr)->getSubExpr();
|
|
|
|
llvm::raw_string_ostream strStream(sizeExpr);
|
|
|
|
argExpr->printPretty(strStream, nullptr, context.getPrintingPolicy());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (idx == 2)
|
|
|
|
{
|
|
|
|
endianExpr = expr;
|
|
|
|
if (!expr->isIntegerConstantExpr(endian, context))
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(expr->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Endian value must be 'BigEndian' or 'LittleEndian'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(expr->getSourceRange(), true));
|
|
|
|
bad = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
if (bad)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int endianVal = endian.getSExtValue();
|
|
|
|
if (endianVal != 0 && endianVal != 1)
|
|
|
|
{
|
|
|
|
if (endianExpr)
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(endianExpr->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Endian value must be 'BigEndian' or 'LittleEndian'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(endianExpr->getSourceRange(), true));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Endian value must be 'BigEndian' or 'LittleEndian'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioOp.empty())
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Unable to use type '" + templateType.getAsString() + "' with Athena");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sizeExpr.empty())
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Unable to use count variable with Athena");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
|
|
|
|
continue;
|
2015-06-17 00:25:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
if (currentEndian != endianVal)
|
|
|
|
{
|
|
|
|
if (endianVal == 0)
|
|
|
|
fileOut << (p ? " writer.setEndian(Athena::LittleEndian);\n" : " reader.setEndian(Athena::LittleEndian);\n");
|
|
|
|
else if (endianVal == 1)
|
|
|
|
fileOut << (p ? " writer.setEndian(Athena::BigEndian);\n" : " reader.setEndian(Athena::BigEndian);\n");
|
|
|
|
currentEndian = endianVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileOut << " /* " << fieldName << " */\n";
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
fileOut << " " << fieldName << ".clear();\n";
|
|
|
|
fileOut << " " << fieldName << ".reserve(" << sizeExpr << ");\n";
|
|
|
|
if (isDNAType)
|
|
|
|
fileOut << " for (int i=0 ; i<(" << sizeExpr << ") ; ++i)\n"
|
|
|
|
" {\n"
|
|
|
|
" " << fieldName << ".emplace_back();\n"
|
|
|
|
" " << fieldName << ".back()." << ioOp << "\n"
|
|
|
|
" }\n";
|
|
|
|
else
|
|
|
|
fileOut << " for (int i=0 ; i<(" << sizeExpr << ") ; ++i)\n " << fieldName << ".push_back(" << ioOp << ");\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fileOut << " for (auto elem : " << fieldName << ")\n";
|
|
|
|
if (isDNAType)
|
|
|
|
fileOut << " elem." << ioOp << "\n";
|
|
|
|
else
|
|
|
|
fileOut << " " << ioOp << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (!tsDecl->getNameAsString().compare("Buffer"))
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
const clang::Expr* sizeExpr = nullptr;
|
|
|
|
std::string sizeExprStr;
|
|
|
|
for (const clang::TemplateArgument& arg : *tsType)
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
if (arg.getKind() == clang::TemplateArgument::Expression)
|
|
|
|
{
|
|
|
|
const clang::UnaryExprOrTypeTraitExpr* uExpr = (clang::UnaryExprOrTypeTraitExpr*)arg.getAsExpr();
|
|
|
|
if (uExpr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
|
|
|
|
uExpr->getKind() == clang::UETT_SizeOf)
|
|
|
|
{
|
|
|
|
const clang::Expr* argExpr = uExpr->getArgumentExpr();
|
|
|
|
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
|
|
|
|
argExpr = ((clang::ParenExpr*)argExpr)->getSubExpr();
|
|
|
|
sizeExpr = argExpr;
|
|
|
|
llvm::raw_string_ostream strStream(sizeExprStr);
|
|
|
|
argExpr->printPretty(strStream, nullptr, context.getPrintingPolicy());
|
|
|
|
}
|
|
|
|
}
|
2015-06-17 00:25:48 +00:00
|
|
|
}
|
2015-06-19 02:55:05 +00:00
|
|
|
if (sizeExprStr.empty())
|
|
|
|
{
|
|
|
|
if (sizeExpr)
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(sizeExpr->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Unable to use size variable with Athena");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(sizeExpr->getSourceRange(), true));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Unable to use size variable with Athena");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileOut << " /* " << fieldName << " */\n";
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
fileOut << " " << fieldName << ".reset(new atUint8[" << sizeExprStr << "]);\n";
|
|
|
|
fileOut << " reader.readUBytesToBuf(" << fieldName << ".get(), " << sizeExprStr << ");\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fileOut << " writer.writeUBytes(" << fieldName << ".get(), " << sizeExprStr << ");\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!tsDecl->getNameAsString().compare("String"))
|
|
|
|
{
|
|
|
|
const clang::Expr* sizeExpr = nullptr;
|
|
|
|
std::string sizeExprStr;
|
|
|
|
for (const clang::TemplateArgument& arg : *tsType)
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
if (arg.getKind() == clang::TemplateArgument::Expression)
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
const clang::Expr* expr = arg.getAsExpr();
|
2015-06-17 00:25:48 +00:00
|
|
|
const clang::UnaryExprOrTypeTraitExpr* uExpr = (clang::UnaryExprOrTypeTraitExpr*)expr;
|
2015-06-19 02:55:05 +00:00
|
|
|
llvm::APSInt sizeLiteral;
|
|
|
|
if (expr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
|
2015-06-17 00:25:48 +00:00
|
|
|
uExpr->getKind() == clang::UETT_SizeOf)
|
|
|
|
{
|
|
|
|
const clang::Expr* argExpr = uExpr->getArgumentExpr();
|
|
|
|
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
|
|
|
|
argExpr = ((clang::ParenExpr*)argExpr)->getSubExpr();
|
2015-06-19 02:55:05 +00:00
|
|
|
sizeExpr = argExpr;
|
|
|
|
llvm::raw_string_ostream strStream(sizeExprStr);
|
|
|
|
argExpr->printPretty(strStream, nullptr, context.getPrintingPolicy());
|
|
|
|
}
|
|
|
|
else if (expr->isIntegerConstantExpr(sizeLiteral, context))
|
|
|
|
{
|
|
|
|
sizeExprStr = sizeLiteral.toString(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileOut << " /* " << fieldName << " */\n";
|
|
|
|
if (!p)
|
|
|
|
fileOut << " " << fieldName << " = reader.readString(" << sizeExprStr << ");\n";
|
|
|
|
else
|
|
|
|
fileOut << " writer.writeString(" << fieldName << ");\n";
|
|
|
|
}
|
|
|
|
else if (!tsDecl->getNameAsString().compare("WString"))
|
|
|
|
{
|
|
|
|
llvm::APSInt endian(64, -1);
|
|
|
|
const clang::Expr* endianExpr = nullptr;
|
|
|
|
if (classParms->size() >= 2)
|
|
|
|
{
|
|
|
|
const clang::NamedDecl* endianParm = classParms->getParam(1);
|
|
|
|
if (endianParm->getKind() == clang::Decl::NonTypeTemplateParm)
|
|
|
|
{
|
|
|
|
const clang::NonTypeTemplateParmDecl* nttParm = (clang::NonTypeTemplateParmDecl*)endianParm;
|
|
|
|
const clang::Expr* defArg = nttParm->getDefaultArgument();
|
|
|
|
endianExpr = defArg;
|
|
|
|
if (!defArg->isIntegerConstantExpr(endian, context))
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(defArg->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Endian value must be 'BigEndian' or 'LittleEndian'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(defArg->getSourceRange(), true));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const clang::Expr* sizeExpr = nullptr;
|
|
|
|
std::string sizeExprStr;
|
|
|
|
size_t idx = 0;
|
|
|
|
bool bad = false;
|
|
|
|
for (const clang::TemplateArgument& arg : *tsType)
|
|
|
|
{
|
|
|
|
if (arg.getKind() == clang::TemplateArgument::Expression)
|
|
|
|
{
|
|
|
|
const clang::Expr* expr = arg.getAsExpr();
|
|
|
|
if (idx == 0)
|
|
|
|
{
|
|
|
|
llvm::APSInt sizeLiteral;
|
|
|
|
const clang::UnaryExprOrTypeTraitExpr* uExpr = (clang::UnaryExprOrTypeTraitExpr*)expr;
|
|
|
|
if (expr->getStmtClass() == clang::Stmt::UnaryExprOrTypeTraitExprClass &&
|
|
|
|
uExpr->getKind() == clang::UETT_SizeOf)
|
|
|
|
{
|
|
|
|
const clang::Expr* argExpr = uExpr->getArgumentExpr();
|
|
|
|
while (argExpr->getStmtClass() == clang::Stmt::ParenExprClass)
|
|
|
|
argExpr = ((clang::ParenExpr*)argExpr)->getSubExpr();
|
|
|
|
sizeExpr = argExpr;
|
|
|
|
llvm::raw_string_ostream strStream(sizeExprStr);
|
|
|
|
argExpr->printPretty(strStream, nullptr, context.getPrintingPolicy());
|
|
|
|
}
|
|
|
|
else if (expr->isIntegerConstantExpr(sizeLiteral, context))
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
sizeExprStr = sizeLiteral.toString(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (idx == 1)
|
|
|
|
{
|
|
|
|
endianExpr = expr;
|
|
|
|
if (!expr->isIntegerConstantExpr(endian, context))
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(expr->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Endian value must be 'BigEndian' or 'LittleEndian'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(expr->getSourceRange(), true));
|
|
|
|
bad = true;
|
|
|
|
break;
|
2015-06-17 00:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-19 02:55:05 +00:00
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
if (bad)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int endianVal = endian.getSExtValue();
|
|
|
|
if (endianVal != 0 && endianVal != 1)
|
|
|
|
{
|
|
|
|
if (endianExpr)
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(endianExpr->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Endian value must be 'BigEndian' or 'LittleEndian'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(endianExpr->getSourceRange(), true));
|
|
|
|
}
|
|
|
|
else
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Endian value must be 'BigEndian' or 'LittleEndian'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
|
2015-06-17 00:25:48 +00:00
|
|
|
}
|
2015-06-19 02:55:05 +00:00
|
|
|
continue;
|
2015-06-17 00:25:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
if (currentEndian != endianVal)
|
|
|
|
{
|
|
|
|
if (endianVal == 0)
|
|
|
|
fileOut << (p ? " writer.setEndian(Athena::LittleEndian);\n" : " reader.setEndian(Athena::LittleEndian);\n");
|
|
|
|
else if (endianVal == 1)
|
|
|
|
fileOut << (p ? " writer.setEndian(Athena::BigEndian);\n" : " reader.setEndian(Athena::BigEndian);\n");
|
|
|
|
currentEndian = endianVal;
|
|
|
|
}
|
2015-06-17 00:25:48 +00:00
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
fileOut << " /* " << fieldName << " */\n";
|
|
|
|
if (!p)
|
|
|
|
fileOut << " " << fieldName << " = reader.readWString(" << sizeExprStr << ");\n";
|
|
|
|
else
|
|
|
|
fileOut << " writer.writeWString(" << fieldName << ");\n";
|
2015-06-17 00:25:48 +00:00
|
|
|
}
|
2015-06-19 02:55:05 +00:00
|
|
|
else if (!tsDecl->getNameAsString().compare("UTF8"))
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
const clang::Expr* sizeExpr = nullptr;
|
|
|
|
std::string sizeExprStr;
|
|
|
|
for (const clang::TemplateArgument& arg : *tsType)
|
|
|
|
{
|
|
|
|
if (arg.getKind() == clang::TemplateArgument::Expression)
|
|
|
|
{
|
|
|
|
const clang::Expr* expr = arg.getAsExpr();
|
|
|
|
const clang::UnaryExprOrTypeTraitExpr* uExpr = (clang::UnaryExprOrTypeTraitExpr*)expr;
|
|
|
|
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)
|
|
|
|
argExpr = ((clang::ParenExpr*)argExpr)->getSubExpr();
|
|
|
|
sizeExpr = argExpr;
|
|
|
|
llvm::raw_string_ostream strStream(sizeExprStr);
|
|
|
|
argExpr->printPretty(strStream, nullptr, context.getPrintingPolicy());
|
|
|
|
}
|
|
|
|
else if (expr->isIntegerConstantExpr(sizeLiteral, context))
|
|
|
|
{
|
|
|
|
sizeExprStr = sizeLiteral.toString(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-17 00:25:48 +00:00
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
fileOut << " /* " << fieldName << " */\n";
|
|
|
|
if (!p)
|
|
|
|
fileOut << " " << fieldName << " = reader.readUnicode(" << sizeExprStr << ");\n";
|
2015-06-18 07:32:11 +00:00
|
|
|
else
|
2015-06-19 02:55:05 +00:00
|
|
|
fileOut << " writer.writeUnicode(" << fieldName << ");\n";
|
2015-06-17 00:25:48 +00:00
|
|
|
}
|
2015-06-19 02:55:05 +00:00
|
|
|
else if (!tsDecl->getNameAsString().compare("Seek"))
|
2015-06-18 07:32:11 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
size_t idx = 0;
|
|
|
|
llvm::APSInt offset(64, 0);
|
|
|
|
llvm::APSInt direction(64, 0);
|
|
|
|
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();
|
|
|
|
if (!idx)
|
|
|
|
{
|
|
|
|
if (!expr->isIntegerConstantExpr(offset, context))
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(expr->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Unable to use non-constant offset expression in Athena");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(expr->getSourceRange(), true));
|
|
|
|
bad = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
directionExpr = expr;
|
|
|
|
if (!expr->isIntegerConstantExpr(direction, context))
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(expr->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Unable to use non-constant direction expression in Athena");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(expr->getSourceRange(), true));
|
|
|
|
bad = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
if (bad)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int64_t offsetVal = offset.getSExtValue();
|
|
|
|
int64_t directionVal = direction.getSExtValue();
|
|
|
|
if (directionVal < 0 || directionVal > 2)
|
|
|
|
{
|
|
|
|
if (directionExpr)
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(directionExpr->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Direction parameter must be 'Begin', 'Current', or 'End'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(directionExpr->getSourceRange(), true));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Direction parameter must be 'Begin', 'Current', or 'End'");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileOut << " /* " << fieldName << " */\n";
|
|
|
|
if (directionVal == 0)
|
|
|
|
{
|
|
|
|
if (!p)
|
|
|
|
fileOut << " reader.seek(" << offsetVal << ", Athena::Begin);\n";
|
|
|
|
else
|
|
|
|
fileOut << " writer.seek(" << offsetVal << ", Athena::Begin);\n";
|
|
|
|
}
|
|
|
|
else if (directionVal == 1)
|
|
|
|
{
|
|
|
|
if (!p)
|
|
|
|
fileOut << " reader.seek(" << offsetVal << ", Athena::Current);\n";
|
|
|
|
else
|
|
|
|
fileOut << " writer.seek(" << offsetVal << ", Athena::Current);\n";
|
|
|
|
}
|
|
|
|
else if (directionVal == 2)
|
|
|
|
{
|
|
|
|
if (!p)
|
|
|
|
fileOut << " reader.seek(" << offsetVal << ", Athena::End);\n";
|
|
|
|
else
|
|
|
|
fileOut << " writer.seek(" << offsetVal << ", Athena::End);\n";
|
|
|
|
}
|
|
|
|
|
2015-06-18 07:32:11 +00:00
|
|
|
}
|
2015-06-19 02:55:05 +00:00
|
|
|
else if (!tsDecl->getNameAsString().compare("Align"))
|
|
|
|
{
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(expr->getLocStart(), AthenaError);
|
|
|
|
diag.AddString("Unable to use non-constant align expression in Athena");
|
|
|
|
diag.AddSourceRange(clang::CharSourceRange(expr->getSourceRange(), true));
|
|
|
|
bad = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bad)
|
|
|
|
continue;
|
2015-06-16 07:11:56 +00:00
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
int64_t alignVal = align.getSExtValue();
|
|
|
|
if (alignVal)
|
|
|
|
{
|
|
|
|
fileOut << " /* " << fieldName << " */\n";
|
|
|
|
if (alignVal == 32)
|
|
|
|
{
|
|
|
|
if (!p)
|
|
|
|
fileOut << " reader.seekAlign32();\n";
|
|
|
|
else
|
|
|
|
fileOut << " writer.seekAlign32();\n";
|
|
|
|
}
|
|
|
|
else if (align.isPowerOf2())
|
|
|
|
{
|
|
|
|
if (!p)
|
|
|
|
fileOut << " reader.seek((reader.position() + " << alignVal-1 << ") & ~" << alignVal-1 << ", SeekOrigin::Begin);\n";
|
|
|
|
else
|
|
|
|
fileOut << " writer.seek((writer.position() + " << alignVal-1 << ") & ~" << alignVal-1 << ", SeekOrigin::Begin);\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!p)
|
|
|
|
fileOut << " reader.seek((reader.position() + " << alignVal-1 << ") / " << alignVal << " * " << alignVal << ", SeekOrigin::Begin);\n";
|
|
|
|
else
|
|
|
|
fileOut << " writer.seek((writer.position() + " << alignVal-1 << ") / " << alignVal << " * " << alignVal << ", SeekOrigin::Begin);\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
else if (regType->getTypeClass() == clang::Type::Record)
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
const clang::CXXRecordDecl* cxxRDecl = regType->getAsCXXRecordDecl();
|
|
|
|
if (cxxRDecl)
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
for (const clang::CXXBaseSpecifier& base : cxxRDecl->bases())
|
2015-06-17 00:25:48 +00:00
|
|
|
{
|
2015-06-19 02:55:05 +00:00
|
|
|
clang::QualType canonType = base.getType().getCanonicalType();
|
|
|
|
if (!canonType.getAsString().compare(0, sizeof(ATHENA_DNA_BASETYPE)-1, ATHENA_DNA_BASETYPE))
|
|
|
|
{
|
|
|
|
fileOut << " /* " << fieldName << " */\n";
|
|
|
|
fileOut << " " << fieldName << (p ? ".write(writer);\n" : ".read(reader);\n");
|
|
|
|
currentEndian = -1;
|
|
|
|
break;
|
|
|
|
}
|
2015-06-17 00:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-19 02:55:05 +00:00
|
|
|
|
2015-06-17 00:25:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 02:49:02 +00:00
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
|
|
|
|
fileOut << "}\n\n";
|
|
|
|
|
2015-06-15 02:49:02 +00:00
|
|
|
}
|
2015-06-16 07:11:56 +00:00
|
|
|
|
2015-06-15 02:49:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-16 02:29:53 +00:00
|
|
|
class ATDNAConsumer : public clang::ASTConsumer
|
2015-06-15 02:49:02 +00:00
|
|
|
{
|
2015-06-16 07:11:56 +00:00
|
|
|
ATDNAEmitVisitor emitVisitor;
|
2015-06-17 00:25:48 +00:00
|
|
|
llvm::raw_fd_ostream& fileOut;
|
2015-06-15 02:49:02 +00:00
|
|
|
public:
|
2015-06-17 00:25:48 +00:00
|
|
|
explicit ATDNAConsumer(clang::ASTContext& context, llvm::raw_fd_ostream& fo)
|
|
|
|
: emitVisitor(context, fo),
|
2015-06-16 07:11:56 +00:00
|
|
|
fileOut(fo) {}
|
2015-06-15 02:49:02 +00:00
|
|
|
void HandleTranslationUnit(clang::ASTContext& context)
|
2015-06-16 07:11:56 +00:00
|
|
|
{
|
|
|
|
/* Write file head */
|
|
|
|
fileOut << "/* Auto generated atdna implementation */\n"
|
2015-06-17 00:25:48 +00:00
|
|
|
"#include <Athena/Global.hpp>\n"
|
2015-06-16 07:11:56 +00:00
|
|
|
"#include <Athena/IStreamReader.hpp>\n"
|
|
|
|
"#include <Athena/IStreamWriter.hpp>\n\n";
|
|
|
|
for (const std::string& inputf : InputFilenames)
|
|
|
|
fileOut << "#include \"" << inputf << "\"\n";
|
|
|
|
fileOut << "\n";
|
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
/* Emit file */
|
2015-06-16 07:11:56 +00:00
|
|
|
emitVisitor.TraverseDecl(context.getTranslationUnitDecl());
|
|
|
|
}
|
2015-06-15 02:49:02 +00:00
|
|
|
};
|
|
|
|
|
2015-06-16 02:29:53 +00:00
|
|
|
class ATDNAAction : public clang::ASTFrontendAction
|
2015-06-15 02:49:02 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-06-16 02:29:53 +00:00
|
|
|
explicit ATDNAAction() {}
|
2015-06-15 02:49:02 +00:00
|
|
|
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance& compiler,
|
|
|
|
llvm::StringRef /*filename*/)
|
|
|
|
{
|
2015-06-17 00:25:48 +00:00
|
|
|
llvm::raw_fd_ostream* fileout;
|
2015-06-16 02:29:53 +00:00
|
|
|
if (OutputFilename.size())
|
|
|
|
fileout = compiler.createOutputFile(OutputFilename, false, true, "", "", true);
|
|
|
|
else
|
|
|
|
fileout = compiler.createDefaultOutputFile(false, "a", "cpp");
|
2015-06-19 02:55:05 +00:00
|
|
|
AthenaError = compiler.getASTContext().getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Error, "Athena error: %0");
|
2015-06-16 02:29:53 +00:00
|
|
|
return std::unique_ptr<clang::ASTConsumer>(new ATDNAConsumer(compiler.getASTContext(), *fileout));
|
2015-06-15 02:49:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-16 02:29:53 +00:00
|
|
|
|
2015-06-15 02:49:02 +00:00
|
|
|
int main(int argc, const char** argv)
|
|
|
|
{
|
2015-06-16 02:29:53 +00:00
|
|
|
llvm::cl::ParseCommandLineOptions(argc, argv, "Athena DNA Generator");
|
|
|
|
if (Help)
|
|
|
|
llvm::cl::PrintHelpMessage();
|
|
|
|
|
2015-06-16 07:15:00 +00:00
|
|
|
std::vector<std::string> args = {"clang-tool",
|
|
|
|
"-fsyntax-only",
|
2015-06-17 00:25:48 +00:00
|
|
|
"-std=c++11"};
|
2015-06-17 01:05:43 +00:00
|
|
|
args.push_back("-I" + std::string(XSTR(INSTALL_PREFIX)) + "/lib/clang/" + CLANG_VERSION_STRING + "/include");
|
2015-06-17 01:20:31 +00:00
|
|
|
args.push_back("-I" + std::string(XSTR(INSTALL_PREFIX)) + "/include/Athena");
|
2015-06-16 07:15:00 +00:00
|
|
|
for (int a=1 ; a<argc ; ++a)
|
|
|
|
args.push_back(argv[a]);
|
2015-06-17 00:25:48 +00:00
|
|
|
|
2015-06-17 00:57:06 +00:00
|
|
|
llvm::IntrusiveRefCntPtr<clang::FileManager> fman(new clang::FileManager(clang::FileSystemOptions()));
|
2015-06-16 07:15:00 +00:00
|
|
|
clang::tooling::ToolInvocation TI(args, new ATDNAAction, fman.get());
|
|
|
|
if (TI.run())
|
|
|
|
return 0;
|
2015-06-16 02:29:53 +00:00
|
|
|
|
|
|
|
return -1;
|
2015-06-15 02:49:02 +00:00
|
|
|
}
|
|
|
|
|