Sema.cpp revision 53d0ea5f5bfa647ec23418bf3a3b7c183b51e4bd
1//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the actions class which performs semantic analysis and 11// builds an AST out of a parse stream. 12// 13//===----------------------------------------------------------------------===// 14 15#include "Sema.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/Lex/Preprocessor.h" 18#include "clang/Basic/Diagnostic.h" 19#include "clang/Parse/Scope.h" 20 21using namespace clang; 22 23bool Sema::isBuiltinObjCType(TypedefDecl *TD) { 24 const char *typeName = TD->getIdentifier()->getName(); 25 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 || 26 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0; 27} 28 29bool Sema::isObjCObjectPointerType(QualType type) const { 30 if (!type->isPointerType() && !type->isObjCQualifiedIdType()) 31 return false; 32 if (type == Context.getObjCIdType() || type == Context.getObjCClassType() || 33 type->isObjCQualifiedIdType()) 34 return true; 35 36 if (type->isPointerType()) { 37 PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr()); 38 type = pointerType->getPointeeType(); 39 } 40 return (type->isObjCInterfaceType() || type->isObjCQualifiedIdType()); 41} 42 43void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { 44 TUScope = S; 45 CurContext = Context.getTranslationUnitDecl(); 46 if (!PP.getLangOptions().ObjC1) return; 47 48 // Synthesize "typedef struct objc_selector *SEL;" 49 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, CurContext, 50 SourceLocation(), 51 &Context.Idents.get("objc_selector"), 52 0); 53 PushOnScopeChains(SelTag, TUScope); 54 55 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); 56 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, 57 SourceLocation(), 58 &Context.Idents.get("SEL"), 59 SelT, 0); 60 PushOnScopeChains(SelTypedef, TUScope); 61 Context.setObjCSelType(SelTypedef); 62 63 // FIXME: Make sure these don't leak! 64 RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct, 65 CurContext, 66 SourceLocation(), 67 &Context.Idents.get("objc_class"), 68 0); 69 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); 70 TypedefDecl *ClassTypedef = 71 TypedefDecl::Create(Context, CurContext, SourceLocation(), 72 &Context.Idents.get("Class"), ClassT, 0); 73 PushOnScopeChains(ClassTag, TUScope); 74 PushOnScopeChains(ClassTypedef, TUScope); 75 Context.setObjCClassType(ClassTypedef); 76 // Synthesize "@class Protocol; 77 ObjCInterfaceDecl *ProtocolDecl = 78 ObjCInterfaceDecl::Create(Context, SourceLocation(), 0, 79 &Context.Idents.get("Protocol"), 80 SourceLocation(), true); 81 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); 82 PushOnScopeChains(ProtocolDecl, TUScope); 83 84 // Synthesize "typedef struct objc_object { Class isa; } *id;" 85 RecordDecl *ObjectTag = 86 RecordDecl::Create(Context, TagDecl::TK_struct, CurContext, 87 SourceLocation(), 88 &Context.Idents.get("objc_object"), 0); 89 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); 90 PushOnScopeChains(ObjectTag, TUScope); 91 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext, 92 SourceLocation(), 93 &Context.Idents.get("id"), 94 ObjT, 0); 95 PushOnScopeChains(IdTypedef, TUScope); 96 Context.setObjCIdType(IdTypedef); 97} 98 99Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) 100 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) { 101 102 // Get IdentifierInfo objects for known functions for which we 103 // do extra checking. 104 IdentifierTable &IT = PP.getIdentifierTable(); 105 106 KnownFunctionIDs[id_printf] = &IT.get("printf"); 107 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf"); 108 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf"); 109 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf"); 110 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf"); 111 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog"); 112 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); 113 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf"); 114 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf"); 115 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf"); 116 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf"); 117 118 TUScope = 0; 119} 120 121/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. 122/// If there is already an implicit cast, merge into the existing one. 123void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) { 124 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return; 125 126 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) 127 ImpCast->setType(Type); 128 else 129 Expr = new ImplicitCastExpr(Type, Expr); 130} 131 132 133 134void Sema::DeleteExpr(ExprTy *E) { 135 delete static_cast<Expr*>(E); 136} 137void Sema::DeleteStmt(StmtTy *S) { 138 delete static_cast<Stmt*>(S); 139} 140 141//===----------------------------------------------------------------------===// 142// Helper functions. 143//===----------------------------------------------------------------------===// 144 145bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { 146 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID); 147 return true; 148} 149 150bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { 151 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1); 152 return true; 153} 154 155bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, 156 const std::string &Msg2) { 157 std::string MsgArr[] = { Msg1, Msg2 }; 158 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2); 159 return true; 160} 161 162bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { 163 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1); 164 return true; 165} 166 167bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, 168 SourceRange Range) { 169 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1); 170 return true; 171} 172 173bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, 174 const std::string &Msg2, SourceRange Range) { 175 std::string MsgArr[] = { Msg1, Msg2 }; 176 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1); 177 return true; 178} 179 180bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, 181 const std::string &Msg2, const std::string &Msg3, 182 SourceRange R1) { 183 std::string MsgArr[] = { Msg1, Msg2, Msg3 }; 184 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1); 185 return true; 186} 187 188bool Sema::Diag(SourceLocation Loc, unsigned DiagID, 189 SourceRange R1, SourceRange R2) { 190 SourceRange RangeArr[] = { R1, R2 }; 191 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2); 192 return true; 193} 194 195bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, 196 SourceRange R1, SourceRange R2) { 197 SourceRange RangeArr[] = { R1, R2 }; 198 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2); 199 return true; 200} 201 202bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, 203 const std::string &Msg2, SourceRange R1, SourceRange R2) { 204 std::string MsgArr[] = { Msg1, Msg2 }; 205 SourceRange RangeArr[] = { R1, R2 }; 206 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2); 207 return true; 208} 209 210const LangOptions &Sema::getLangOptions() const { 211 return PP.getLangOptions(); 212} 213