forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREPL.cpp
1196 lines (1013 loc) · 39.7 KB
/
REPL.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
//===-- REPL.cpp - the integrated REPL ------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Immediate/Immediate.h"
#include "ImmediateImpl.h"
#include "swift/Subsystems.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/Module.h"
#include "swift/AST/NameLookup.h"
#include "swift/Frontend/Frontend.h"
#include "swift/IDE/REPLCodeCompletion.h"
#include "swift/IDE/Utils.h"
#include "swift/Parse/PersistentParserState.h"
#include "swift/SIL/SILModule.h"
#include "swift/SILPasses/Passes.h"
#include "llvm/ExecutionEngine/MCJIT.h"
#include "llvm/IR/Module.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Process.h"
#if defined(__APPLE__)
// FIXME: Support REPL on non-Apple platforms. Ubuntu 14.10's editline does not
// include the wide character entry points needed by the REPL yet.
#include <histedit.h>
#endif // __APPLE__
using namespace swift;
using namespace swift::immediate;
namespace {
class REPLContext {
public:
/// The SourceMgr buffer ID of the REPL input.
unsigned CurBufferID;
/// The index into the source file's Decls at which to start
/// typechecking the next REPL input.
unsigned CurElem;
/// The index into the source file's Decls at which to start
/// irgenning the next REPL input.
unsigned CurIRGenElem;
};
enum class REPLInputKind : int {
/// The REPL got a "quit" signal.
REPLQuit,
/// Empty whitespace-only input.
Empty,
/// A REPL directive, such as ':help'.
REPLDirective,
/// Swift source code.
SourceCode,
};
template<size_t N>
class ConvertForWcharSize;
template<>
class ConvertForWcharSize<2> {
public:
static ConversionResult ConvertFromUTF8(const char** sourceStart,
const char* sourceEnd,
wchar_t** targetStart,
wchar_t* targetEnd,
ConversionFlags flags) {
return ConvertUTF8toUTF16(reinterpret_cast<const UTF8**>(sourceStart),
reinterpret_cast<const UTF8*>(sourceEnd),
reinterpret_cast<UTF16**>(targetStart),
reinterpret_cast<UTF16*>(targetEnd),
flags);
}
static ConversionResult ConvertToUTF8(const wchar_t** sourceStart,
const wchar_t* sourceEnd,
char** targetStart,
char* targetEnd,
ConversionFlags flags) {
return ConvertUTF16toUTF8(reinterpret_cast<const UTF16**>(sourceStart),
reinterpret_cast<const UTF16*>(sourceEnd),
reinterpret_cast<UTF8**>(targetStart),
reinterpret_cast<UTF8*>(targetEnd),
flags);
}
};
template<>
class ConvertForWcharSize<4> {
public:
static ConversionResult ConvertFromUTF8(const char** sourceStart,
const char* sourceEnd,
wchar_t** targetStart,
wchar_t* targetEnd,
ConversionFlags flags) {
return ConvertUTF8toUTF32(reinterpret_cast<const UTF8**>(sourceStart),
reinterpret_cast<const UTF8*>(sourceEnd),
reinterpret_cast<UTF32**>(targetStart),
reinterpret_cast<UTF32*>(targetEnd),
flags);
}
static ConversionResult ConvertToUTF8(const wchar_t** sourceStart,
const wchar_t* sourceEnd,
char** targetStart,
char* targetEnd,
ConversionFlags flags) {
return ConvertUTF32toUTF8(reinterpret_cast<const UTF32**>(sourceStart),
reinterpret_cast<const UTF32*>(sourceEnd),
reinterpret_cast<UTF8**>(targetStart),
reinterpret_cast<UTF8*>(targetEnd),
flags);
}
};
using Convert = ConvertForWcharSize<sizeof(wchar_t)>;
static void convertFromUTF8(llvm::StringRef utf8,
llvm::SmallVectorImpl<wchar_t> &out) {
size_t reserve = out.size() + utf8.size();
out.reserve(reserve);
const char *utf8_begin = utf8.begin();
wchar_t *wide_begin = out.end();
auto res = Convert::ConvertFromUTF8(&utf8_begin, utf8.end(),
&wide_begin, out.data() + reserve,
lenientConversion);
assert(res == conversionOK && "utf8-to-wide conversion failed!");
(void)res;
out.set_size(wide_begin - out.begin());
}
static void convertToUTF8(llvm::ArrayRef<wchar_t> wide,
llvm::SmallVectorImpl<char> &out) {
size_t reserve = out.size() + wide.size()*4;
out.reserve(reserve);
const wchar_t *wide_begin = wide.begin();
char *utf8_begin = out.end();
auto res = Convert::ConvertToUTF8(&wide_begin, wide.end(),
&utf8_begin, out.data() + reserve,
lenientConversion);
assert(res == conversionOK && "wide-to-utf8 conversion failed!");
(void)res;
out.set_size(utf8_begin - out.begin());
}
} // end anonymous namespace
static bool appendToREPLFile(SourceFile &SF,
PersistentParserState &PersistentState,
REPLContext &RC,
std::unique_ptr<llvm::MemoryBuffer> Buffer) {
assert(SF.Kind == SourceFileKind::REPL && "Can't append to a non-REPL file");
SourceManager &SrcMgr = SF.getParentModule()->getASTContext().SourceMgr;
RC.CurBufferID = SrcMgr.addNewSourceBuffer(std::move(Buffer));
bool FoundAnySideEffects = false;
unsigned CurElem = RC.CurElem;
bool Done;
do {
FoundAnySideEffects |=
parseIntoSourceFile(SF, RC.CurBufferID, &Done, nullptr,
&PersistentState);
performTypeChecking(SF, PersistentState.getTopLevelContext(), None,
CurElem);
CurElem = SF.Decls.size();
} while (!Done);
return FoundAnySideEffects;
}
#if defined(__APPLE__)
/// An arbitrary, otherwise-unused char value that editline interprets as
/// entering/leaving "literal mode", meaning it passes prompt characters through
/// to the terminal without affecting the line state. This prevents color
/// escape sequences from interfering with editline's internal state.
static constexpr wchar_t LITERAL_MODE_CHAR = L'\1';
/// Append a terminal escape sequence in "literal mode" so that editline
/// ignores it.
static void appendEscapeSequence(SmallVectorImpl<wchar_t> &dest,
llvm::StringRef src)
{
dest.push_back(LITERAL_MODE_CHAR);
convertFromUTF8(src, dest);
dest.push_back(LITERAL_MODE_CHAR);
}
/// The main REPL prompt string.
static const wchar_t * const PS1 = L"(swift) ";
/// The REPL prompt string for line continuations.
static const wchar_t * const PS2 = L" ";
class REPLInput;
class REPLEnvironment;
namespace {
/// Observe that we are processing REPL input. Dump source and reset any
/// colorization before dying.
class PrettyStackTraceREPL : public llvm::PrettyStackTraceEntry {
REPLInput &Input;
public:
PrettyStackTraceREPL(REPLInput &Input) : Input(Input) {}
void print(llvm::raw_ostream &out) const override;
};
}
/// EditLine wrapper that implements the user interface behavior for reading
/// user input to the REPL. All of its methods must be usable from a separate
/// thread and so shouldn't touch anything outside of the EditLine, History,
/// and member object state.
///
/// FIXME: Need the module for completions! Currently REPLRunLoop uses
/// synchronous messaging between the REPLInput thread and the main thread,
/// and client code shouldn't have access to the AST, so only one thread will
/// be accessing the module at a time. However, if REPLRunLoop
/// (or a new REPL application) ever requires asynchronous messaging between
/// REPLInput and REPLEnvironment, or if client code expected to be able to
/// grovel into the REPL's AST, then locking will be necessary to serialize
/// access to the AST.
class REPLInput {
PrettyStackTraceREPL StackTrace;
EditLine *e;
HistoryW *h;
size_t PromptContinuationLevel;
bool NeedPromptContinuation;
bool ShowColors;
bool PromptedForLine;
bool Outdented;
REPLCompletions completions;
llvm::SmallVector<wchar_t, 80> PromptString;
/// A buffer for all lines that the user entered, but we have not parsed yet.
llvm::SmallString<128> CurrentLines;
llvm::SmallString<16> CodeCompletionErasedBytes;
public:
REPLEnvironment &Env;
bool Autoindent;
REPLInput(REPLEnvironment &env)
: StackTrace(*this), Env(env), Autoindent(true)
{
// Only show colors if both stderr and stdout have colors.
ShowColors = llvm::errs().has_colors() && llvm::outs().has_colors();
// Make sure the terminal color gets restored when the REPL is quit.
if (ShowColors)
atexit([] {
llvm::outs().resetColor();
llvm::errs().resetColor();
});
e = el_init("swift", stdin, stdout, stderr);
h = history_winit();
PromptContinuationLevel = 0;
el_wset(e, EL_EDITOR, L"emacs");
el_wset(e, EL_PROMPT_ESC, PromptFn, LITERAL_MODE_CHAR);
el_wset(e, EL_CLIENTDATA, (void*)this);
el_wset(e, EL_HIST, history, h);
el_wset(e, EL_SIGNAL, 1);
el_wset(e, EL_GETCFN, GetCharFn);
// Provide special outdenting behavior for '}' and ':'.
el_wset(e, EL_ADDFN, L"swift-close-brace", L"Reduce {} indentation level",
BindingFn<&REPLInput::onCloseBrace>);
el_wset(e, EL_BIND, L"}", L"swift-close-brace", nullptr);
el_wset(e, EL_ADDFN, L"swift-colon", L"Reduce label indentation level",
BindingFn<&REPLInput::onColon>);
el_wset(e, EL_BIND, L":", L"swift-colon", nullptr);
// Provide special indent/completion behavior for tab.
el_wset(e, EL_ADDFN, L"swift-indent-or-complete",
L"Indent line or trigger completion",
BindingFn<&REPLInput::onIndentOrComplete>);
el_wset(e, EL_BIND, L"\t", L"swift-indent-or-complete", nullptr);
el_wset(e, EL_ADDFN, L"swift-complete",
L"Trigger completion",
BindingFn<&REPLInput::onComplete>);
// Provide some common bindings to complement editline's defaults.
// ^W should delete previous word, not the entire line.
el_wset(e, EL_BIND, L"\x17", L"ed-delete-prev-word", nullptr);
// ^_ should undo.
el_wset(e, EL_BIND, L"\x1f", L"vi-undo", nullptr);
HistEventW ev;
history_w(h, &ev, H_SETSIZE, 800);
}
~REPLInput() {
if (ShowColors)
llvm::outs().resetColor();
// FIXME: This should not be needed, but seems to help when stdout is being
// redirected to a file. Perhaps there is some underlying editline bug
// where it is setting stdout into some weird state and not restoring it
// with el_end?
llvm::outs().flush();
fflush(stdout);
el_end(e);
}
SourceFile &getREPLInputFile();
REPLInputKind getREPLInput(SmallVectorImpl<char> &Result) {
ide::SourceCompleteResult SCR;
SCR.IsComplete = true;
unsigned CurChunkLines = 0;
wchar_t TotalLine[4096] = L"";
CurrentLines.clear();
// Reset color before showing the prompt.
if (ShowColors)
llvm::outs().resetColor();
do {
// Read one line.
PromptContinuationLevel = SCR.IndentLevel;
NeedPromptContinuation = !SCR.IsComplete;
PromptedForLine = false;
Outdented = false;
int LineCount;
size_t LineStart = CurrentLines.size();
const wchar_t* WLine = el_wgets(e, &LineCount);
if (!WLine) {
// End-of-file.
if (PromptedForLine)
llvm::outs() << "\n";
return REPLInputKind::REPLQuit;
}
if (Autoindent) {
size_t indent = PromptContinuationLevel*2;
CurrentLines.append(indent, ' ');
}
convertToUTF8(llvm::makeArrayRef(WLine, WLine + wcslen(WLine)),
CurrentLines);
wcslcat(TotalLine, WLine, sizeof(TotalLine) / sizeof(*TotalLine));
++CurChunkLines;
// If we detect a line starting with a colon, treat it as a special
// REPL escape.
char const *s = CurrentLines.data() + LineStart;
char const *p = s;
while (p < CurrentLines.end() && isspace(*p)) {
++p;
}
if (p == CurrentLines.end()) {
if (!SCR.IsComplete) continue;
return REPLInputKind::Empty;
}
if (CurChunkLines == 1 && SCR.IndentLevel == 0 && *p == ':') {
// Colorize the response output.
if (ShowColors)
llvm::outs().changeColor(llvm::raw_ostream::GREEN);
Result.clear();
Result.append(CurrentLines.begin(), CurrentLines.end());
// The lexer likes null-terminated data.
Result.push_back('\0');
Result.pop_back();
// Enter the line into the line history.
HistEventW ev;
history_w(h, &ev, H_ENTER, TotalLine);
return REPLInputKind::REPLDirective;
}
SCR = ide::isSourceInputComplete(CurrentLines.str());
// Keep reading if input is unfinished.
} while (!SCR.IsComplete);
// Enter the line into the line history.
HistEventW ev;
history_w(h, &ev, H_ENTER, TotalLine);
Result.clear();
Result.append(CurrentLines.begin(), CurrentLines.end());
// The lexer likes null-terminated data.
Result.push_back('\0');
Result.pop_back();
// Colorize the response output.
if (ShowColors)
llvm::outs().changeColor(llvm::raw_ostream::CYAN);
return REPLInputKind::SourceCode;
}
private:
static wchar_t *PromptFn(EditLine *e) {
void* clientdata;
el_wget(e, EL_CLIENTDATA, &clientdata);
return const_cast<wchar_t*>(((REPLInput*)clientdata)->getPrompt());
}
const wchar_t *getPrompt() {
PromptString.clear();
if (ShowColors) {
const char *colorCode =
llvm::sys::Process::OutputColor(llvm::raw_ostream::YELLOW,
false, false);
if (colorCode)
appendEscapeSequence(PromptString, colorCode);
}
if (!NeedPromptContinuation)
PromptString.insert(PromptString.end(), PS1, PS1 + wcslen(PS1));
else {
PromptString.insert(PromptString.end(), PS2, PS2 + wcslen(PS2));
if (Autoindent)
PromptString.append(2*PromptContinuationLevel, L' ');
}
if (ShowColors) {
const char *colorCode = llvm::sys::Process::ResetColor();
if (colorCode)
appendEscapeSequence(PromptString, colorCode);
}
PromptedForLine = true;
PromptString.push_back(L'\0');
return PromptString.data();
}
/// Custom GETCFN to reset completion state after typing.
static int GetCharFn(EditLine *e, wchar_t *out) {
void* clientdata;
el_wget(e, EL_CLIENTDATA, &clientdata);
REPLInput *that = (REPLInput*)clientdata;
wint_t c;
while (errno = 0, (c = getwc(stdin)) == WEOF) {
if (errno == EINTR)
continue;
*out = L'\0';
return feof(stdin) ? 0 : -1;
}
// If the user typed anything other than tab, reset the completion state.
if (c != L'\t') {
that->completions.reset();
that->CodeCompletionErasedBytes.clear();
}
*out = wchar_t(c);
return 1;
}
template<unsigned char (REPLInput::*method)(int)>
static unsigned char BindingFn(EditLine *e, int ch) {
void *clientdata;
el_wget(e, EL_CLIENTDATA, &clientdata);
return (((REPLInput*)clientdata)->*method)(ch);
}
bool isAtStartOfLine(const LineInfoW *line) {
for (wchar_t c : llvm::makeArrayRef(line->buffer,
line->cursor - line->buffer)) {
if (!iswspace(c))
return false;
}
return true;
}
// /^\s*\w+\s*:$/
bool lineLooksLikeLabel(const LineInfoW *line) {
const wchar_t *p = line->buffer;
while (p != line->cursor && iswspace(*p))
++p;
if (p == line->cursor)
return false;
do {
++p;
} while (p != line->cursor && (iswalnum(*p) || *p == L'_'));
while (p != line->cursor && iswspace(*p))
++p;
return p+1 == line->cursor || *p == L':';
}
// /^\s*set\s*\(.*\)\s*:$/
bool lineLooksLikeSetter(const LineInfoW *line) {
const wchar_t *p = line->buffer;
while (p != line->cursor && iswspace(*p))
++p;
if (p == line->cursor || *p++ != L's')
return false;
if (p == line->cursor || *p++ != L'e')
return false;
if (p == line->cursor || *p++ != L't')
return false;
while (p != line->cursor && iswspace(*p))
++p;
if (p == line->cursor || *p++ != L'(')
return false;
if (line->cursor - p < 2 || line->cursor[-1] != L':')
return false;
p = line->cursor - 1;
while (iswspace(*--p));
return *p == L')';
}
// /^\s*case.*:$/
bool lineLooksLikeCase(const LineInfoW *line) {
const wchar_t *p = line->buffer;
while (p != line->cursor && iswspace(*p))
++p;
if (p == line->cursor || *p++ != L'c')
return false;
if (p == line->cursor || *p++ != L'a')
return false;
if (p == line->cursor || *p++ != L's')
return false;
if (p == line->cursor || *p++ != L'e')
return false;
return line->cursor[-1] == ':';
}
void outdent() {
// If we didn't already outdent, do so.
if (!Outdented) {
if (PromptContinuationLevel > 0)
--PromptContinuationLevel;
Outdented = true;
}
}
unsigned char onColon(int ch) {
// Add the character to the string.
wchar_t s[2] = {(wchar_t)ch, 0};
el_winsertstr(e, s);
const LineInfoW *line = el_wline(e);
// Outdent if the line looks like a label.
if (lineLooksLikeLabel(line))
outdent();
// Outdent if the line looks like a setter.
else if (lineLooksLikeSetter(line))
outdent();
// Outdent if the line looks like a 'case' label.
else if (lineLooksLikeCase(line))
outdent();
return CC_REFRESH;
}
unsigned char onCloseBrace(int ch) {
bool atStart = isAtStartOfLine(el_wline(e));
// Add the character to the string.
wchar_t s[2] = {(wchar_t)ch, 0};
el_winsertstr(e, s);
// Don't outdent if we weren't at the start of the line.
if (!atStart) {
return CC_REFRESH;
}
outdent();
return CC_REFRESH;
}
unsigned char onIndentOrComplete(int ch) {
const LineInfoW *line = el_wline(e);
// FIXME: UTF-8? What's that?
size_t cursorPos = line->cursor - line->buffer;
// If there's nothing but whitespace before the cursor, indent to the next
// 2-character tab stop.
if (isAtStartOfLine(line)) {
const wchar_t *indent = cursorPos & 1 ? L" " : L" ";
el_winsertstr(e, indent);
return CC_REFRESH;
}
// Otherwise, look for completions.
return onComplete(ch);
}
void insertStringRef(StringRef s) {
if (s.empty())
return;
// Convert s to wchar_t* and null-terminate for el_winsertstr.
SmallVector<wchar_t, 64> TmpStr;
convertFromUTF8(s, TmpStr);
TmpStr.push_back(L'\0');
el_winsertstr(e, TmpStr.data());
}
void displayCompletions(llvm::ArrayRef<llvm::StringRef> list) {
// FIXME: Do the print-completions-below-the-prompt thing bash does.
llvm::outs() << '\n';
// Trim the completion list to the terminal size.
int lines_int = 0, columns_int = 0;
// NB: EL_GETTC doesn't work with el_wget (?!)
el_get(e, EL_GETTC, "li", &lines_int);
el_get(e, EL_GETTC, "co", &columns_int);
assert(lines_int > 0 && columns_int > 0 && "negative or zero screen size?!");
auto lines = size_t(lines_int), columns = size_t(columns_int);
size_t trimToColumns = columns > 2 ? columns - 2 : 0;
size_t trimmed = 0;
if (list.size() > lines - 1) {
size_t trimToLines = lines > 2 ? lines - 2 : 0;
trimmed = list.size() - trimToLines;
list = list.slice(0, trimToLines);
}
for (StringRef completion : list) {
if (completion.size() > trimToColumns)
completion = completion.slice(0, trimToColumns);
llvm::outs() << " " << completion << '\n';
}
if (trimmed > 0)
llvm::outs() << " (and " << trimmed << " more)\n";
}
unsigned char onComplete(int ch) {
const LineInfoW *line = el_wline(e);
llvm::ArrayRef<wchar_t> wprefix(line->buffer, line->cursor - line->buffer);
llvm::SmallString<64> Prefix;
Prefix.assign(CurrentLines);
convertToUTF8(wprefix, Prefix);
if (!completions) {
// If we aren't currently working with a completion set, generate one.
completions.populate(getREPLInputFile(), Prefix);
// Display the common root of the found completions and beep unless we
// found a unique one.
insertStringRef(completions.getRoot());
return completions.isUnique()
? CC_REFRESH
: CC_REFRESH_BEEP;
}
// Otherwise, advance through the completion state machine.
switch (completions.getState()) {
case CompletionState::CompletedRoot:
// We completed the root. Next step is to display the completion list.
displayCompletions(completions.getCompletionList());
completions.setState(CompletionState::DisplayedCompletionList);
return CC_REDISPLAY;
case CompletionState::DisplayedCompletionList: {
// Complete the next completion stem in the cycle.
const auto Last = completions.getPreviousStem();
el_wdeletestr(e, Last.InsertableString.size());
Prefix.resize(Prefix.size() - Last.InsertableString.size());
insertStringRef(CodeCompletionErasedBytes);
Prefix.append(CodeCompletionErasedBytes);
const auto Next = completions.getNextStem();
CodeCompletionErasedBytes.clear();
if (Next.NumBytesToErase != 0) {
CodeCompletionErasedBytes.assign(Prefix.end() - Next.NumBytesToErase, Prefix.end());
el_wdeletestr(e, Next.NumBytesToErase);
}
insertStringRef(Next.InsertableString);
return CC_REFRESH;
}
case CompletionState::Empty:
case CompletionState::Unique:
// We already provided a definitive completion--nothing else to do.
return CC_REFRESH_BEEP;
case CompletionState::Invalid:
llvm_unreachable("got an invalid completion set?!");
}
}
};
enum class PrintOrDump { Print, Dump };
static void printOrDumpDecl(Decl *d, PrintOrDump which) {
if (which == PrintOrDump::Print) {
d->print(llvm::outs());
llvm::outs() << '\n';
} else
d->dump(llvm::outs());
}
/// The compiler and execution environment for the REPL.
class REPLEnvironment {
CompilerInstance &CI;
public:
SourceFile &REPLInputFile;
private:
ProcessCmdLine CmdLine;
llvm::SmallPtrSet<swift::Module *, 8> ImportedModules;
SmallVector<llvm::Function*, 8> InitFns;
bool RanGlobalInitializers;
llvm::LLVMContext &LLVMContext;
llvm::Module *Module;
llvm::StringSet<> FuncsAlreadyGenerated;
llvm::StringSet<> GlobalsAlreadyEmitted;
llvm::Module DumpModule;
llvm::SmallString<128> DumpSource;
llvm::ExecutionEngine *EE;
IRGenOptions IRGenOpts;
const SILOptions SILOpts;
REPLInput Input;
REPLContext RC;
PersistentParserState PersistentState;
private:
void stripPreviouslyGenerated(llvm::Module &M) {
for (auto &function : M.getFunctionList()) {
function.setVisibility(llvm::GlobalValue::DefaultVisibility);
if (FuncsAlreadyGenerated.count(function.getName()))
function.deleteBody();
else {
if (function.getName() != SWIFT_ENTRY_POINT_FUNCTION)
FuncsAlreadyGenerated.insert(function.getName());
}
}
for (auto &global : M.globals()) {
if (!global.hasName())
continue;
if (global.hasUnnamedAddr())
continue;
global.setVisibility(llvm::GlobalValue::DefaultVisibility);
if (!global.hasAvailableExternallyLinkage() &&
!global.hasAppendingLinkage() &&
!global.hasCommonLinkage()) {
global.setLinkage(llvm::GlobalValue::ExternalLinkage);
if (GlobalsAlreadyEmitted.count(global.getName()))
global.setInitializer(nullptr);
else
GlobalsAlreadyEmitted.insert(global.getName());
}
}
for (auto alias = M.alias_begin(); alias != M.alias_end();) {
alias->setVisibility(llvm::GlobalValue::DefaultVisibility);
if (!alias->hasAvailableExternallyLinkage() &&
!alias->hasAppendingLinkage() &&
!alias->hasCommonLinkage()) {
alias->setLinkage(llvm::GlobalValue::ExternalLinkage);
if (GlobalsAlreadyEmitted.count(alias->getName())) {
// Replace already-emitted aliases with external declarations.
SmallString<32> name = alias->getName();
alias->setName("");
auto external = new llvm::GlobalVariable(
M,
alias->getType()->getPointerElementType(),
/*isConstant*/ false,
alias->getLinkage(),
/*initializer*/ nullptr,
name);
alias->replaceAllUsesWith(external);
auto &aliasToRemove = *alias;
++alias;
aliasToRemove.eraseFromParent();
} else {
GlobalsAlreadyEmitted.insert(alias->getName());
++alias;
}
}
}
}
bool executeSwiftSource(llvm::StringRef Line, const ProcessCmdLine &CmdLine) {
// Parse the current line(s).
auto InputBuf = std::unique_ptr<llvm::MemoryBuffer>(
llvm::MemoryBuffer::getMemBufferCopy(Line, "<REPL Input>"));
bool ShouldRun = appendToREPLFile(REPLInputFile, PersistentState, RC,
std::move(InputBuf));
// SILGen the module and produce SIL diagnostics.
std::unique_ptr<SILModule> sil;
if (!CI.getASTContext().hadError()) {
sil = performSILGeneration(REPLInputFile, CI.getSILOptions(),
RC.CurIRGenElem);
performSILLinking(sil.get());
runSILDiagnosticPasses(*sil);
}
if (CI.getASTContext().hadError()) {
if (CI.getDiags().hasFatalErrorOccurred())
return false;
CI.getASTContext().Diags.resetHadAnyError();
while (REPLInputFile.Decls.size() > RC.CurElem)
REPLInputFile.Decls.pop_back();
// FIXME: Handling of "import" declarations? Is there any other
// state which needs to be reset?
return true;
}
RC.CurElem = REPLInputFile.Decls.size();
DumpSource += Line;
// If we didn't see an expression, statement, or decl which might have
// side-effects, keep reading.
if (!ShouldRun)
return true;
// IRGen the current line(s).
// FIXME: We shouldn't need to use the global context here, but
// something is persisting across calls to performIRGeneration.
auto LineModule = performIRGeneration(IRGenOpts, REPLInputFile, sil.get(),
"REPLLine", llvm::getGlobalContext(),
RC.CurIRGenElem);
RC.CurIRGenElem = RC.CurElem;
if (CI.getASTContext().hadError())
return false;
// LineModule will get destroy by the following link process.
// Make a copy of it to be able to correct produce DumpModule.
std::unique_ptr<llvm::Module> SaveLineModule(CloneModule(LineModule.get()));
if (!linkLLVMModules(Module, LineModule.get()
// TODO: reactivate the linker mode if it is
// supported in llvm again. Otherwise remove the
// commented code completely.
/*, llvm::Linker::PreserveSource */)) {
return false;
}
std::unique_ptr<llvm::Module> NewModule(CloneModule(Module));
Module->getFunction("main")->eraseFromParent();
stripPreviouslyGenerated(*NewModule);
if (!linkLLVMModules(&DumpModule, SaveLineModule.get()
// TODO: reactivate the linker mode if it is
// supported in llvm again. Otherwise remove the
// commented code completely.
/*, llvm::Linker::DestroySource */)) {
return false;
}
llvm::Function *DumpModuleMain = DumpModule.getFunction("main");
DumpModuleMain->setName("repl.line");
if (IRGenImportedModules(CI, *NewModule, ImportedModules, InitFns,
IRGenOpts, SILOpts))
return false;
llvm::Module *TempModule = NewModule.get();
EE->addModule(std::move(NewModule));
EE->finalizeObject();
for (auto InitFn : InitFns)
EE->runFunctionAsMain(InitFn, CmdLine, 0);
InitFns.clear();
// FIXME: The way we do this is really ugly... we should be able to
// improve this.
if (!RanGlobalInitializers) {
EE->runStaticConstructorsDestructors(*TempModule, false);
RanGlobalInitializers = true;
}
llvm::Function *EntryFn = TempModule->getFunction("main");
EE->runFunctionAsMain(EntryFn, CmdLine, 0);
return true;
}
public:
REPLEnvironment(CompilerInstance &CI,
const ProcessCmdLine &CmdLine,
llvm::LLVMContext &LLVMCtx,
bool ParseStdlib)
: CI(CI),
REPLInputFile(CI.getMainModule()->
getMainSourceFile(SourceFileKind::REPL)),
CmdLine(CmdLine),
RanGlobalInitializers(false),
LLVMContext(LLVMCtx),
Module(new llvm::Module("REPL", LLVMContext)),
DumpModule("REPL", LLVMContext),
IRGenOpts(),
SILOpts(),
Input(*this),
RC{
/*BufferID*/ 0U,
/*CurElem*/ 0,
/*CurIRGenElem*/ 0
}
{
ASTContext &Ctx = CI.getASTContext();
if (!loadSwiftRuntime(Ctx.SearchPathOpts.RuntimeLibraryPath)) {
CI.getDiags().diagnose(SourceLoc(),
diag::error_immediate_mode_missing_stdlib);
return;
}
tryLoadLibraries(CI.getLinkLibraries(), Ctx.SearchPathOpts, CI.getDiags());
llvm::EngineBuilder builder(
std::move(std::unique_ptr<llvm::Module>(Module)));
std::string ErrorMsg;
llvm::TargetOptions TargetOpt;
std::string CPU;
std::vector<std::string> Features;
std::tie(TargetOpt, CPU, Features)
= getIRTargetOptions(IRGenOpts, CI.getASTContext());
builder.setRelocationModel(llvm::Reloc::PIC_);
builder.setTargetOptions(TargetOpt);
builder.setMCPU(CPU);
builder.setMAttrs(Features);
builder.setErrorStr(&ErrorMsg);
builder.setEngineKind(llvm::EngineKind::JIT);
EE = builder.create();
IRGenOpts.OutputFilenames.clear();
IRGenOpts.Optimize = false;
IRGenOpts.OutputKind = IRGenOutputKind::Module;
IRGenOpts.UseJIT = true;
IRGenOpts.DebugInfoKind = IRGenDebugInfoKind::None;
if (!ParseStdlib) {
// Force standard library to be loaded immediately. This forces any
// errors to appear upfront, and helps eliminate some nasty lag after the
// first statement is typed into the REPL.
static const char WarmUpStmt[] = "Void()\n";
auto Buffer =
llvm::MemoryBuffer::getMemBufferCopy(WarmUpStmt,
"<REPL Initialization>");
appendToREPLFile(REPLInputFile, PersistentState, RC, std::move(Buffer));
if (Ctx.hadError())
return;
}
RC.CurElem = RC.CurIRGenElem = REPLInputFile.Decls.size();
if (llvm::sys::Process::StandardInIsUserInput())
llvm::outs() <<
"*** You are running Swift's integrated REPL, ***\n"
"*** intended for testing purposes only. ***\n"
"*** The full REPL is built as part of LLDB. ***\n"
"*** Type ':help' for assistance. ***\n";
}
swift::Module *getMainModule() const {
return REPLInputFile.getParentModule();