blob: 5b7a3094d12d00a382f329781ce11fec2f4de0b0 [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;
Chris Lattner2ae34ed2008-02-06 00:46:58 +000045 if (!PP.getLangOptions().ObjC1) return;
46
47 TypedefType *t;
48
49 // Add the built-in ObjC types.
50 t = cast<TypedefType>(Context.getObjCIdType().getTypePtr());
51 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
52 TUScope->AddDecl(t->getDecl());
53 t = cast<TypedefType>(Context.getObjCClassType().getTypePtr());
54 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
55 TUScope->AddDecl(t->getDecl());
56 ObjCInterfaceType *it = cast<ObjCInterfaceType>(Context.getObjCProtoType());
57 ObjCInterfaceDecl *IDecl = it->getDecl();
58 IDecl->getIdentifier()->setFETokenInfo(IDecl);
59 TUScope->AddDecl(IDecl);
Steve Naroff69d63752008-02-24 16:25:02 +000060
61 // Synthesize "typedef struct objc_selector *SEL;"
62 RecordDecl *SelTag = new RecordDecl(Decl::Struct, SourceLocation(),
63 &Context.Idents.get("objc_selector"), 0);
64 SelTag->getIdentifier()->setFETokenInfo(SelTag);
65 TUScope->AddDecl(SelTag);
66
67 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
68 TypedefDecl *SelTypedef = new TypedefDecl(SourceLocation(),
69 &Context.Idents.get("SEL"),
70 SelT, 0);
71 SelTypedef->getIdentifier()->setFETokenInfo(SelTypedef);
72 TUScope->AddDecl(SelTypedef);
73 Context.setObjCSelType(SelTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +000074}
75
Chris Lattner2ae34ed2008-02-06 00:46:58 +000076Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
77 : PP(pp), Context(ctxt), Consumer(consumer),
78 CurFunctionDecl(0), CurMethodDecl(0) {
Chris Lattner59907c42007-08-10 20:18:51 +000079
80 // Get IdentifierInfo objects for known functions for which we
81 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +000082 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +000083
Chris Lattner2ae34ed2008-02-06 00:46:58 +000084 KnownFunctionIDs[id_printf] = &IT.get("printf");
85 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
86 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
87 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
88 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
89 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
90 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
91 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
92 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
93 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +000094
Steve Naroff69d63752008-02-24 16:25:02 +000095 // FIXME: Move this initialization up to Sema::ActOnTranslationUnitScope()
96 // and make sure the decls get inserted into TUScope!
Steve Naroff8ee529b2007-10-31 18:42:27 +000097 if (PP.getLangOptions().ObjC1) {
98 // Synthesize "typedef struct objc_class *Class;"
99 RecordDecl *ClassTag = new RecordDecl(Decl::Struct, SourceLocation(),
100 &IT.get("objc_class"), 0);
101 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
102 TypedefDecl *ClassTypedef = new TypedefDecl(SourceLocation(),
103 &Context.Idents.get("Class"),
104 ClassT, 0);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000105 Context.setObjCClassType(ClassTypedef);
Steve Naroff8ee529b2007-10-31 18:42:27 +0000106
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +0000107 // Synthesize "@class Protocol;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000108 ObjCInterfaceDecl *ProtocolDecl = new ObjCInterfaceDecl(SourceLocation(), 0,
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +0000109 &Context.Idents.get("Protocol"), true);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000110 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +0000111
Steve Naroff8ee529b2007-10-31 18:42:27 +0000112 // Synthesize "typedef struct objc_object { Class isa; } *id;"
113 RecordDecl *ObjectTag = new RecordDecl(Decl::Struct, SourceLocation(),
114 &IT.get("objc_object"), 0);
115 FieldDecl *IsaDecl = new FieldDecl(SourceLocation(), 0,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000116 Context.getObjCClassType());
Steve Naroff8ee529b2007-10-31 18:42:27 +0000117 ObjectTag->defineBody(&IsaDecl, 1);
118 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
119 TypedefDecl *IdTypedef = new TypedefDecl(SourceLocation(),
120 &Context.Idents.get("id"),
121 ObjT, 0);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000122 Context.setObjCIdType(IdTypedef);
Steve Naroff8ee529b2007-10-31 18:42:27 +0000123 }
Steve Naroff3b950172007-10-10 21:53:07 +0000124 TUScope = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125}
126
Chris Lattner1e0a3902008-01-16 19:17:22 +0000127/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
128/// If there is already an implicit cast, merge into the existing one.
129void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattner438757c2008-02-13 18:01:07 +0000130 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000131
132 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
133 ImpCast->setType(Type);
134 else
135 Expr = new ImplicitCastExpr(Type, Expr);
136}
137
138
139
Chris Lattner394a3fd2007-08-31 04:53:24 +0000140void Sema::DeleteExpr(ExprTy *E) {
141 delete static_cast<Expr*>(E);
142}
143void Sema::DeleteStmt(StmtTy *S) {
144 delete static_cast<Stmt*>(S);
145}
146
Reid Spencer5f016e22007-07-11 17:01:13 +0000147//===----------------------------------------------------------------------===//
148// Helper functions.
149//===----------------------------------------------------------------------===//
150
151bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000152 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 return true;
154}
155
156bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000157 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 return true;
159}
160
161bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
162 const std::string &Msg2) {
163 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000164 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 return true;
166}
167
168bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000169 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 return true;
171}
172
173bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
174 SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000175 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 return true;
177}
178
179bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
180 const std::string &Msg2, SourceRange Range) {
181 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000182 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 return true;
184}
185
Chris Lattner4667ac32008-01-03 23:38:43 +0000186bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
187 const std::string &Msg2, const std::string &Msg3,
188 SourceRange R1) {
189 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
190 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
191 return true;
192}
193
Reid Spencer5f016e22007-07-11 17:01:13 +0000194bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
195 SourceRange R1, SourceRange R2) {
196 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000197 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 return true;
199}
200
201bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
202 SourceRange R1, SourceRange R2) {
203 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000204 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 return true;
206}
207
208bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
209 const std::string &Msg2, SourceRange R1, SourceRange R2) {
210 std::string MsgArr[] = { Msg1, Msg2 };
211 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000212 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 return true;
214}
215
216const LangOptions &Sema::getLangOptions() const {
217 return PP.getLangOptions();
218}