Sema.cpp revision b752f289026ad8e5f44851b20e009a27ed61eefc
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 29/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer 30/// to an object type. This includes "id" and "Class" (two 'special' pointers 31/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified 32/// ID type). 33bool Sema::isObjCObjectPointerType(QualType Ty) const { 34 if (Ty->isObjCQualifiedIdType()) 35 return true; 36 37 if (!Ty->isPointerType()) 38 return false; 39 40 // Check to see if this is 'id' or 'Class', both of which are typedefs for 41 // pointer types. This looks for the typedef specifically, not for the 42 // underlying type. 43 if (Ty == Context.getObjCIdType() || Ty == Context.getObjCClassType()) 44 return true; 45 46 // If this a pointer to an interface (e.g. NSString*), it is ok. 47 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType(); 48} 49 50void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { 51 TUScope = S; 52 CurContext = Context.getTranslationUnitDecl(); 53 if (!PP.getLangOptions().ObjC1) return; 54 55 // Synthesize "typedef struct objc_selector *SEL;" 56 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, 57 CurContext, 58 SourceLocation(), 59 &Context.Idents.get("objc_selector"), 60 0); 61 PushOnScopeChains(SelTag, TUScope); 62 63 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); 64 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, 65 SourceLocation(), 66 &Context.Idents.get("SEL"), 67 SelT, 0); 68 PushOnScopeChains(SelTypedef, TUScope); 69 Context.setObjCSelType(SelTypedef); 70 71 // FIXME: Make sure these don't leak! 72 RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct, 73 CurContext, 74 SourceLocation(), 75 &Context.Idents.get("objc_class"), 76 0); 77 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); 78 TypedefDecl *ClassTypedef = 79 TypedefDecl::Create(Context, CurContext, SourceLocation(), 80 &Context.Idents.get("Class"), ClassT, 0); 81 PushOnScopeChains(ClassTag, TUScope); 82 PushOnScopeChains(ClassTypedef, TUScope); 83 Context.setObjCClassType(ClassTypedef); 84 // Synthesize "@class Protocol; 85 ObjCInterfaceDecl *ProtocolDecl = 86 ObjCInterfaceDecl::Create(Context, SourceLocation(), 87 &Context.Idents.get("Protocol"), 88 SourceLocation(), true); 89 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); 90 PushOnScopeChains(ProtocolDecl, TUScope); 91 92 // Synthesize "typedef struct objc_object { Class isa; } *id;" 93 RecordDecl *ObjectTag = 94 RecordDecl::Create(Context, TagDecl::TK_struct, CurContext, 95 SourceLocation(), 96 &Context.Idents.get("objc_object"), 0); 97 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); 98 PushOnScopeChains(ObjectTag, TUScope); 99 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext, 100 SourceLocation(), 101 &Context.Idents.get("id"), 102 ObjT, 0); 103 PushOnScopeChains(IdTypedef, TUScope); 104 Context.setObjCIdType(IdTypedef); 105} 106 107Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) 108 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) { 109 110 // Get IdentifierInfo objects for known functions for which we 111 // do extra checking. 112 IdentifierTable &IT = PP.getIdentifierTable(); 113 114 KnownFunctionIDs[id_printf] = &IT.get("printf"); 115 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf"); 116 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf"); 117 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf"); 118 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf"); 119 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog"); 120 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); 121 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf"); 122 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf"); 123 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf"); 124 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf"); 125 126 TUScope = 0; 127 if (getLangOptions().CPlusPlus) 128 FieldCollector.reset(new CXXFieldCollector()); 129} 130 131/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. 132/// If there is already an implicit cast, merge into the existing one. 133void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) { 134 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return; 135 136 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) 137 ImpCast->setType(Type); 138 else 139 Expr = new ImplicitCastExpr(Type, Expr); 140} 141 142 143 144void Sema::DeleteExpr(ExprTy *E) { 145 delete static_cast<Expr*>(E); 146} 147void Sema::DeleteStmt(StmtTy *S) { 148 delete static_cast<Stmt*>(S); 149} 150 151//===----------------------------------------------------------------------===// 152// Helper functions. 153//===----------------------------------------------------------------------===// 154 155bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { 156 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID); 157 return true; 158} 159 160bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { 161 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1); 162 return true; 163} 164 165bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, 166 const std::string &Msg2) { 167 std::string MsgArr[] = { Msg1, Msg2 }; 168 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2); 169 return true; 170} 171 172bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { 173 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1); 174 return true; 175} 176 177bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, 178 SourceRange Range) { 179 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1); 180 return true; 181} 182 183bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, 184 const std::string &Msg2, SourceRange Range) { 185 std::string MsgArr[] = { Msg1, Msg2 }; 186 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1); 187 return true; 188} 189 190bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, 191 const std::string &Msg2, const std::string &Msg3, 192 SourceRange R1) { 193 std::string MsgArr[] = { Msg1, Msg2, Msg3 }; 194 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1); 195 return true; 196} 197 198bool Sema::Diag(SourceLocation Loc, unsigned DiagID, 199 SourceRange R1, SourceRange R2) { 200 SourceRange RangeArr[] = { R1, R2 }; 201 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2); 202 return true; 203} 204 205bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, 206 SourceRange R1, SourceRange R2) { 207 SourceRange RangeArr[] = { R1, R2 }; 208 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2); 209 return true; 210} 211 212bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, 213 const std::string &Msg2, SourceRange R1, SourceRange R2) { 214 std::string MsgArr[] = { Msg1, Msg2 }; 215 SourceRange RangeArr[] = { R1, R2 }; 216 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2); 217 return true; 218} 219 220const LangOptions &Sema::getLangOptions() const { 221 return PP.getLangOptions(); 222} 223