Sema.cpp revision bda0b626e74513950405c27525af87e214e605e2
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 if (!PP.getLangOptions().ObjC1) return; 46 47 TypedefType *t; 48 49 // Add the built-in ObjC types. 50 t = cast<TypedefType>(Context.getObjCIdType().getTypePtr()); 51 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); 52 TUScope->AddDecl(t->getDecl()); 53 t = cast<TypedefType>(Context.getObjCClassType().getTypePtr()); 54 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); 55 TUScope->AddDecl(t->getDecl()); 56 ObjCInterfaceType *it = cast<ObjCInterfaceType>(Context.getObjCProtoType()); 57 ObjCInterfaceDecl *IDecl = it->getDecl(); 58 IDecl->getIdentifier()->setFETokenInfo(IDecl); 59 TUScope->AddDecl(IDecl); 60 61 // Synthesize "typedef struct objc_selector *SEL;" 62 RecordDecl *SelTag = RecordDecl::Create(Context, Decl::Struct, 63 SourceLocation(), 64 &Context.Idents.get("objc_selector"), 65 0); 66 SelTag->getIdentifier()->setFETokenInfo(SelTag); 67 TUScope->AddDecl(SelTag); 68 69 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); 70 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, SourceLocation(), 71 &Context.Idents.get("SEL"), 72 SelT, 0); 73 SelTypedef->getIdentifier()->setFETokenInfo(SelTypedef); 74 TUScope->AddDecl(SelTypedef); 75 Context.setObjCSelType(SelTypedef); 76} 77 78Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) 79 : PP(pp), Context(ctxt), Consumer(consumer), 80 CurFunctionDecl(0), CurMethodDecl(0) { 81 82 // Get IdentifierInfo objects for known functions for which we 83 // do extra checking. 84 IdentifierTable &IT = PP.getIdentifierTable(); 85 86 KnownFunctionIDs[id_printf] = &IT.get("printf"); 87 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf"); 88 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf"); 89 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf"); 90 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf"); 91 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); 92 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf"); 93 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf"); 94 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf"); 95 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf"); 96 97 // FIXME: Move this initialization up to Sema::ActOnTranslationUnitScope() 98 // and make sure the decls get inserted into TUScope! 99 if (PP.getLangOptions().ObjC1) { 100 // Synthesize "typedef struct objc_class *Class;" 101 RecordDecl *ClassTag = RecordDecl::Create(Context, Decl::Struct, 102 SourceLocation(), 103 &IT.get("objc_class"), 0); 104 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); 105 TypedefDecl *ClassTypedef = 106 TypedefDecl::Create(Context, SourceLocation(), 107 &Context.Idents.get("Class"), ClassT, 0); 108 Context.setObjCClassType(ClassTypedef); 109 110 // Synthesize "@class Protocol; 111 ObjCInterfaceDecl *ProtocolDecl = new ObjCInterfaceDecl(SourceLocation(), 0, 112 &Context.Idents.get("Protocol"), true); 113 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); 114 115 // Synthesize "typedef struct objc_object { Class isa; } *id;" 116 RecordDecl *ObjectTag = 117 RecordDecl::Create(Context, Decl::Struct, SourceLocation(), 118 &IT.get("objc_object"), 0); 119 FieldDecl *IsaDecl = new FieldDecl(SourceLocation(), 0, 120 Context.getObjCClassType()); 121 ObjectTag->defineBody(&IsaDecl, 1); 122 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); 123 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, SourceLocation(), 124 &Context.Idents.get("id"), 125 ObjT, 0); 126 Context.setObjCIdType(IdTypedef); 127 } 128 TUScope = 0; 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