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 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 29 | bool Sema::isObjCObjectPointerType(QualType type) const { |
| 30 | if (!type->isPointerType() && !type->isObjCQualifiedIdType()) |
Fariborz Jahanian | 1f990d6 | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 31 | return false; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 32 | if (type == Context.getObjCIdType() || type == Context.getObjCClassType() || |
| 33 | type->isObjCQualifiedIdType()) |
Fariborz Jahanian | 1f990d6 | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 34 | return true; |
| 35 | |
Fariborz Jahanian | 540fc53 | 2008-01-07 18:14:04 +0000 | [diff] [blame] | 36 | if (type->isPointerType()) { |
Fariborz Jahanian | 1f990d6 | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 37 | PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr()); |
| 38 | type = pointerType->getPointeeType(); |
| 39 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 40 | return (type->isObjCInterfaceType() || type->isObjCQualifiedIdType()); |
Fariborz Jahanian | 1f990d6 | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 43 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 44 | TUScope = S; |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 45 | if (!PP.getLangOptions().ObjC1) return; |
| 46 | |
| 47 | TypedefType *t; |
| 48 | |
| 49 | // Add the built-in ObjC types. |
| 50 | t = cast<TypedefType>(Context.getObjCIdType().getTypePtr()); |
| 51 | t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); |
| 52 | TUScope->AddDecl(t->getDecl()); |
| 53 | t = cast<TypedefType>(Context.getObjCClassType().getTypePtr()); |
| 54 | t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); |
| 55 | TUScope->AddDecl(t->getDecl()); |
| 56 | ObjCInterfaceType *it = cast<ObjCInterfaceType>(Context.getObjCProtoType()); |
| 57 | ObjCInterfaceDecl *IDecl = it->getDecl(); |
| 58 | IDecl->getIdentifier()->setFETokenInfo(IDecl); |
| 59 | TUScope->AddDecl(IDecl); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 60 | |
| 61 | // Synthesize "typedef struct objc_selector *SEL;" |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame^] | 62 | RecordDecl *SelTag = RecordDecl::Create(Context, Decl::Struct, |
| 63 | SourceLocation(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 64 | &Context.Idents.get("objc_selector"), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame^] | 65 | 0); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 66 | SelTag->getIdentifier()->setFETokenInfo(SelTag); |
| 67 | TUScope->AddDecl(SelTag); |
| 68 | |
| 69 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame^] | 70 | TypedefDecl *SelTypedef = TypedefDecl::Create(Context, SourceLocation(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 71 | &Context.Idents.get("SEL"), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame^] | 72 | SelT, 0); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 73 | SelTypedef->getIdentifier()->setFETokenInfo(SelTypedef); |
| 74 | TUScope->AddDecl(SelTypedef); |
| 75 | Context.setObjCSelType(SelTypedef); |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 78 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) |
| 79 | : PP(pp), Context(ctxt), Consumer(consumer), |
| 80 | CurFunctionDecl(0), CurMethodDecl(0) { |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 81 | |
| 82 | // Get IdentifierInfo objects for known functions for which we |
| 83 | // do extra checking. |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 84 | IdentifierTable &IT = PP.getIdentifierTable(); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 86 | KnownFunctionIDs[id_printf] = &IT.get("printf"); |
| 87 | KnownFunctionIDs[id_fprintf] = &IT.get("fprintf"); |
| 88 | KnownFunctionIDs[id_sprintf] = &IT.get("sprintf"); |
| 89 | KnownFunctionIDs[id_snprintf] = &IT.get("snprintf"); |
| 90 | KnownFunctionIDs[id_asprintf] = &IT.get("asprintf"); |
| 91 | KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); |
| 92 | KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf"); |
| 93 | KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf"); |
| 94 | KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf"); |
| 95 | KnownFunctionIDs[id_vprintf] = &IT.get("vprintf"); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 96 | |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 97 | // FIXME: Move this initialization up to Sema::ActOnTranslationUnitScope() |
| 98 | // and make sure the decls get inserted into TUScope! |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 99 | if (PP.getLangOptions().ObjC1) { |
| 100 | // Synthesize "typedef struct objc_class *Class;" |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame^] | 101 | RecordDecl *ClassTag = RecordDecl::Create(Context, Decl::Struct, |
| 102 | SourceLocation(), |
| 103 | &IT.get("objc_class"), 0); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 104 | QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 105 | TypedefDecl *ClassTypedef = |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame^] | 106 | TypedefDecl::Create(Context, SourceLocation(), |
| 107 | &Context.Idents.get("Class"), ClassT, 0); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 108 | Context.setObjCClassType(ClassTypedef); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 109 | |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 110 | // Synthesize "@class Protocol; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 111 | ObjCInterfaceDecl *ProtocolDecl = new ObjCInterfaceDecl(SourceLocation(), 0, |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 112 | &Context.Idents.get("Protocol"), true); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 113 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 114 | |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 115 | // Synthesize "typedef struct objc_object { Class isa; } *id;" |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 116 | RecordDecl *ObjectTag = |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame^] | 117 | RecordDecl::Create(Context, Decl::Struct, SourceLocation(), |
| 118 | &IT.get("objc_object"), 0); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 119 | FieldDecl *IsaDecl = new FieldDecl(SourceLocation(), 0, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 120 | Context.getObjCClassType()); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 121 | ObjectTag->defineBody(&IsaDecl, 1); |
| 122 | QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame^] | 123 | TypedefDecl *IdTypedef = TypedefDecl::Create(Context, SourceLocation(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 124 | &Context.Idents.get("id"), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame^] | 125 | ObjT, 0); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 126 | Context.setObjCIdType(IdTypedef); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 127 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 128 | TUScope = 0; |
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 | } |