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" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 21 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 24 | bool Sema::isBuiltinObjCType(TypedefDecl *TD) { |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 25 | const char *typeName = TD->getIdentifier()->getName(); |
| 26 | return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 || |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 27 | strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0; |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 30 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 31 | TUScope = S; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 32 | CurContext = Context.getTranslationUnitDecl(); |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 33 | if (!PP.getLangOptions().ObjC1) return; |
| 34 | |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 35 | // Synthesize "typedef struct objc_selector *SEL;" |
Chris Lattner | b752f28 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 36 | RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, |
| 37 | CurContext, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 38 | SourceLocation(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 39 | &Context.Idents.get("objc_selector"), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 40 | 0); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 41 | PushOnScopeChains(SelTag, TUScope); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 42 | |
| 43 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 44 | TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, |
| 45 | SourceLocation(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 46 | &Context.Idents.get("SEL"), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 47 | SelT, 0); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 48 | PushOnScopeChains(SelTypedef, TUScope); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 49 | Context.setObjCSelType(SelTypedef); |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 27933c1 | 2008-06-21 22:44:51 +0000 | [diff] [blame] | 51 | // FIXME: Make sure these don't leak! |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 52 | RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct, |
| 53 | CurContext, |
| 54 | SourceLocation(), |
| 55 | &Context.Idents.get("objc_class"), |
| 56 | 0); |
| 57 | QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); |
| 58 | TypedefDecl *ClassTypedef = |
| 59 | TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 60 | &Context.Idents.get("Class"), ClassT, 0); |
| 61 | PushOnScopeChains(ClassTag, TUScope); |
| 62 | PushOnScopeChains(ClassTypedef, TUScope); |
| 63 | Context.setObjCClassType(ClassTypedef); |
| 64 | // Synthesize "@class Protocol; |
| 65 | ObjCInterfaceDecl *ProtocolDecl = |
Chris Lattner | b752f28 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 66 | ObjCInterfaceDecl::Create(Context, SourceLocation(), |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 67 | &Context.Idents.get("Protocol"), |
| 68 | SourceLocation(), true); |
| 69 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
| 70 | PushOnScopeChains(ProtocolDecl, TUScope); |
| 71 | |
| 72 | // Synthesize "typedef struct objc_object { Class isa; } *id;" |
| 73 | RecordDecl *ObjectTag = |
| 74 | RecordDecl::Create(Context, TagDecl::TK_struct, CurContext, |
| 75 | SourceLocation(), |
| 76 | &Context.Idents.get("objc_object"), 0); |
| 77 | QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); |
| 78 | PushOnScopeChains(ObjectTag, TUScope); |
| 79 | TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext, |
| 80 | SourceLocation(), |
| 81 | &Context.Idents.get("id"), |
| 82 | ObjT, 0); |
| 83 | PushOnScopeChains(IdTypedef, TUScope); |
| 84 | Context.setObjCIdType(IdTypedef); |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 87 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 88 | : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) { |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 89 | |
| 90 | // Get IdentifierInfo objects for known functions for which we |
| 91 | // do extra checking. |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 92 | IdentifierTable &IT = PP.getIdentifierTable(); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 94 | KnownFunctionIDs[id_printf] = &IT.get("printf"); |
| 95 | KnownFunctionIDs[id_fprintf] = &IT.get("fprintf"); |
| 96 | KnownFunctionIDs[id_sprintf] = &IT.get("sprintf"); |
| 97 | KnownFunctionIDs[id_snprintf] = &IT.get("snprintf"); |
| 98 | KnownFunctionIDs[id_asprintf] = &IT.get("asprintf"); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 99 | KnownFunctionIDs[id_NSLog] = &IT.get("NSLog"); |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 100 | KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); |
| 101 | KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf"); |
| 102 | KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf"); |
| 103 | KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf"); |
| 104 | KnownFunctionIDs[id_vprintf] = &IT.get("vprintf"); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 105 | |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 106 | TUScope = 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 107 | if (getLangOptions().CPlusPlus) |
| 108 | FieldCollector.reset(new CXXFieldCollector()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 111 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
| 112 | /// If there is already an implicit cast, merge into the existing one. |
| 113 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) { |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 114 | if (Context.getCanonicalType(Expr->getType()) == |
| 115 | Context.getCanonicalType(Type)) return; |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 116 | |
| 117 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) |
| 118 | ImpCast->setType(Type); |
| 119 | else |
| 120 | Expr = new ImplicitCastExpr(Type, Expr); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 125 | void Sema::DeleteExpr(ExprTy *E) { |
| 126 | delete static_cast<Expr*>(E); |
| 127 | } |
| 128 | void Sema::DeleteStmt(StmtTy *S) { |
| 129 | delete static_cast<Stmt*>(S); |
| 130 | } |
| 131 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | //===----------------------------------------------------------------------===// |
| 133 | // Helper functions. |
| 134 | //===----------------------------------------------------------------------===// |
| 135 | |
| 136 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 137 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | |
| 141 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 142 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | return true; |
| 144 | } |
| 145 | |
| 146 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 147 | const std::string &Msg2) { |
| 148 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 149 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | |
| 153 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 154 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | return true; |
| 156 | } |
| 157 | |
| 158 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 159 | SourceRange Range) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 160 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
| 164 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 165 | const std::string &Msg2, SourceRange Range) { |
| 166 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 167 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | |
Chris Lattner | 4667ac3 | 2008-01-03 23:38:43 +0000 | [diff] [blame] | 171 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 172 | const std::string &Msg2, const std::string &Msg3, |
| 173 | SourceRange R1) { |
| 174 | std::string MsgArr[] = { Msg1, Msg2, Msg3 }; |
| 175 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1); |
| 176 | return true; |
| 177 | } |
| 178 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, |
| 180 | SourceRange R1, SourceRange R2) { |
| 181 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 182 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 187 | SourceRange R1, SourceRange R2) { |
| 188 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 189 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | |
| 193 | bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, |
| 194 | const std::string &Msg2, SourceRange R1, SourceRange R2) { |
| 195 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 196 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 197 | PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | |
| 201 | const LangOptions &Sema::getLangOptions() const { |
| 202 | return PP.getLangOptions(); |
| 203 | } |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 204 | |
| 205 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
| 206 | return dyn_cast<ObjCMethodDecl>(CurContext); |
| 207 | } |