blob: 52858cb893e97fde4dff8253f4baf50c0ce240fa [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
38 if (type == Context.getObjCIdType() || type == Context.getObjCClassType())
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000039 return true;
40
Fariborz Jahanian540fc532008-01-07 18:14:04 +000041 if (type->isPointerType()) {
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000042 PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr());
43 type = pointerType->getPointeeType();
44 }
Chris Lattner22e684a2008-07-21 04:03:34 +000045 return type->isObjCInterfaceType() || type->isObjCQualifiedIdType();
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000046}
47
Steve Naroffb216c882007-10-09 22:01:59 +000048void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
49 TUScope = S;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000050 CurContext = Context.getTranslationUnitDecl();
Chris Lattner2ae34ed2008-02-06 00:46:58 +000051 if (!PP.getLangOptions().ObjC1) return;
52
Steve Naroff69d63752008-02-24 16:25:02 +000053 // Synthesize "typedef struct objc_selector *SEL;"
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000054 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
Chris Lattnerc63e6602008-03-15 21:32:50 +000055 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000056 &Context.Idents.get("objc_selector"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000057 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000058 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000059
60 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000061 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
62 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000063 &Context.Idents.get("SEL"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000064 SelT, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000065 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000066 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000067
Chris Lattner27933c12008-06-21 22:44:51 +000068 // FIXME: Make sure these don't leak!
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000069 RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
70 CurContext,
71 SourceLocation(),
72 &Context.Idents.get("objc_class"),
73 0);
74 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
75 TypedefDecl *ClassTypedef =
76 TypedefDecl::Create(Context, CurContext, SourceLocation(),
77 &Context.Idents.get("Class"), ClassT, 0);
78 PushOnScopeChains(ClassTag, TUScope);
79 PushOnScopeChains(ClassTypedef, TUScope);
80 Context.setObjCClassType(ClassTypedef);
81 // Synthesize "@class Protocol;
82 ObjCInterfaceDecl *ProtocolDecl =
83 ObjCInterfaceDecl::Create(Context, SourceLocation(), 0,
84 &Context.Idents.get("Protocol"),
85 SourceLocation(), true);
86 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
87 PushOnScopeChains(ProtocolDecl, TUScope);
88
89 // Synthesize "typedef struct objc_object { Class isa; } *id;"
90 RecordDecl *ObjectTag =
91 RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
92 SourceLocation(),
93 &Context.Idents.get("objc_object"), 0);
94 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
95 PushOnScopeChains(ObjectTag, TUScope);
96 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
97 SourceLocation(),
98 &Context.Idents.get("id"),
99 ObjT, 0);
100 PushOnScopeChains(IdTypedef, TUScope);
101 Context.setObjCIdType(IdTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +0000102}
103
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000104Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000105 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) {
Chris Lattner59907c42007-08-10 20:18:51 +0000106
107 // Get IdentifierInfo objects for known functions for which we
108 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000109 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +0000110
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000111 KnownFunctionIDs[id_printf] = &IT.get("printf");
112 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
113 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
114 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
115 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000116 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000117 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
118 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
119 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
120 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
121 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000122
Steve Naroff3b950172007-10-10 21:53:07 +0000123 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000124 if (getLangOptions().CPlusPlus)
125 FieldCollector.reset(new CXXFieldCollector());
Reid Spencer5f016e22007-07-11 17:01:13 +0000126}
127
Chris Lattner1e0a3902008-01-16 19:17:22 +0000128/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
129/// If there is already an implicit cast, merge into the existing one.
130void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattner438757c2008-02-13 18:01:07 +0000131 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000132
133 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
134 ImpCast->setType(Type);
135 else
136 Expr = new ImplicitCastExpr(Type, Expr);
137}
138
139
140
Chris Lattner394a3fd2007-08-31 04:53:24 +0000141void Sema::DeleteExpr(ExprTy *E) {
142 delete static_cast<Expr*>(E);
143}
144void Sema::DeleteStmt(StmtTy *S) {
145 delete static_cast<Stmt*>(S);
146}
147
Reid Spencer5f016e22007-07-11 17:01:13 +0000148//===----------------------------------------------------------------------===//
149// Helper functions.
150//===----------------------------------------------------------------------===//
151
152bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000153 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 return true;
155}
156
157bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000158 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 return true;
160}
161
162bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
163 const std::string &Msg2) {
164 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000165 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 return true;
167}
168
169bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000170 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 return true;
172}
173
174bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
175 SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000176 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 return true;
178}
179
180bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
181 const std::string &Msg2, SourceRange Range) {
182 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000183 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 return true;
185}
186
Chris Lattner4667ac32008-01-03 23:38:43 +0000187bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
188 const std::string &Msg2, const std::string &Msg3,
189 SourceRange R1) {
190 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
191 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
192 return true;
193}
194
Reid Spencer5f016e22007-07-11 17:01:13 +0000195bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
196 SourceRange R1, SourceRange R2) {
197 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000198 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 return true;
200}
201
202bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
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, &Msg, 1, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 return true;
207}
208
209bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
210 const std::string &Msg2, SourceRange R1, SourceRange R2) {
211 std::string MsgArr[] = { Msg1, Msg2 };
212 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000213 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 return true;
215}
216
217const LangOptions &Sema::getLangOptions() const {
218 return PP.getLangOptions();
219}