2007-07-11 21:01:13 +04:00
|
|
|
//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the actions class which performs semantic analysis and
|
|
|
|
// builds an AST out of a parse stream.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 08:54:23 +04:00
|
|
|
#include "clang/AST/Expr.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-10-31 21:42:27 +03:00
|
|
|
#include "clang/Parse/Scope.h"
|
2007-08-11 00:18:51 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-01-07 22:49:32 +03:00
|
|
|
bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
|
2007-10-31 21:42:27 +03:00
|
|
|
const char *typeName = TD->getIdentifier()->getName();
|
|
|
|
return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
|
2007-12-07 03:18:54 +03:00
|
|
|
strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
|
2007-10-10 02:01:59 +04:00
|
|
|
}
|
|
|
|
|
2007-10-31 21:42:27 +03:00
|
|
|
void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
|
|
|
|
TUScope = S;
|
2008-04-17 18:40:12 +04:00
|
|
|
CurContext = Context.getTranslationUnitDecl();
|
2008-02-06 03:46:58 +03:00
|
|
|
if (!PP.getLangOptions().ObjC1) return;
|
|
|
|
|
2008-02-24 19:25:02 +03:00
|
|
|
// Synthesize "typedef struct objc_selector *SEL;"
|
2008-07-21 11:06:49 +04:00
|
|
|
RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct,
|
|
|
|
CurContext,
|
2008-03-16 00:32:50 +03:00
|
|
|
SourceLocation(),
|
2008-03-15 09:12:44 +03:00
|
|
|
&Context.Idents.get("objc_selector"),
|
2008-03-16 00:32:50 +03:00
|
|
|
0);
|
2008-04-12 04:47:19 +04:00
|
|
|
PushOnScopeChains(SelTag, TUScope);
|
2008-02-24 19:25:02 +03:00
|
|
|
|
|
|
|
QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
|
2008-04-04 10:12:32 +04:00
|
|
|
TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
|
|
|
|
SourceLocation(),
|
2008-03-15 09:12:44 +03:00
|
|
|
&Context.Idents.get("SEL"),
|
2008-03-16 00:32:50 +03:00
|
|
|
SelT, 0);
|
2008-04-12 04:47:19 +04:00
|
|
|
PushOnScopeChains(SelTypedef, TUScope);
|
2008-02-24 19:25:02 +03:00
|
|
|
Context.setObjCSelType(SelTypedef);
|
2008-06-22 00:20:39 +04:00
|
|
|
|
2008-06-22 02:44:51 +04:00
|
|
|
// FIXME: Make sure these don't leak!
|
2008-06-22 00:20:39 +04:00
|
|
|
RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
|
|
|
|
CurContext,
|
|
|
|
SourceLocation(),
|
|
|
|
&Context.Idents.get("objc_class"),
|
|
|
|
0);
|
|
|
|
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 11:06:49 +04:00
|
|
|
ObjCInterfaceDecl::Create(Context, SourceLocation(),
|
2008-06-22 00:20:39 +04:00
|
|
|
&Context.Idents.get("Protocol"),
|
|
|
|
SourceLocation(), true);
|
|
|
|
Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
|
|
|
|
PushOnScopeChains(ProtocolDecl, TUScope);
|
|
|
|
|
|
|
|
// Synthesize "typedef struct objc_object { Class isa; } *id;"
|
|
|
|
RecordDecl *ObjectTag =
|
|
|
|
RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
|
|
|
|
SourceLocation(),
|
|
|
|
&Context.Idents.get("objc_object"), 0);
|
|
|
|
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 00:40:23 +04:00
|
|
|
}
|
2007-10-11 01:53:07 +04:00
|
|
|
|
2008-02-06 03:46:58 +03:00
|
|
|
Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
|
2008-06-28 10:07:14 +04:00
|
|
|
: PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) {
|
2007-08-11 00:18:51 +04:00
|
|
|
|
|
|
|
// Get IdentifierInfo objects for known functions for which we
|
|
|
|
// do extra checking.
|
2008-02-06 03:46:58 +03:00
|
|
|
IdentifierTable &IT = PP.getIdentifierTable();
|
2007-08-11 00:18:51 +04:00
|
|
|
|
2008-02-06 03:46:58 +03: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-16 22:00:42 +04:00
|
|
|
KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
|
2008-02-06 03:46:58 +03: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-10-31 21:42:27 +03:00
|
|
|
|
2007-10-11 01:53:07 +04:00
|
|
|
TUScope = 0;
|
2008-07-01 14:37:29 +04:00
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
FieldCollector.reset(new CXXFieldCollector());
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-01-16 22:17:22 +03: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-07-27 02:17:49 +04:00
|
|
|
if (Context.getCanonicalType(Expr->getType()) ==
|
|
|
|
Context.getCanonicalType(Type)) return;
|
2008-01-16 22:17:22 +03:00
|
|
|
|
|
|
|
if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
|
|
|
|
ImpCast->setType(Type);
|
|
|
|
else
|
|
|
|
Expr = new ImplicitCastExpr(Type, Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-08-31 08:53:24 +04:00
|
|
|
void Sema::DeleteExpr(ExprTy *E) {
|
|
|
|
delete static_cast<Expr*>(E);
|
|
|
|
}
|
|
|
|
void Sema::DeleteStmt(StmtTy *S) {
|
|
|
|
delete static_cast<Stmt*>(S);
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
|
2007-12-13 01:39:36 +03:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
|
2007-07-11 21:01:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
|
2007-12-13 01:39:36 +03:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
|
2007-07-11 21:01:13 +04: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 01:39:36 +03:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
|
2007-07-11 21:01:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
|
2007-12-13 01:39:36 +03:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
|
2007-07-11 21:01:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
|
|
|
|
SourceRange Range) {
|
2007-12-13 01:39:36 +03:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
|
2007-07-11 21:01:13 +04: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 01:39:36 +03:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
|
2007-07-11 21:01:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-01-04 02:38:43 +03: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-07-11 21:01:13 +04:00
|
|
|
bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
|
|
|
|
SourceRange R1, SourceRange R2) {
|
|
|
|
SourceRange RangeArr[] = { R1, R2 };
|
2007-12-13 01:39:36 +03:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
|
2007-07-11 21:01:13 +04: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 01:39:36 +03:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
|
2007-07-11 21:01:13 +04: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 01:39:36 +03:00
|
|
|
PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
|
2007-07-11 21:01:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const LangOptions &Sema::getLangOptions() const {
|
|
|
|
return PP.getLangOptions();
|
|
|
|
}
|