blob: 61a8948e4d76409a53b73469c417a035712f3139 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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 Naroff8ee529b2007-10-31 18:42:27 +000019#include "clang/Parse/Scope.h"
Chris Lattner59907c42007-08-10 20:18:51 +000020
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Ted Kremeneka526c5c2008-01-07 19:49:32 +000023bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroff8ee529b2007-10-31 18:42:27 +000024 const char *typeName = TD->getIdentifier()->getName();
25 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +000026 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroff8ee529b2007-10-31 18:42:27 +000027}
28
Chris Lattner0b6d1e62008-07-21 04:13:58 +000029/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
Chris Lattnerb49b5722008-07-21 04:16:33 +000030/// 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 Lattner0b6d1e62008-07-21 04:13:58 +000033bool Sema::isObjCObjectPointerType(QualType Ty) const {
34 if (Ty->isObjCQualifiedIdType())
Chris Lattner22e684a2008-07-21 04:03:34 +000035 return true;
36
Chris Lattner0b6d1e62008-07-21 04:13:58 +000037 if (!Ty->isPointerType())
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000038 return false;
Chris Lattner22e684a2008-07-21 04:03:34 +000039
Chris Lattner17af2a62008-07-21 04:09:54 +000040 // 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 Lattner0b6d1e62008-07-21 04:13:58 +000043 if (Ty == Context.getObjCIdType() || Ty == Context.getObjCClassType())
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000044 return true;
45
Chris Lattner0b6d1e62008-07-21 04:13:58 +000046 // If this a pointer to an interface (e.g. NSString*), it is ok.
47 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000048}
49
Steve Naroffb216c882007-10-09 22:01:59 +000050void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
51 TUScope = S;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000052 CurContext = Context.getTranslationUnitDecl();
Chris Lattner2ae34ed2008-02-06 00:46:58 +000053 if (!PP.getLangOptions().ObjC1) return;
54
Steve Naroff69d63752008-02-24 16:25:02 +000055 // Synthesize "typedef struct objc_selector *SEL;"
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000056 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
Chris Lattnerc63e6602008-03-15 21:32:50 +000057 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000058 &Context.Idents.get("objc_selector"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000059 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000060 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000061
62 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000063 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
64 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000065 &Context.Idents.get("SEL"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000066 SelT, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000067 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000068 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000069
Chris Lattner27933c12008-06-21 22:44:51 +000070 // FIXME: Make sure these don't leak!
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000071 RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
72 CurContext,
73 SourceLocation(),
74 &Context.Idents.get("objc_class"),
75 0);
76 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
77 TypedefDecl *ClassTypedef =
78 TypedefDecl::Create(Context, CurContext, SourceLocation(),
79 &Context.Idents.get("Class"), ClassT, 0);
80 PushOnScopeChains(ClassTag, TUScope);
81 PushOnScopeChains(ClassTypedef, TUScope);
82 Context.setObjCClassType(ClassTypedef);
83 // Synthesize "@class Protocol;
84 ObjCInterfaceDecl *ProtocolDecl =
85 ObjCInterfaceDecl::Create(Context, SourceLocation(), 0,
86 &Context.Idents.get("Protocol"),
87 SourceLocation(), true);
88 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
89 PushOnScopeChains(ProtocolDecl, TUScope);
90
91 // Synthesize "typedef struct objc_object { Class isa; } *id;"
92 RecordDecl *ObjectTag =
93 RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
94 SourceLocation(),
95 &Context.Idents.get("objc_object"), 0);
96 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
97 PushOnScopeChains(ObjectTag, TUScope);
98 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
99 SourceLocation(),
100 &Context.Idents.get("id"),
101 ObjT, 0);
102 PushOnScopeChains(IdTypedef, TUScope);
103 Context.setObjCIdType(IdTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +0000104}
105
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000106Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000107 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) {
Chris Lattner59907c42007-08-10 20:18:51 +0000108
109 // Get IdentifierInfo objects for known functions for which we
110 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000111 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +0000112
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000113 KnownFunctionIDs[id_printf] = &IT.get("printf");
114 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
115 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
116 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
117 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000118 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000119 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
120 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
121 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
122 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
123 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000124
Steve Naroff3b950172007-10-10 21:53:07 +0000125 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000126 if (getLangOptions().CPlusPlus)
127 FieldCollector.reset(new CXXFieldCollector());
Reid Spencer5f016e22007-07-11 17:01:13 +0000128}
129
Chris Lattner1e0a3902008-01-16 19:17:22 +0000130/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
131/// If there is already an implicit cast, merge into the existing one.
132void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattner438757c2008-02-13 18:01:07 +0000133 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000134
135 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
136 ImpCast->setType(Type);
137 else
138 Expr = new ImplicitCastExpr(Type, Expr);
139}
140
141
142
Chris Lattner394a3fd2007-08-31 04:53:24 +0000143void Sema::DeleteExpr(ExprTy *E) {
144 delete static_cast<Expr*>(E);
145}
146void Sema::DeleteStmt(StmtTy *S) {
147 delete static_cast<Stmt*>(S);
148}
149
Reid Spencer5f016e22007-07-11 17:01:13 +0000150//===----------------------------------------------------------------------===//
151// Helper functions.
152//===----------------------------------------------------------------------===//
153
154bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000155 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 return true;
157}
158
159bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000160 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 return true;
162}
163
164bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
165 const std::string &Msg2) {
166 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000167 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 return true;
169}
170
171bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000172 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 return true;
174}
175
176bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
177 SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000178 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 return true;
180}
181
182bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
183 const std::string &Msg2, SourceRange Range) {
184 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000185 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 return true;
187}
188
Chris Lattner4667ac32008-01-03 23:38:43 +0000189bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
190 const std::string &Msg2, const std::string &Msg3,
191 SourceRange R1) {
192 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
193 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
194 return true;
195}
196
Reid Spencer5f016e22007-07-11 17:01:13 +0000197bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
198 SourceRange R1, SourceRange R2) {
199 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000200 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 return true;
202}
203
204bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
205 SourceRange R1, SourceRange R2) {
206 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000207 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 return true;
209}
210
211bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
212 const std::string &Msg2, SourceRange R1, SourceRange R2) {
213 std::string MsgArr[] = { Msg1, Msg2 };
214 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000215 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 return true;
217}
218
219const LangOptions &Sema::getLangOptions() const {
220 return PP.getLangOptions();
221}