blob: 8f340db14e0d78be00513215bb4879f30a7ee000 [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
Steve Naroff8ee529b2007-10-31 18:42:27 +000023bool Sema::isBuiltinObjcType(TypedefDecl *TD) {
24 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
Fariborz Jahanian1f990d62008-01-04 00:27:46 +000029bool Sema::isObjcObjectPointerType(QualType type) const {
30 if (!type->isPointerType() && !type->isObjcQualifiedIdType())
31 return false;
32 if (type == Context.getObjcIdType() || type == Context.getObjcClassType() ||
33 type->isObjcQualifiedIdType())
34 return true;
35
36 while (type->isPointerType()) {
37 PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr());
38 type = pointerType->getPointeeType();
39 }
40 return (type->isObjcInterfaceType() || type->isObjcQualifiedIdType());
41}
42
Steve Naroffb216c882007-10-09 22:01:59 +000043void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
44 TUScope = S;
Steve Naroff8ee529b2007-10-31 18:42:27 +000045 if (PP.getLangOptions().ObjC1) {
46 TypedefType *t;
Chris Lattnerfb1051b2007-10-30 20:57:56 +000047
Steve Naroff8ee529b2007-10-31 18:42:27 +000048 // Add the built-in ObjC types.
49 t = dyn_cast<TypedefType>(Context.getObjcIdType().getTypePtr());
50 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
51 TUScope->AddDecl(t->getDecl());
52 t = dyn_cast<TypedefType>(Context.getObjcClassType().getTypePtr());
53 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
54 TUScope->AddDecl(t->getDecl());
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +000055 ObjcInterfaceType *it = dyn_cast<ObjcInterfaceType>(Context.getObjcProtoType());
56 ObjcInterfaceDecl *IDecl = it->getDecl();
57 IDecl->getIdentifier()->setFETokenInfo(IDecl);
58 TUScope->AddDecl(IDecl);
Steve Naroff8ee529b2007-10-31 18:42:27 +000059 t = dyn_cast<TypedefType>(Context.getObjcSelType().getTypePtr());
60 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
61 TUScope->AddDecl(t->getDecl());
Steve Naroff3b950172007-10-10 21:53:07 +000062 }
Steve Naroff3b950172007-10-10 21:53:07 +000063}
64
Steve Naroff89307ff2007-11-29 23:05:20 +000065Sema::Sema(Preprocessor &pp, ASTContext &ctxt)
Anders Carlsson6c19a042007-11-30 20:01:38 +000066 : PP(pp), Context(ctxt), CurFunctionDecl(0), CurMethodDecl(0) {
Chris Lattner59907c42007-08-10 20:18:51 +000067
68 // Get IdentifierInfo objects for known functions for which we
69 // do extra checking.
70 IdentifierTable& IT = PP.getIdentifierTable();
71
72 KnownFunctionIDs[ id_printf ] = &IT.get("printf");
73 KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
74 KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
75 KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
Chris Lattner59907c42007-08-10 20:18:51 +000076 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremeneke0eb80a2007-08-10 21:13:51 +000077 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattner59907c42007-08-10 20:18:51 +000078 KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
79 KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
80 KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
81 KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +000082
83 if (PP.getLangOptions().ObjC1) {
84 // Synthesize "typedef struct objc_class *Class;"
85 RecordDecl *ClassTag = new RecordDecl(Decl::Struct, SourceLocation(),
86 &IT.get("objc_class"), 0);
87 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
88 TypedefDecl *ClassTypedef = new TypedefDecl(SourceLocation(),
89 &Context.Idents.get("Class"),
90 ClassT, 0);
91 Context.setObjcClassType(ClassTypedef);
92
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +000093 // Synthesize "@class Protocol;
94 ObjcInterfaceDecl *ProtocolDecl = new ObjcInterfaceDecl(SourceLocation(), 0,
95 &Context.Idents.get("Protocol"), true);
96 Context.setObjcProtoType(Context.getObjcInterfaceType(ProtocolDecl));
97
Steve Naroff8ee529b2007-10-31 18:42:27 +000098 // Synthesize "typedef struct objc_object { Class isa; } *id;"
99 RecordDecl *ObjectTag = new RecordDecl(Decl::Struct, SourceLocation(),
100 &IT.get("objc_object"), 0);
101 FieldDecl *IsaDecl = new FieldDecl(SourceLocation(), 0,
102 Context.getObjcClassType());
103 ObjectTag->defineBody(&IsaDecl, 1);
104 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
105 TypedefDecl *IdTypedef = new TypedefDecl(SourceLocation(),
106 &Context.Idents.get("id"),
107 ObjT, 0);
108 Context.setObjcIdType(IdTypedef);
109
110 // Synthesize "typedef struct objc_selector *SEL;"
111 RecordDecl *SelTag = new RecordDecl(Decl::Struct, SourceLocation(),
112 &IT.get("objc_selector"), 0);
113 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
114 TypedefDecl *SelTypedef = new TypedefDecl(SourceLocation(),
115 &Context.Idents.get("SEL"),
116 SelT, 0);
117 Context.setObjcSelType(SelTypedef);
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +0000118
Steve Naroff8ee529b2007-10-31 18:42:27 +0000119 }
Steve Naroff3b950172007-10-10 21:53:07 +0000120 TUScope = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000121}
122
Chris Lattner394a3fd2007-08-31 04:53:24 +0000123void Sema::DeleteExpr(ExprTy *E) {
124 delete static_cast<Expr*>(E);
125}
126void Sema::DeleteStmt(StmtTy *S) {
127 delete static_cast<Stmt*>(S);
128}
129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130//===----------------------------------------------------------------------===//
131// Helper functions.
132//===----------------------------------------------------------------------===//
133
134bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000135 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 return true;
137}
138
139bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000140 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 return true;
142}
143
144bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
145 const std::string &Msg2) {
146 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000147 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 return true;
149}
150
151bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000152 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 return true;
154}
155
156bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
157 SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000158 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,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, SourceRange Range) {
164 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000165 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 return true;
167}
168
Chris Lattner4667ac32008-01-03 23:38:43 +0000169bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
170 const std::string &Msg2, const std::string &Msg3,
171 SourceRange R1) {
172 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
173 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
174 return true;
175}
176
Reid Spencer5f016e22007-07-11 17:01:13 +0000177bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
178 SourceRange R1, SourceRange R2) {
179 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000180 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 return true;
182}
183
184bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
185 SourceRange R1, SourceRange R2) {
186 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000187 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 return true;
189}
190
191bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
192 const std::string &Msg2, SourceRange R1, SourceRange R2) {
193 std::string MsgArr[] = { Msg1, Msg2 };
194 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000195 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 return true;
197}
198
199const LangOptions &Sema::getLangOptions() const {
200 return PP.getLangOptions();
201}