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