blob: b18e5861b94d7b7b5e3ad95336bcf8ee3e35f454 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang0a73dd82014-11-19 16:18:08 -05007#ifndef COMPILER_TRANSLATOR_INFOSINK_H_
8#define COMPILER_TRANSLATOR_INFOSINK_H_
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000010#include <math.h>
Jamie Madilld7f21352013-10-30 17:53:15 -040011#include <stdlib.h>
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/Common.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000013
Jamie Madill45bcc782016-11-07 13:58:48 -050014namespace sh
15{
16
alokp@chromium.orgb892fc62010-05-06 19:10:34 +000017// Returns the fractional part of the given floating-point number.
18inline float fractionalPart(float f) {
alokp@chromium.org92984ef2010-05-06 22:49:37 +000019 float intPart = 0.0f;
20 return modff(f, &intPart);
alokp@chromium.orgb892fc62010-05-06 19:10:34 +000021}
22
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000023//
24// TPrefixType is used to centralize how info log messages start.
25// See below.
26//
27enum TPrefixType {
28 EPrefixNone,
29 EPrefixWarning,
30 EPrefixError,
31 EPrefixInternalError,
32 EPrefixUnimplemented,
33 EPrefixNote
34};
35
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036//
37// Encapsulate info logs for all objects that have them.
38//
39// The methods are a general set of tools for getting a variety of
40// messages and types inserted into the log.
41//
42class TInfoSinkBase {
43public:
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000044 TInfoSinkBase() {}
45
46 template <typename T>
47 TInfoSinkBase& operator<<(const T& t) {
48 TPersistStringStream stream;
49 stream << t;
50 sink.append(stream.str());
alokp@chromium.orgb892fc62010-05-06 19:10:34 +000051 return *this;
52 }
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000053 // Override << operator for specific types. It is faster to append strings
54 // and characters directly to the sink.
55 TInfoSinkBase& operator<<(char c) {
56 sink.append(1, c);
57 return *this;
58 }
59 TInfoSinkBase& operator<<(const char* str) {
60 sink.append(str);
61 return *this;
62 }
63 TInfoSinkBase& operator<<(const TPersistString& str) {
64 sink.append(str);
65 return *this;
66 }
67 TInfoSinkBase& operator<<(const TString& str) {
68 sink.append(str.c_str());
69 return *this;
70 }
71 // Make sure floats are written with correct precision.
72 TInfoSinkBase& operator<<(float f) {
73 // Make sure that at least one decimal point is written. If a number
74 // does not have a fractional part, the default precision format does
75 // not write the decimal portion which gets interpreted as integer by
76 // the compiler.
77 TPersistStringStream stream;
78 if (fractionalPart(f) == 0.0f) {
daniel@transgaming.comb0a1dcc2010-06-07 02:06:26 +000079 stream.precision(1);
80 stream << std::showpoint << std::fixed << f;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000081 } else {
daniel@transgaming.comb0a1dcc2010-06-07 02:06:26 +000082 stream.unsetf(std::ios::fixed);
83 stream.unsetf(std::ios::scientific);
84 stream.precision(8);
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000085 stream << f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086 }
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000087 sink.append(stream.str());
88 return *this;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089 }
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000090 // Write boolean values as their names instead of integral value.
91 TInfoSinkBase& operator<<(bool b) {
92 const char* str = b ? "true" : "false";
93 sink.append(str);
94 return *this;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095 }
96
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000097 void erase() { sink.clear(); }
alokp@chromium.org7beea402010-09-15 21:18:34 +000098 int size() { return static_cast<int>(sink.size()); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099
alokp@chromium.org4e4facd2010-06-02 15:21:22 +0000100 const TPersistString& str() const { return sink; }
101 const char* c_str() const { return sink.c_str(); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
Jamie Madill075edd82013-07-08 13:30:19 -0400103 void prefix(TPrefixType p);
104 void location(int file, int line);
105 void location(const TSourceLoc& loc);
106 void message(TPrefixType p, const TSourceLoc& loc, const char* m);
alokp@chromium.org4e4facd2010-06-02 15:21:22 +0000107
108private:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109 TPersistString sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110};
111
112class TInfoSink {
113public:
114 TInfoSinkBase info;
115 TInfoSinkBase debug;
116 TInfoSinkBase obj;
117};
118
Jamie Madill45bcc782016-11-07 13:58:48 -0500119} // namespace sh
120
Geoff Lang0a73dd82014-11-19 16:18:08 -0500121#endif // COMPILER_TRANSLATOR_INFOSINK_H_