blob: 32c769b64d98f61f9af2ce11abc893dd793b4f25 [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;"
Chris Lattnerb752f282008-07-21 07:06:49 +000056 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct,
57 CurContext,
Chris Lattnerc63e6602008-03-15 21:32:50 +000058 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000059 &Context.Idents.get("objc_selector"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000060 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000061 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000062
63 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000064 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
65 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000066 &Context.Idents.get("SEL"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000067 SelT, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000068 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000069 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000070
Chris Lattner27933c12008-06-21 22:44:51 +000071 // FIXME: Make sure these don't leak!
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000072 RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
73 CurContext,
74 SourceLocation(),
75 &Context.Idents.get("objc_class"),
76 0);
77 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
78 TypedefDecl *ClassTypedef =
79 TypedefDecl::Create(Context, CurContext, SourceLocation(),
80 &Context.Idents.get("Class"), ClassT, 0);
81 PushOnScopeChains(ClassTag, TUScope);
82 PushOnScopeChains(ClassTypedef, TUScope);
83 Context.setObjCClassType(ClassTypedef);
84 // Synthesize "@class Protocol;
85 ObjCInterfaceDecl *ProtocolDecl =
Chris Lattnerb752f282008-07-21 07:06:49 +000086 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000087 &Context.Idents.get("Protocol"),
88 SourceLocation(), true);
89 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
90 PushOnScopeChains(ProtocolDecl, TUScope);
91
92 // Synthesize "typedef struct objc_object { Class isa; } *id;"
93 RecordDecl *ObjectTag =
94 RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
95 SourceLocation(),
96 &Context.Idents.get("objc_object"), 0);
97 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
98 PushOnScopeChains(ObjectTag, TUScope);
99 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
100 SourceLocation(),
101 &Context.Idents.get("id"),
102 ObjT, 0);
103 PushOnScopeChains(IdTypedef, TUScope);
104 Context.setObjCIdType(IdTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +0000105}
106
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000107Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000108 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) {
Chris Lattner59907c42007-08-10 20:18:51 +0000109
110 // Get IdentifierInfo objects for known functions for which we
111 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000112 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +0000113
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000114 KnownFunctionIDs[id_printf] = &IT.get("printf");
115 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
116 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
117 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
118 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000119 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000120 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
121 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
122 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
123 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
124 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000125
Steve Naroff3b950172007-10-10 21:53:07 +0000126 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000127 if (getLangOptions().CPlusPlus)
128 FieldCollector.reset(new CXXFieldCollector());
Reid Spencer5f016e22007-07-11 17:01:13 +0000129}
130
Chris Lattner1e0a3902008-01-16 19:17:22 +0000131/// 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.
133void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattner438757c2008-02-13 18:01:07 +0000134 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000135
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 Lattner394a3fd2007-08-31 04:53:24 +0000144void Sema::DeleteExpr(ExprTy *E) {
145 delete static_cast<Expr*>(E);
146}
147void Sema::DeleteStmt(StmtTy *S) {
148 delete static_cast<Stmt*>(S);
149}
150
Reid Spencer5f016e22007-07-11 17:01:13 +0000151//===----------------------------------------------------------------------===//
152// Helper functions.
153//===----------------------------------------------------------------------===//
154
155bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000156 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 return true;
158}
159
160bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000161 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 return true;
163}
164
165bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
166 const std::string &Msg2) {
167 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000168 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 return true;
170}
171
172bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000173 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 return true;
175}
176
177bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
178 SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000179 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 return true;
181}
182
183bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
184 const std::string &Msg2, SourceRange Range) {
185 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000186 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 return true;
188}
189
Chris Lattner4667ac32008-01-03 23:38:43 +0000190bool 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 Spencer5f016e22007-07-11 17:01:13 +0000198bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
199 SourceRange R1, SourceRange R2) {
200 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000201 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 return true;
203}
204
205bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
206 SourceRange R1, SourceRange R2) {
207 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000208 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 return true;
210}
211
212bool 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 Kremenek9c728dc2007-12-12 22:39:36 +0000216 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 return true;
218}
219
220const LangOptions &Sema::getLangOptions() const {
221 return PP.getLangOptions();
222}