blob: f34aa6362fcbf133c6ec1ff7c721d0c1c7b3a673 [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//
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//
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"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000019
Chris Lattnerc11438c2006-08-18 05:17:52 +000020using namespace clang;
21
Steve Naroffc62adb62007-10-09 22:01:59 +000022void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
23 TUScope = S;
24}
25
Steve Naroff7f549f12007-10-10 21:53:07 +000026QualType Sema::GetObjcIdType() {
27 assert(TUScope && "GetObjcIdType(): Top-level scope is null");
28 if (ObjcIdType.isNull()) {
29 IdentifierInfo *IdIdent = &Context.Idents.get("id");
30 ScopedDecl *IdDecl = LookupScopedDecl(IdIdent, Decl::IDNS_Ordinary,
31 SourceLocation(), TUScope);
32 TypedefDecl *IdTypedef = dyn_cast_or_null<TypedefDecl>(IdDecl);
33 assert(IdTypedef && "GetObjcIdType(): Couldn't find 'id' type");
34 ObjcIdType = Context.getTypedefType(IdTypedef);
35 }
36 return ObjcIdType;
37}
38
39
Steve Naroff2c055d22007-02-28 19:32:13 +000040Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
41 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattnerb87b1b32007-08-10 20:18:51 +000042
43 // Get IdentifierInfo objects for known functions for which we
44 // do extra checking.
45 IdentifierTable& IT = PP.getIdentifierTable();
46
47 KnownFunctionIDs[ id_printf ] = &IT.get("printf");
48 KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
49 KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
50 KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
Chris Lattnerb87b1b32007-08-10 20:18:51 +000051 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremenekcfc94192007-08-10 21:13:51 +000052 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattnerb87b1b32007-08-10 20:18:51 +000053 KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
54 KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
55 KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
56 KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
Steve Naroff7f549f12007-10-10 21:53:07 +000057
58 TUScope = 0;
59 ObjcIdType = QualType();
Steve Naroff38d31b42007-02-28 01:22:02 +000060}
Chris Lattnercb6a3822006-11-10 06:20:45 +000061
Chris Lattner57c523f2007-08-31 04:53:24 +000062void Sema::DeleteExpr(ExprTy *E) {
63 delete static_cast<Expr*>(E);
64}
65void Sema::DeleteStmt(StmtTy *S) {
66 delete static_cast<Stmt*>(S);
67}
68
Chris Lattnerc11438c2006-08-18 05:17:52 +000069//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +000070// Helper functions.
71//===----------------------------------------------------------------------===//
72
Chris Lattnerd6647d32007-05-16 17:56:50 +000073bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
74 PP.getDiagnostics().Report(Loc, DiagID);
75 return true;
76}
77
Steve Narofff1e53692007-03-23 22:27:02 +000078bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Chris Lattnerd6647d32007-05-16 17:56:50 +000079 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
80 return true;
81}
82
83bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
84 const std::string &Msg2) {
85 std::string MsgArr[] = { Msg1, Msg2 };
86 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +000087 return true;
Chris Lattnereaafe1222006-11-10 05:17:58 +000088}
89
Steve Naroff71ce2e02007-05-18 22:53:50 +000090bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
91 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
Steve Naroffbc2f0992007-03-30 20:09:34 +000092 return true;
93}
94
Steve Naroff71ce2e02007-05-18 22:53:50 +000095bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
96 SourceRange Range) {
97 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
98 return true;
99}
100
101bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
102 const std::string &Msg2, SourceRange Range) {
103 std::string MsgArr[] = { Msg1, Msg2 };
104 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
105 return true;
106}
107
108bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
109 SourceRange R1, SourceRange R2) {
110 SourceRange RangeArr[] = { R1, R2 };
111 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
112 return true;
113}
114
115bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
116 SourceRange R1, SourceRange R2) {
117 SourceRange RangeArr[] = { R1, R2 };
118 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
119 return true;
120}
121
122bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
123 const std::string &Msg2, SourceRange R1, SourceRange R2) {
124 std::string MsgArr[] = { Msg1, Msg2 };
125 SourceRange RangeArr[] = { R1, R2 };
126 PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000127 return true;
Steve Naroff8160ea22007-03-06 01:09:46 +0000128}
129
Chris Lattnerac18be92006-11-20 06:49:47 +0000130const LangOptions &Sema::getLangOptions() const {
Steve Naroff38d31b42007-02-28 01:22:02 +0000131 return PP.getLangOptions();
Chris Lattnerac18be92006-11-20 06:49:47 +0000132}