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" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Basic/Diagnostic.h" |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Scope.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 20 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 23 | bool Sema::isBuiltinObjCType(TypedefDecl *TD) { |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 24 | const char *typeName = TD->getIdentifier()->getName(); |
| 25 | return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 || |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 26 | strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0; |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Chris Lattner | 0b6d1e6 | 2008-07-21 04:13:58 +0000 | [diff] [blame] | 29 | /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer |
Chris Lattner | b49b572 | 2008-07-21 04:16:33 +0000 | [diff] [blame] | 30 | /// to an object type. This includes "id" and "Class" (two 'special' pointers |
| 31 | /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified |
| 32 | /// ID type). |
Chris Lattner | 0b6d1e6 | 2008-07-21 04:13:58 +0000 | [diff] [blame] | 33 | bool Sema::isObjCObjectPointerType(QualType Ty) const { |
| 34 | if (Ty->isObjCQualifiedIdType()) |
Chris Lattner | 22e684a | 2008-07-21 04:03:34 +0000 | [diff] [blame] | 35 | return true; |
| 36 | |
Chris Lattner | 0b6d1e6 | 2008-07-21 04:13:58 +0000 | [diff] [blame] | 37 | if (!Ty->isPointerType()) |
Fariborz Jahanian | 1f990d6 | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 38 | return false; |
Chris Lattner | 22e684a | 2008-07-21 04:03:34 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 17af2a6 | 2008-07-21 04:09:54 +0000 | [diff] [blame] | 40 | // Check to see if this is 'id' or 'Class', both of which are typedefs for |
| 41 | // pointer types. This looks for the typedef specifically, not for the |
| 42 | // underlying type. |
Chris Lattner | 0b6d1e6 | 2008-07-21 04:13:58 +0000 | [diff] [blame] | 43 | if (Ty == Context.getObjCIdType() || Ty == Context.getObjCClassType()) |
Fariborz Jahanian | 1f990d6 | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 44 | return true; |
| 45 | |
Chris Lattner | 0b6d1e6 | 2008-07-21 04:13:58 +0000 | [diff] [blame] | 46 | // If this a pointer to an interface (e.g. NSString*), it is ok. |
| 47 | return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType(); |
Fariborz Jahanian | 1f990d6 | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 50 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 51 | TUScope = S; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 52 | CurContext = Context.getTranslationUnitDecl(); |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 53 | if (!PP.getLangOptions().ObjC1) return; |
| 54 | |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 55 | // Synthesize "typedef struct objc_selector *SEL;" |
Chris Lattner | b752f28 | 2008-07-21 07:06:49 +0000 | [diff] [blame^] | 56 | RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, |
| 57 | CurContext, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 58 | SourceLocation(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 59 | &Context.Idents.get("objc_selector"), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 60 | 0); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 61 | PushOnScopeChains(SelTag, TUScope); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 62 | |
| 63 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 64 | TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, |
| 65 | SourceLocation(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 66 | &Context.Idents.get("SEL"), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 67 | SelT, 0); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 68 | PushOnScopeChains(SelTypedef, TUScope); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 69 | Context.setObjCSelType(SelTypedef); |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 27933c1 | 2008-06-21 22:44:51 +0000 | [diff] [blame] | 71 | // FIXME: Make sure these don't leak! |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 72 | RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct, |
| 73 | CurContext, |
| 74 | SourceLocation(), |
| 75 | &Context.Idents.get("objc_class"), |
| 76 | 0); |
| 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;" |
| 93 | RecordDecl *ObjectTag = |
| 94 | RecordDecl::Create(Context, TagDecl::TK_struct, CurContext, |
| 95 | SourceLocation(), |
| 96 | &Context.Idents.get("objc_object"), 0); |
| 97 | QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); |
| 98 | PushOnScopeChains(ObjectTag, TUScope); |
| 99 | TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext, |
| 100 | SourceLocation(), |
| 101 | &Context.Idents.get("id"), |
| 102 | ObjT, 0); |
| 103 | PushOnScopeChains(IdTypedef, TUScope); |
| 104 | Context.setObjCIdType(IdTypedef); |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 107 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 108 | : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) { |
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 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +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_snprintf] = &IT.get("snprintf"); |
| 118 | KnownFunctionIDs[id_asprintf] = &IT.get("asprintf"); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 119 | KnownFunctionIDs[id_NSLog] = &IT.get("NSLog"); |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 120 | KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); |
| 121 | KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf"); |
| 122 | KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf"); |
| 123 | KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf"); |
| 124 | KnownFunctionIDs[id_vprintf] = &IT.get("vprintf"); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 125 | |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 126 | TUScope = 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 127 | if (getLangOptions().CPlusPlus) |
| 128 | FieldCollector.reset(new CXXFieldCollector()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 131 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
| 132 | /// If there is already an implicit cast, merge into the existing one. |
| 133 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) { |
Chris Lattner | 438757c | 2008-02-13 18:01:07 +0000 | [diff] [blame] | 134 | if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return; |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 135 | |
| 136 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) |
| 137 | ImpCast->setType(Type); |
| 138 | else |
| 139 | Expr = new ImplicitCastExpr(Type, Expr); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 144 | void Sema::DeleteExpr(ExprTy *E) { |
| 145 | delete static_cast<Expr*>(E); |
| 146 | } |
| 147 | void Sema::DeleteStmt(StmtTy *S) { |
| 148 | delete static_cast<Stmt*>(S); |
| 149 | } |
| 150 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | //===----------------------------------------------------------------------===// |
| 152 | // Helper functions. |
| 153 | //===----------------------------------------------------------------------===// |
| 154 | |
| 155 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 156 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | |
| 160 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 161 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | return true; |
| 163 | } |
| 164 | |
| 165 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 166 | const std::string &Msg2) { |
| 167 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 168 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | |
| 172 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 173 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | |
| 177 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 178 | SourceRange Range) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 179 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | return true; |
| 181 | } |
| 182 | |
| 183 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 184 | const std::string &Msg2, SourceRange Range) { |
| 185 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 186 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | |
Chris Lattner | 4667ac3 | 2008-01-03 23:38:43 +0000 | [diff] [blame] | 190 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 191 | const std::string &Msg2, const std::string &Msg3, |
| 192 | SourceRange R1) { |
| 193 | std::string MsgArr[] = { Msg1, Msg2, Msg3 }; |
| 194 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1); |
| 195 | return true; |
| 196 | } |
| 197 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, |
| 199 | SourceRange R1, SourceRange R2) { |
| 200 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 201 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | return true; |
| 203 | } |
| 204 | |
| 205 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 206 | SourceRange R1, SourceRange R2) { |
| 207 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 208 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | return true; |
| 210 | } |
| 211 | |
| 212 | bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, |
| 213 | const std::string &Msg2, SourceRange R1, SourceRange R2) { |
| 214 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 215 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 216 | PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
| 220 | const LangOptions &Sema::getLangOptions() const { |
| 221 | return PP.getLangOptions(); |
| 222 | } |