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" |
| 20 | #include "clang/Basic/Diagnostic.h" |
| 21 | using namespace clang; |
| 22 | |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 23 | static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) { |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 24 | if (C.getLangOptions().CPlusPlus) |
| 25 | return CXXRecordDecl::Create(C, TagDecl::TK_struct, |
| 26 | C.getTranslationUnitDecl(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 27 | SourceLocation(), &C.Idents.get(Name)); |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame^] | 28 | |
| 29 | return RecordDecl::Create(C, TagDecl::TK_struct, |
| 30 | C.getTranslationUnitDecl(), |
| 31 | SourceLocation(), &C.Idents.get(Name)); |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Steve Naroff | 9637a9b | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 34 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 35 | TUScope = S; |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 36 | PushDeclContext(Context.getTranslationUnitDecl()); |
Chris Lattner | a8c2d59 | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 37 | if (!PP.getLangOptions().ObjC1) return; |
| 38 | |
Steve Naroff | 8f635e0 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 39 | // Synthesize "typedef struct objc_selector *SEL;" |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 40 | RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector"); |
Argiris Kirtzidis | 951f25b | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 41 | PushOnScopeChains(SelTag, TUScope); |
Steve Naroff | 8f635e0 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 42 | |
| 43 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 44 | TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, |
| 45 | SourceLocation(), |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 46 | &Context.Idents.get("SEL"), |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 47 | SelT, 0); |
Argiris Kirtzidis | 951f25b | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 48 | PushOnScopeChains(SelTypedef, TUScope); |
Steve Naroff | 8f635e0 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 49 | Context.setObjCSelType(SelTypedef); |
Chris Lattner | 4e9553a | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 1062d3a | 2008-06-21 22:44:51 +0000 | [diff] [blame] | 51 | // FIXME: Make sure these don't leak! |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 52 | RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class"); |
Chris Lattner | 4e9553a | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 53 | QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); |
| 54 | TypedefDecl *ClassTypedef = |
| 55 | TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 56 | &Context.Idents.get("Class"), ClassT, 0); |
| 57 | PushOnScopeChains(ClassTag, TUScope); |
| 58 | PushOnScopeChains(ClassTypedef, TUScope); |
| 59 | Context.setObjCClassType(ClassTypedef); |
| 60 | // Synthesize "@class Protocol; |
| 61 | ObjCInterfaceDecl *ProtocolDecl = |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 62 | ObjCInterfaceDecl::Create(Context, SourceLocation(), |
Chris Lattner | 4e9553a | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 63 | &Context.Idents.get("Protocol"), |
| 64 | SourceLocation(), true); |
| 65 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
| 66 | PushOnScopeChains(ProtocolDecl, TUScope); |
| 67 | |
| 68 | // Synthesize "typedef struct objc_object { Class isa; } *id;" |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 69 | RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object"); |
| 70 | |
Chris Lattner | 4e9553a | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 71 | QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); |
| 72 | PushOnScopeChains(ObjectTag, TUScope); |
| 73 | TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext, |
| 74 | SourceLocation(), |
| 75 | &Context.Idents.get("id"), |
| 76 | ObjT, 0); |
| 77 | PushOnScopeChains(IdTypedef, TUScope); |
| 78 | Context.setObjCIdType(IdTypedef); |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Chris Lattner | a8c2d59 | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 81 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 82 | : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0),PreDeclaratorDC(0), |
| 83 | CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()) { |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 84 | |
| 85 | // Get IdentifierInfo objects for known functions for which we |
| 86 | // do extra checking. |
Chris Lattner | a8c2d59 | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 87 | IdentifierTable &IT = PP.getIdentifierTable(); |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 88 | |
Daniel Dunbar | 0ab03e6 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 89 | KnownFunctionIDs[id_printf] = &IT.get("printf"); |
| 90 | KnownFunctionIDs[id_fprintf] = &IT.get("fprintf"); |
| 91 | KnownFunctionIDs[id_sprintf] = &IT.get("sprintf"); |
| 92 | KnownFunctionIDs[id_sprintf_chk] = &IT.get("__builtin___sprintf_chk"); |
| 93 | KnownFunctionIDs[id_snprintf] = &IT.get("snprintf"); |
| 94 | KnownFunctionIDs[id_snprintf_chk] = &IT.get("__builtin___snprintf_chk"); |
| 95 | KnownFunctionIDs[id_asprintf] = &IT.get("asprintf"); |
| 96 | KnownFunctionIDs[id_NSLog] = &IT.get("NSLog"); |
| 97 | KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); |
| 98 | KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf"); |
| 99 | KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf"); |
| 100 | KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf"); |
| 101 | KnownFunctionIDs[id_vsprintf_chk] = &IT.get("__builtin___vsprintf_chk"); |
| 102 | KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); |
| 103 | KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk"); |
| 104 | KnownFunctionIDs[id_vprintf] = &IT.get("vprintf"); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 105 | |
Daniel Dunbar | 4837ae7 | 2008-08-14 22:04:54 +0000 | [diff] [blame] | 106 | SuperID = &IT.get("super"); |
| 107 | |
Steve Naroff | 453a878 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 108 | // ObjC builtin typedef names. |
| 109 | Ident_id = &IT.get("id"); |
| 110 | Ident_Class = &IT.get("Class"); |
| 111 | Ident_SEL = &IT.get("SEL"); |
| 112 | Ident_Protocol = &IT.get("Protocol"); |
| 113 | |
Sebastian Redl | b93b49c | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 114 | Ident_StdNs = &IT.get("std"); |
| 115 | Ident_TypeInfo = 0; |
| 116 | StdNamespace = 0; |
| 117 | |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 118 | TUScope = 0; |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 119 | if (getLangOptions().CPlusPlus) |
| 120 | FieldCollector.reset(new CXXFieldCollector()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 123 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
| 124 | /// If there is already an implicit cast, merge into the existing one. |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 125 | /// If isLvalue, the result of the cast is an lvalue. |
| 126 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) { |
Mon P Wang | c6c9274 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 127 | QualType ExprTy = Context.getCanonicalType(Expr->getType()); |
| 128 | QualType TypeTy = Context.getCanonicalType(Ty); |
| 129 | |
| 130 | if (ExprTy == TypeTy) |
| 131 | return; |
| 132 | |
| 133 | if (Expr->getType().getTypePtr()->isPointerType() && |
| 134 | Ty.getTypePtr()->isPointerType()) { |
| 135 | QualType ExprBaseType = |
| 136 | cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType(); |
| 137 | QualType BaseType = |
| 138 | cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType(); |
| 139 | if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) { |
| 140 | Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast, |
| 141 | Expr->getSourceRange()); |
| 142 | } |
| 143 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 144 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 145 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
Mon P Wang | c6c9274 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 146 | ImpCast->setType(Ty); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 147 | ImpCast->setLvalueCast(isLvalue); |
| 148 | } else |
| 149 | Expr = new ImplicitCastExpr(Ty, Expr, isLvalue); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Chris Lattner | b26b7ad | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 152 | void Sema::DeleteExpr(ExprTy *E) { |
| 153 | delete static_cast<Expr*>(E); |
| 154 | } |
| 155 | void Sema::DeleteStmt(StmtTy *S) { |
| 156 | delete static_cast<Stmt*>(S); |
| 157 | } |
| 158 | |
Chris Lattner | c1aea81 | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 159 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 160 | /// translation unit when EOF is reached and all but the top-level scope is |
| 161 | /// popped. |
| 162 | void Sema::ActOnEndOfTranslationUnit() { |
| 163 | |
| 164 | } |
| 165 | |
| 166 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 167 | //===----------------------------------------------------------------------===// |
| 168 | // Helper functions. |
| 169 | //===----------------------------------------------------------------------===// |
| 170 | |
Chris Lattner | 429558c | 2008-11-18 21:53:24 +0000 | [diff] [blame] | 171 | DiagnosticInfo Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
| 172 | return PP.getDiagnostics().Report(FullSourceLoc(Loc, PP.getSourceManager()), |
| 173 | DiagID); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 177 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID) << Msg; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
| 181 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 182 | const std::string &Msg2) { |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 183 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID) << Msg1 << Msg2; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 184 | return true; |
| 185 | } |
| 186 | |
Argiris Kirtzidis | cb6d412 | 2008-08-24 13:14:02 +0000 | [diff] [blame] | 187 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const SourceRange& Range) { |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 188 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID) << Range; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
| 192 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
Argiris Kirtzidis | cb6d412 | 2008-08-24 13:14:02 +0000 | [diff] [blame] | 193 | const SourceRange& Range) { |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 194 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID) << Msg << Range; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 195 | return true; |
| 196 | } |
| 197 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 198 | const LangOptions &Sema::getLangOptions() const { |
| 199 | return PP.getLangOptions(); |
| 200 | } |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 201 | |
| 202 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Steve Naroff | 55debea | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 203 | DeclContext *DC = CurContext; |
| 204 | while (isa<BlockDecl>(DC)) |
| 205 | DC = DC->getParent(); |
| 206 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 207 | } |