-
Notifications
You must be signed in to change notification settings - Fork 593
/
Gen.cpp
3316 lines (2949 loc) · 101 KB
/
Gen.cpp
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#include "Gen.h"
#include "../Ice/FileUtil.h"
#include "../Slice/FileTracker.h"
#include "../Slice/Util.h"
#include "CPlusPlusUtil.h"
#include "Ice/StringUtil.h"
#include <algorithm>
#include <cassert>
#include <limits>
#include <string.h>
using namespace std;
using namespace Slice;
using namespace IceInternal;
namespace
{
bool isConstexprType(const TypePtr& type)
{
BuiltinPtr bp = dynamic_pointer_cast<Builtin>(type);
if (bp)
{
switch (bp->kind())
{
case Builtin::KindByte:
case Builtin::KindBool:
case Builtin::KindShort:
case Builtin::KindInt:
case Builtin::KindLong:
case Builtin::KindFloat:
case Builtin::KindDouble:
case Builtin::KindValue:
case Builtin::KindObject:
case Builtin::KindObjectProxy:
{
return true;
}
default:
{
return false;
}
}
}
else if (
dynamic_pointer_cast<Enum>(type) || dynamic_pointer_cast<InterfaceDecl>(type) ||
dynamic_pointer_cast<ClassDecl>(type))
{
return true;
}
else
{
StructPtr s = dynamic_pointer_cast<Struct>(type);
if (s)
{
DataMemberList members = s->dataMembers();
for (DataMemberList::const_iterator i = members.begin(); i != members.end(); ++i)
{
if (!isConstexprType((*i)->type()))
{
return false;
}
}
return true;
}
return false;
}
}
string getDeprecatedAttribute(const ContainedPtr& p1)
{
string deprecatedAttribute;
if (p1->isDeprecated())
{
if (auto reason = p1->getDeprecationReason())
{
deprecatedAttribute = "[[deprecated(\"" + *reason + "\")]] ";
}
else
{
deprecatedAttribute = "[[deprecated]] ";
}
}
return deprecatedAttribute;
}
void writeConstantValue(
IceInternal::Output& out,
const TypePtr& type,
const SyntaxTreeBasePtr& valueType,
const string& value,
TypeContext typeContext,
const MetadataList& metadata,
const string& scope)
{
ConstPtr constant = dynamic_pointer_cast<Const>(valueType);
if (constant)
{
out << getUnqualified(fixKwd(constant->scoped()), scope);
}
else
{
BuiltinPtr bp = dynamic_pointer_cast<Builtin>(type);
if (bp && bp->kind() == Builtin::KindString)
{
if ((typeContext & TypeContext::UseWstring) != TypeContext::None ||
findMetadata(metadata) == "wstring") // wide strings
{
out << "L\"";
out << toStringLiteral(value, "\a\b\f\n\r\t\v", "?", UCN, 0);
out << "\"";
}
else // narrow strings
{
out << "\"" << toStringLiteral(value, "\a\b\f\n\r\t\v", "?", Octal, 0) << "\"";
}
}
else if (bp && bp->kind() == Builtin::KindLong)
{
out << "INT64_C(" << value << ")";
}
else if (bp && bp->kind() == Builtin::KindFloat)
{
out << value;
if (value.find(".") == string::npos)
{
out << ".0";
}
out << "F";
}
else
{
EnumPtr ep = dynamic_pointer_cast<Enum>(type);
if (ep && valueType)
{
EnumeratorPtr enumerator = dynamic_pointer_cast<Enumerator>(valueType);
assert(enumerator);
bool unscoped = findMetadata(ep->getMetadata()) == "%unscoped";
if (unscoped)
{
out << getUnqualified(fixKwd(ep->scope() + enumerator->name()), scope);
}
else
{
out << getUnqualified(fixKwd(enumerator->scoped()), scope);
}
}
else if (!ep)
{
out << value;
}
}
}
}
string toDllClassExport(const string& dllExport)
{
if (!dllExport.empty())
{
return "ICE_CLASS(" + dllExport.substr(0, dllExport.size() - 1) + ") ";
}
else
{
return "";
}
}
string toDllMemberExport(const string& dllExport)
{
if (!dllExport.empty())
{
return "ICE_MEMBER(" + dllExport.substr(0, dllExport.size() - 1) + ") ";
}
else
{
return "";
}
}
// Marshals the parameters of an outgoing request.
void writeInParamsLambda(
IceInternal::Output& C,
const OperationPtr& p,
const ParamDeclList& inParams,
const string& scope)
{
if (inParams.empty())
{
C << "nullptr";
}
else
{
C << "[&](" << getUnqualified("::Ice::OutputStream*", scope) << " ostr)";
C << sb;
writeMarshalCode(C, inParams, nullptr);
if (p->sendsClasses())
{
C << nl << "ostr->writePendingValues();";
}
C << eb;
}
}
void throwUserExceptionLambda(IceInternal::Output& C, ExceptionList throws, const string& scope)
{
if (throws.empty())
{
C << "nullptr";
}
else
{
throws.sort();
throws.unique();
//
// Arrange exceptions into most-derived to least-derived order. If we don't
// do this, a base exception handler can appear before a derived exception
// handler, causing compiler warnings and resulting in the base exception
// being marshaled instead of the derived exception.
//
throws.sort(Slice::DerivedToBaseCompare());
C << "[](const " << getUnqualified("::Ice::UserException&", scope) << " ex)";
C << sb;
C << nl << "try";
C << sb;
C << nl << "ex.ice_throw();";
C << eb;
//
// Generate a catch block for each legal user exception.
//
for (const auto& ex : throws)
{
C << nl << "catch(const " << getUnqualified(fixKwd(ex->scoped()), scope) << "&)";
C << sb;
C << nl << "throw;";
C << eb;
}
C << nl << "catch(const " << getUnqualified("::Ice::UserException&", scope) << ")";
C << sb;
C << eb;
C << eb;
}
}
string marshaledResultStructName(const string& name)
{
assert(!name.empty());
string stName = IceInternal::toUpper(name.substr(0, 1)) + name.substr(1);
stName += "MarshaledResult";
return stName;
}
string condMove(bool moveIt, const string& str) { return moveIt ? string("::std::move(") + str + ")" : str; }
string escapeParam(const ParamDeclList& params, const string& name)
{
string r = name;
for (const auto& param : params)
{
if (fixKwd(param->name()) == name)
{
r = name + "_";
break;
}
}
return r;
}
/// Returns a doxygen formatted link to the provided Slice identifier.
string cppLinkFormatter(string identifier, string memberComponent)
{
string result = "{@link ";
if (!identifier.empty())
{
result += fixKwd(identifier);
}
if (!memberComponent.empty())
{
result += "#" + fixKwd(memberComponent);
}
return result + "}";
}
void writeDocLines(Output& out, const StringList& lines, bool commentFirst, const string& space = " ")
{
auto l = lines.cbegin();
if (!commentFirst)
{
assert(l != lines.cend());
out << *l;
l++;
}
for (; l != lines.cend(); ++l)
{
out << nl << " *";
if (!l->empty())
{
out << space << *l;
}
}
}
void writeSeeAlso(Output& out, const StringList& lines, const string& space = " ")
{
for (const string& line : lines)
{
out << nl << " *";
if (!line.empty())
{
out << space << "@see " << line;
}
}
}
string getDocSentence(const StringList& lines)
{
//
// Extract the first sentence.
//
ostringstream ostr;
for (StringList::const_iterator i = lines.begin(); i != lines.end(); ++i)
{
const string ws = " \t";
if (i->empty())
{
break;
}
if (i != lines.begin() && i->find_first_not_of(ws) == 0)
{
ostr << " ";
}
string::size_type pos = i->find('.');
if (pos == string::npos)
{
ostr << *i;
}
else if (pos == i->size() - 1)
{
ostr << *i;
break;
}
else
{
//
// Assume a period followed by whitespace indicates the end of the sentence.
//
while (pos != string::npos)
{
if (ws.find((*i)[pos + 1]) != string::npos)
{
break;
}
pos = i->find('.', pos + 1);
}
if (pos != string::npos)
{
ostr << i->substr(0, pos + 1);
break;
}
else
{
ostr << *i;
}
}
}
return ostr.str();
}
enum class GenerateDeprecated
{
Yes,
No
};
void
writeDocSummary(Output& out, const ContainedPtr& p, GenerateDeprecated generateDeprecated = GenerateDeprecated::Yes)
{
if (p->comment().empty())
{
return;
}
CommentPtr doc = p->parseComment(cppLinkFormatter);
out << nl << "/**";
if (!doc->overview().empty())
{
writeDocLines(out, doc->overview(), true);
}
if (!doc->misc().empty())
{
writeDocLines(out, doc->misc(), true);
}
if (!doc->seeAlso().empty())
{
writeSeeAlso(out, doc->seeAlso());
}
if (generateDeprecated == GenerateDeprecated::Yes)
{
if (!doc->deprecated().empty())
{
out << nl << " *";
out << nl << " * @deprecated ";
writeDocLines(out, doc->deprecated(), false);
}
else if (doc->isDeprecated())
{
out << nl << " *";
out << nl << " * @deprecated";
}
}
if (dynamic_pointer_cast<ClassDef>(p) || dynamic_pointer_cast<ClassDecl>(p) ||
dynamic_pointer_cast<Struct>(p) || dynamic_pointer_cast<Slice::Exception>(p))
{
UnitPtr unt = p->container()->unit();
string file = p->file();
assert(!file.empty());
DefinitionContextPtr dc = unt->findDefinitionContext(file);
assert(dc);
// TODO: why do we ignore all instances of this metadata except the first?
if (auto headerFile = dc->getMetadataArgs("cpp:doxygen:include"))
{
out << nl << " * \\headerfile " << *headerFile;
}
}
out << nl << " */";
}
enum OpDocParamType
{
OpDocInParams,
OpDocOutParams,
OpDocAllParams
};
void writeOpDocParams(
Output& out,
const OperationPtr& op,
const CommentPtr& doc,
OpDocParamType type,
const StringList& preParams = StringList(),
const StringList& postParams = StringList())
{
ParamDeclList params;
switch (type)
{
case OpDocInParams:
params = op->inParameters();
break;
case OpDocOutParams:
params = op->outParameters();
break;
case OpDocAllParams:
params = op->parameters();
break;
}
if (!preParams.empty())
{
writeDocLines(out, preParams, true);
}
map<string, StringList> paramDoc = doc->parameters();
for (const auto& param : params)
{
map<string, StringList>::iterator q = paramDoc.find(param->name());
if (q != paramDoc.end())
{
out << nl << " * @param " << fixKwd(q->first) << " ";
writeDocLines(out, q->second, false);
}
}
if (!postParams.empty())
{
writeDocLines(out, postParams, true);
}
}
void writeOpDocExceptions(Output& out, const OperationPtr& op, const CommentPtr& doc)
{
for (const auto& [name, lines] : doc->exceptions())
{
string scopedName = name;
// Try to locate the exception's definition using the name given in the comment.
ExceptionPtr ex = op->container()->lookupException(name, false);
if (ex)
{
scopedName = ex->scoped().substr(2);
}
out << nl << " * @throws " << scopedName << " ";
writeDocLines(out, lines, false);
}
}
void writeOpDocSummary(
Output& out,
const OperationPtr& op,
const CommentPtr& doc,
OpDocParamType type,
bool showExceptions,
GenerateDeprecated generateDeprecated = GenerateDeprecated::Yes,
const StringList& preParams = StringList(),
const StringList& postParams = StringList(),
const StringList& returns = StringList())
{
out << nl << "/**";
const auto& overview = doc->overview();
if (!overview.empty())
{
writeDocLines(out, overview, true);
}
writeOpDocParams(out, op, doc, type, preParams, postParams);
if (!returns.empty())
{
out << nl << " * @return ";
writeDocLines(out, returns, false);
}
if (showExceptions)
{
writeOpDocExceptions(out, op, doc);
}
const auto& misc = doc->misc();
if (!misc.empty())
{
writeDocLines(out, misc, true);
}
const auto& seeAlso = doc->seeAlso();
if (!seeAlso.empty())
{
writeSeeAlso(out, seeAlso);
}
if (generateDeprecated == GenerateDeprecated::Yes)
{
const auto& deprecated = doc->deprecated();
if (!deprecated.empty())
{
out << nl << " *";
out << nl << " * @deprecated ";
writeDocLines(out, deprecated, false);
}
else if (doc->isDeprecated())
{
out << nl << " *";
out << nl << " * @deprecated";
}
}
// we don't generate the @deprecated doc-comment for servants
out << nl << " */";
}
// Returns the client-side result type for an operation - can be void, a single type, or a tuple.
string createOutgoingAsyncTypeParam(const vector<string>& elements)
{
switch (elements.size())
{
case 0:
return "void";
case 1:
return elements.front();
default:
{
ostringstream os;
Output out(os);
out << "::std::tuple" << sabrk;
for (const auto& element : elements)
{
out << element;
}
out << eabrk;
return os.str();
}
}
}
// Returns the C++ types that make up the client-side result type of an operation (first return type then out
// parameter types, as per the future API).
vector<string> createOutgoingAsyncParams(const OperationPtr& p, const string& scope, TypeContext typeContext)
{
vector<string> elements;
TypePtr ret = p->returnType();
if (ret)
{
elements.push_back(typeToString(ret, p->returnIsOptional(), scope, p->getMetadata(), typeContext));
}
for (const auto& param : p->outParameters())
{
elements.push_back(
typeToString(param->type(), param->optional(), scope, param->getMetadata(), typeContext));
}
return elements;
}
string createLambdaResponse(const OperationPtr& p, TypeContext typeContext)
{
ostringstream os;
Output out(os);
out << "::std::function<void" << spar << createOutgoingAsyncParams(p, "", typeContext) << epar << ">";
return os.str();
}
}
Slice::Gen::Gen(
const string& base,
const string& headerExtension,
const string& sourceExtension,
const vector<string>& extraHeaders,
const string& include,
const vector<string>& includePaths,
const string& dllExport,
const string& dir)
: _base(base),
_headerExtension(headerExtension),
_sourceExtension(sourceExtension),
_extraHeaders(extraHeaders),
_include(include),
_includePaths(includePaths),
_dllExport(dllExport),
_dir(dir)
{
for (vector<string>::iterator p = _includePaths.begin(); p != _includePaths.end(); ++p)
{
*p = fullPath(*p);
}
string::size_type pos = _base.find_last_of("/\\");
if (pos != string::npos)
{
_base.erase(0, pos + 1);
}
}
Slice::Gen::~Gen()
{
H << "\n\n#include <Ice/PopDisableWarnings.h>";
H << "\n#endif\n";
C << '\n';
H.close();
C.close();
}
void
Slice::Gen::generate(const UnitPtr& p)
{
string file = p->topLevelFile();
DefinitionContextPtr dc = p->findDefinitionContext(file);
assert(dc);
//
// Give precedence to header-ext/source-ext file metadata.
//
string headerExtension = getHeaderExt(file, p);
if (!headerExtension.empty())
{
_headerExtension = headerExtension;
}
string sourceExtension = getSourceExt(file, p);
if (!sourceExtension.empty())
{
_sourceExtension = sourceExtension;
}
//
// Give precedence to --dll-export command-line option
//
if (_dllExport.empty())
{
if (auto dllExport = dc->getMetadataArgs("cpp:dll-export"))
{
_dllExport = *dllExport;
}
}
string fileH = _base + "." + _headerExtension;
string fileC = _base + "." + _sourceExtension;
if (!_dir.empty())
{
fileH = _dir + '/' + fileH;
fileC = _dir + '/' + fileC;
}
H.open(fileH.c_str());
if (!H)
{
ostringstream os;
os << "cannot open `" << fileH << "': " << IceInternal::errorToString(errno);
throw FileException(__FILE__, __LINE__, os.str());
}
FileTracker::instance()->addFile(fileH);
C.open(fileC.c_str());
if (!C)
{
ostringstream os;
os << "cannot open `" << fileC << "': " << IceInternal::errorToString(errno);
throw FileException(__FILE__, __LINE__, os.str());
}
FileTracker::instance()->addFile(fileC);
printHeader(H);
printGeneratedHeader(H, _base + ".ice");
printHeader(C);
printGeneratedHeader(C, _base + ".ice");
string s = _base + "." + _headerExtension;
if (_include.size())
{
s = _include + '/' + s;
}
transform(s.begin(), s.end(), s.begin(), ToIfdef());
H << "\n#ifndef __" << s << "__";
H << "\n#define __" << s << "__";
H << '\n';
validateMetadata(p);
writeExtraHeaders(C);
if (_dllExport.size())
{
C << "\n#ifndef " << _dllExport << "_EXPORTS";
C << "\n# define " << _dllExport << "_EXPORTS";
C << "\n#endif";
}
C << "\n#define ICE_BUILDING_GENERATED_CODE";
C << "\n#include \"";
if (_include.size())
{
C << _include << '/';
}
C << _base << "." << _headerExtension << "\"";
H << "\n#include <Ice/PushDisableWarnings.h>";
if (!dc->hasMetadata("cpp:no-default-include"))
{
H << "\n#include <Ice/Ice.h>";
}
for (const string& includeFile : p->includeFiles())
{
string extension = getHeaderExt(includeFile, p);
if (extension.empty())
{
extension = _headerExtension;
}
if (isAbsolutePath(includeFile))
{
// This means mcpp found the .ice file in its -I paths. So we generate an angled include for the C++ header.
H << "\n#include <" << changeInclude(includeFile, _includePaths) << "." << extension << ">";
}
else
{
H << "\n#include \"" << removeExtension(includeFile) << "." << extension << "\"";
}
}
// Emit #include statements for any 'cpp:include' metadata directives in the top-level Slice file.
{
MetadataList fileMetadata = dc->getMetadata();
for (MetadataList::const_iterator q = fileMetadata.begin(); q != fileMetadata.end();)
{
MetadataPtr metadata = *q++;
string_view directive = metadata->directive();
string_view arguments = metadata->arguments();
if (directive == "cpp:include")
{
if (!arguments.empty())
{
H << nl << "#include <" << arguments << ">";
}
else
{
ostringstream ostr;
ostr << "ignoring invalid file metadata '" << *metadata << "'";
p->warning(metadata->file(), metadata->line(), InvalidMetadata, ostr.str());
fileMetadata.remove(metadata);
}
}
else if (directive == "cpp:source-include")
{
if (!arguments.empty())
{
C << nl << "#include <" << arguments << ">";
}
else
{
ostringstream ostr;
ostr << "ignoring invalid file metadata '" << *metadata << "'";
p->warning(metadata->file(), metadata->line(), InvalidMetadata, ostr.str());
fileMetadata.remove(metadata);
}
}
}
dc->setMetadata(std::move(fileMetadata));
}
if (!dc->hasMetadata("cpp:no-default-include"))
{
// For simplicity, we include these extra headers all the time.
C << "\n#include <Ice/AsyncResponseHandler.h>"; // for async dispatches
C << "\n#include <Ice/FactoryTable.h>"; // for class and exception factories
C << "\n#include <Ice/OutgoingAsync.h>"; // for proxies
}
//
// Disable shadow and deprecation warnings in .cpp file
//
C << sp;
C.zeroIndent();
C << nl << "#if defined(_MSC_VER)";
C << nl << "# pragma warning(disable : 4458) // declaration of ... hides class member";
C << nl << "# pragma warning(disable : 4996) // ... was declared deprecated";
C << nl << "#elif defined(__clang__)";
C << nl << "# pragma clang diagnostic ignored \"-Wshadow\"";
C << nl << "# pragma clang diagnostic ignored \"-Wdeprecated-declarations\"";
C << nl << "#elif defined(__GNUC__)";
C << nl << "# pragma GCC diagnostic ignored \"-Wshadow\"";
C << nl << "# pragma GCC diagnostic ignored \"-Wdeprecated-declarations\"";
C << nl << "#endif";
printVersionCheck(H);
printVersionCheck(C);
printDllExportStuff(H, _dllExport);
if (_dllExport.size())
{
_dllExport += " ";
}
{
ForwardDeclVisitor forwardDeclVisitor(H);
p->visit(&forwardDeclVisitor);
DefaultFactoryVisitor defaultFactoryVisitor(C);
p->visit(&defaultFactoryVisitor);
ProxyVisitor proxyVisitor(H, C, _dllExport);
p->visit(&proxyVisitor);
DataDefVisitor dataDefVisitor(H, C, _dllExport);
p->visit(&dataDefVisitor);
InterfaceVisitor interfaceVisitor(H, C, _dllExport);
p->visit(&interfaceVisitor);
if (!dc->hasMetadata("cpp:no-stream"))
{
StreamVisitor streamVisitor(H);
p->visit(&streamVisitor);
}
}
}
void
Slice::Gen::writeExtraHeaders(IceInternal::Output& out)
{
for (string header : _extraHeaders)
{
string guard;
string::size_type pos = header.rfind(',');
if (pos != string::npos)
{
header = header.substr(0, pos);
guard = header.substr(pos + 1);
}
if (!guard.empty())
{
out << "\n#ifndef " << guard;
out << "\n#define " << guard;
}
out << "\n#include <" << header << '>';
if (!guard.empty())
{
out << "\n#endif";
}
}
}
void
Slice::Gen::validateMetadata(const UnitPtr& u)
{
MetadataVisitor visitor;
u->visit(&visitor);
}
bool
Slice::Gen::MetadataVisitor::visitUnitStart(const UnitPtr& unit)
{
// Validate file metadata in the top-level file and all included files.
for (const string& file : unit->allFiles())
{
DefinitionContextPtr dc = unit->findDefinitionContext(file);
MetadataList fileMetadata = dc->getMetadata();
assert(dc);
bool seenHeaderExtension = false;
bool seenSourceExtension = false;
bool seenDllExport = false;
for (MetadataList::const_iterator r = fileMetadata.begin(); r != fileMetadata.end();)
{
MetadataPtr s = *r++;
string_view directive = s->directive();
string_view arguments = s->arguments();
if (directive.find("cpp:") == 0)
{
static const string cppIncludePrefix = "cpp:include";
static const string cppNoDefaultInclude = "cpp:no-default-include";
static const string cppNoStream = "cpp:no-stream";
static const string cppSourceIncludePrefix = "cpp:source-include";
static const string cppHeaderExtPrefix = "cpp:header-ext";
static const string cppSourceExtPrefix = "cpp:source-ext";
static const string cppDllExportPrefix = "cpp:dll-export";
static const string cppDoxygenIncludePrefix = "cpp:doxygen";
if (directive == cppNoDefaultInclude || directive == cppNoStream)
{
continue;
}
else if (directive == cppIncludePrefix && !arguments.empty())
{
continue;
}
else if (directive == cppSourceIncludePrefix && !arguments.empty())
{
continue;
}
else if (directive == cppHeaderExtPrefix && !arguments.empty())
{
if (seenHeaderExtension)
{
ostringstream ostr;
ostr << "ignoring invalid file metadata '" << *s
<< "': directive can appear only once per file";
unit->warning(s->file(), s->line(), InvalidMetadata, ostr.str());
fileMetadata.remove(s);
}
seenHeaderExtension = true;
continue;
}
else if (directive == cppSourceExtPrefix && !arguments.empty())
{
if (seenSourceExtension)
{
ostringstream ostr;
ostr << "ignoring invalid file metadata '" << *s
<< "': directive can appear only once per file";
unit->warning(s->file(), s->line(), InvalidMetadata, ostr.str());
fileMetadata.remove(s);
}
seenSourceExtension = true;
continue;
}
else if (directive == cppDllExportPrefix && !arguments.empty())
{
if (seenDllExport)
{
ostringstream ostr;
ostr << "ignoring invalid file metadata '" << *s
<< "': directive can appear only once per file";
unit->warning(s->file(), s->line(), InvalidMetadata, ostr.str());
fileMetadata.remove(s);
}
seenDllExport = true;
continue;
}
else if (directive == cppDoxygenIncludePrefix && arguments.find("include:") == 0)
{
continue;
}