blob: 4d1a0537798ff4ebbb90f28f3a255dd3b2709e1e [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 Lattner22e684a2008-07-21 04:03:34 +000029/// isObjCObjectPointerType - Returns true if type is an objective-c pointer
30/// to an object type; such as "id", "Class", Intf*, id<P>, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000031bool Sema::isObjCObjectPointerType(QualType type) const {
Chris Lattner22e684a2008-07-21 04:03:34 +000032 if (type->isObjCQualifiedIdType())
33 return true;
34
35 if (!type->isPointerType())
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000036 return false;
Chris Lattner22e684a2008-07-21 04:03:34 +000037
Chris Lattner17af2a62008-07-21 04:09:54 +000038 // Check to see if this is 'id' or 'Class', both of which are typedefs for
39 // pointer types. This looks for the typedef specifically, not for the
40 // underlying type.
Chris Lattner22e684a2008-07-21 04:03:34 +000041 if (type == Context.getObjCIdType() || type == Context.getObjCClassType())
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000042 return true;
43
Chris Lattner17af2a62008-07-21 04:09:54 +000044 const PointerType *pointerType = type->getAsPointerType();
45 type = pointerType->getPointeeType();
Chris Lattner22e684a2008-07-21 04:03:34 +000046 return type->isObjCInterfaceType() || type->isObjCQualifiedIdType();
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000047}
48
Steve Naroffb216c882007-10-09 22:01:59 +000049void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
50 TUScope = S;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000051 CurContext = Context.getTranslationUnitDecl();
Chris Lattner2ae34ed2008-02-06 00:46:58 +000052 if (!PP.getLangOptions().ObjC1) return;
53
Steve Naroff69d63752008-02-24 16:25:02 +000054 // Synthesize "typedef struct objc_selector *SEL;"
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000055 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
Chris Lattnerc63e6602008-03-15 21:32:50 +000056 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000057 &Context.Idents.get("objc_selector"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000058 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000059 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000060
61 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000062 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
63 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000064 &Context.Idents.get("SEL"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000065 SelT, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000066 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000067 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000068
Chris Lattner27933c12008-06-21 22:44:51 +000069 // FIXME: Make sure these don't leak!
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000070 RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
71 CurContext,
72 SourceLocation(),
73 &Context.Idents.get("objc_class"),
74 0);
75 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
76 TypedefDecl *ClassTypedef =
77 TypedefDecl::Create(Context, CurContext, SourceLocation(),
78 &Context.Idents.get("Class"), ClassT, 0);
79 PushOnScopeChains(ClassTag, TUScope);
80 PushOnScopeChains(ClassTypedef, TUScope);
81 Context.setObjCClassType(ClassTypedef);
82 // Synthesize "@class Protocol;
83 ObjCInterfaceDecl *ProtocolDecl =
84 ObjCInterfaceDecl::Create(Context, SourceLocation(), 0,
85 &Context.Idents.get("Protocol"),
86 SourceLocation(), true);
87 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
88 PushOnScopeChains(ProtocolDecl, TUScope);
89
90 // Synthesize "typedef struct objc_object { Class isa; } *id;"
91 RecordDecl *ObjectTag =
92 RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
93 SourceLocation(),
94 &Context.Idents.get("objc_object"), 0);
95 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
96 PushOnScopeChains(ObjectTag, TUScope);
97 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
98 SourceLocation(),
99 &Context.Idents.get("id"),
100 ObjT, 0);
101 PushOnScopeChains(IdTypedef, TUScope);
102 Context.setObjCIdType(IdTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +0000103}
104
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000105Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000106 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) {
Chris Lattner59907c42007-08-10 20:18:51 +0000107
108 // Get IdentifierInfo objects for known functions for which we
109 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000110 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +0000111
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000112 KnownFunctionIDs[id_printf] = &IT.get("printf");
113 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
114 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
115 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
116 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000117 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000118 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
119 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
120 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
121 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
122 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000123
Steve Naroff3b950172007-10-10 21:53:07 +0000124 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000125 if (getLangOptions().CPlusPlus)
126 FieldCollector.reset(new CXXFieldCollector());
Reid Spencer5f016e22007-07-11 17:01:13 +0000127}
128
Chris Lattner1e0a3902008-01-16 19:17:22 +0000129/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
130/// If there is already an implicit cast, merge into the existing one.
131void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattner438757c2008-02-13 18:01:07 +0000132 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000133
134 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
135 ImpCast->setType(Type);
136 else
137 Expr = new ImplicitCastExpr(Type, Expr);
138}
139
140
141
Chris Lattner394a3fd2007-08-31 04:53:24 +0000142void Sema::DeleteExpr(ExprTy *E) {
143 delete static_cast<Expr*>(E);
144}
145void Sema::DeleteStmt(StmtTy *S) {
146 delete static_cast<Stmt*>(S);
147}
148
Reid Spencer5f016e22007-07-11 17:01:13 +0000149//===----------------------------------------------------------------------===//
150// Helper functions.
151//===----------------------------------------------------------------------===//
152
153bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000154 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 return true;
156}
157
158bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000159 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 return true;
161}
162
163bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
164 const std::string &Msg2) {
165 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000166 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 return true;
168}
169
170bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000171 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 return true;
173}
174
175bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
176 SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000177 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 return true;
179}
180
181bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
182 const std::string &Msg2, SourceRange Range) {
183 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000184 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 return true;
186}
187
Chris Lattner4667ac32008-01-03 23:38:43 +0000188bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
189 const std::string &Msg2, const std::string &Msg3,
190 SourceRange R1) {
191 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
192 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
193 return true;
194}
195
Reid Spencer5f016e22007-07-11 17:01:13 +0000196bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
197 SourceRange R1, SourceRange R2) {
198 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000199 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 return true;
201}
202
203bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
204 SourceRange R1, SourceRange R2) {
205 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000206 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 return true;
208}
209
210bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
211 const std::string &Msg2, SourceRange R1, SourceRange R2) {
212 std::string MsgArr[] = { Msg1, Msg2 };
213 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000214 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 return true;
216}
217
218const LangOptions &Sema::getLangOptions() const {
219 return PP.getLangOptions();
220}