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
|
/* ____ ____ ____ ____ ______ *\
** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
** /_____/\____/\___/\____/____/ **
** **
** $Id$
\* */
package scalac;
import ch.epfl.lamp.util.CodePrinter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.*;
import scala.tools.util.AbstractFile;
import scala.tools.util.ClassPath;
import scala.tools.util.Position;
import scala.tools.util.SourceFile;
import scala.tools.util.SourceReader;
import scala.tools.util.Reporter;
import scalac.ast.*;
import scalac.ast.parser.*;
import scalac.ast.printer.*;
import scalac.atree.ATreePrinter;
import scalac.backend.Primitives;
// !!! <<< Interpreter stuff
import scalac.symtab.*;
// !!! >>> Interpreter stuff
import scalac.symtab.Definitions;
import scalac.symtab.classfile.PackageParser;
import scalac.symtab.classfile.CLRPackageParser;
import scalac.typechecker.AnalyzerPhase;
import scalac.typechecker.Infer;
import scalac.util.*;
/** The global environment of a compiler run
*
* @author Matthias Zenger
* @version 1.0
*/
public abstract class Global {
public static Global instance;
/** global options
*/
public final boolean noimports;
public final boolean nopredefs;
public final boolean separate;
//public final boolean optimize;
public final boolean debug;
public final boolean debuginfo;
public final boolean explaintypes;
public final boolean uniqid;
public final boolean newMatch;
public final boolean xmlPreserveWS;
public final boolean runTimeTypes;
public final boolean printtypes;
public final boolean printtokens;
public final String outpath;
public final String encoding;
public final String target;
/** the message reporter
*/
public final Reporter reporter;
/** a stack for maintaining timestamps
*/
private final Stack startTimes = new Stack();
/** the source file charset
*/
private final Charset charset;
/** the source file decoder
*/
private final CharsetDecoder decoder;
/** the source file reader
*/
private final SourceReader reader;
/** all compilation units
*/
public CompilationUnit[] units;
/** the class path
*/
public final ClassPath classPath;
/** the global tree factory
*/
public final TreeFactory make;
/** the tree generator
*/
public final TreeGen treeGen;
/** the global tree printer
*/
public final PrintWriter writer;
public final TreePrinter treePrinter;
/** documentation comments of symbols
*/
public final Map/*<Symbol, String>*/ mapSymbolComment = new HashMap();
/** attributes of symbols
*/
public final Map/*<Symbol, Pair<Symbol, AConstant[]>>*/ mapSymbolAttr = new HashMap();
/** views associated with (upper-bounded) type parameters
*/
public final Map/*<Symbol, Symbol>*/ viewOfTypeParam = new HashMap();
/** Pickled info of top-level symbols
*/
public final Map/*<Symbol, Pickle>*/ symdata = new HashMap();
/** The compiler command arguments
*/
public final CompilerCommand args;
/** The set of currently compiled top-level symbols
*/
public HashMap/*<Symbol,Sourcefile>*/ compiledNow = new HashMap();
/** The set of already compiled sourcefiles
*/
public HashSet/*<SourceFile>*/ compiledUnits = new HashSet();
/** the current phase
*/
public Phase currentPhase;
/** the global definitions
*/
public Definitions definitions;
/** the global primitives
*/
public final Primitives primitives;
/** compilation phases.
*/
public final CompilerPhases PHASE;
/** compilation targets
*/
public static final String TARGET_INT;
public static final String TARGET_JVM;
public static final String TARGET_MSIL;
public static final String TARGET_JVMFROMICODE;
public static final String[] TARGETS = new String[] {
TARGET_INT = "int",
TARGET_JVM = "jvm",
TARGET_MSIL = "msil",
TARGET_JVMFROMICODE = "jvmfromicode"
};
/** tree printers
*/
public static final String PRINTER_TEXT;
public static final String PRINTER_HTML;
public static final String PRINTER_SWING;
public static final String[] PRINTERS = new String[] {
PRINTER_TEXT = "text",
PRINTER_HTML = "html",
PRINTER_SWING = "swing",
};
/**
* Creates an instance variable.
*
* @param args
*/
public Global(CompilerCommand args) {
this(args, false);
}
/** hooks for installing printers
*/
public abstract Infer newInfer();
public abstract TreePrinter newTextTreePrinter(PrintWriter writer);
public abstract TreePrinter newHTMLTreePrinter(PrintWriter writer);
public abstract TreePrinter newSwingTreePrinter(PrintWriter writer);
/**
* Creates an instance variable.
*
* @param args
* @param interpret
*/
public Global(CompilerCommand args, boolean interpret) {
assert Debug.initialize() || true;
if (Global.instance != null) // jaco bug: can't use assert here
/* throw */ Debug.abort("duplicate creation of Global");
Global.instance = this;
this.args = args;
this.reporter = args.reporter();
this.start(); // timestamp to compute the total time
this.noimports = args.noimports.value;
this.nopredefs = args.nopredefs.value;
//this.optimize = args.optimize.optimize;
this.debug = args.debug.value;
this.debuginfo = args.debuginfo.value;
this.uniqid = args.uniqid.value;
this.newMatch = args.Xnewmatch.value;
this.xmlPreserveWS = args.XpreserveWS.value;
this.runTimeTypes = args.XrunTimeTypes.value;
this.explaintypes = args.explaintypes.value;
this.printtypes = args.types.value;
this.printtokens = args.print.tokens;
this.classPath = args.classpath();
if (reporter.verbose) reporter.inform("classpath = " + classPath);
this.outpath = args.outpath();
String encoding = args.encoding.value;
Charset charset = null;
try {
charset = Charset.forName(encoding);
} catch (IllegalCharsetNameException exception) {
args.encoding.error("illegal charset name '" + encoding + "'");
} catch (UnsupportedCharsetException exception) {
args.encoding.error("unsupported charset '" + encoding + "'");
}
if (charset == null) {
encoding = "ISO-8859-1"; // A mandatory charset
charset = Charset.forName(encoding);
}
this.encoding = encoding;
this.charset = charset;
this.decoder = charset.newDecoder();
this.reader = new SourceReader(decoder);
this.target = interpret ? TARGET_INT : args.target.value.intern();
this.separate = args.separate.value.equals("yes") ||
args.separate.value.equals("default") && !this.target.equals(TARGET_INT);
String printFile = args.printfile.value;
OutputStream stream;
try {
stream = "-".equals(printFile)
? System.out
: new FileOutputStream(printFile);
} catch (FileNotFoundException e) {
error("unable to open file " + printFile + ". Printing on console");
stream = System.out;
}
this.writer = new PrintWriter(stream, debug);
if (args.printer.value.equals(PRINTER_HTML)) {
this.treePrinter = newHTMLTreePrinter(writer);
} else if (args.printer.value.equals(PRINTER_SWING)) {
this.treePrinter = newSwingTreePrinter(writer);
} else {
if (!args.printer.value.equals(PRINTER_TEXT))
error("unknown printer kind: " + args.printer.value);
this.treePrinter = newTextTreePrinter(writer);
}
this.make = new DefaultTreeFactory();
this.PHASE = args.phases;
// if (!optimize) PHASE.remove(args.phases.OPTIMIZE);
// TODO: Enable TailCall for other backends when they handle LabelDefs
if (!runTimeTypes) args.phases.TYPESASVALUES.addSkipFlag();
if (target != TARGET_MSIL) args.phases.GENMSIL.addSkipFlag();
if (target != TARGET_JVM) args.phases.GENJVM.addSkipFlag();
if (target != TARGET_JVMFROMICODE) {
args.phases.ICODE.addSkipFlag();
args.phases.GENJVMFROMICODE.addSkipFlag();
} else {
;//args.phases.ERASURE.addSkipFlag();
}
PHASE.freeze();
PhaseDescriptor[] descriptors = PHASE.phases();
for (int i = 0; i <= PHASE.ANALYZER.id(); i++)
if (!descriptors[i].hasSkipFlag()) descriptors[i].create(this);
this.treeGen = ((AnalyzerPhase)PHASE.ANALYZER.phase()).gen;
this.primitives = new Primitives(this);
assert !descriptors[0].hasSkipFlag();
for (int i = PHASE.ANALYZER.id() + 1; i < descriptors.length; i++)
if (!descriptors[i].hasSkipFlag()) descriptors[i].create(this);
this.currentPhase = PHASE.INITIAL.phase();
}
/**
* Moves to next phase.
*/
public void nextPhase() {
assert currentPhase.next != null;
currentPhase = currentPhase.next;
}
/** Move to previous phase
*/
public void prevPhase() {
assert currentPhase.prev != null;
currentPhase = currentPhase.prev;
}
/** Creates a virtual source file with given name and content. */
public SourceFile getSourceFile(String sourcename, String content) {
return new SourceFile(sourcename, content.toCharArray());
}
/** Reads and returns the source file in file with given name. */
public SourceFile getSourceFile(String filename) throws IOException {
AbstractFile file = AbstractFile.getFile(filename);
if (file == null) throw new FileNotFoundException(
"source file '" + filename + "' could not be found");
return getSourceFile(file);
}
/** Reads and returns the source file in given abstract file. */
public SourceFile getSourceFile(AbstractFile file) throws IOException {
return new SourceFile(file, reader.read(file));
}
/** Reads and returns the source file of given clasz. */
public SourceFile getSourceFile(Symbol clasz) throws IOException {
assert clasz.isClass() && clasz.isStatic(): Debug.show(clasz);
AbstractFile file = classPath.getRoot().lookupPath(
SourceRepresentation.externalizeFileName(clasz, ".scala"), false);
if (file == null) throw new FileNotFoundException(
"source file for " + clasz + " could not be found");
return getSourceFile(file);
}
/** Returns the root symbol loader. */
public SymbolLoader getRootLoader() {
return target == TARGET_MSIL
? new CLRPackageParser(this, classPath.getRoot())
: new PackageParser(this, classPath.getRoot());
}
/** the top-level compilation process
*/
public void compile(String[] files, boolean console) {
reporter.resetCounters();
// parse files
List units = new ArrayList(files.length);
for (int i = 0; i < files.length; i++) {
try {
SourceFile source = getSourceFile(files[i]);
units.add(new CompilationUnit(this, source, console));
compiledUnits.add(source);
} catch (IOException exception) {
error(exception.getMessage());
}
}
this.units = (CompilationUnit[])units.toArray(new CompilationUnit[units.size()]);
compile();
}
/**
* The top-level compilation process.
*
* @param filename
* @param input
* @param console
*/
public void compile(String filename, String input, boolean console) {
reporter.resetCounters();
SourceFile source = getSourceFile(filename, input);
compiledUnits.add(source);
units = new CompilationUnit[]{new CompilationUnit(this, source, console)};
compile();
}
/** compile all compilation units
*/
private void compile() {
treePrinter.begin();
currentPhase = PHASE.INITIAL.phase();
// apply successive phases and pray that it works
while (currentPhase.next != null && reporter.errors() == 0) {
currentPhase = currentPhase.next;
start();
// System.out.println("*** " + currentPhase.descriptor.description() + " ***");
currentPhase.apply(units);
stop(currentPhase.descriptor.taskDescription());
if (currentPhase.descriptor.hasPrintFlag()) print();
if (currentPhase.descriptor.hasGraphFlag())
currentPhase.graph(this);
// if (currentPhase.descriptor.hasCheckFlag()) // !!!
if (currentPhase == PHASE.PARSER.phase()) fix1();
if (currentPhase == PHASE.ANALYZER.phase()) fix2();
}
if (reporter.errors() != 0) {
imports.clear();
for (Iterator i = compiledNow.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry)i.next();
Symbol clasz = (Symbol)entry.getKey();
AbstractFile file = ((SourceFile)entry.getValue()).getFile();
clasz.reset(new SourceCompleter(this, file));
}
}
symdata.clear();
compiledNow.clear();
treePrinter.end();
}
/** Compiles an additional source file. */
public void compileLate(SourceFile source, boolean mixinOnly) {
if (!compiledUnits.contains(source)) {
compiledUnits.add(source);
CompilationUnit unit = new CompilationUnit(this, source, false, mixinOnly);
Phase backup = currentPhase;
// !!! add code to print/skip/graph as in compile
currentPhase = PHASE.PARSER.phase();
PHASE.PARSER.phase().apply(new CompilationUnit[] {unit});
currentPhase = PHASE.ANALYZER.phase();
((AnalyzerPhase)PHASE.ANALYZER.phase()).lateEnter(unit);
// !!! add code for later phases?
currentPhase = backup;
}
}
private void print() {
if (currentPhase.id == PHASE.MAKEBOXINGEXPLICIT.id()) {
boolean html = args.printer.value.equals(PRINTER_HTML);
if (html) writer.println("<pre>");
ATreePrinter printer = new ATreePrinter(new CodePrinter(writer));
boolean next = currentPhase.next != null;
if (next) currentPhase = currentPhase.next;
printer.printGlobal(this);
if (next) currentPhase = currentPhase.prev;
if (html) writer.println("</pre>");
} else if (currentPhase.id == PHASE.ICODE.id()) {
boolean html = args.printer.value.equals(PRINTER_HTML);
if (html) writer.println("<pre>");
ATreePrinter printer = ((scalac.transformer.ICodePhase)currentPhase).getPrinter(new CodePrinter(writer)); // Oh !!
boolean next = currentPhase.next != null;
if (next) currentPhase = currentPhase.next;
printer.printGlobal(this);
if (next) currentPhase = currentPhase.prev;
if (html) writer.println("</pre>");
} else {
// go to next phase to print symbols with their new type
boolean next = currentPhase.next != null;
if (next) currentPhase = currentPhase.next;
treePrinter.print(this);
if (next) currentPhase = currentPhase.prev;
}
}
// !!! <<< Interpreter stuff
public static final String CONSOLE_S = "$console$";
private static final Name
SHOW_DEFINITION_N = Name.fromString("showDefinition"),
SHOW_VALUE_DEFINITION_N = Name.fromString("showValueDefinition"),
SET_EVALUATION_RESULT_N = Name.fromString("setEvaluationResult");
private Symbol INTERPRETER;
private Symbol PRINTER;
private Symbol SHOW_VALUE;
private Symbol SET_EVALUATION_RESULT;
private Symbol SHOW_DEFINITION;
private Symbol SHOW_VALUE_DEFINITION;
private Symbol INTERPRETER() {
if (INTERPRETER == null)
INTERPRETER = definitions.getModule("scala.runtime.InterpreterSupport");
return INTERPRETER;
}
private Symbol SHOW_DEFINITION() {
if (SHOW_DEFINITION == null)
SHOW_DEFINITION = INTERPRETER().lookup(SHOW_DEFINITION_N);
return SHOW_DEFINITION;
}
private Symbol SHOW_VALUE_DEFINITION() {
if (SHOW_VALUE_DEFINITION == null)
SHOW_VALUE_DEFINITION = INTERPRETER().lookup(SHOW_VALUE_DEFINITION_N);
return SHOW_VALUE_DEFINITION;
}
private Symbol SET_EVALUATION_RESULT() {
if (SET_EVALUATION_RESULT == null)
SET_EVALUATION_RESULT = INTERPRETER().lookup(SET_EVALUATION_RESULT_N);
return SET_EVALUATION_RESULT;
}
private int module = 0;
private List imports = new ArrayList();
public Symbol console;
private void fix1() {
for (int i = 0; i < units.length; i++) {
if (units[i].console) fix1(units[i]);
}
nextPhase();
for (int i = 0; i < imports.size(); i++) {
Symbol module = (Symbol)imports.get(i);
((scalac.typechecker.AnalyzerPhase)PHASE.ANALYZER.phase()).addConsoleImport(module);
}
prevPhase();
}
private void fix1(CompilationUnit unit) {
unit.body = new Tree[] {
make.ModuleDef(Position.FIRSTPOS,
0, Name.fromString(CONSOLE_S+module), Tree.Empty,
make.Template(Position.FIRSTPOS, new Tree[]{
make.Apply(Position.FIRSTPOS,
make.Select(Position.FIRSTPOS,
make.Ident(Position.FIRSTPOS, Names.scala),
Names.AnyRef.toTypeName()),
new Tree[0])},
unit.body))};
module++;
}
private void fix2() {
for (int i = 0; i < units.length; i++) {
if (units[i].console) fix2(units[i]);
}
}
private void fix2(CompilationUnit unit) {
imports.clear();
for (int i = 0; i < unit.body.length; i++) {
switch (unit.body[i]) {
case ModuleDef(_, _, _, Tree.Template impl):
Symbol symbol = unit.body[i].symbol();
if (!symbol.name.toString().startsWith(CONSOLE_S)) break;
console = symbol;
if (impl.body.length <= 0) break;
imports.add(unit.body[i].symbol());
Tree last = impl.body[impl.body.length - 1];
if (last != Tree.Empty && last.isTerm()) {
impl.body[impl.body.length - 1] =
treeGen.Apply(last.pos,
treeGen.Select(last.pos,
treeGen.mkGlobalRef(last.pos, INTERPRETER()),
SET_EVALUATION_RESULT()),
new Tree[] {
last,
treeGen.mkStringLit(
last.pos, show(last.getType()))});
}
TreeList body = new TreeList();
for (int j = 0; j < impl.body.length; j++)
fix2(body, impl.body[j]);
impl.body = body.toArray();
break;
}
}
}
private void fix2(TreeList body, Tree tree) {
body.append(tree);
switch (tree) {
case PatDef(_, _, _): // !!! impossible (removed by analyzer)
assert false : Debug.show(tree);
return;
case ClassDef(_, _, _, _, _, _):
case PackageDef(_, _):
case ModuleDef(_, _, _, _):
case DefDef(_, _, _, _, _, _):
case AbsTypeDef(_, _, _, _):
case AliasTypeDef(_, _, _, _):
if (!mustShow(tree.symbol())) return;
body.append(
treeGen.Apply(tree.pos,
treeGen.Select(tree.pos,
treeGen.mkGlobalRef(tree.pos, INTERPRETER()),
SHOW_DEFINITION()),
new Tree[] {
treeGen.mkStringLit(tree.pos, show(tree.symbol()))}));
return;
case ValDef(_, _, _, _):
if (!mustShow(tree.symbol())) return;
body.append(
treeGen.Apply(tree.pos,
treeGen.Select(tree.pos,
treeGen.mkGlobalRef(tree.pos, INTERPRETER()),
SHOW_VALUE_DEFINITION()),
new Tree[] {
treeGen.mkStringLit(tree.pos, show(tree.symbol())),
treeGen.Ident(tree.pos, tree.symbol())}));
return;
default:
return;
}
}
private boolean mustShow(Symbol symbol) {
return !symbol.isAccessor();
}
private String show(Symbol symbol) {
return new InterpreterPrinter().printSignature(symbol).toString();
}
private String show(Type type) {
return new InterpreterPrinter().printType(type).toString();
}
private class InterpreterPrinter extends SymbolTablePrinter {
public InterpreterPrinter() {
super(" ");
}
public String getSymbolName(Symbol symbol) {
String string = super.getSymbolName(symbol);
if (!debug) {
int index = string.indexOf('$');
if (index > 0) string = string.substring(0, index);
}
return string;
}
}
// !!! >>> Interpreter stuff
/** stop the compilation process immediately
*/
public static Error fail() {
throw new ApplicationError();
}
/** stop the compilation process immediately
*/
public static Error fail(String message) {
throw new ApplicationError(message);
}
/** stop the compilation process immediately
*/
public static Error fail(String message, Object object) {
throw new ApplicationError(message, object);
}
/** stop the compilation process immediately
*/
public static Error fail(Object value) {
throw new ApplicationError(value);
}
/** stop the compilation process immediately
*/
public static Error fail(String message, Throwable cause) {
throw new ApplicationError(message, cause);
}
/** stop the compilation process immediately
*/
public static Error fail(Throwable cause) {
throw new ApplicationError(cause);
}
/** issue a global error
*/
public void error(String message) {
reporter.error(null, message);
}
/** issue a global warning
*/
public void warning(String message) {
reporter.warning(null, message);
}
/** issue an operation note
*/
public void operation(String message) {
reporter.inform("[" + message + "]");
}
/** issue a debug message from currentPhase
*/
// the boolean return value is here to let one write "assert log( ... )"
public boolean log(String message) {
if (log()) {
reporter.report("[log " + currentPhase + "] " + message);
}
return true;
}
/** return true if logging is switched on for the current phase
*/
public boolean log() {
return currentPhase.descriptor.hasLogFlag();
}
/** start a new timer
*/
public void start() {
startTimes.push(new Long(System.currentTimeMillis()));
}
/**
* issue timing information
*
* @param message
*/
public void stop(String message) {
long start = ((Long)startTimes.pop()).longValue();
reporter.inform("[" + message + " in " +
(System.currentTimeMillis() - start) + "ms]");
}
}
|