blob: 93210ab70f491b39f7157503bdb248f65af57b4a [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
Steve Naroff69d63752008-02-24 16:25:02 +000048 // Synthesize "typedef struct objc_selector *SEL;"
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000049 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
Chris Lattnerc63e6602008-03-15 21:32:50 +000050 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000051 &Context.Idents.get("objc_selector"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000052 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000053 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000054
55 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000056 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
57 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000058 &Context.Idents.get("SEL"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000059 SelT, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000060 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000061 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000062
Chris Lattner27933c12008-06-21 22:44:51 +000063 // FIXME: Make sure these don't leak!
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000064 RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
65 CurContext,
66 SourceLocation(),
67 &Context.Idents.get("objc_class"),
68 0);
69 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
70 TypedefDecl *ClassTypedef =
71 TypedefDecl::Create(Context, CurContext, SourceLocation(),
72 &Context.Idents.get("Class"), ClassT, 0);
73 PushOnScopeChains(ClassTag, TUScope);
74 PushOnScopeChains(ClassTypedef, TUScope);
75 Context.setObjCClassType(ClassTypedef);
76 // Synthesize "@class Protocol;
77 ObjCInterfaceDecl *ProtocolDecl =
78 ObjCInterfaceDecl::Create(Context, SourceLocation(), 0,
79 &Context.Idents.get("Protocol"),
80 SourceLocation(), true);
81 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
82 PushOnScopeChains(ProtocolDecl, TUScope);
83
84 // Synthesize "typedef struct objc_object { Class isa; } *id;"
85 RecordDecl *ObjectTag =
86 RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
87 SourceLocation(),
88 &Context.Idents.get("objc_object"), 0);
89 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
90 PushOnScopeChains(ObjectTag, TUScope);
91 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
92 SourceLocation(),
93 &Context.Idents.get("id"),
94 ObjT, 0);
95 PushOnScopeChains(IdTypedef, TUScope);
96 Context.setObjCIdType(IdTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +000097}
98
Chris Lattner2ae34ed2008-02-06 00:46:58 +000099Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
100 : PP(pp), Context(ctxt), Consumer(consumer),
Chris Lattner0ed844b2008-04-04 06:12:32 +0000101 CurFunctionDecl(0), CurMethodDecl(0), CurContext(0) {
Chris Lattner59907c42007-08-10 20:18:51 +0000102
103 // Get IdentifierInfo objects for known functions for which we
104 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000105 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +0000106
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000107 KnownFunctionIDs[id_printf] = &IT.get("printf");
108 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
109 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
110 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
111 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000112 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000113 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
114 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
115 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
116 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
117 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000118
Steve Naroff3b950172007-10-10 21:53:07 +0000119 TUScope = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000120}
121
Chris Lattner1e0a3902008-01-16 19:17:22 +0000122/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
123/// If there is already an implicit cast, merge into the existing one.
124void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattner438757c2008-02-13 18:01:07 +0000125 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000126
127 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
128 ImpCast->setType(Type);
129 else
130 Expr = new ImplicitCastExpr(Type, Expr);
131}
132
133
134
Chris Lattner394a3fd2007-08-31 04:53:24 +0000135void Sema::DeleteExpr(ExprTy *E) {
136 delete static_cast<Expr*>(E);
137}
138void Sema::DeleteStmt(StmtTy *S) {
139 delete static_cast<Stmt*>(S);
140}
141
Reid Spencer5f016e22007-07-11 17:01:13 +0000142//===----------------------------------------------------------------------===//
143// Helper functions.
144//===----------------------------------------------------------------------===//
145
146bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000147 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 return true;
149}
150
151bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000152 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 return true;
154}
155
156bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
157 const std::string &Msg2) {
158 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000159 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 return true;
161}
162
163bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000164 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 return true;
166}
167
168bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
169 SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000170 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 return true;
172}
173
174bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
175 const std::string &Msg2, SourceRange Range) {
176 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000177 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 return true;
179}
180
Chris Lattner4667ac32008-01-03 23:38:43 +0000181bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
182 const std::string &Msg2, const std::string &Msg3,
183 SourceRange R1) {
184 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
185 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
186 return true;
187}
188
Reid Spencer5f016e22007-07-11 17:01:13 +0000189bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
190 SourceRange R1, SourceRange R2) {
191 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000192 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 return true;
194}
195
196bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
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, &Msg, 1, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 return true;
201}
202
203bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
204 const std::string &Msg2, SourceRange R1, SourceRange R2) {
205 std::string MsgArr[] = { Msg1, Msg2 };
206 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000207 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 return true;
209}
210
211const LangOptions &Sema::getLangOptions() const {
212 return PP.getLangOptions();
213}