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"
|
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-11-01 02:42:27 +08:00
|
|
|
#include "clang/Parse/Scope.h"
|
2007-08-11 04:18:51 +08:00
|
|
|
|
2006-08-18 13:17:52 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
|
2007-11-01 02:42:27 +08:00
|
|
|
const char *typeName = TD->getIdentifier()->getName();
|
|
|
|
return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
|
2007-12-07 08:18:54 +08:00
|
|
|
strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
|
2007-10-10 06:01:59 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
bool Sema::isObjCObjectPointerType(QualType type) const {
|
|
|
|
if (!type->isPointerType() && !type->isObjCQualifiedIdType())
|
2008-01-04 08:27:46 +08:00
|
|
|
return false;
|
2008-01-08 03:49:32 +08:00
|
|
|
if (type == Context.getObjCIdType() || type == Context.getObjCClassType() ||
|
|
|
|
type->isObjCQualifiedIdType())
|
2008-01-04 08:27:46 +08:00
|
|
|
return true;
|
|
|
|
|
2008-01-08 02:14:04 +08:00
|
|
|
if (type->isPointerType()) {
|
2008-01-04 08:27:46 +08:00
|
|
|
PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr());
|
|
|
|
type = pointerType->getPointeeType();
|
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
return (type->isObjCInterfaceType() || type->isObjCQualifiedIdType());
|
2008-01-04 08:27:46 +08:00
|
|
|
}
|
|
|
|
|
2007-11-01 02:42:27 +08:00
|
|
|
void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
|
|
|
|
TUScope = S;
|
2008-04-17 22:40:12 +08:00
|
|
|
CurContext = Context.getTranslationUnitDecl();
|
2008-02-06 08:46:58 +08:00
|
|
|
if (!PP.getLangOptions().ObjC1) return;
|
|
|
|
|
|
|
|
TypedefType *t;
|
|
|
|
|
|
|
|
// Add the built-in ObjC types.
|
|
|
|
t = cast<TypedefType>(Context.getObjCIdType().getTypePtr());
|
2008-04-12 08:47:19 +08:00
|
|
|
PushOnScopeChains(t->getDecl(), TUScope);
|
2008-02-06 08:46:58 +08:00
|
|
|
t = cast<TypedefType>(Context.getObjCClassType().getTypePtr());
|
2008-04-12 08:47:19 +08:00
|
|
|
PushOnScopeChains(t->getDecl(), TUScope);
|
2008-02-06 08:46:58 +08:00
|
|
|
ObjCInterfaceType *it = cast<ObjCInterfaceType>(Context.getObjCProtoType());
|
|
|
|
ObjCInterfaceDecl *IDecl = it->getDecl();
|
2008-04-12 08:47:19 +08:00
|
|
|
PushOnScopeChains(IDecl, TUScope);
|
2008-02-25 00:25:02 +08:00
|
|
|
|
|
|
|
// Synthesize "typedef struct objc_selector *SEL;"
|
2008-06-10 07:19:58 +08:00
|
|
|
RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
|
2008-03-16 05:32:50 +08:00
|
|
|
SourceLocation(),
|
2008-03-15 14:12:44 +08:00
|
|
|
&Context.Idents.get("objc_selector"),
|
2008-03-16 05:32:50 +08:00
|
|
|
0);
|
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);
|
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)
|
|
|
|
: PP(pp), Context(ctxt), Consumer(consumer),
|
2008-04-04 14:12:32 +08:00
|
|
|
CurFunctionDecl(0), CurMethodDecl(0), CurContext(0) {
|
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-02-06 08:46:58 +08:00
|
|
|
KnownFunctionIDs[id_printf] = &IT.get("printf");
|
|
|
|
KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
|
|
|
|
KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
|
|
|
|
KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
|
|
|
|
KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
|
2008-06-17 02:00:42 +08:00
|
|
|
KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
|
2008-02-06 08:46:58 +08:00
|
|
|
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_vprintf] = &IT.get("vprintf");
|
2007-11-01 02:42:27 +08:00
|
|
|
|
2008-02-25 00:25:02 +08:00
|
|
|
// FIXME: Move this initialization up to Sema::ActOnTranslationUnitScope()
|
|
|
|
// and make sure the decls get inserted into TUScope!
|
2008-05-27 13:09:49 +08:00
|
|
|
// FIXME: And make sure they don't leak!
|
2007-11-01 02:42:27 +08:00
|
|
|
if (PP.getLangOptions().ObjC1) {
|
2008-04-17 22:40:12 +08:00
|
|
|
TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl();
|
|
|
|
|
2007-11-01 02:42:27 +08:00
|
|
|
// Synthesize "typedef struct objc_class *Class;"
|
2008-06-10 07:19:58 +08:00
|
|
|
RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
|
2008-04-17 22:40:12 +08:00
|
|
|
TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2008-03-16 05:32:50 +08:00
|
|
|
&IT.get("objc_class"), 0);
|
2007-11-01 02:42:27 +08:00
|
|
|
QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
|
2008-03-15 14:12:44 +08:00
|
|
|
TypedefDecl *ClassTypedef =
|
2008-04-17 22:40:12 +08:00
|
|
|
TypedefDecl::Create(Context, TUDecl, SourceLocation(),
|
2008-03-16 05:32:50 +08:00
|
|
|
&Context.Idents.get("Class"), ClassT, 0);
|
2008-01-08 03:49:32 +08:00
|
|
|
Context.setObjCClassType(ClassTypedef);
|
2007-11-01 02:42:27 +08:00
|
|
|
|
2007-12-07 08:18:54 +08:00
|
|
|
// Synthesize "@class Protocol;
|
2008-03-16 09:15:50 +08:00
|
|
|
ObjCInterfaceDecl *ProtocolDecl =
|
|
|
|
ObjCInterfaceDecl::Create(Context, SourceLocation(), 0,
|
2008-04-12 03:35:35 +08:00
|
|
|
&Context.Idents.get("Protocol"),
|
|
|
|
SourceLocation(), true);
|
2008-01-08 03:49:32 +08:00
|
|
|
Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
|
2007-12-07 08:18:54 +08:00
|
|
|
|
2007-11-01 02:42:27 +08:00
|
|
|
// Synthesize "typedef struct objc_object { Class isa; } *id;"
|
2008-03-15 14:12:44 +08:00
|
|
|
RecordDecl *ObjectTag =
|
2008-06-10 07:19:58 +08:00
|
|
|
RecordDecl::Create(Context, TagDecl::TK_struct, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2008-03-16 05:32:50 +08:00
|
|
|
&IT.get("objc_object"), 0);
|
2008-06-04 11:03:48 +08:00
|
|
|
FieldDecl *IsaDecl = FieldDecl::Create(Context, SourceLocation(),
|
|
|
|
&Context.Idents.get("isa"),
|
2008-03-16 08:16:02 +08:00
|
|
|
Context.getObjCClassType());
|
2007-11-01 02:42:27 +08:00
|
|
|
ObjectTag->defineBody(&IsaDecl, 1);
|
|
|
|
QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
|
2008-04-17 22:40:12 +08:00
|
|
|
TypedefDecl *IdTypedef = TypedefDecl::Create(Context, TUDecl,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation(),
|
2008-03-15 14:12:44 +08:00
|
|
|
&Context.Idents.get("id"),
|
2008-03-16 05:32:50 +08:00
|
|
|
ObjT, 0);
|
2008-01-08 03:49:32 +08:00
|
|
|
Context.setObjCIdType(IdTypedef);
|
2007-11-01 02:42:27 +08:00
|
|
|
}
|
2007-10-11 05:53:07 +08:00
|
|
|
TUScope = 0;
|
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.
|
|
|
|
void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
|
2008-02-14 02:01:07 +08:00
|
|
|
if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
|
2008-01-17 03:17:22 +08:00
|
|
|
|
|
|
|
if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
|
|
|
|
ImpCast->setType(Type);
|
|
|
|
else
|
|
|
|
Expr = new ImplicitCastExpr(Type, Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-05-19 06:53:50 +08:00
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, 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,
|
|
|
|
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,
|
|
|
|
const std::string &Msg2, SourceRange Range) {
|
|
|
|
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,
|
|
|
|
SourceRange R1) {
|
|
|
|
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,
|
|
|
|
SourceRange R1, SourceRange R2) {
|
|
|
|
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,
|
|
|
|
SourceRange R1, SourceRange R2) {
|
|
|
|
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,
|
|
|
|
const std::string &Msg2, SourceRange R1, SourceRange R2) {
|
|
|
|
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
|
|
|
}
|