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