Sema.cpp revision ef177820100ab583b08fd3056e2a5a52ee4b1629
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 TypedefType *t; 49 50 // Add the built-in ObjC types. 51 t = cast<TypedefType>(Context.getObjCIdType().getTypePtr()); 52 PushOnScopeChains(t->getDecl(), TUScope); 53 t = cast<TypedefType>(Context.getObjCClassType().getTypePtr()); 54 PushOnScopeChains(t->getDecl(), TUScope); 55 ObjCInterfaceType *it = cast<ObjCInterfaceType>(Context.getObjCProtoType()); 56 ObjCInterfaceDecl *IDecl = it->getDecl(); 57 PushOnScopeChains(IDecl, TUScope); 58 59 // Synthesize "typedef struct objc_selector *SEL;" 60 RecordDecl *SelTag = RecordDecl::Create(Context, Decl::Struct, CurContext, 61 SourceLocation(), 62 &Context.Idents.get("objc_selector"), 63 0); 64 PushOnScopeChains(SelTag, TUScope); 65 66 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); 67 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, 68 SourceLocation(), 69 &Context.Idents.get("SEL"), 70 SelT, 0); 71 PushOnScopeChains(SelTypedef, TUScope); 72 Context.setObjCSelType(SelTypedef); 73} 74 75Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) 76 : PP(pp), Context(ctxt), Consumer(consumer), 77 CurFunctionDecl(0), CurMethodDecl(0), CurContext(0) { 78 79 // Get IdentifierInfo objects for known functions for which we 80 // do extra checking. 81 IdentifierTable &IT = PP.getIdentifierTable(); 82 83 KnownFunctionIDs[id_printf] = &IT.get("printf"); 84 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf"); 85 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf"); 86 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf"); 87 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf"); 88 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); 89 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf"); 90 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf"); 91 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf"); 92 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf"); 93 94 // FIXME: Move this initialization up to Sema::ActOnTranslationUnitScope() 95 // and make sure the decls get inserted into TUScope! 96 if (PP.getLangOptions().ObjC1) { 97 TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl(); 98 99 // Synthesize "typedef struct objc_class *Class;" 100 RecordDecl *ClassTag = RecordDecl::Create(Context, Decl::Struct, 101 TUDecl, 102 SourceLocation(), 103 &IT.get("objc_class"), 0); 104 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); 105 TypedefDecl *ClassTypedef = 106 TypedefDecl::Create(Context, TUDecl, SourceLocation(), 107 &Context.Idents.get("Class"), ClassT, 0); 108 Context.setObjCClassType(ClassTypedef); 109 110 // Synthesize "@class Protocol; 111 ObjCInterfaceDecl *ProtocolDecl = 112 ObjCInterfaceDecl::Create(Context, SourceLocation(), 0, 113 &Context.Idents.get("Protocol"), 114 SourceLocation(), true); 115 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); 116 117 // Synthesize "typedef struct objc_object { Class isa; } *id;" 118 RecordDecl *ObjectTag = 119 RecordDecl::Create(Context, Decl::Struct, TUDecl, 120 SourceLocation(), 121 &IT.get("objc_object"), 0); 122 FieldDecl *IsaDecl = FieldDecl::Create(Context, SourceLocation(), 0, 123 Context.getObjCClassType()); 124 ObjectTag->defineBody(&IsaDecl, 1); 125 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); 126 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, TUDecl, 127 SourceLocation(), 128 &Context.Idents.get("id"), 129 ObjT, 0); 130 Context.setObjCIdType(IdTypedef); 131 } 132 TUScope = 0; 133} 134 135/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. 136/// If there is already an implicit cast, merge into the existing one. 137void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) { 138 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return; 139 140 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) 141 ImpCast->setType(Type); 142 else 143 Expr = new ImplicitCastExpr(Type, Expr); 144} 145 146 147 148void Sema::DeleteExpr(ExprTy *E) { 149 delete static_cast<Expr*>(E); 150} 151void Sema::DeleteStmt(StmtTy *S) { 152 delete static_cast<Stmt*>(S); 153} 154 155//===----------------------------------------------------------------------===// 156// Helper functions. 157//===----------------------------------------------------------------------===// 158 159bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { 160 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID); 161 return true; 162} 163 164bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { 165 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1); 166 return true; 167} 168 169bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, 170 const std::string &Msg2) { 171 std::string MsgArr[] = { Msg1, Msg2 }; 172 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2); 173 return true; 174} 175 176bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { 177 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1); 178 return true; 179} 180 181bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, 182 SourceRange Range) { 183 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1); 184 return true; 185} 186 187bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, 188 const std::string &Msg2, SourceRange Range) { 189 std::string MsgArr[] = { Msg1, Msg2 }; 190 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1); 191 return true; 192} 193 194bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, 195 const std::string &Msg2, const std::string &Msg3, 196 SourceRange R1) { 197 std::string MsgArr[] = { Msg1, Msg2, Msg3 }; 198 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1); 199 return true; 200} 201 202bool Sema::Diag(SourceLocation Loc, unsigned DiagID, 203 SourceRange R1, SourceRange R2) { 204 SourceRange RangeArr[] = { R1, R2 }; 205 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2); 206 return true; 207} 208 209bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, 210 SourceRange R1, SourceRange R2) { 211 SourceRange RangeArr[] = { R1, R2 }; 212 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2); 213 return true; 214} 215 216bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, 217 const std::string &Msg2, SourceRange R1, SourceRange R2) { 218 std::string MsgArr[] = { Msg1, Msg2 }; 219 SourceRange RangeArr[] = { R1, R2 }; 220 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2); 221 return true; 222} 223 224const LangOptions &Sema::getLangOptions() const { 225 return PP.getLangOptions(); 226} 227