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