2006-11-10 12:58:55 +08:00
|
|
|
//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
|
2006-08-17 13:51:27 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-08-17 13:51:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2006-11-10 12:58:55 +08:00
|
|
|
// This file implements the actions class which performs semantic analysis and
|
|
|
|
// builds an AST out of a parse stream.
|
2006-08-17 13:51:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-10 12:58:55 +08:00
|
|
|
#include "Sema.h"
|
2006-11-10 14:20:45 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-08-11 12:54:23 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2006-10-06 13:22:26 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2007-05-17 01:56:50 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-08-11 04:18:51 +08:00
|
|
|
|
2006-08-18 13:17:52 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-08-24 06:20:38 +08:00
|
|
|
static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name)
|
|
|
|
{
|
|
|
|
if (C.getLangOptions().CPlusPlus)
|
|
|
|
return CXXRecordDecl::Create(C, TagDecl::TK_struct,
|
|
|
|
C.getTranslationUnitDecl(),
|
2008-09-05 09:34:33 +08:00
|
|
|
SourceLocation(), &C.Idents.get(Name));
|
2008-08-24 06:20:38 +08:00
|
|
|
else
|
|
|
|
return RecordDecl::Create(C, TagDecl::TK_struct,
|
|
|
|
C.getTranslationUnitDecl(),
|
2008-09-05 09:34:33 +08:00
|
|
|
SourceLocation(), &C.Idents.get(Name));
|
2008-08-24 06:20:38 +08:00
|
|
|
}
|
|
|
|
|
2007-11-01 02:42:27 +08:00
|
|
|
void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
|
|
|
|
TUScope = S;
|
2008-11-09 01:17:31 +08:00
|
|
|
PushDeclContext(Context.getTranslationUnitDecl());
|
2008-02-06 08:46:58 +08:00
|
|
|
if (!PP.getLangOptions().ObjC1) return;
|
|
|
|
|
2008-02-25 00:25:02 +08:00
|
|
|
// Synthesize "typedef struct objc_selector *SEL;"
|
2008-08-24 06:20:38 +08:00
|
|
|
RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
|
2008-04-12 08:47:19 +08:00
|
|
|
PushOnScopeChains(SelTag, TUScope);
|
2008-02-25 00:25:02 +08:00
|
|
|
|
|
|
|
QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
|
2008-04-04 14:12:32 +08:00
|
|
|
TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
|
|
|
|
SourceLocation(),
|
2008-03-15 14:12:44 +08:00
|
|
|
&Context.Idents.get("SEL"),
|
2008-03-16 05:32:50 +08:00
|
|
|
SelT, 0);
|
2008-04-12 08:47:19 +08:00
|
|
|
PushOnScopeChains(SelTypedef, TUScope);
|
2008-02-25 00:25:02 +08:00
|
|
|
Context.setObjCSelType(SelTypedef);
|
2008-06-22 04:20:39 +08:00
|
|
|
|
2008-06-22 06:44:51 +08:00
|
|
|
// FIXME: Make sure these don't leak!
|
2008-08-24 06:20:38 +08:00
|
|
|
RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
|
2008-06-22 04:20:39 +08:00
|
|
|
QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
|
|
|
|
TypedefDecl *ClassTypedef =
|
|
|
|
TypedefDecl::Create(Context, CurContext, SourceLocation(),
|
|
|
|
&Context.Idents.get("Class"), ClassT, 0);
|
|
|
|
PushOnScopeChains(ClassTag, TUScope);
|
|
|
|
PushOnScopeChains(ClassTypedef, TUScope);
|
|
|
|
Context.setObjCClassType(ClassTypedef);
|
|
|
|
// Synthesize "@class Protocol;
|
|
|
|
ObjCInterfaceDecl *ProtocolDecl =
|
2008-07-21 15:06:49 +08:00
|
|
|
ObjCInterfaceDecl::Create(Context, SourceLocation(),
|
2008-06-22 04:20:39 +08:00
|
|
|
&Context.Idents.get("Protocol"),
|
|
|
|
SourceLocation(), true);
|
|
|
|
Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
|
|
|
|
PushOnScopeChains(ProtocolDecl, TUScope);
|
|
|
|
|
|
|
|
// Synthesize "typedef struct objc_object { Class isa; } *id;"
|
2008-08-24 06:20:38 +08:00
|
|
|
RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
|
|
|
|
|
2008-06-22 04:20:39 +08:00
|
|
|
QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
|
|
|
|
PushOnScopeChains(ObjectTag, TUScope);
|
|
|
|
TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
|
|
|
|
SourceLocation(),
|
|
|
|
&Context.Idents.get("id"),
|
|
|
|
ObjT, 0);
|
|
|
|
PushOnScopeChains(IdTypedef, TUScope);
|
|
|
|
Context.setObjCIdType(IdTypedef);
|
2007-10-17 04:40:23 +08:00
|
|
|
}
|
2007-10-11 05:53:07 +08:00
|
|
|
|
2008-02-06 08:46:58 +08:00
|
|
|
Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
|
2008-11-09 01:17:31 +08:00
|
|
|
: PP(pp), Context(ctxt), Consumer(consumer), CurContext(0),PreDeclaratorDC(0),
|
|
|
|
CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()) {
|
2007-08-11 04:18:51 +08:00
|
|
|
|
|
|
|
// Get IdentifierInfo objects for known functions for which we
|
|
|
|
// do extra checking.
|
2008-02-06 08:46:58 +08:00
|
|
|
IdentifierTable &IT = PP.getIdentifierTable();
|
2007-08-11 04:18:51 +08:00
|
|
|
|
2008-10-03 02:44:07 +08:00
|
|
|
KnownFunctionIDs[id_printf] = &IT.get("printf");
|
|
|
|
KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
|
|
|
|
KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
|
|
|
|
KnownFunctionIDs[id_sprintf_chk] = &IT.get("__builtin___sprintf_chk");
|
|
|
|
KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
|
|
|
|
KnownFunctionIDs[id_snprintf_chk] = &IT.get("__builtin___snprintf_chk");
|
|
|
|
KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
|
|
|
|
KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
|
|
|
|
KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
|
|
|
|
KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
|
|
|
|
KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
|
|
|
|
KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
|
|
|
|
KnownFunctionIDs[id_vsprintf_chk] = &IT.get("__builtin___vsprintf_chk");
|
|
|
|
KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
|
|
|
|
KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk");
|
|
|
|
KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
|
2007-11-01 02:42:27 +08:00
|
|
|
|
2008-08-15 06:04:54 +08:00
|
|
|
SuperID = &IT.get("super");
|
|
|
|
|
2008-09-09 22:32:20 +08:00
|
|
|
// ObjC builtin typedef names.
|
|
|
|
Ident_id = &IT.get("id");
|
|
|
|
Ident_Class = &IT.get("Class");
|
|
|
|
Ident_SEL = &IT.get("SEL");
|
|
|
|
Ident_Protocol = &IT.get("Protocol");
|
|
|
|
|
2008-11-11 19:37:55 +08:00
|
|
|
Ident_StdNs = &IT.get("std");
|
|
|
|
Ident_TypeInfo = 0;
|
|
|
|
StdNamespace = 0;
|
|
|
|
|
2007-10-11 05:53:07 +08:00
|
|
|
TUScope = 0;
|
2008-07-01 18:37:29 +08:00
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
FieldCollector.reset(new CXXFieldCollector());
|
2007-02-28 09:22:02 +08:00
|
|
|
}
|
2006-11-10 14:20:45 +08:00
|
|
|
|
2008-01-17 03:17:22 +08:00
|
|
|
/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
|
|
|
|
/// If there is already an implicit cast, merge into the existing one.
|
Implement support for operator overloading using candidate operator
functions for built-in operators, e.g., the builtin
bool operator==(int const*, int const*)
can be used for the expression "x1 == x2" given:
struct X {
operator int const*();
} x1, x2;
The scheme for handling these built-in operators is relatively simple:
for each candidate required by the standard, create a special kind of
candidate function for the built-in. If overload resolution picks the
built-in operator, we perform the appropriate conversions on the
arguments and then let the normal built-in operator take care of it.
There may be some optimization opportunity left: if we can reduce the
number of built-in operator overloads we generate, overload resolution
for these cases will go faster. However, one must be careful when
doing this: GCC generates too few operator overloads in our little
test program, and fails to compile it because none of the overloads it
generates match.
Note that we only support operator overload for non-member binary
operators at the moment. The other operators will follow.
As part of this change, ImplicitCastExpr can now be an lvalue.
llvm-svn: 59148
2008-11-13 01:17:38 +08:00
|
|
|
/// If isLvalue, the result of the cast is an lvalue.
|
|
|
|
void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
|
2008-09-04 16:38:01 +08:00
|
|
|
QualType ExprTy = Context.getCanonicalType(Expr->getType());
|
|
|
|
QualType TypeTy = Context.getCanonicalType(Ty);
|
|
|
|
|
|
|
|
if (ExprTy == TypeTy)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Expr->getType().getTypePtr()->isPointerType() &&
|
|
|
|
Ty.getTypePtr()->isPointerType()) {
|
|
|
|
QualType ExprBaseType =
|
|
|
|
cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
|
|
|
|
QualType BaseType =
|
|
|
|
cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
|
|
|
|
if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
|
|
|
|
Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast,
|
|
|
|
Expr->getSourceRange());
|
|
|
|
}
|
|
|
|
}
|
2008-01-17 03:17:22 +08:00
|
|
|
|
Implement support for operator overloading using candidate operator
functions for built-in operators, e.g., the builtin
bool operator==(int const*, int const*)
can be used for the expression "x1 == x2" given:
struct X {
operator int const*();
} x1, x2;
The scheme for handling these built-in operators is relatively simple:
for each candidate required by the standard, create a special kind of
candidate function for the built-in. If overload resolution picks the
built-in operator, we perform the appropriate conversions on the
arguments and then let the normal built-in operator take care of it.
There may be some optimization opportunity left: if we can reduce the
number of built-in operator overloads we generate, overload resolution
for these cases will go faster. However, one must be careful when
doing this: GCC generates too few operator overloads in our little
test program, and fails to compile it because none of the overloads it
generates match.
Note that we only support operator overload for non-member binary
operators at the moment. The other operators will follow.
As part of this change, ImplicitCastExpr can now be an lvalue.
llvm-svn: 59148
2008-11-13 01:17:38 +08:00
|
|
|
if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
|
2008-09-04 16:38:01 +08:00
|
|
|
ImpCast->setType(Ty);
|
Implement support for operator overloading using candidate operator
functions for built-in operators, e.g., the builtin
bool operator==(int const*, int const*)
can be used for the expression "x1 == x2" given:
struct X {
operator int const*();
} x1, x2;
The scheme for handling these built-in operators is relatively simple:
for each candidate required by the standard, create a special kind of
candidate function for the built-in. If overload resolution picks the
built-in operator, we perform the appropriate conversions on the
arguments and then let the normal built-in operator take care of it.
There may be some optimization opportunity left: if we can reduce the
number of built-in operator overloads we generate, overload resolution
for these cases will go faster. However, one must be careful when
doing this: GCC generates too few operator overloads in our little
test program, and fails to compile it because none of the overloads it
generates match.
Note that we only support operator overload for non-member binary
operators at the moment. The other operators will follow.
As part of this change, ImplicitCastExpr can now be an lvalue.
llvm-svn: 59148
2008-11-13 01:17:38 +08:00
|
|
|
ImpCast->setLvalueCast(isLvalue);
|
|
|
|
} else
|
|
|
|
Expr = new ImplicitCastExpr(Ty, Expr, isLvalue);
|
2008-01-17 03:17:22 +08:00
|
|
|
}
|
|
|
|
|
2007-08-31 12:53:24 +08:00
|
|
|
void Sema::DeleteExpr(ExprTy *E) {
|
|
|
|
delete static_cast<Expr*>(E);
|
|
|
|
}
|
|
|
|
void Sema::DeleteStmt(StmtTy *S) {
|
|
|
|
delete static_cast<Stmt*>(S);
|
|
|
|
}
|
|
|
|
|
2008-08-23 11:19:52 +08:00
|
|
|
/// ActOnEndOfTranslationUnit - This is called at the very end of the
|
|
|
|
/// translation unit when EOF is reached and all but the top-level scope is
|
|
|
|
/// popped.
|
|
|
|
void Sema::ActOnEndOfTranslationUnit() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-10 13:17:58 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-05-17 01:56:50 +08:00
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
|
2007-12-13 06:39:36 +08:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
|
2007-05-17 01:56:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-24 06:27:02 +08:00
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
|
2007-12-13 06:39:36 +08:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
|
2007-05-17 01:56:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
|
|
|
|
const std::string &Msg2) {
|
|
|
|
std::string MsgArr[] = { Msg1, Msg2 };
|
2007-12-13 06:39:36 +08:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
|
2007-03-24 06:27:02 +08:00
|
|
|
return true;
|
2006-11-10 13:17:58 +08:00
|
|
|
}
|
|
|
|
|
2008-08-24 21:14:02 +08:00
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const SourceRange& Range) {
|
2007-12-13 06:39:36 +08:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
|
2007-03-31 04:09:34 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-05-19 06:53:50 +08:00
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
|
2008-08-24 21:14:02 +08:00
|
|
|
const SourceRange& Range) {
|
2007-12-13 06:39:36 +08:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
|
2007-05-19 06:53:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
|
2008-08-24 21:14:02 +08:00
|
|
|
const std::string &Msg2, const SourceRange& Range) {
|
2007-05-19 06:53:50 +08:00
|
|
|
std::string MsgArr[] = { Msg1, Msg2 };
|
2007-12-13 06:39:36 +08:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
|
2007-05-19 06:53:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-01-04 07:38:43 +08:00
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
|
|
|
|
const std::string &Msg2, const std::string &Msg3,
|
2008-08-24 21:14:02 +08:00
|
|
|
const SourceRange& R1) {
|
2008-01-04 07:38:43 +08:00
|
|
|
std::string MsgArr[] = { Msg1, Msg2, Msg3 };
|
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-05-19 06:53:50 +08:00
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
|
2008-08-24 21:14:02 +08:00
|
|
|
const SourceRange& R1, const SourceRange& R2) {
|
2007-05-19 06:53:50 +08:00
|
|
|
SourceRange RangeArr[] = { R1, R2 };
|
2007-12-13 06:39:36 +08:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
|
2007-05-19 06:53:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
|
2008-08-24 21:14:02 +08:00
|
|
|
const SourceRange& R1, const SourceRange& R2) {
|
2007-05-19 06:53:50 +08:00
|
|
|
SourceRange RangeArr[] = { R1, R2 };
|
2007-12-13 06:39:36 +08:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
|
2007-05-19 06:53:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
|
2008-08-24 21:14:02 +08:00
|
|
|
const std::string &Msg2, const SourceRange& R1,
|
|
|
|
const SourceRange& R2) {
|
2007-05-19 06:53:50 +08:00
|
|
|
std::string MsgArr[] = { Msg1, Msg2 };
|
|
|
|
SourceRange RangeArr[] = { R1, R2 };
|
2007-12-13 06:39:36 +08:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
|
2007-03-24 06:27:02 +08:00
|
|
|
return true;
|
2007-03-06 09:09:46 +08:00
|
|
|
}
|
|
|
|
|
2006-11-20 14:49:47 +08:00
|
|
|
const LangOptions &Sema::getLangOptions() const {
|
2007-02-28 09:22:02 +08:00
|
|
|
return PP.getLangOptions();
|
2006-11-20 14:49:47 +08:00
|
|
|
}
|
2008-08-11 13:35:13 +08:00
|
|
|
|
|
|
|
ObjCMethodDecl *Sema::getCurMethodDecl() {
|
|
|
|
return dyn_cast<ObjCMethodDecl>(CurContext);
|
|
|
|
}
|