!T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
}
+void CodeGenModule::SetGlobalValueAttributes(const FunctionDecl *FD,
+ llvm::GlobalValue *GV) {
+ // TODO: Set up linkage and many other things. Note, this is a simple
+ // approximation of what we really want.
+ if (FD->getStorageClass() == FunctionDecl::Static)
+ GV->setLinkage(llvm::Function::InternalLinkage);
+ else if (FD->getAttr<DLLImportAttr>())
+ GV->setLinkage(llvm::Function::DLLImportLinkage);
+ else if (FD->getAttr<DLLExportAttr>())
+ GV->setLinkage(llvm::Function::DLLExportLinkage);
+ else if (FD->getAttr<WeakAttr>() || FD->isInline())
+ GV->setLinkage(llvm::Function::WeakLinkage);
+
+ if (const VisibilityAttr *attr = FD->getAttr<VisibilityAttr>())
+ CodeGenModule::setVisibility(GV, attr->getVisibility());
+ // FIXME: else handle -fvisibility
+}
+
void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD,
llvm::Function *F,
const llvm::FunctionType *FTy) {
if (FD->getAttr<FastCallAttr>())
F->setCallingConv(llvm::CallingConv::Fast);
- // TODO: Set up linkage and many other things. Note, this is a simple
- // approximation of what we really want.
- if (FD->getStorageClass() == FunctionDecl::Static)
- F->setLinkage(llvm::Function::InternalLinkage);
- else if (FD->getAttr<DLLImportAttr>())
- F->setLinkage(llvm::Function::DLLImportLinkage);
- else if (FD->getAttr<DLLExportAttr>())
- F->setLinkage(llvm::Function::DLLExportLinkage);
- else if (FD->getAttr<WeakAttr>() || FD->isInline())
- F->setLinkage(llvm::Function::WeakLinkage);
-
- if (const VisibilityAttr *attr = FD->getAttr<VisibilityAttr>())
- CodeGenModule::setVisibility(F, attr->getVisibility());
- // FIXME: else handle -fvisibility
+ SetGlobalValueAttributes(FD, F);
}
// If it doesn't already exist, just create and return an entry.
if (F == 0) {
// FIXME: param attributes for sext/zext etc.
- F = llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
- D->getName(), &getModule());
+ if (D->getBody() || !D->getAttr<AliasAttr>())
+ F = llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
+ D->getName(), &getModule());
+ else {
+ const std::string& aliaseeName = D->getAttr<AliasAttr>()->getAliasee();
+ llvm::Function *aliasee = getModule().getFunction(aliaseeName);
+ llvm::GlobalValue *alias = new llvm::GlobalAlias(aliasee->getType(),
+ llvm::Function::ExternalLinkage,
+ D->getName(),
+ aliasee,
+ &getModule());
+ SetGlobalValueAttributes(D, alias);
+ return Entry = alias;
+ }
SetFunctionAttributes(D, F, FTy);
return Entry = F;
void CodeGenModule::EmitFunction(const FunctionDecl *FD) {
// If this is not a prototype, emit the body.
- if (!FD->isThisDeclarationADefinition())
+ if (!FD->isThisDeclarationADefinition()) {
+ if (FD->getAttr<AliasAttr>())
+ GetAddrOfFunctionDecl(FD, true);
return;
+ }
// If the function is a static, defer code generation until later so we can
// easily omit unused statics.
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
if (FD->getBody())
CodeGenFunction(*this).GenerateCode(FD);
+ else if (FD->getAttr<AliasAttr>())
+ GetAddrOfFunctionDecl(FD, true);
} else {
EmitGlobalVarInit(cast<VarDecl>(D));
}
}
// FIXME: Diagnostic?
break;
+ case AttributeList::AT_alias:
+ HandleAliasAttribute(New, Attr);
+ break;
case AttributeList::AT_deprecated:
HandleDeprecatedAttribute(New, Attr);
break;
rawAttr->getName()->getName());
}
+void Sema::HandleAliasAttribute(Decl *d, AttributeList *rawAttr) {
+ // check the attribute arguments.
+ if (rawAttr->getNumArgs() != 1) {
+ Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
+ std::string("1"));
+ return;
+ }
+
+ Expr *Arg = static_cast<Expr*>(rawAttr->getArg(0));
+ Arg = Arg->IgnoreParenCasts();
+ StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
+
+ if (Str == 0 || Str->isWide()) {
+ Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_string,
+ "alias", std::string("1"));
+ return;
+ }
+
+ const char *Alias = Str->getStrData();
+ unsigned AliasLen = Str->getByteLength();
+
+ // FIXME: check if target symbol exists in current file
+
+ d->addAttr(new AliasAttr(std::string(Alias, AliasLen)));
+}
+
void Sema::HandleNoReturnAttribute(Decl *d, AttributeList *rawAttr) {
// check the attribute arguments.
if (rawAttr->getNumArgs() != 0) {