blob: 5b7a3094d12d00a382f329781ce11fec2f4de0b0 [file] [log] [blame]
Chris Lattnerddd6fc82006-11-10 04:58:55 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerddd6fc82006-11-10 04:58:55 +000010// This file implements the actions class which performs semantic analysis and
11// builds an AST out of a parse stream.
Chris Lattner3e7bd4e2006-08-17 05:51:27 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerddd6fc82006-11-10 04:58:55 +000015#include "Sema.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000016#include "clang/AST/ASTContext.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000017#include "clang/Lex/Preprocessor.h"
Chris Lattnerd6647d32007-05-16 17:56:50 +000018#include "clang/Basic/Diagnostic.h"
Steve Naroff6d40db02007-10-31 18:42:27 +000019#include "clang/Parse/Scope.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000020
Chris Lattnerc11438c2006-08-18 05:17:52 +000021using namespace clang;
22
Ted Kremenek1b0ea822008-01-07 19:49:32 +000023bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroff6d40db02007-10-31 18:42:27 +000024 const char *typeName = TD->getIdentifier()->getName();
25 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +000026 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroff6d40db02007-10-31 18:42:27 +000027}
28
Ted Kremenek1b0ea822008-01-07 19:49:32 +000029bool Sema::isObjCObjectPointerType(QualType type) const {
30 if (!type->isPointerType() && !type->isObjCQualifiedIdType())
Fariborz Jahanian775d5d02008-01-04 00:27:46 +000031 return false;
Ted Kremenek1b0ea822008-01-07 19:49:32 +000032 if (type == Context.getObjCIdType() || type == Context.getObjCClassType() ||
33 type->isObjCQualifiedIdType())
Fariborz Jahanian775d5d02008-01-04 00:27:46 +000034 return true;
35
Fariborz Jahaniand5450b72008-01-07 18:14:04 +000036 if (type->isPointerType()) {
Fariborz Jahanian775d5d02008-01-04 00:27:46 +000037 PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr());
38 type = pointerType->getPointeeType();
39 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +000040 return (type->isObjCInterfaceType() || type->isObjCQualifiedIdType());
Fariborz Jahanian775d5d02008-01-04 00:27:46 +000041}
42
Steve Naroffc62adb62007-10-09 22:01:59 +000043void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
44 TUScope = S;
Chris Lattnerfe0e0af2008-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 Narofff0ed31a2008-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 Naroff7f549f12007-10-10 21:53:07 +000074}
75
Chris Lattnerfe0e0af2008-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 Lattnerb87b1b32007-08-10 20:18:51 +000079
80 // Get IdentifierInfo objects for known functions for which we
81 // do extra checking.
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000082 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattnerb87b1b32007-08-10 20:18:51 +000083
Chris Lattnerfe0e0af2008-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 Naroff6d40db02007-10-31 18:42:27 +000094
Steve Narofff0ed31a2008-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 Naroff6d40db02007-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 Kremenek1b0ea822008-01-07 19:49:32 +0000105 Context.setObjCClassType(ClassTypedef);
Steve Naroff6d40db02007-10-31 18:42:27 +0000106
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +0000107 // Synthesize "@class Protocol;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000108 ObjCInterfaceDecl *ProtocolDecl = new ObjCInterfaceDecl(SourceLocation(), 0,
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +0000109 &Context.Idents.get("Protocol"), true);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000110 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +0000111
Steve Naroff6d40db02007-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 Kremenek1b0ea822008-01-07 19:49:32 +0000116 Context.getObjCClassType());
Steve Naroff6d40db02007-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 Kremenek1b0ea822008-01-07 19:49:32 +0000122 Context.setObjCIdType(IdTypedef);
Steve Naroff6d40db02007-10-31 18:42:27 +0000123 }
Steve Naroff7f549f12007-10-10 21:53:07 +0000124 TUScope = 0;
Steve Naroff38d31b42007-02-28 01:22:02 +0000125}
Chris Lattnercb6a3822006-11-10 06:20:45 +0000126
Chris Lattnera65e1f32008-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 Lattner7b7ace52008-02-13 18:01:07 +0000130 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
Chris Lattnera65e1f32008-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 Lattner57c523f2007-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
Chris Lattnerc11438c2006-08-18 05:17:52 +0000147//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +0000148// Helper functions.
149//===----------------------------------------------------------------------===//
150
Chris Lattnerd6647d32007-05-16 17:56:50 +0000151bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000152 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Chris Lattnerd6647d32007-05-16 17:56:50 +0000153 return true;
154}
155
Steve Narofff1e53692007-03-23 22:27:02 +0000156bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000157 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattnerd6647d32007-05-16 17:56:50 +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 Kremenek1daa3cf2007-12-12 22:39:36 +0000164 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000165 return true;
Chris Lattnereaafe1222006-11-10 05:17:58 +0000166}
167
Steve Naroff71ce2e02007-05-18 22:53:50 +0000168bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000169 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Steve Naroffbc2f0992007-03-30 20:09:34 +0000170 return true;
171}
172
Steve Naroff71ce2e02007-05-18 22:53:50 +0000173bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
174 SourceRange Range) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000175 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Steve Naroff71ce2e02007-05-18 22:53:50 +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 Kremenek1daa3cf2007-12-12 22:39:36 +0000182 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000183 return true;
184}
185
Chris Lattner816dea22008-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
Steve Naroff71ce2e02007-05-18 22:53:50 +0000194bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
195 SourceRange R1, SourceRange R2) {
196 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000197 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Steve Naroff71ce2e02007-05-18 22:53:50 +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 Kremenek1daa3cf2007-12-12 22:39:36 +0000204 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Steve Naroff71ce2e02007-05-18 22:53:50 +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 Kremenek1daa3cf2007-12-12 22:39:36 +0000212 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000213 return true;
Steve Naroff8160ea22007-03-06 01:09:46 +0000214}
215
Chris Lattnerac18be92006-11-20 06:49:47 +0000216const LangOptions &Sema::getLangOptions() const {
Steve Naroff38d31b42007-02-28 01:22:02 +0000217 return PP.getLangOptions();
Chris Lattnerac18be92006-11-20 06:49:47 +0000218}