Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 22 | /// ConvertQualTypeToStringFn - This function is used to pretty print the |
| 23 | /// specified QualType as a string in diagnostics. |
Chris Lattner | 254de7d | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 24 | static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val, |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 25 | const char *Modifier, unsigned ModLen, |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 26 | const char *Argument, unsigned ArgLen, |
| 27 | llvm::SmallVectorImpl<char> &Output) { |
Chris Lattner | f5b269a | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 254de7d | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 29 | std::string S; |
| 30 | if (Kind == Diagnostic::ak_qualtype) { |
| 31 | QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val))); |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 254de7d | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 33 | // FIXME: Playing with std::string is really slow. |
| 34 | S = Ty.getAsString(); |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 35 | |
| 36 | assert(ModLen == 0 && ArgLen == 0 && |
| 37 | "Invalid modifier for QualType argument"); |
| 38 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame^] | 39 | } else if (Kind == Diagnostic::ak_declarationname) { |
Chris Lattner | 254de7d | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 40 | |
| 41 | DeclarationName N = DeclarationName::getFromOpaqueInteger(Val); |
| 42 | S = N.getAsString(); |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 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"); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame^] | 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 | |
Chris Lattner | 254de7d | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 58 | } |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 59 | Output.append(S.begin(), S.end()); |
| 60 | } |
| 61 | |
| 62 | |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 63 | static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) { |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 64 | if (C.getLangOptions().CPlusPlus) |
| 65 | return CXXRecordDecl::Create(C, TagDecl::TK_struct, |
| 66 | C.getTranslationUnitDecl(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 67 | SourceLocation(), &C.Idents.get(Name)); |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 68 | |
| 69 | return RecordDecl::Create(C, TagDecl::TK_struct, |
| 70 | C.getTranslationUnitDecl(), |
| 71 | SourceLocation(), &C.Idents.get(Name)); |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Steve Naroff | 9637a9b | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 74 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 75 | TUScope = S; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 76 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Chris Lattner | a8c2d59 | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 77 | if (!PP.getLangOptions().ObjC1) return; |
| 78 | |
Steve Naroff | 8f635e0 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 79 | // Synthesize "typedef struct objc_selector *SEL;" |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 80 | RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector"); |
Argiris Kirtzidis | 951f25b | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 81 | PushOnScopeChains(SelTag, TUScope); |
Steve Naroff | 8f635e0 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 82 | |
| 83 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 84 | TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, |
| 85 | SourceLocation(), |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 86 | &Context.Idents.get("SEL"), |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 87 | SelT); |
Argiris Kirtzidis | 951f25b | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 88 | PushOnScopeChains(SelTypedef, TUScope); |
Steve Naroff | 8f635e0 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 89 | Context.setObjCSelType(SelTypedef); |
Chris Lattner | 4e9553a | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 1062d3a | 2008-06-21 22:44:51 +0000 | [diff] [blame] | 91 | // FIXME: Make sure these don't leak! |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 92 | RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class"); |
Chris Lattner | 4e9553a | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 93 | QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); |
| 94 | TypedefDecl *ClassTypedef = |
| 95 | TypedefDecl::Create(Context, CurContext, SourceLocation(), |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 96 | &Context.Idents.get("Class"), ClassT); |
Chris Lattner | 4e9553a | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 97 | PushOnScopeChains(ClassTag, TUScope); |
| 98 | PushOnScopeChains(ClassTypedef, TUScope); |
| 99 | Context.setObjCClassType(ClassTypedef); |
| 100 | // Synthesize "@class Protocol; |
| 101 | ObjCInterfaceDecl *ProtocolDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 102 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
Chris Lattner | 4e9553a | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 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;" |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 109 | RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object"); |
| 110 | |
Chris Lattner | 4e9553a | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 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"), |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 116 | ObjT); |
Chris Lattner | 4e9553a | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 117 | PushOnScopeChains(IdTypedef, TUScope); |
| 118 | Context.setObjCIdType(IdTypedef); |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Chris Lattner | a8c2d59 | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 121 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) |
Chris Lattner | 6d89a8a | 2009-01-22 19:21:44 +0000 | [diff] [blame] | 122 | : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), |
| 123 | Diags(PP.getDiagnostics()), |
Chris Lattner | f9ea6a4 | 2008-11-22 08:28:49 +0000 | [diff] [blame] | 124 | SourceMgr(PP.getSourceManager()), CurContext(0), PreDeclaratorDC(0), |
Sebastian Redl | b5ee874 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 125 | CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()), |
| 126 | GlobalNewDeleteDeclared(false) { |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 127 | |
| 128 | // Get IdentifierInfo objects for known functions for which we |
| 129 | // do extra checking. |
Chris Lattner | a8c2d59 | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 130 | IdentifierTable &IT = PP.getIdentifierTable(); |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 131 | |
Daniel Dunbar | 0ab03e6 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 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"); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 148 | |
Sebastian Redl | b93b49c | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 149 | StdNamespace = 0; |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 150 | TUScope = 0; |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 151 | if (getLangOptions().CPlusPlus) |
| 152 | FieldCollector.reset(new CXXFieldCollector()); |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 153 | |
| 154 | // Tell diagnostics how to render things from the AST library. |
Chris Lattner | f5b269a | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 155 | PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 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. |
Nate Begeman | 7903d05 | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 160 | /// If isLvalue, the result of the cast is an lvalue. |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 161 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) { |
Mon P Wang | c6c9274 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 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()) { |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 175 | Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast) |
| 176 | << Expr->getSourceRange(); |
Mon P Wang | c6c9274 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 179 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 180 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
Mon P Wang | c6c9274 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 181 | ImpCast->setType(Ty); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 182 | ImpCast->setLvalueCast(isLvalue); |
| 183 | } else |
| 184 | Expr = new ImplicitCastExpr(Ty, Expr, isLvalue); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Chris Lattner | b26b7ad | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 187 | void Sema::DeleteExpr(ExprTy *E) { |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 188 | if (E) static_cast<Expr*>(E)->Destroy(Context); |
Chris Lattner | b26b7ad | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 189 | } |
| 190 | void Sema::DeleteStmt(StmtTy *S) { |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 191 | if (S) static_cast<Stmt*>(S)->Destroy(Context); |
Chris Lattner | b26b7ad | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | c1aea81 | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 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. |
| 197 | void Sema::ActOnEndOfTranslationUnit() { |
| 198 | |
| 199 | } |
| 200 | |
| 201 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 202 | //===----------------------------------------------------------------------===// |
| 203 | // Helper functions. |
| 204 | //===----------------------------------------------------------------------===// |
| 205 | |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 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. |
| 209 | FunctionDecl *Sema::getCurFunctionDecl() { |
| 210 | DeclContext *DC = CurContext; |
| 211 | while (isa<BlockDecl>(DC)) |
| 212 | DC = DC->getParent(); |
| 213 | return dyn_cast<FunctionDecl>(DC); |
| 214 | } |
| 215 | |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 216 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Steve Naroff | 55debea | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 217 | DeclContext *DC = CurContext; |
| 218 | while (isa<BlockDecl>(DC)) |
| 219 | DC = DC->getParent(); |
| 220 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 221 | } |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 222 | |
| 223 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
| 224 | DeclContext *DC = CurContext; |
| 225 | while (isa<BlockDecl>(DC)) |
| 226 | DC = DC->getParent(); |
| 227 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 228 | return cast<NamedDecl>(DC); |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |