blob: 1a012706927bdb839cfc9f32d988b2ec40b74e0f [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 Naroff2c055d22007-02-28 19:32:13 +000022Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
23 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattnerb87b1b32007-08-10 20:18:51 +000024
25 // Get IdentifierInfo objects for known functions for which we
26 // do extra checking.
27 IdentifierTable& IT = PP.getIdentifierTable();
28
29 KnownFunctionIDs[ id_printf ] = &IT.get("printf");
30 KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
31 KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
32 KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
Chris Lattnerb87b1b32007-08-10 20:18:51 +000033 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremenekcfc94192007-08-10 21:13:51 +000034 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattnerb87b1b32007-08-10 20:18:51 +000035 KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
36 KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
37 KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
38 KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
Steve Naroff38d31b42007-02-28 01:22:02 +000039}
Chris Lattnercb6a3822006-11-10 06:20:45 +000040
Chris Lattner57c523f2007-08-31 04:53:24 +000041void Sema::DeleteExpr(ExprTy *E) {
42 delete static_cast<Expr*>(E);
43}
44void Sema::DeleteStmt(StmtTy *S) {
45 delete static_cast<Stmt*>(S);
46}
47
Chris Lattnerc11438c2006-08-18 05:17:52 +000048//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +000049// Helper functions.
50//===----------------------------------------------------------------------===//
51
Chris Lattnerd6647d32007-05-16 17:56:50 +000052bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
53 PP.getDiagnostics().Report(Loc, DiagID);
54 return true;
55}
56
Steve Narofff1e53692007-03-23 22:27:02 +000057bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Chris Lattnerd6647d32007-05-16 17:56:50 +000058 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
59 return true;
60}
61
62bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
63 const std::string &Msg2) {
64 std::string MsgArr[] = { Msg1, Msg2 };
65 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +000066 return true;
Chris Lattnereaafe1222006-11-10 05:17:58 +000067}
68
Steve Naroff71ce2e02007-05-18 22:53:50 +000069bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
70 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
Steve Naroffbc2f0992007-03-30 20:09:34 +000071 return true;
72}
73
Steve Naroff71ce2e02007-05-18 22:53:50 +000074bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
75 SourceRange Range) {
76 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
77 return true;
78}
79
80bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
81 const std::string &Msg2, SourceRange Range) {
82 std::string MsgArr[] = { Msg1, Msg2 };
83 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
84 return true;
85}
86
87bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
88 SourceRange R1, SourceRange R2) {
89 SourceRange RangeArr[] = { R1, R2 };
90 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
91 return true;
92}
93
94bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
95 SourceRange R1, SourceRange R2) {
96 SourceRange RangeArr[] = { R1, R2 };
97 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
98 return true;
99}
100
101bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
102 const std::string &Msg2, SourceRange R1, SourceRange R2) {
103 std::string MsgArr[] = { Msg1, Msg2 };
104 SourceRange RangeArr[] = { R1, R2 };
105 PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000106 return true;
Steve Naroff8160ea22007-03-06 01:09:46 +0000107}
108
Chris Lattnerac18be92006-11-20 06:49:47 +0000109const LangOptions &Sema::getLangOptions() const {
Steve Naroff38d31b42007-02-28 01:22:02 +0000110 return PP.getLangOptions();
Chris Lattnerac18be92006-11-20 06:49:47 +0000111}