-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathCodeGeneratorUtilities.cxx
More file actions
710 lines (674 loc) · 26.9 KB
/
CodeGeneratorUtilities.cxx
File metadata and controls
710 lines (674 loc) · 26.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*!
* \file mfront/src/CodeGeneratorUtilities.cxx
* \brief
*
* \author Thomas Helfer
* \date 23 oct 2008
* \copyright Copyright (C) 2006-2025 CEA/DEN, EDF R&D. All rights
* reserved.
* This project is publicly released under either the GNU GPL Licence with
* linking exception or the CECILL-A licence. A copy of thoses licences are
* delivered with the sources of TFEL. CEA or EDF may also distribute this
* project under specific licensing conditions.
*/
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <utility>
#include <stdexcept>
#include "TFEL/Raise.hxx"
#include "TFEL/Config/TFELConfig.hxx"
#include "TFEL/Config/GetTFELVersion.h"
#include "TFEL/Glossary/GlossaryEntry.hxx"
#include "TFEL/Utilities/StringAlgorithms.hxx"
#include "TFEL/System/System.hxx"
#include "MFront/MFrontDebugMode.hxx"
#include "MFront/VariableDescription.hxx"
#include "MFront/StaticVariableDescription.hxx"
#include "MFront/FileDescription.hxx"
#include "MFront/MaterialKnowledgeDescription.hxx"
#include "MFront/MaterialPropertyDescription.hxx"
#include "MFront/MaterialPropertyParametersHandler.hxx"
#include "MFront/SymbolsGenerator.hxx"
#include "MFront/CodeGeneratorUtilities.hxx"
#ifndef _MSC_VER
static const char* const constexpr_c = "constexpr";
#else
static const char* const constexpr_c = "const";
#endif
namespace mfront {
void exportStringSymbol(std::ostream& os,
const std::string_view n,
const std::string_view v) {
exportSymbol(os, "const char*", n, '"' + std::string{v} + '"');
} // end of exportStringSymbol
void exportUnsignedShortSymbol(std::ostream& os,
const std::string_view n,
const unsigned short v) {
std::ostringstream value;
value << v;
exportSymbol(os, "unsigned short", n, value.str() + "u");
} // end of exportUnsignedShortSymbol
void writeParametersSymbols(std::ostream& os,
const std::string_view n,
const MaterialPropertyDescription& mpd) {
if (areParametersTreatedAsStaticVariables(mpd)) {
writeParametersDeclarationSymbols(os, n, {});
return;
}
writeParametersDeclarationSymbols(os, n, mpd.parameters);
writeParametersDefaultValuesSymbols(os, n, mpd.parameters);
writeBoundsSymbols(os, n, mpd.parameters);
writePhysicalBoundsSymbols(os, n, mpd.parameters);
} // end of writeParametersSymbols
void writeParametersDeclarationSymbols(
std::ostream& os,
const std::string_view n,
const VariableDescriptionContainer& parameters) {
exportUnsignedShortSymbol(os, std::string{n} + "_nParameters",
parameters.getNumberOfVariables());
exportArrayOfStringsSymbol(os, std::string{n} + "_Parameters",
parameters.getExternalNames());
if (parameters.empty()) {
exportSymbol(os, "const int *", std::string{n} + "_ParametersTypes",
"nullptr");
return;
}
const auto size = [¶meters] {
auto s = std::size_t{};
for (const auto& p : parameters) {
s += p.arraySize;
}
return s;
}();
os << "MFRONT_EXPORT_ARRAY_OF_SYMBOLS(int, " << n << "_ParametersTypes, "
<< size << ", MFRONT_EXPORT_ARRAY_ARGUMENTS(";
for (auto p = parameters.begin(); p != parameters.end();) {
for (unsigned short is = 0; is != p->arraySize;) {
if (p->type == "int") {
os << "1";
} else if (p->type == "ushort") {
os << "2";
} else {
const auto f = SupportedTypes::getTypeFlag(p->type);
tfel::raise_if(f != SupportedTypes::SCALAR,
"SymbolsGenerator::writeParametersSymbols: "
"internal error, unsupported type "
"for parameter '" +
p->name + "'");
os << "0";
}
if (++is != p->arraySize) {
os << ",";
}
}
if (++p != parameters.end()) {
os << ",";
}
}
os << "));\n\n";
} // end of writeParametersDeclarationSymbols
void writeParametersDefaultValuesSymbols(
std::ostream& os,
const std::string_view n,
const VariableDescriptionContainer& parameters) {
const auto prec = os.precision();
for (const auto& p : parameters) {
const auto f = SupportedTypes::getTypeFlag(p.type);
if (f != SupportedTypes::SCALAR) {
tfel::raise(
"writeParameterDefaultValueSymbols: "
"unsupported paramaeter type '" +
p.type + "'");
}
os.precision(14);
if (p.arraySize != 1u) {
tfel::raise(
"writeParameterDefaultValueSymbols: "
"array of parameters is not supported");
}
exportSymbol(
os, "double",
std::string{n} + "_" + p.getExternalName() + "_ParameterDefaultValue",
p.getAttribute<double>(VariableDescription::defaultValue));
os.precision(prec);
}
} // end of writeParametersDefaultValuesSymbols
void exportArrayOfIntegersSymbol(std::ostream& os,
const std::string_view s,
const std::vector<int>& values) {
if (values.empty()) {
exportSymbol(os, "const int *", s, "nullptr");
return;
}
auto i = decltype(values.size()){};
auto p = values.begin();
os << "MFRONT_EXPORT_ARRAY_OF_SYMBOLS(int, " << s << ", " << values.size()
<< ", MFRONT_EXPORT_ARRAY_ARGUMENTS(";
while (p != values.end()) {
os << *p;
if (++p != values.end()) {
if (i % 5 == 0) {
os << ",\n";
} else {
os << ",";
}
}
++i;
}
os << "));\n\n";
} // end of exportArrayOfIntegersSymbol
void exportArrayOfStringsSymbol(std::ostream& os,
const std::string_view s,
const std::vector<std::string>& values) {
if (values.empty()) {
exportSymbol(os, "const char * const *", s, "nullptr");
return;
}
auto i = decltype(values.size()){};
auto p = values.begin();
os << "MFRONT_EXPORT_ARRAY_OF_SYMBOLS(const char *, " << s << ", "
<< values.size() << ", MFRONT_EXPORT_ARRAY_ARGUMENTS(";
while (p != values.end()) {
os << '"' << *p << '"';
if (++p != values.end()) {
if (i % 5 == 0) {
os << ",\n";
} else {
os << ",";
}
}
++i;
}
os << "));\n\n";
} // end of exportArrayOfStringsSymbol
void writeVariablesNamesSymbol(
std::ostream& out,
const std::string_view name,
const mfront::MaterialPropertyDescription& mpd) {
exportStringSymbol(out, std::string{name} + "_output",
mpd.output.getExternalName());
exportUnsignedShortSymbol(out, std::string{name} + "_nargs",
mpd.inputs.size());
if (!mpd.inputs.empty()) {
out << "MFRONT_EXPORT_ARRAY_OF_SYMBOLS(const char *, " << name
<< "_args, " << mpd.inputs.size()
<< ", MFRONT_EXPORT_ARRAY_ARGUMENTS(";
for (auto p3 = mpd.inputs.begin(); p3 != mpd.inputs.end();) {
const auto iname = '\"' + p3->getExternalName() + '\"';
out << iname;
if (++p3 != mpd.inputs.end()) {
out << ",";
}
}
out << "));\n\n";
}
} // end of writeVariableNamesSymbol
void writeBoundsSymbol(std::ostream& out,
const std::string_view n,
const std::string_view vn,
const std::string_view bt,
const VariableBoundsDescription& b) {
if ((b.boundsType == VariableBoundsDescription::LOWER) ||
(b.boundsType == VariableBoundsDescription::LOWERANDUPPER)) {
out << "MFRONT_EXPORT_SYMBOL(long double, " //
<< n << "_" << vn << "_"
<< "Lower" << bt << "Bound, " //
<< "static_cast<long double>(" << b.lowerBound << "));\n\n";
}
if ((b.boundsType == VariableBoundsDescription::UPPER) ||
(b.boundsType == VariableBoundsDescription::LOWERANDUPPER)) {
out << "MFRONT_EXPORT_SYMBOL(long double, " //
<< n << "_" << vn << "_"
<< "Upper" << bt << "Bound, " //
<< "static_cast<long double>(" << b.upperBound << "));\n\n";
}
} // end of writeBoundsSymbol
void writeBoundsSymbols(std::ostream& os,
const std::string_view n,
const VariableDescriptionContainer& vc) {
const auto prec = os.precision();
os.precision(14);
for (const auto& v : vc) {
if (v.arraySize == 1u) {
if (!v.hasBounds()) {
continue;
}
mfront::writeBoundsSymbol(os, n, v.getExternalName(), "",
v.getBounds());
} else {
for (auto idx = 0; idx != v.arraySize; ++idx) {
if (!v.hasBounds(idx)) {
continue;
}
mfront::writeBoundsSymbol(os, n,
v.getExternalName() + "_mfront_index_" +
std::to_string(idx) + "_",
"", v.getBounds(idx));
}
}
}
os.precision(prec);
} // end of writeBoundsSymbols
void writePhysicalBoundsSymbols(std::ostream& os,
const std::string_view n,
const VariableDescriptionContainer& vc) {
const auto prec = os.precision();
os.precision(14);
for (const auto& v : vc) {
if (v.arraySize == 1u) {
if (!v.hasPhysicalBounds()) {
continue;
}
mfront::writeBoundsSymbol(os, n, v.getExternalName(), "Physical",
v.getPhysicalBounds());
} else {
for (auto idx = 0; idx != v.arraySize; ++idx) {
if (!v.hasPhysicalBounds(idx)) {
continue;
}
mfront::writeBoundsSymbol(os, n,
v.getExternalName() + "_mfront_index_" +
std::to_string(idx) + "_",
"Physical", v.getPhysicalBounds(idx));
}
}
}
os.precision(prec);
} // end of writePhysicalBoundsSymbols
void writeVariablesBoundsSymbols(
std::ostream& out,
const std::string_view name,
const mfront::MaterialPropertyDescription& mpd) {
const auto prec = out.precision();
out.precision(14);
for (const auto& v : mpd.inputs) {
if (v.arraySize == 1u) {
if (!v.hasBounds()) {
continue;
}
writeBoundsSymbol(out, name, v.getExternalName(), "", v.getBounds());
if (!v.hasPhysicalBounds()) {
continue;
}
writeBoundsSymbol(out, name, v.getExternalName(), "Physical",
v.getPhysicalBounds());
} else {
for (auto idx = 0; idx != v.arraySize; ++idx) {
if (!v.hasBounds(idx)) {
continue;
}
writeBoundsSymbol(out, name,
v.getExternalName() + "_mfront_index_" +
std::to_string(idx) + "_",
"", v.getBounds(idx));
if (!v.hasPhysicalBounds(idx)) {
continue;
}
writeBoundsSymbol(out, name,
v.getExternalName() + "_mfront_index_" +
std::to_string(idx) + "_",
"Physical", v.getPhysicalBounds(idx));
}
}
}
out.precision(prec);
} // end of writeVariablesBoundsSymbols
void writeFileDescriptionSymbols(std::ostream& os,
const std::string_view n,
const FileDescription& fd) {
auto description = [&fd] {
auto rtrim = [](const std::string_view s) {
auto end = s.find_last_not_of(" \n\r\t\f\v");
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
};
auto transform = [rtrim](const std::string_view s) {
return tfel::utilities::replace_all(rtrim(s), "\\", "\\\\");
};
auto r = std::string{};
auto lines = std::vector<std::string>{};
std::istringstream d(fd.description);
for (std::string line; std::getline(d, line);) {
if (!r.empty()) {
r += "\\n";
}
if (tfel::utilities::starts_with(line, "* ")) {
r += transform(line.substr(2));
} else {
r += transform(rtrim(line));
}
}
return r;
}();
exportStringSymbol(os, std::string{n} + "_author", fd.authorName);
exportStringSymbol(os, std::string{n} + "_date", fd.date);
exportStringSymbol(os, std::string{n} + "_description", description);
} // end of writeFileDescriptionSymbols
void writeValidatorSymbol(std::ostream& os,
const std::string_view n,
const MaterialKnowledgeDescription& d) {
const auto validator = d.getAttribute<std::string>(
MaterialKnowledgeDescription::validator, "");
exportStringSymbol(os, std::string{n} + "_validator", validator);
} // end of writeValidatorSymbols
void writeBuildIdentifierSymbol(std::ostream& os,
const std::string_view n,
const MaterialKnowledgeDescription& d) {
const auto build_id = [&d]() -> std::string {
const auto* const bid = std::getenv("TFEL_BUILD_ID");
if (bid != nullptr) {
return bid;
}
return d.getAttribute<std::string>(
MaterialKnowledgeDescription::buildIdentifier, "");
}();
exportStringSymbol(os, std::string{n} + "_build_id", build_id);
} // end of writeBuildIdentifierSymbols
void writeEntryPointSymbol(std::ostream& out, const std::string_view n) {
writeEntryPointSymbol(out, n, n);
} // end of writeEntryPointSymbols
void writeEntryPointSymbol(std::ostream& out,
const std::string_view n,
const std::string_view n2) {
exportStringSymbol(out, std::string{n} + "_mfront_ept", n2);
} // end of writeEntryPointSymbols
void writeTFELVersionSymbol(std::ostream& out, const std::string_view n) {
exportStringSymbol(out, std::string{n} + "_tfel_version",
::getTFELVersion());
} // end of writeTFELVersionSymbol
void writeUnitSystemSymbol(std::ostream& out,
const std::string_view n,
const MaterialKnowledgeDescription& d) {
if (d.hasUnitSystem()) {
exportStringSymbol(out, std::string{n} + "_unit_system",
d.getUnitSystem());
} else {
exportStringSymbol(out, std::string{n} + "_unit_system", "");
}
} // end of writeUnitSystemSymbol
void writeInterfaceSymbol(std::ostream& out,
const std::string_view n,
const std::string_view i) {
exportStringSymbol(out, std::string{n} + "_mfront_interface", i);
} // end of writeInterfaceSymbol
void writeLawSymbol(std::ostream& out,
const std::string_view n,
const std::string_view l) {
exportStringSymbol(out, std::string{n} + "_mfront_law", l);
} // end of writeLawSymbol
void writeMaterialSymbol(std::ostream& out,
const std::string_view n,
const std::string_view m) {
if (!m.empty()) {
exportStringSymbol(out, std::string{n} + "_mfront_material", m);
}
} // end of writeMaterialSymbol
void writeMaterialKnowledgeTypeSymbol(std::ostream& out,
const std::string_view n,
const MaterialKnowledgeType& t) {
const auto s = [&t] {
if (t == MaterialKnowledgeType::MATERIALPROPERTY) {
return 0;
} else if (t == MaterialKnowledgeType::BEHAVIOUR) {
return 1;
} else if (t == MaterialKnowledgeType::MODEL) {
return 2;
} else {
tfel::raise(
"writeMaterialKnowledgeTypeSymbol: "
"internal error, (unsupported material knowledge type)");
}
}();
exportUnsignedShortSymbol(out, std::string{n} + "_mfront_mkt", s);
} // end of writeMaterialKnowledgeTypeSymbol
void writeMaterialLaws(std::ostream& os,
const std::vector<std::string>& materialLaws) {
for (const auto& l : materialLaws) {
os << "using mfront::" << l << ";\n"
<< "using mfront::" << l << "_checkBounds;\n";
}
} // end of writeMaterialLaws
void writeStaticVariables(std::ostream& os,
const StaticVariableDescriptionContainer& vc,
const std::string_view f) {
if (vc.empty()) {
return;
}
for (const auto& v : vc) {
if (getDebugMode()) {
printLinePragma(os, v.lineNumber, f);
}
if (v.type == "short") {
os << "static " << constexpr_c << " short " << v.name << " = "
<< static_cast<short>(v.value) << ";\n";
} else if (v.type == "ushort") {
os << "static " << constexpr_c << " unsigned short " << v.name << " = "
<< static_cast<unsigned short>(v.value) << ";\n";
} else if (v.type == "int") {
os << "static " << constexpr_c << " int " << v.name << " = "
<< static_cast<int>(v.value) << ";\n";
} else if (v.type == "uint") {
os << "static " << constexpr_c << " unsigned int " << v.name << " = "
<< static_cast<unsigned int>(v.value) << ";\n";
} else if (v.type == "long") {
os << "static " << constexpr_c << " long " << v.name << " = "
<< static_cast<long>(v.value) << ";\n";
} else if (v.type == "ulong") {
os << "static " << constexpr_c << " unsigned long " << v.name << " = "
<< static_cast<unsigned long>(v.value) << ";\n";
} else if (v.type == "float") {
os << "static " << constexpr_c << " float " << v.name << " = "
<< static_cast<float>(v.value) << ";\n";
} else if ((v.type == "double") || (v.type == "real")) {
os << "static " << constexpr_c << " double " << v.name << " = "
<< static_cast<double>(v.value) << ";\n";
} else if (v.type == "ldouble") {
os << "static " << constexpr_c << " long double " << v.name << " = "
<< static_cast<long double>(v.value) << ";\n";
} else {
std::ostringstream msg;
msg << "writeStaticVariables: "
<< "type '" + v.type << "' of variable '" << v.name
<< "' defined at line '" << v.lineNumber << "' "
<< "is not a supported type for a static variable."
<< "Supported types are short, ushort, int, uint, long, ulong,"
<< "float, double and ldouble.";
tfel::raise(msg.str());
}
}
os << '\n';
} // end of writeStaticVariables
void writeF77FUNCMacros(std::ostream& f) {
auto def = [&f] {
f << "#ifndef F77_FUNC\n"
<< "#define F77_FUNC(X,Y) X##_\n"
<< "#endif\n"
<< "#ifndef F77_FUNC_\n"
<< "#define F77_FUNC_(X,Y) X##_\n"
<< "#endif\n";
};
f << "#if (defined GNU_FORTRAN_COMPILER)\n";
def();
f << "#elif (defined INTEL_FORTRAN_COMPILER)\n"
<< "#ifdef _WIN32\n"
<< "#ifndef F77_FUNC\n"
<< "#define F77_FUNC(X,Y) Y\n"
<< "#endif\n"
<< "#ifndef F77_FUNC_\n"
<< "#define F77_FUNC_(X,Y) Y\n"
<< "#endif\n"
<< "#else\n";
def();
f << "#endif /* _WIN32 */\n"
<< "#else\n";
def();
f << "#endif\n\n";
} // end of writeF77FuncMacros
void writeExportDirectives(std::ostream& os, const bool b) {
os << "#ifdef _WIN32\n"
<< "#ifndef NOMINMAX\n"
<< "#define NOMINMAX\n"
<< "#endif /* NOMINMAX */\n"
<< "#include <windows.h>\n"
<< "#ifdef small\n"
<< "#undef small\n"
<< "#endif /* small */\n"
<< "#endif /* _WIN32 */\n\n";
if (b) {
os << "#ifdef _WIN32\n"
<< "#ifndef MFRONT_SHAREDOBJ\n"
<< "#ifdef MFRONT_COMPILING\n"
<< "#define MFRONT_SHAREDOBJ __declspec(dllexport)\n"
<< "#else /* MFRONT_COMPILING */\n"
<< "#define MFRONT_SHAREDOBJ __declspec(dllimport)\n"
<< "#endif /* MFRONT_COMPILING */\n"
<< "#endif /* MFRONT_SHAREDOBJ */\n"
<< "#else\n"
<< "#ifndef MFRONT_SHAREDOBJ\n"
<< "#ifdef __GNUC__\n"
<< "#define MFRONT_SHAREDOBJ "
<< "__attribute__((visibility(\"default\")))\n"
<< "#else\n"
<< "#define MFRONT_SHAREDOBJ\n"
<< "#endif /* __GNUC__ */\n"
<< "#endif /* MFRONT_SHAREDOBJ */\n"
<< "#endif /* _WIN32 */\n\n";
} else {
os << "#ifndef MFRONT_SHAREDOBJ\n"
<< "#define MFRONT_SHAREDOBJ TFEL_VISIBILITY_EXPORT\n"
<< "#endif /* MFRONT_SHAREDOBJ */\n\n";
}
os << "#ifndef MFRONT_EXPORT_SYMBOL\n"
<< "#define MFRONT_EXPORT_SYMBOL(TYPE, NAME, VALUE) \\\n"
<< " MFRONT_SHAREDOBJ extern TYPE NAME; \\\n"
<< " MFRONT_SHAREDOBJ TYPE NAME = VALUE\n"
<< "#endif /* MFRONT_EXPORT_SYMBOL*/\n\n"
<< "#ifndef MFRONT_EXPORT_ARRAY_ARGUMENTS\n"
<< "#define MFRONT_EXPORT_ARRAY_ARGUMENTS(...) __VA_ARGS__\n"
<< "#endif /* MFRONT_EXPORT_ARRAY_ARGUMENTS */\n\n"
<< "#ifndef MFRONT_EXPORT_ARRAY_OF_SYMBOLS\n"
<< "#define MFRONT_EXPORT_ARRAY_OF_SYMBOLS(TYPE, NAME, SIZE, VALUE) \\\n"
<< " MFRONT_SHAREDOBJ extern TYPE NAME[SIZE]; \\\n"
<< " MFRONT_SHAREDOBJ TYPE NAME[SIZE] = {VALUE}\n"
<< "#endif /* MFRONT_EXPORT_ARRAY_OF_SYMBOLS*/\n\n";
} // end of writeExportDirectives
static void writeBoundsChecks(std::ostream& os,
const VariableDescription& v,
const std::string_view n,
const std::string_view space_dimension,
const std::string_view policy,
const bool addThis,
const bool checkEndOfTimeStepValue,
const bool physicalBounds) {
if (physicalBounds) {
if (!v.hasPhysicalBounds()) {
return;
}
} else {
if (!v.hasBounds()) {
return;
}
}
const auto& bounds = physicalBounds ? v.getPhysicalBounds() : v.getBounds();
const auto numeric_type = [&v] {
if (v.isScalar()) {
return v.type;
}
return "tfel::math::numeric_type<" + v.type + ">";
}();
const auto this_pointer = addThis ? "this->" : "";
const auto policy_argument = [&physicalBounds, &policy]() -> std::string {
if (!physicalBounds) {
return ", " + std::string{policy};
}
return "";
}();
if (bounds.boundsType == VariableBoundsDescription::LOWER) {
os << "tfel::material::BoundsCheck<" << space_dimension
<< ">::lowerBoundCheck(\"" << n << "\", " << this_pointer << n << ","
<< "static_cast<" << numeric_type << ">(" << bounds.lowerBound << ")"
<< policy_argument << ");\n";
if (checkEndOfTimeStepValue) {
os << "tfel::material::BoundsCheck<" << space_dimension
<< ">::lowerBoundCheck(\"" << n << "+d" << n << "\", "
<< this_pointer << n << "+this->d" << n << ","
<< "static_cast<" << numeric_type << ">(" << bounds.lowerBound << ")"
<< policy_argument << ");\n";
}
} else if (bounds.boundsType == VariableBoundsDescription::UPPER) {
os << "tfel::material::BoundsCheck<" << space_dimension
<< ">::upperBoundCheck(\"" << n << "\", " << this_pointer << n << ","
<< "static_cast<" << numeric_type << ">(" << bounds.upperBound << ")"
<< policy_argument << ");\n";
if (checkEndOfTimeStepValue) {
os << "tfel::material::BoundsCheck<" << space_dimension
<< ">::upperBoundCheck(\"" << n << "+d" << n << "\", "
<< this_pointer << n << "+this->d" << n << ","
<< "static_cast<" << numeric_type << ">(" << bounds.upperBound << ")"
<< policy_argument << ");\n";
}
} else if (bounds.boundsType == VariableBoundsDescription::LOWERANDUPPER) {
os << "tfel::material::BoundsCheck<" << space_dimension
<< ">::lowerAndUpperBoundsChecks(\"" << n << "\", " << this_pointer
<< n << ","
<< "static_cast<" << numeric_type << ">(" << bounds.lowerBound << "),"
<< "static_cast<" << numeric_type << ">(" << bounds.upperBound << ")"
<< policy_argument << ");\n";
if (checkEndOfTimeStepValue) {
os << "tfel::material::BoundsCheck<" << space_dimension
<< ">::lowerAndUpperBoundsChecks(\"" << n << "+d" << n << "\", "
<< this_pointer << n << "+this->d" << n << ","
<< "static_cast<" << numeric_type << ">(" << bounds.lowerBound
<< "),"
<< "static_cast<" << numeric_type << ">(" << bounds.upperBound << ")"
<< policy_argument << ");\n";
}
} else {
tfel::raise(
"writeBoundsChecks: internal error (unsupported bounds type)");
}
}
void writeBoundsChecks(std::ostream& os,
const VariableDescription& v,
const std::string_view n,
const std::string_view space_dimension,
const std::string_view policy,
const bool addThis,
const bool checkEndOfTimeStepValue) {
writeBoundsChecks(os, v, n, space_dimension, policy, addThis,
checkEndOfTimeStepValue, false);
} // end of writeBoundsChecks
void writePhysicalBoundsChecks(std::ostream& os,
const VariableDescription& v,
const std::string_view n,
const std::string_view space_dimension,
const bool addThis,
const bool checkEndOfTimeStepValue) {
writeBoundsChecks(os, v, n, space_dimension, "", addThis,
checkEndOfTimeStepValue, true);
} // end of writePhysicalBoundsChecks
std::string printLinePragma(const std::size_t ln, std::string_view f) {
if (ln == 0) {
return {};
}
#if (defined _WIN32 || defined _WIN64) && (!defined __CYGWIN__)
return "#line " + std::to_string(ln) + " \"" +
tfel::utilities::replace_all(f, "\\", "\\\\") + "\"\n";
#else
return "#line " + std::to_string(ln) + " \"" + std::string{f} + "\"\n";
#endif
}
void printLinePragma(std::ostream& os,
const std::size_t ln,
std::string_view f) {
if (ln == 0) {
return;
}
#if (defined _WIN32 || defined _WIN64) && (!defined __CYGWIN__)
os << "#line " << ln << " \""
<< tfel::utilities::replace_all(f, "\\", "\\\\") << "\"\n";
#else
os << "#line " << ln << " \"" << f << "\"\n";
#endif
}
} // end of namespace mfront