blob: 323483586bca690c0c11603a3f425e21e5f5e79f [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 ||
26 strcmp(typeName, "SEL") == 0;
27}
28
Steve Naroffb216c882007-10-09 22:01:59 +000029void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
30 TUScope = S;
Steve Naroff8ee529b2007-10-31 18:42:27 +000031 if (PP.getLangOptions().ObjC1) {
32 TypedefType *t;
Chris Lattnerfb1051b2007-10-30 20:57:56 +000033
Steve Naroff8ee529b2007-10-31 18:42:27 +000034 // Add the built-in ObjC types.
35 t = dyn_cast<TypedefType>(Context.getObjcIdType().getTypePtr());
36 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
37 TUScope->AddDecl(t->getDecl());
38 t = dyn_cast<TypedefType>(Context.getObjcClassType().getTypePtr());
39 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
40 TUScope->AddDecl(t->getDecl());
41 t = dyn_cast<TypedefType>(Context.getObjcSelType().getTypePtr());
42 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
43 TUScope->AddDecl(t->getDecl());
Steve Naroff3b950172007-10-10 21:53:07 +000044 }
Steve Naroff3b950172007-10-10 21:53:07 +000045}
46
Steve Naroff8ee529b2007-10-31 18:42:27 +000047/// FIXME: remove this.
Fariborz Jahanian390d50a2007-10-17 16:58:11 +000048/// GetObjcProtoType - See comments for Sema::GetObjcIdType above; replace "id"
49/// with "Protocol".
50QualType Sema::GetObjcProtoType(SourceLocation Loc) {
51 assert(TUScope && "GetObjcProtoType(): Top-level scope is null");
52 if (Context.getObjcProtoType().isNull()) {
53 IdentifierInfo *ProtoIdent = &Context.Idents.get("Protocol");
54 ScopedDecl *ProtoDecl = LookupScopedDecl(ProtoIdent, Decl::IDNS_Ordinary,
55 SourceLocation(), TUScope);
56 TypedefDecl *ObjcProtoTypedef = dyn_cast_or_null<TypedefDecl>(ProtoDecl);
57 if (!ObjcProtoTypedef) {
58 Diag(Loc, diag::err_missing_proto_definition);
59 return QualType();
60 }
61 Context.setObjcProtoType(ObjcProtoTypedef);
62 }
63 return Context.getObjcProtoType();
64}
65
Reid Spencer5f016e22007-07-11 17:01:13 +000066Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
67 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattner59907c42007-08-10 20:18:51 +000068
69 // Get IdentifierInfo objects for known functions for which we
70 // do extra checking.
71 IdentifierTable& IT = PP.getIdentifierTable();
72
73 KnownFunctionIDs[ id_printf ] = &IT.get("printf");
74 KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
75 KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
76 KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
Chris Lattner59907c42007-08-10 20:18:51 +000077 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremeneke0eb80a2007-08-10 21:13:51 +000078 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattner59907c42007-08-10 20:18:51 +000079 KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
80 KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
81 KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
82 KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +000083
84 if (PP.getLangOptions().ObjC1) {
85 // Synthesize "typedef struct objc_class *Class;"
86 RecordDecl *ClassTag = new RecordDecl(Decl::Struct, SourceLocation(),
87 &IT.get("objc_class"), 0);
88 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
89 TypedefDecl *ClassTypedef = new TypedefDecl(SourceLocation(),
90 &Context.Idents.get("Class"),
91 ClassT, 0);
92 Context.setObjcClassType(ClassTypedef);
93
94 // Synthesize "typedef struct objc_object { Class isa; } *id;"
95 RecordDecl *ObjectTag = new RecordDecl(Decl::Struct, SourceLocation(),
96 &IT.get("objc_object"), 0);
97 FieldDecl *IsaDecl = new FieldDecl(SourceLocation(), 0,
98 Context.getObjcClassType());
99 ObjectTag->defineBody(&IsaDecl, 1);
100 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
101 TypedefDecl *IdTypedef = new TypedefDecl(SourceLocation(),
102 &Context.Idents.get("id"),
103 ObjT, 0);
104 Context.setObjcIdType(IdTypedef);
105
106 // Synthesize "typedef struct objc_selector *SEL;"
107 RecordDecl *SelTag = new RecordDecl(Decl::Struct, SourceLocation(),
108 &IT.get("objc_selector"), 0);
109 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
110 TypedefDecl *SelTypedef = new TypedefDecl(SourceLocation(),
111 &Context.Idents.get("SEL"),
112 SelT, 0);
113 Context.setObjcSelType(SelTypedef);
114 }
Steve Naroff3b950172007-10-10 21:53:07 +0000115 TUScope = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000116}
117
Chris Lattner394a3fd2007-08-31 04:53:24 +0000118void Sema::DeleteExpr(ExprTy *E) {
119 delete static_cast<Expr*>(E);
120}
121void Sema::DeleteStmt(StmtTy *S) {
122 delete static_cast<Stmt*>(S);
123}
124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125//===----------------------------------------------------------------------===//
126// Helper functions.
127//===----------------------------------------------------------------------===//
128
129bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
130 PP.getDiagnostics().Report(Loc, DiagID);
131 return true;
132}
133
134bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
135 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
136 return true;
137}
138
139bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
140 const std::string &Msg2) {
141 std::string MsgArr[] = { Msg1, Msg2 };
142 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
143 return true;
144}
145
146bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
147 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
148 return true;
149}
150
151bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
152 SourceRange Range) {
153 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
154 return true;
155}
156
157bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
158 const std::string &Msg2, SourceRange Range) {
159 std::string MsgArr[] = { Msg1, Msg2 };
160 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
161 return true;
162}
163
164bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
165 SourceRange R1, SourceRange R2) {
166 SourceRange RangeArr[] = { R1, R2 };
167 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
168 return true;
169}
170
171bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
172 SourceRange R1, SourceRange R2) {
173 SourceRange RangeArr[] = { R1, R2 };
174 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
175 return true;
176}
177
178bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
179 const std::string &Msg2, SourceRange R1, SourceRange R2) {
180 std::string MsgArr[] = { Msg1, Msg2 };
181 SourceRange RangeArr[] = { R1, R2 };
182 PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
183 return true;
184}
185
186const LangOptions &Sema::getLangOptions() const {
187 return PP.getLangOptions();
188}