Sema.cpp revision 371f258e61e1365b951b17931a3c5ac1530fd1a0
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/AST/DeclObjC.h" 18#include "clang/AST/Expr.h" 19#include "clang/Lex/Preprocessor.h" 20#include "clang/Basic/Diagnostic.h" 21using namespace clang; 22 23/// ConvertQualTypeToStringFn - This function is used to pretty print the 24/// specified QualType as a string in diagnostics. 25static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val, 26 const char *Modifier, unsigned ModLen, 27 const char *Argument, unsigned ArgLen, 28 llvm::SmallVectorImpl<char> &Output) { 29 30 std::string S; 31 if (Kind == Diagnostic::ak_qualtype) { 32 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val))); 33 34 // FIXME: Playing with std::string is really slow. 35 S = Ty.getAsString(); 36 37 assert(ModLen == 0 && ArgLen == 0 && 38 "Invalid modifier for QualType argument"); 39 40 } else { 41 assert(Kind == Diagnostic::ak_declarationname); 42 43 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val); 44 S = N.getAsString(); 45 46 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0) 47 S = '+' + S; 48 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0) 49 S = '-' + S; 50 else 51 assert(ModLen == 0 && ArgLen == 0 && 52 "Invalid modifier for DeclarationName argument"); 53 } 54 Output.append(S.begin(), S.end()); 55} 56 57 58static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) { 59 if (C.getLangOptions().CPlusPlus) 60 return CXXRecordDecl::Create(C, TagDecl::TK_struct, 61 C.getTranslationUnitDecl(), 62 SourceLocation(), &C.Idents.get(Name)); 63 64 return RecordDecl::Create(C, TagDecl::TK_struct, 65 C.getTranslationUnitDecl(), 66 SourceLocation(), &C.Idents.get(Name)); 67} 68 69void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { 70 TUScope = S; 71 PushDeclContext(Context.getTranslationUnitDecl()); 72 if (!PP.getLangOptions().ObjC1) return; 73 74 // Synthesize "typedef struct objc_selector *SEL;" 75 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector"); 76 PushOnScopeChains(SelTag, TUScope); 77 78 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); 79 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, 80 SourceLocation(), 81 &Context.Idents.get("SEL"), 82 SelT, 0); 83 PushOnScopeChains(SelTypedef, TUScope); 84 Context.setObjCSelType(SelTypedef); 85 86 // FIXME: Make sure these don't leak! 87 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class"); 88 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); 89 TypedefDecl *ClassTypedef = 90 TypedefDecl::Create(Context, CurContext, SourceLocation(), 91 &Context.Idents.get("Class"), ClassT, 0); 92 PushOnScopeChains(ClassTag, TUScope); 93 PushOnScopeChains(ClassTypedef, TUScope); 94 Context.setObjCClassType(ClassTypedef); 95 // Synthesize "@class Protocol; 96 ObjCInterfaceDecl *ProtocolDecl = 97 ObjCInterfaceDecl::Create(Context, SourceLocation(), 98 &Context.Idents.get("Protocol"), 99 SourceLocation(), true); 100 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); 101 PushOnScopeChains(ProtocolDecl, TUScope); 102 103 // Synthesize "typedef struct objc_object { Class isa; } *id;" 104 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object"); 105 106 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); 107 PushOnScopeChains(ObjectTag, TUScope); 108 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext, 109 SourceLocation(), 110 &Context.Idents.get("id"), 111 ObjT, 0); 112 PushOnScopeChains(IdTypedef, TUScope); 113 Context.setObjCIdType(IdTypedef); 114} 115 116Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) 117 : PP(pp), Context(ctxt), Consumer(consumer), Diags(PP.getDiagnostics()), 118 SourceMgr(PP.getSourceManager()), CurContext(0), PreDeclaratorDC(0), 119 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()), 120 GlobalNewDeleteDeclared(false) { 121 122 // Get IdentifierInfo objects for known functions for which we 123 // do extra checking. 124 IdentifierTable &IT = PP.getIdentifierTable(); 125 126 KnownFunctionIDs[id_printf] = &IT.get("printf"); 127 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf"); 128 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf"); 129 KnownFunctionIDs[id_sprintf_chk] = &IT.get("__builtin___sprintf_chk"); 130 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf"); 131 KnownFunctionIDs[id_snprintf_chk] = &IT.get("__builtin___snprintf_chk"); 132 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf"); 133 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog"); 134 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); 135 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf"); 136 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf"); 137 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf"); 138 KnownFunctionIDs[id_vsprintf_chk] = &IT.get("__builtin___vsprintf_chk"); 139 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); 140 KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk"); 141 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf"); 142 143 StdNamespace = 0; 144 TUScope = 0; 145 if (getLangOptions().CPlusPlus) 146 FieldCollector.reset(new CXXFieldCollector()); 147 148 // Tell diagnostics how to render things from the AST library. 149 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn); 150} 151 152/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. 153/// If there is already an implicit cast, merge into the existing one. 154 /// If isLvalue, the result of the cast is an lvalue. 155void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) { 156 QualType ExprTy = Context.getCanonicalType(Expr->getType()); 157 QualType TypeTy = Context.getCanonicalType(Ty); 158 159 if (ExprTy == TypeTy) 160 return; 161 162 if (Expr->getType().getTypePtr()->isPointerType() && 163 Ty.getTypePtr()->isPointerType()) { 164 QualType ExprBaseType = 165 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType(); 166 QualType BaseType = 167 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType(); 168 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) { 169 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast) 170 << Expr->getSourceRange(); 171 } 172 } 173 174 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { 175 ImpCast->setType(Ty); 176 ImpCast->setLvalueCast(isLvalue); 177 } else 178 Expr = new ImplicitCastExpr(Ty, Expr, isLvalue); 179} 180 181void Sema::DeleteExpr(ExprTy *E) { 182 delete static_cast<Expr*>(E); 183} 184void Sema::DeleteStmt(StmtTy *S) { 185 delete static_cast<Stmt*>(S); 186} 187 188/// ActOnEndOfTranslationUnit - This is called at the very end of the 189/// translation unit when EOF is reached and all but the top-level scope is 190/// popped. 191void Sema::ActOnEndOfTranslationUnit() { 192 193} 194 195 196//===----------------------------------------------------------------------===// 197// Helper functions. 198//===----------------------------------------------------------------------===// 199 200const LangOptions &Sema::getLangOptions() const { 201 return PP.getLangOptions(); 202} 203 204/// getCurFunctionDecl - If inside of a function body, this returns a pointer 205/// to the function decl for the function being parsed. If we're currently 206/// in a 'block', this returns the containing context. 207FunctionDecl *Sema::getCurFunctionDecl() { 208 DeclContext *DC = CurContext; 209 while (isa<BlockDecl>(DC)) 210 DC = DC->getParent(); 211 return dyn_cast<FunctionDecl>(DC); 212} 213 214ObjCMethodDecl *Sema::getCurMethodDecl() { 215 DeclContext *DC = CurContext; 216 while (isa<BlockDecl>(DC)) 217 DC = DC->getParent(); 218 return dyn_cast<ObjCMethodDecl>(DC); 219} 220 221NamedDecl *Sema::getCurFunctionOrMethodDecl() { 222 DeclContext *DC = CurContext; 223 while (isa<BlockDecl>(DC)) 224 DC = DC->getParent(); 225 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) 226 return cast<NamedDecl>(DC); 227 return 0; 228} 229 230