Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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 | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame] | 10 | // This file implements the actions class which performs semantic analysis and |
| 11 | // builds an AST out of a parse stream. |
Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame] | 15 | #include "Sema.h" |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | d6647d3 | 2007-05-16 17:56:50 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Steve Naroff | 6d40db0 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Scope.h" |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 20 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 23 | bool Sema::isBuiltinObjCType(TypedefDecl *TD) { |
Steve Naroff | 6d40db0 | 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 | 2bbd03a | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 26 | strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0; |
Steve Naroff | 6d40db0 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 29 | bool Sema::isObjCObjectPointerType(QualType type) const { |
| 30 | if (!type->isPointerType() && !type->isObjCQualifiedIdType()) |
Fariborz Jahanian | 775d5d0 | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 31 | return false; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 32 | if (type == Context.getObjCIdType() || type == Context.getObjCClassType() || |
| 33 | type->isObjCQualifiedIdType()) |
Fariborz Jahanian | 775d5d0 | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 34 | return true; |
| 35 | |
Fariborz Jahanian | d5450b7 | 2008-01-07 18:14:04 +0000 | [diff] [blame] | 36 | if (type->isPointerType()) { |
Fariborz Jahanian | 775d5d0 | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 37 | PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr()); |
| 38 | type = pointerType->getPointeeType(); |
| 39 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 40 | return (type->isObjCInterfaceType() || type->isObjCQualifiedIdType()); |
Fariborz Jahanian | 775d5d0 | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Steve Naroff | c62adb6 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 43 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 44 | TUScope = S; |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 45 | CurContext = Context.getTranslationUnitDecl(); |
Chris Lattner | fe0e0af | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 46 | if (!PP.getLangOptions().ObjC1) return; |
| 47 | |
Steve Naroff | f0ed31a | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 48 | // Synthesize "typedef struct objc_selector *SEL;" |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 49 | RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, CurContext, |
Chris Lattner | 96c460d | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 50 | SourceLocation(), |
Chris Lattner | a7b3287 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 51 | &Context.Idents.get("objc_selector"), |
Chris Lattner | 96c460d | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 52 | 0); |
Argyrios Kyrtzidis | b8a4920 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 53 | PushOnScopeChains(SelTag, TUScope); |
Steve Naroff | f0ed31a | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 54 | |
| 55 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 56 | TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, |
| 57 | SourceLocation(), |
Chris Lattner | a7b3287 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 58 | &Context.Idents.get("SEL"), |
Chris Lattner | 96c460d | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 59 | SelT, 0); |
Argyrios Kyrtzidis | b8a4920 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 60 | PushOnScopeChains(SelTypedef, TUScope); |
Steve Naroff | f0ed31a | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 61 | Context.setObjCSelType(SelTypedef); |
Chris Lattner | 5a92bab | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 7fa2758 | 2008-06-21 22:44:51 +0000 | [diff] [blame] | 63 | // FIXME: Make sure these don't leak! |
Chris Lattner | 5a92bab | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 64 | RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct, |
| 65 | CurContext, |
| 66 | SourceLocation(), |
| 67 | &Context.Idents.get("objc_class"), |
| 68 | 0); |
| 69 | QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); |
| 70 | TypedefDecl *ClassTypedef = |
| 71 | TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 72 | &Context.Idents.get("Class"), ClassT, 0); |
| 73 | PushOnScopeChains(ClassTag, TUScope); |
| 74 | PushOnScopeChains(ClassTypedef, TUScope); |
| 75 | Context.setObjCClassType(ClassTypedef); |
| 76 | // Synthesize "@class Protocol; |
| 77 | ObjCInterfaceDecl *ProtocolDecl = |
| 78 | ObjCInterfaceDecl::Create(Context, SourceLocation(), 0, |
| 79 | &Context.Idents.get("Protocol"), |
| 80 | SourceLocation(), true); |
| 81 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
| 82 | PushOnScopeChains(ProtocolDecl, TUScope); |
| 83 | |
| 84 | // Synthesize "typedef struct objc_object { Class isa; } *id;" |
| 85 | RecordDecl *ObjectTag = |
| 86 | RecordDecl::Create(Context, TagDecl::TK_struct, CurContext, |
| 87 | SourceLocation(), |
| 88 | &Context.Idents.get("objc_object"), 0); |
| 89 | QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); |
| 90 | PushOnScopeChains(ObjectTag, TUScope); |
| 91 | TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext, |
| 92 | SourceLocation(), |
| 93 | &Context.Idents.get("id"), |
| 94 | ObjT, 0); |
| 95 | PushOnScopeChains(IdTypedef, TUScope); |
| 96 | Context.setObjCIdType(IdTypedef); |
Steve Naroff | 7f549f1 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Chris Lattner | fe0e0af | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 99 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 100 | : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) { |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 101 | |
| 102 | // Get IdentifierInfo objects for known functions for which we |
| 103 | // do extra checking. |
Chris Lattner | fe0e0af | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 104 | IdentifierTable &IT = PP.getIdentifierTable(); |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 105 | |
Chris Lattner | fe0e0af | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 106 | KnownFunctionIDs[id_printf] = &IT.get("printf"); |
| 107 | KnownFunctionIDs[id_fprintf] = &IT.get("fprintf"); |
| 108 | KnownFunctionIDs[id_sprintf] = &IT.get("sprintf"); |
| 109 | KnownFunctionIDs[id_snprintf] = &IT.get("snprintf"); |
| 110 | KnownFunctionIDs[id_asprintf] = &IT.get("asprintf"); |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 111 | KnownFunctionIDs[id_NSLog] = &IT.get("NSLog"); |
Chris Lattner | fe0e0af | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 112 | KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); |
| 113 | KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf"); |
| 114 | KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf"); |
| 115 | KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf"); |
| 116 | KnownFunctionIDs[id_vprintf] = &IT.get("vprintf"); |
Steve Naroff | 6d40db0 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 117 | |
Steve Naroff | 7f549f1 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 118 | TUScope = 0; |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame^] | 119 | if (getLangOptions().CPlusPlus) |
| 120 | FieldCollector.reset(new CXXFieldCollector()); |
Steve Naroff | 38d31b4 | 2007-02-28 01:22:02 +0000 | [diff] [blame] | 121 | } |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 122 | |
Chris Lattner | a65e1f3 | 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. |
| 125 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) { |
Chris Lattner | 7b7ace5 | 2008-02-13 18:01:07 +0000 | [diff] [blame] | 126 | if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return; |
Chris Lattner | a65e1f3 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 127 | |
| 128 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) |
| 129 | ImpCast->setType(Type); |
| 130 | else |
| 131 | Expr = new ImplicitCastExpr(Type, Expr); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
Chris Lattner | 57c523f | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 136 | void Sema::DeleteExpr(ExprTy *E) { |
| 137 | delete static_cast<Expr*>(E); |
| 138 | } |
| 139 | void Sema::DeleteStmt(StmtTy *S) { |
| 140 | delete static_cast<Stmt*>(S); |
| 141 | } |
| 142 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 143 | //===----------------------------------------------------------------------===// |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 144 | // Helper functions. |
| 145 | //===----------------------------------------------------------------------===// |
| 146 | |
Chris Lattner | d6647d3 | 2007-05-16 17:56:50 +0000 | [diff] [blame] | 147 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 148 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID); |
Chris Lattner | d6647d3 | 2007-05-16 17:56:50 +0000 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 152 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 153 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1); |
Chris Lattner | d6647d3 | 2007-05-16 17:56:50 +0000 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | |
| 157 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 158 | const std::string &Msg2) { |
| 159 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 160 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 161 | return true; |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 164 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 165 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1); |
Steve Naroff | bc2f099 | 2007-03-30 20:09:34 +0000 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 169 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 170 | SourceRange Range) { |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 171 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1); |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 172 | return true; |
| 173 | } |
| 174 | |
| 175 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 176 | const std::string &Msg2, SourceRange Range) { |
| 177 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 178 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1); |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | |
Chris Lattner | 816dea2 | 2008-01-03 23:38:43 +0000 | [diff] [blame] | 182 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 183 | const std::string &Msg2, const std::string &Msg3, |
| 184 | SourceRange R1) { |
| 185 | std::string MsgArr[] = { Msg1, Msg2, Msg3 }; |
| 186 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1); |
| 187 | return true; |
| 188 | } |
| 189 | |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 190 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, |
| 191 | SourceRange R1, SourceRange R2) { |
| 192 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 193 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2); |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 194 | return true; |
| 195 | } |
| 196 | |
| 197 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 198 | SourceRange R1, SourceRange R2) { |
| 199 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 200 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2); |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | |
| 204 | bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, |
| 205 | const std::string &Msg2, SourceRange R1, SourceRange R2) { |
| 206 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 207 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 208 | PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 209 | return true; |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 212 | const LangOptions &Sema::getLangOptions() const { |
Steve Naroff | 38d31b4 | 2007-02-28 01:22:02 +0000 | [diff] [blame] | 213 | return PP.getLangOptions(); |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 214 | } |