Cleanup #1489: Delete GPU dummy mempool
[charm.git] / src / xlat-i / xi-util.h
blobfa41134771d4d75047b09c091cdb8fb01f7a4f4f
1 #ifndef _XI_UTIL_H
2 #define _XI_UTIL_H
4 #include <iostream>
5 #include <sstream>
6 #include <string>
7 #include <vector>
9 #include "conv-config.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #define XLAT_ERROR(...) \
15 do { \
16 if (xi::num_errors++ == xi::MAX_NUM_ERRORS) { \
17 exit(1); \
18 } else { \
19 pretty_msg("error", __VA_ARGS__); \
20 } \
21 } while (0)
23 #define XLAT_ERROR_NOCOL(str, line) XLAT_ERROR((str), -1, -1, (line), (line))
25 #define XLAT_NOTE(str, line) pretty_msg("note", (str), -1, -1, (line), (line))
27 extern unsigned int lineno;
29 namespace xi {
31 extern void pretty_msg(std::string type, std::string msg, int first_col = -1,
32 int last_col = -1, int first_line = -1, int last_line = -1);
34 extern const int MAX_NUM_ERRORS;
35 extern int num_errors;
37 extern std::vector<std::string> inputBuffer;
39 #if CMK_ISATTY_DECL
40 #ifdef __cplusplus
41 extern "C" int isatty(int);
42 #endif
43 #endif
45 #define SZ 1024
47 class XStr {
48 private:
49 char* s;
50 unsigned int len, blklen;
51 void initTo(const char* _s);
52 void operator=(const XStr& str); //<- don't use this
53 public:
54 // MAB: following append methods were earlier private. However,
55 // in order to minimize changes to sdag translator, they have been made
56 // public. Once the sdag translator is fully embedded in charmxi,
57 // they will be made private again.
58 void append(const char* _s);
59 void append(char c);
60 // MAB: the print method is needed for debugging sdag translator.
61 // this too will go away later.
62 void print(int indent) {
63 for (int i = 0; i < indent; i++) std::cout << " ";
64 std::cout << get_string();
66 /// Appends character c to every line
67 void line_append(const char c);
68 /// pads with spaces and appends character c to every line. Also converts tabs to
69 /// spaces.
70 void line_append_padding(const char c, int lineWidth = 80);
71 // Replace all occurences of character "a" in string with character "b"
72 void replace(const char a, const char b);
74 public:
75 XStr();
76 XStr(const char* _s);
77 XStr(const XStr& _s); // Copy constructor
78 ~XStr() { delete[] s; }
79 void clear();
80 char* get_string(void) const { return s; }
81 const char* get_string_const(void) const { return s; }
82 // this is to allow XStr to be substituted for CString in
83 // structured dagger translator without a lot of changes
84 char* charstar(void) const { return get_string(); }
85 // This operator allows us to use XStr's interchangably with char *'s:
86 operator char*() { return get_string(); }
87 size_t length() const { return len; }
88 // Comparison operators
89 int operator==(XStr& s2) const { return 0 == strcmp(s, s2.s); }
90 int operator!=(XStr& s2) const { return 0 != strcmp(s, s2.s); }
91 int operator==(const char* s2) const { return 0 == strcmp(s, s2); }
92 int operator!=(const char* s2) const { return 0 != strcmp(s, s2); }
93 // Addition operator
94 XStr operator+(const XStr& s2) const {
95 XStr ret(*this);
96 ret.append(s2.s);
97 return ret;
99 // Insertion operators
100 XStr& operator<<(const char* _s) {
101 append(_s);
102 return *this;
104 // XStr& operator << (const string & _s) { append(_s.c_str()); return *this;}
105 XStr& operator<<(char c) {
106 append(c);
107 return *this;
109 XStr& operator<<(int i);
110 XStr& operator<<(const XStr& x) {
111 append(x.get_string_const());
112 return *this;
114 XStr& operator<<(const XStr* x) {
115 append(x->get_string_const());
116 return *this;
118 void spew(const char* b, const char* a1 = 0, const char* a2 = 0, const char* a3 = 0,
119 const char* a4 = 0, const char* a5 = 0);
122 #define endx "\n"
124 class Printable {
125 public:
126 virtual void print(XStr& str) = 0;
127 // This lets us cast printables to XStr
128 operator XStr() {
129 XStr ret;
130 print(ret);
131 return ret;
133 // These let us stream Printables to XStr.
134 virtual ~Printable() {}
135 friend XStr& operator<<(XStr& str, Printable& p) {
136 p.print(str);
137 return str;
139 friend XStr& operator<<(XStr& str, Printable* p) {
140 p->print(str);
141 return str;
145 void templateGuardBegin(bool templateOnly, XStr& str);
146 void templateGuardEnd(XStr& str);
148 inline void indentBy(XStr& s, int num) {
149 for (int i = 0; i < num; i++) s << " ";
152 class TVarList;
153 XStr generateTemplateSpec(TVarList* tspec, bool printDefault = true);
155 typedef enum {
156 forAll = 0,
157 forIndividual = 1,
158 forSection = 2,
159 forPython = 3,
160 forIndex = -1
161 } forWhom;
163 const char* forWhomStr(forWhom w);
165 // FIXME: this same function is used for both syntax error messages as well as
166 // e.g. code generation errors
167 void die(const char* why, int line = -1);
169 char* fortranify(const char* s, const char* suff1 = "", const char* suff2 = "",
170 const char* suff3 = "");
172 void templateGuardBegin(bool templateOnly, XStr& str);
173 void templateGuardEnd(XStr& str);
175 std::string addLineNumbers(char* str, const char* filename);
176 extern void sanitizeComments(std::string& code);
177 extern void sanitizeStrings(std::string& code);
178 extern void desanitizeCode(std::string& code);
180 } // namespace xi
182 namespace Prefix {
184 extern const char* Proxy;
185 extern const char* ProxyElement;
186 extern const char* ProxySection;
187 extern const char* Message;
188 extern const char* Index;
189 extern const char* Python;
191 } // namespace Prefix
193 #endif