blob: bb5316a126ac15f7885d407ede4dc0191f45701f [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
Ted Kremeneka526c5c2008-01-07 19:49:32 +000029bool Sema::isObjCObjectPointerType(QualType type) const {
30 if (!type->isPointerType() && !type->isObjCQualifiedIdType())
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000031 return false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000032 if (type == Context.getObjCIdType() || type == Context.getObjCClassType() ||
33 type->isObjCQualifiedIdType())
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000034 return true;
35
Fariborz Jahanian540fc532008-01-07 18:14:04 +000036 if (type->isPointerType()) {
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000037 PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr());
38 type = pointerType->getPointeeType();
39 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000040 return (type->isObjCInterfaceType() || type->isObjCQualifiedIdType());
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000041}
42
Steve Naroffb216c882007-10-09 22:01:59 +000043void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
44 TUScope = S;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000045 CurContext = Context.getTranslationUnitDecl();
Chris Lattner2ae34ed2008-02-06 00:46:58 +000046 if (!PP.getLangOptions().ObjC1) return;
47
48 TypedefType *t;
49
50 // Add the built-in ObjC types.
51 t = cast<TypedefType>(Context.getObjCIdType().getTypePtr());
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000052 PushOnScopeChains(t->getDecl(), TUScope);
Chris Lattner2ae34ed2008-02-06 00:46:58 +000053 t = cast<TypedefType>(Context.getObjCClassType().getTypePtr());
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000054 PushOnScopeChains(t->getDecl(), TUScope);
Chris Lattner2ae34ed2008-02-06 00:46:58 +000055 ObjCInterfaceType *it = cast<ObjCInterfaceType>(Context.getObjCProtoType());
56 ObjCInterfaceDecl *IDecl = it->getDecl();
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000057 PushOnScopeChains(IDecl, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000058
59 // Synthesize "typedef struct objc_selector *SEL;"
Chris Lattner0ed844b2008-04-04 06:12:32 +000060 RecordDecl *SelTag = RecordDecl::Create(Context, Decl::Struct, CurContext,
Chris Lattnerc63e6602008-03-15 21:32:50 +000061 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000062 &Context.Idents.get("objc_selector"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000063 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000064 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000065
66 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000067 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
68 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000069 &Context.Idents.get("SEL"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000070 SelT, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000071 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000072 Context.setObjCSelType(SelTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +000073}
74
Chris Lattner2ae34ed2008-02-06 00:46:58 +000075Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
76 : PP(pp), Context(ctxt), Consumer(consumer),
Chris Lattner0ed844b2008-04-04 06:12:32 +000077 CurFunctionDecl(0), CurMethodDecl(0), CurContext(0) {
Chris Lattner59907c42007-08-10 20:18:51 +000078
79 // Get IdentifierInfo objects for known functions for which we
80 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +000081 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +000082
Chris Lattner2ae34ed2008-02-06 00:46:58 +000083 KnownFunctionIDs[id_printf] = &IT.get("printf");
84 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
85 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
86 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
87 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
88 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
89 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
90 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
91 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
92 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +000093
Steve Naroff69d63752008-02-24 16:25:02 +000094 // FIXME: Move this initialization up to Sema::ActOnTranslationUnitScope()
95 // and make sure the decls get inserted into TUScope!
Steve Naroff8ee529b2007-10-31 18:42:27 +000096 if (PP.getLangOptions().ObjC1) {
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000097 TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl();
98
Steve Naroff8ee529b2007-10-31 18:42:27 +000099 // Synthesize "typedef struct objc_class *Class;"
Chris Lattnerc63e6602008-03-15 21:32:50 +0000100 RecordDecl *ClassTag = RecordDecl::Create(Context, Decl::Struct,
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000101 TUDecl,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000102 SourceLocation(),
Chris Lattnerc63e6602008-03-15 21:32:50 +0000103 &IT.get("objc_class"), 0);
Steve Naroff8ee529b2007-10-31 18:42:27 +0000104 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000105 TypedefDecl *ClassTypedef =
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000106 TypedefDecl::Create(Context, TUDecl, SourceLocation(),
Chris Lattnerc63e6602008-03-15 21:32:50 +0000107 &Context.Idents.get("Class"), ClassT, 0);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000108 Context.setObjCClassType(ClassTypedef);
Steve Naroff8ee529b2007-10-31 18:42:27 +0000109
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +0000110 // Synthesize "@class Protocol;
Chris Lattner0e77ba02008-03-16 01:15:50 +0000111 ObjCInterfaceDecl *ProtocolDecl =
112 ObjCInterfaceDecl::Create(Context, SourceLocation(), 0,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000113 &Context.Idents.get("Protocol"),
114 SourceLocation(), true);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000115 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +0000116
Steve Naroff8ee529b2007-10-31 18:42:27 +0000117 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000118 RecordDecl *ObjectTag =
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000119 RecordDecl::Create(Context, Decl::Struct, TUDecl,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000120 SourceLocation(),
Chris Lattnerc63e6602008-03-15 21:32:50 +0000121 &IT.get("objc_object"), 0);
Chris Lattnerb048c982008-04-06 04:47:34 +0000122 FieldDecl *IsaDecl = FieldDecl::Create(Context, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +0000123 Context.getObjCClassType());
Steve Naroff8ee529b2007-10-31 18:42:27 +0000124 ObjectTag->defineBody(&IsaDecl, 1);
125 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000126 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, TUDecl,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000127 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000128 &Context.Idents.get("id"),
Chris Lattnerc63e6602008-03-15 21:32:50 +0000129 ObjT, 0);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000130 Context.setObjCIdType(IdTypedef);
Steve Naroff8ee529b2007-10-31 18:42:27 +0000131 }
Steve Naroff3b950172007-10-10 21:53:07 +0000132 TUScope = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000133}
134
Chris Lattner1e0a3902008-01-16 19:17:22 +0000135/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
136/// If there is already an implicit cast, merge into the existing one.
137void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattner438757c2008-02-13 18:01:07 +0000138 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000139
140 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
141 ImpCast->setType(Type);
142 else
143 Expr = new ImplicitCastExpr(Type, Expr);
144}
145
146
147
Chris Lattner394a3fd2007-08-31 04:53:24 +0000148void Sema::DeleteExpr(ExprTy *E) {
149 delete static_cast<Expr*>(E);
150}
151void Sema::DeleteStmt(StmtTy *S) {
152 delete static_cast<Stmt*>(S);
153}
154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155//===----------------------------------------------------------------------===//
156// Helper functions.
157//===----------------------------------------------------------------------===//
158
159bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000160 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 return true;
162}
163
164bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000165 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 return true;
167}
168
169bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
170 const std::string &Msg2) {
171 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000172 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 return true;
174}
175
176bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000177 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 return true;
179}
180
181bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
182 SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000183 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 return true;
185}
186
187bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
188 const std::string &Msg2, SourceRange Range) {
189 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000190 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 return true;
192}
193
Chris Lattner4667ac32008-01-03 23:38:43 +0000194bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
195 const std::string &Msg2, const std::string &Msg3,
196 SourceRange R1) {
197 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
198 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
199 return true;
200}
201
Reid Spencer5f016e22007-07-11 17:01:13 +0000202bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
203 SourceRange R1, SourceRange R2) {
204 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000205 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 return true;
207}
208
209bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
210 SourceRange R1, SourceRange R2) {
211 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000212 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 return true;
214}
215
216bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
217 const std::string &Msg2, SourceRange R1, SourceRange R2) {
218 std::string MsgArr[] = { Msg1, Msg2 };
219 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000220 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 return true;
222}
223
224const LangOptions &Sema::getLangOptions() const {
225 return PP.getLangOptions();
226}