blob: 9e379e4be7460c91f3867682acca00adff324f26 [file] [log] [blame]
Chris Lattnerddd6fc82006-11-10 04:58:55 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerddd6fc82006-11-10 04:58:55 +000010// This file implements the actions class which performs semantic analysis and
11// builds an AST out of a parse stream.
Chris Lattner3e7bd4e2006-08-17 05:51:27 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerddd6fc82006-11-10 04:58:55 +000015#include "Sema.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000016#include "clang/AST/ASTContext.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000017#include "clang/Lex/Preprocessor.h"
Chris Lattnerd6647d32007-05-16 17:56:50 +000018#include "clang/Basic/Diagnostic.h"
Steve Naroff6d40db02007-10-31 18:42:27 +000019#include "clang/Parse/Scope.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000020
Chris Lattnerc11438c2006-08-18 05:17:52 +000021using namespace clang;
22
Ted Kremenek1b0ea822008-01-07 19:49:32 +000023bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroff6d40db02007-10-31 18:42:27 +000024 const char *typeName = TD->getIdentifier()->getName();
25 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +000026 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroff6d40db02007-10-31 18:42:27 +000027}
28
Ted Kremenek1b0ea822008-01-07 19:49:32 +000029bool Sema::isObjCObjectPointerType(QualType type) const {
30 if (!type->isPointerType() && !type->isObjCQualifiedIdType())
Fariborz Jahanian775d5d02008-01-04 00:27:46 +000031 return false;
Ted Kremenek1b0ea822008-01-07 19:49:32 +000032 if (type == Context.getObjCIdType() || type == Context.getObjCClassType() ||
33 type->isObjCQualifiedIdType())
Fariborz Jahanian775d5d02008-01-04 00:27:46 +000034 return true;
35
Fariborz Jahaniand5450b72008-01-07 18:14:04 +000036 if (type->isPointerType()) {
Fariborz Jahanian775d5d02008-01-04 00:27:46 +000037 PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr());
38 type = pointerType->getPointeeType();
39 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +000040 return (type->isObjCInterfaceType() || type->isObjCQualifiedIdType());
Fariborz Jahanian775d5d02008-01-04 00:27:46 +000041}
42
Steve Naroffc62adb62007-10-09 22:01:59 +000043void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
44 TUScope = S;
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000045 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 Narofff0ed31a2008-02-24 16:25:02 +000060
61 // Synthesize "typedef struct objc_selector *SEL;"
Chris Lattnerc5ffed42008-04-04 06:12:32 +000062 RecordDecl *SelTag = RecordDecl::Create(Context, Decl::Struct, CurContext,
Chris Lattner96c460d2008-03-15 21:32:50 +000063 SourceLocation(),
Chris Lattnera7b32872008-03-15 06:12:44 +000064 &Context.Idents.get("objc_selector"),
Chris Lattner96c460d2008-03-15 21:32:50 +000065 0);
Steve Narofff0ed31a2008-02-24 16:25:02 +000066 SelTag->getIdentifier()->setFETokenInfo(SelTag);
67 TUScope->AddDecl(SelTag);
68
69 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattnerc5ffed42008-04-04 06:12:32 +000070 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
71 SourceLocation(),
Chris Lattnera7b32872008-03-15 06:12:44 +000072 &Context.Idents.get("SEL"),
Chris Lattner96c460d2008-03-15 21:32:50 +000073 SelT, 0);
Steve Narofff0ed31a2008-02-24 16:25:02 +000074 SelTypedef->getIdentifier()->setFETokenInfo(SelTypedef);
75 TUScope->AddDecl(SelTypedef);
76 Context.setObjCSelType(SelTypedef);
Steve Naroff7f549f12007-10-10 21:53:07 +000077}
78
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000079Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
80 : PP(pp), Context(ctxt), Consumer(consumer),
Chris Lattnerc5ffed42008-04-04 06:12:32 +000081 CurFunctionDecl(0), CurMethodDecl(0), CurContext(0) {
Chris Lattnerb87b1b32007-08-10 20:18:51 +000082
83 // Get IdentifierInfo objects for known functions for which we
84 // do extra checking.
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000085 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattnerb87b1b32007-08-10 20:18:51 +000086
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000087 KnownFunctionIDs[id_printf] = &IT.get("printf");
88 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
89 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
90 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
91 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
92 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
93 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
94 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
95 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
96 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff6d40db02007-10-31 18:42:27 +000097
Steve Narofff0ed31a2008-02-24 16:25:02 +000098 // FIXME: Move this initialization up to Sema::ActOnTranslationUnitScope()
99 // and make sure the decls get inserted into TUScope!
Steve Naroff6d40db02007-10-31 18:42:27 +0000100 if (PP.getLangOptions().ObjC1) {
101 // Synthesize "typedef struct objc_class *Class;"
Chris Lattner96c460d2008-03-15 21:32:50 +0000102 RecordDecl *ClassTag = RecordDecl::Create(Context, Decl::Struct,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000103 NULL,
104 SourceLocation(),
Chris Lattner96c460d2008-03-15 21:32:50 +0000105 &IT.get("objc_class"), 0);
Steve Naroff6d40db02007-10-31 18:42:27 +0000106 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
Chris Lattnera7b32872008-03-15 06:12:44 +0000107 TypedefDecl *ClassTypedef =
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000108 TypedefDecl::Create(Context, NULL, SourceLocation(),
Chris Lattner96c460d2008-03-15 21:32:50 +0000109 &Context.Idents.get("Class"), ClassT, 0);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000110 Context.setObjCClassType(ClassTypedef);
Steve Naroff6d40db02007-10-31 18:42:27 +0000111
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +0000112 // Synthesize "@class Protocol;
Chris Lattner96c501e2008-03-16 01:15:50 +0000113 ObjCInterfaceDecl *ProtocolDecl =
114 ObjCInterfaceDecl::Create(Context, SourceLocation(), 0,
115 &Context.Idents.get("Protocol"), true);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000116 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +0000117
Steve Naroff6d40db02007-10-31 18:42:27 +0000118 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Chris Lattnera7b32872008-03-15 06:12:44 +0000119 RecordDecl *ObjectTag =
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000120 RecordDecl::Create(Context, Decl::Struct, NULL,
121 SourceLocation(),
Chris Lattner96c460d2008-03-15 21:32:50 +0000122 &IT.get("objc_object"), 0);
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000123 FieldDecl *IsaDecl = FieldDecl::Create(Context, ObjectTag,
124 SourceLocation(), 0,
Chris Lattneree1284a2008-03-16 00:16:02 +0000125 Context.getObjCClassType());
Steve Naroff6d40db02007-10-31 18:42:27 +0000126 ObjectTag->defineBody(&IsaDecl, 1);
127 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000128 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, NULL,
129 SourceLocation(),
Chris Lattnera7b32872008-03-15 06:12:44 +0000130 &Context.Idents.get("id"),
Chris Lattner96c460d2008-03-15 21:32:50 +0000131 ObjT, 0);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000132 Context.setObjCIdType(IdTypedef);
Steve Naroff6d40db02007-10-31 18:42:27 +0000133 }
Steve Naroff7f549f12007-10-10 21:53:07 +0000134 TUScope = 0;
Steve Naroff38d31b42007-02-28 01:22:02 +0000135}
Chris Lattnercb6a3822006-11-10 06:20:45 +0000136
Chris Lattnera65e1f32008-01-16 19:17:22 +0000137/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
138/// If there is already an implicit cast, merge into the existing one.
139void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattner7b7ace52008-02-13 18:01:07 +0000140 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
Chris Lattnera65e1f32008-01-16 19:17:22 +0000141
142 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
143 ImpCast->setType(Type);
144 else
145 Expr = new ImplicitCastExpr(Type, Expr);
146}
147
148
149
Chris Lattner57c523f2007-08-31 04:53:24 +0000150void Sema::DeleteExpr(ExprTy *E) {
151 delete static_cast<Expr*>(E);
152}
153void Sema::DeleteStmt(StmtTy *S) {
154 delete static_cast<Stmt*>(S);
155}
156
Chris Lattnerc11438c2006-08-18 05:17:52 +0000157//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +0000158// Helper functions.
159//===----------------------------------------------------------------------===//
160
Chris Lattnerd6647d32007-05-16 17:56:50 +0000161bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000162 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Chris Lattnerd6647d32007-05-16 17:56:50 +0000163 return true;
164}
165
Steve Narofff1e53692007-03-23 22:27:02 +0000166bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000167 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattnerd6647d32007-05-16 17:56:50 +0000168 return true;
169}
170
171bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
172 const std::string &Msg2) {
173 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000174 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000175 return true;
Chris Lattnereaafe1222006-11-10 05:17:58 +0000176}
177
Steve Naroff71ce2e02007-05-18 22:53:50 +0000178bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000179 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Steve Naroffbc2f0992007-03-30 20:09:34 +0000180 return true;
181}
182
Steve Naroff71ce2e02007-05-18 22:53:50 +0000183bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
184 SourceRange Range) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000185 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000186 return true;
187}
188
189bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
190 const std::string &Msg2, SourceRange Range) {
191 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000192 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000193 return true;
194}
195
Chris Lattner816dea22008-01-03 23:38:43 +0000196bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
197 const std::string &Msg2, const std::string &Msg3,
198 SourceRange R1) {
199 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
200 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
201 return true;
202}
203
Steve Naroff71ce2e02007-05-18 22:53:50 +0000204bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
205 SourceRange R1, SourceRange R2) {
206 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000207 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000208 return true;
209}
210
211bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
212 SourceRange R1, SourceRange R2) {
213 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000214 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000215 return true;
216}
217
218bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
219 const std::string &Msg2, SourceRange R1, SourceRange R2) {
220 std::string MsgArr[] = { Msg1, Msg2 };
221 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000222 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000223 return true;
Steve Naroff8160ea22007-03-06 01:09:46 +0000224}
225
Chris Lattnerac18be92006-11-20 06:49:47 +0000226const LangOptions &Sema::getLangOptions() const {
Steve Naroff38d31b42007-02-28 01:22:02 +0000227 return PP.getLangOptions();
Chris Lattnerac18be92006-11-20 06:49:47 +0000228}