Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
| 20 | #include "clang/Basic/Diagnostic.h" |
| 21 | using namespace clang; |
| 22 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 23 | /// ConvertQualTypeToStringFn - This function is used to pretty print the |
| 24 | /// specified QualType as a string in diagnostics. |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 25 | static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val, |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 26 | const char *Modifier, unsigned ModLen, |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 27 | const char *Argument, unsigned ArgLen, |
| 28 | llvm::SmallVectorImpl<char> &Output) { |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 30 | std::string S; |
| 31 | if (Kind == Diagnostic::ak_qualtype) { |
| 32 | QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val))); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 34 | // FIXME: Playing with std::string is really slow. |
| 35 | S = Ty.getAsString(); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 36 | |
| 37 | assert(ModLen == 0 && ArgLen == 0 && |
| 38 | "Invalid modifier for QualType argument"); |
| 39 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 40 | } else { |
| 41 | assert(Kind == Diagnostic::ak_declarationname); |
| 42 | |
| 43 | DeclarationName N = DeclarationName::getFromOpaqueInteger(Val); |
| 44 | S = N.getAsString(); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 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"); |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 53 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 54 | Output.append(S.begin(), S.end()); |
| 55 | } |
| 56 | |
| 57 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 58 | static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) { |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 59 | if (C.getLangOptions().CPlusPlus) |
| 60 | return CXXRecordDecl::Create(C, TagDecl::TK_struct, |
| 61 | C.getTranslationUnitDecl(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 62 | SourceLocation(), &C.Idents.get(Name)); |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 63 | |
| 64 | return RecordDecl::Create(C, TagDecl::TK_struct, |
| 65 | C.getTranslationUnitDecl(), |
| 66 | SourceLocation(), &C.Idents.get(Name)); |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 69 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 70 | TUScope = S; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 71 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 72 | if (!PP.getLangOptions().ObjC1) return; |
| 73 | |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 74 | // Synthesize "typedef struct objc_selector *SEL;" |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 75 | RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector"); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 76 | PushOnScopeChains(SelTag, TUScope); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 77 | |
| 78 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 79 | TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, |
| 80 | SourceLocation(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 81 | &Context.Idents.get("SEL"), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 82 | SelT, 0); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 83 | PushOnScopeChains(SelTypedef, TUScope); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 84 | Context.setObjCSelType(SelTypedef); |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 27933c1 | 2008-06-21 22:44:51 +0000 | [diff] [blame] | 86 | // FIXME: Make sure these don't leak! |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 87 | RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class"); |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 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 = |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 97 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 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;" |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 104 | RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object"); |
| 105 | |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 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); |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 116 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) |
Chris Lattner | 3cfa928 | 2008-11-22 08:28:49 +0000 | [diff] [blame] | 117 | : PP(pp), Context(ctxt), Consumer(consumer), Diags(PP.getDiagnostics()), |
| 118 | SourceMgr(PP.getSourceManager()), CurContext(0), PreDeclaratorDC(0), |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 119 | CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()), |
| 120 | GlobalNewDeleteDeclared(false) { |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 121 | |
| 122 | // Get IdentifierInfo objects for known functions for which we |
| 123 | // do extra checking. |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 124 | IdentifierTable &IT = PP.getIdentifierTable(); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 125 | |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 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"); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 142 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 143 | StdNamespace = 0; |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 144 | TUScope = 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 145 | if (getLangOptions().CPlusPlus) |
| 146 | FieldCollector.reset(new CXXFieldCollector()); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 147 | |
| 148 | // Tell diagnostics how to render things from the AST library. |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 149 | PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 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. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 154 | /// If isLvalue, the result of the cast is an lvalue. |
| 155 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) { |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 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()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 169 | Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast) |
| 170 | << Expr->getSourceRange(); |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 173 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 174 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 175 | ImpCast->setType(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 176 | ImpCast->setLvalueCast(isLvalue); |
| 177 | } else |
| 178 | Expr = new ImplicitCastExpr(Ty, Expr, isLvalue); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 181 | void Sema::DeleteExpr(ExprTy *E) { |
Sebastian Redl | cfb664c | 2008-12-22 17:51:10 +0000 | [diff] [blame] | 182 | static_cast<Expr*>(E)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 183 | } |
| 184 | void Sema::DeleteStmt(StmtTy *S) { |
Sebastian Redl | cfb664c | 2008-12-22 17:51:10 +0000 | [diff] [blame] | 185 | static_cast<Stmt*>(S)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 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. |
| 191 | void Sema::ActOnEndOfTranslationUnit() { |
| 192 | |
| 193 | } |
| 194 | |
| 195 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | //===----------------------------------------------------------------------===// |
| 197 | // Helper functions. |
| 198 | //===----------------------------------------------------------------------===// |
| 199 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | const LangOptions &Sema::getLangOptions() const { |
| 201 | return PP.getLangOptions(); |
| 202 | } |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 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. |
| 207 | FunctionDecl *Sema::getCurFunctionDecl() { |
| 208 | DeclContext *DC = CurContext; |
| 209 | while (isa<BlockDecl>(DC)) |
| 210 | DC = DC->getParent(); |
| 211 | return dyn_cast<FunctionDecl>(DC); |
| 212 | } |
| 213 | |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 214 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Steve Naroff | d7612e1 | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 215 | DeclContext *DC = CurContext; |
| 216 | while (isa<BlockDecl>(DC)) |
| 217 | DC = DC->getParent(); |
| 218 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 219 | } |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 220 | |
| 221 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
| 222 | DeclContext *DC = CurContext; |
| 223 | while (isa<BlockDecl>(DC)) |
| 224 | DC = DC->getParent(); |
| 225 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 226 | return cast<ScopedDecl>(DC); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |