Cleanup #1489: Delete GPU dummy mempool
[charm.git] / src / xlat-i / xi-util.C
blob219849ca3573514388179704eabf5735c1eed1d0
1 #include "xi-Template.h"
2 #include "xi-util.h"
4 namespace xi {
6 void XStr::append(const char* _s) {
7   len += strlen(_s);
8   if (len >= blklen) {
9     while (len >= blklen) {
10       blklen += SZ;
11     }
12     char* tmp = s;
13     s = new char[blklen];
14     strcpy(s, tmp);
15     delete[] tmp;
16   }
17   strcat(s, _s);
20 void XStr::append(char c) {
21   char tmp[2];
22   tmp[0] = c;
23   tmp[1] = '\0';
24   append(tmp);
27 void XStr::initTo(const char* _s) {
28   len = strlen(_s);
29   blklen = SZ;
30   while (len >= blklen) {
31     blklen += SZ;
32   }
33   s = new char[blklen];
34   strcpy(s, _s);
37 XStr::XStr() { initTo(""); }
38 XStr::XStr(const char* _s) { initTo(_s); }
39 XStr::XStr(const XStr& _s) { initTo(_s.get_string_const()); }
41 void XStr::clear() {
42   delete[] s;
43   initTo("");
46 XStr& XStr::operator<<(int i) {
47   char tmp[100];
48   sprintf(tmp, "%d", i);
49   append(tmp);
50   return *this;
53 void XStr::line_append(const char c) {
54   XStr xs;
55   for (unsigned int i = 0; i < len; i++) {
56     if (s[i] == '\n')
57       xs << c << "\n";
58     else
59       xs << s[i];
60   }
61   delete[] s;
62   initTo(xs.charstar());
65 void XStr::line_append_padding(const char c, int lineWidth) {
66   XStr xs;
67   int count = 0;
69   for (unsigned int i = 0; i < len; i++) {
70     if (s[i] == '\n') {
71       // found line ending
72       while (count++ < lineWidth - 1) xs << " ";
73       xs << c << "\n";
74       count = 0;
75     } else if (s[i] == '\t') {
76       // found tab, convert to 2 spaces
77       xs << "  ";
78       count += 2;
79     } else {
80       // found non-line ending
81       xs << s[i];
82       count++;
83     }
84   }
85   delete[] s;
86   initTo(xs.charstar());
89 void XStr::spew(const char* b, const char* a1, const char* a2, const char* a3,
90                 const char* a4, const char* a5) {
91   using std::cout;
92   int i, length = strlen(b);
93   for (i = 0; i < length; i++) {
94     switch (b[i]) {
95       case '\001':
96         if (a1 == 0) {
97           cout << "Internal Error\n";
98           abort();
99         }
100         append(a1);
101         break;
102       case '\002':
103         if (a2 == 0) {
104           cout << "Internal Error\n";
105           abort();
106         }
107         append(a2);
108         break;
109       case '\003':
110         if (a3 == 0) {
111           cout << "Internal Error\n";
112           abort();
113         }
114         append(a3);
115         break;
116       case '\004':
117         if (a4 == 0) {
118           cout << "Internal Error\n";
119           abort();
120         }
121         append(a4);
122         break;
123       case '\005':
124         if (a5 == 0) {
125           cout << "Internal Error\n";
126           abort();
127         }
128         append(a5);
129         break;
130       default:
131         append(b[i]);
132     }
133   }
136 void XStr::replace(const char a, const char b) {
137   for (unsigned int i = 0; i < len; i++) {
138     if (s[i] == a) s[i] = b;
139   }
142 extern const char* cur_file;
144 // Fatal error function
145 void die(const char* why, int line) {
146   if (line == -1)
147     fprintf(stderr, "%s: Charmxi fatal error> %s\n", cur_file, why);
148   else
149     fprintf(stderr, "%s:%d: Charmxi fatal error> %s\n", cur_file, line, why);
150   exit(1);
153 char* fortranify(const char* s, const char* suff1, const char* suff2, const char* suff3) {
154   int i, len1 = strlen(s), len2 = strlen(suff1), len3 = strlen(suff2),
155          len4 = strlen(suff3);
156   int c = len1 + len2 + len3 + len4;
157   char str[1024], strUpper[1024];
158   strcpy(str, s);
159   strcat(str, suff1);
160   strcat(str, suff2);
161   strcat(str, suff3);
162   for (i = 0; i < c + 1; i++) str[i] = tolower(str[i]);
163   for (i = 0; i < c + 1; i++) strUpper[i] = toupper(str[i]);
164   char* retVal;
165   retVal = new char[2 * c + 20];
166   strcpy(retVal, "FTN_NAME(");
167   strcat(retVal, strUpper);
168   strcat(retVal, ",");
169   strcat(retVal, str);
170   strcat(retVal, ")");
172   return retVal;
175 XStr generateTemplateSpec(TVarList* tspec, bool printDefault) {
176   XStr str;
178   if (tspec) {
179     str << "template <";
180     tspec->genLong(str, printDefault);
181     str << "> ";
182   }
184   return str;
187 const char* forWhomStr(forWhom w) {
188   switch (w) {
189     case forAll:
190       return Prefix::Proxy;
191     case forIndividual:
192       return Prefix::ProxyElement;
193     case forSection:
194       return Prefix::ProxySection;
195     case forIndex:
196       return Prefix::Index;
197     case forPython:
198       return "";
199     default:
200       return NULL;
201   };
204 // Make the name lower case
205 void templateGuardBegin(bool templateOnly, XStr& str) {
206   if (templateOnly)
207     str << "#ifdef "
208         << "CK_TEMPLATES_ONLY\n";
209   else
210     str << "#ifndef "
211         << "CK_TEMPLATES_ONLY\n";
213 void templateGuardEnd(XStr& str) { str << "#endif /* CK_TEMPLATES_ONLY */\n"; }
215 // This replaces the line containing a single '#' token with a '#line'
216 // directive, as described in SerialConstruct::generateCode.
217 std::string addLineNumbers(char* str, const char* filename) {
218   int lineNo = 1;
219   std::string s(str);
220   for (int i = 0; i < s.length(); ++i) {
221     switch (s[i]) {
222       case '\n':
223         lineNo++;
224         break;
225       case '#':
226         if (i > 0 && s[i - 1] == '\n' && s[i + 1] == '\n') {
227           std::stringstream ss;
228           ss << "#line " << lineNo + 1 << " \"" << filename << "\"";
229           s.replace(s.begin() + i, s.begin() + i + 1, ss.str());
230         }
231     }
232   }
233   return s;
236 // The following three functions are a workaround for bug #734.
237 void sanitizeRange(std::string& code, int i, int j) {
238   for (int k = i; k <= j; ++k) {
239     switch (code[k]) {
240       case '{':
241         code[k] = 0x0E;
242         break;
243       case '}':
244         code[k] = 0x0F;
245         break;
246       case '(':
247         code[k] = 0x10;
248         break;
249       case ')':
250         code[k] = 0x11;
251         break;
252       case '[':
253         code[k] = 0x12;
254         break;
255       case ']':
256         code[k] = 0x13;
257         break;
258       case ';':
259         code[k] = 0x14;
260         break;
261       case ':':
262         code[k] = 0x15;
263         break;
264       case ',':
265         code[k] = 0x16;
266         break;
267       default:
268         break;
269     }
270   }
273 void desanitizeCode(std::string& code) {
274   for (int i = 0; i < code.size(); ++i) {
275     switch (code[i]) {
276       case 0x0E:
277         code[i] = '{';
278         break;
279       case 0x0F:
280         code[i] = '}';
281         break;
282       case 0x10:
283         code[i] = '(';
284         break;
285       case 0x11:
286         code[i] = ')';
287         break;
288       case 0x12:
289         code[i] = '[';
290         break;
291       case 0x13:
292         code[i] = ']';
293         break;
294       case 0x14:
295         code[i] = ';';
296         break;
297       case 0x15:
298         code[i] = ':';
299         break;
300       case 0x16:
301         code[i] = ',';
302         break;
303       default:
304         break;
305     }
306   }
309 void sanitizeComments(std::string& code) {
310   int h, i;
312   if(code.length() == 0)
313     return;
315   for (i = 0; i < code.length() - 1; ++i) {
316     if (code[i] == '/') {
317       h = i + 2;
318       switch (code[i + 1]) {
319         case '*':
320           // Case 1: /* */
321           i += 2;
322           for (; !(code[i] == '*' && code[i + 1] == '/'); ++i)
323             ;
324           break;
326         case '/':
327           // Case 2: //
328           while (code[++i] != '\n')
329             ;
330           break;
332         default:
333           // Case 3: not a comment
334           continue;
335           break;
336       }
338       sanitizeRange(code, h, i - 1);
339       ++i;
340     }
341   }
344 void sanitizeStrings(std::string& code) {
345   int h, i;
346   bool in_string = false;
347   for (i = 0; i < code.size(); ++i) {
348     if (code[i] == '\\') {
349       // The next character cannot possibly end a string since this '\'
350       // would escape it; just skip over it.
351       ++i;
352     } else if (code[i] == '"') {
353       if (in_string) {
354         sanitizeRange(code, h, i - 1);
355         in_string = false;
356       } else {
357         h = i + 1;
358         in_string = true;
359       }
360     }
361   }
364 // charmxi error printing methods
365 std::string _get_caret_line(int err_line_start, int first_col, int last_col) {
366   std::string caret_line(first_col - err_line_start - 1, ' ');
367   caret_line += std::string(last_col - first_col + 1, '^');
369   return caret_line;
372 void _pretty_header(std::string type, std::string msg, int first_col, int last_col,
373                     int first_line, int last_line) {
374   std::cerr << cur_file << ":" << first_line << ":";
376   if (first_col != -1) std::cerr << first_col << "-" << last_col << ": ";
378   std::cerr << type << ": " << msg << std::endl;
381 void _pretty_print(std::string type, std::string msg, int first_col, int last_col,
382                    int first_line, int last_line) {
383   _pretty_header(type, msg, first_col, last_col, first_line, last_line);
385   if (first_line <= inputBuffer.size() && first_line <= last_line &&
386       first_col <= last_col) {
387     std::string err_line = inputBuffer[first_line - 1];
389     if (err_line.length() != 0) {
390       int err_line_start = err_line.find_first_not_of(" \t\r\n");
391       err_line.erase(0, err_line_start);
393       std::string caret_line;
394       if (first_col != -1)
395         caret_line = _get_caret_line(err_line_start, first_col, last_col);
397       std::cerr << "  " << err_line << std::endl;
399       if (first_col != -1) std::cerr << "  " << caret_line;
400       std::cerr << std::endl;
401     }
402   }
405 void pretty_msg(std::string type, std::string msg, int first_col, int last_col,
406                 int first_line, int last_line) {
407   if (first_line == -1) first_line = lineno;
408   if (last_line == -1) last_line = lineno;
409   _pretty_print(type, msg, first_col, last_col, first_line, last_line);
412 void pretty_msg_noline(std::string type, std::string msg, int first_col, int last_col,
413                        int first_line, int last_line) {
414   if (first_line == -1) first_line = lineno;
415   if (last_line == -1) last_line = lineno;
416   _pretty_header(type, msg, first_col, last_col, first_line, last_line);
419 }  // namespace xi
421 namespace Prefix {
423 const char* Proxy = "CProxy_";
424 const char* ProxyElement = "CProxyElement_";
425 const char* ProxySection = "CProxySection_";
426 const char* Message = "CMessage_";
427 const char* Index = "CkIndex_";
428 const char* Python = "CkPython_";
430 }  // namespace Prefix