summaryrefslogtreecommitdiff
path: root/sources/scala/tools/scaladoc/SymbolTablePrinter.java
blob: b7c7621148ed5528d5b67874fffdc79c076a86bc (plain) (blame)
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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scala.tools.scaladoc;

import java.io.StringWriter;

import ch.epfl.lamp.util.CodePrinter;
import ch.epfl.lamp.util.HTMLPrinter;

import scalac.Global;
import scalac.symtab.*;
import scalac.symtab.Scope.SymbolIterator;
import scalac.symtab.Type.Constraint;
import scalac.util.Debug;
import scalac.util.Name;
import scalac.util.NameTransformer;

// Lines modified or added by Vincent are marked "vincent".

/**
 * This class provides methods to print symbols and types.
 */
public class SymbolTablePrinter extends scalac.symtab.SymbolTablePrinter {

    //########################################################################
    // Private Fields

    /** The global environment */
    private final Global global;

    /** The underlying code printer */
    private final HTMLPrinter page;

    /** The HTML generator using this object */ // vincent
    HTMLGenerator htmlGenerator;

    //########################################################################
    // Public Constructors

    /**
     * Creates a new instance.
     *
     * @param htmlgenerator
     * @param page
     */
    public SymbolTablePrinter(HTMLGenerator generator, HTMLPrinter page) {
        super(page.getCodePrinter());
        this.global = Global.instance;
	this.page = page;
        this.htmlGenerator = generator;
    }

    //########################################################################
    // Public Methods - Printing scopes

    /**
     * Prints the members of the given scope.
     *
     * @param scope
     */
    public SymbolTablePrinter printScope(Scope scope) {
        return printScope(scope, false);
    }

    /**
     * Prints the members of the given scope.
     *
     * @param scope
     * @param lazy
     */
    public SymbolTablePrinter printScope(Scope scope, boolean lazy) {
        boolean first = true;
        for (SymbolIterator i = scope.iterator(); i.hasNext(); ) {
            Symbol member = i.next();
            if (!mustShowMember(member)) continue;
            if (first) print("{").indent(); else print(", ");
            first = false;
            line();
            printSignature(member, Symbol.NONE, false);
        }
        if (!first)
            line().undent().print("}");
        else if (!lazy)
            print("{}");
        return this;
    }

    /**
     * Returns <code>true</code> iff the given member must be printed.
     * The default implementation always returns <code>true</code>.
     *
     * @param member
     */
    public boolean mustShowMember(Symbol member) {
        return true;
    }

    //########################################################################
    // Public Methods - Printing symbols

    /**
     * Returns the inner string of the given symbol.
     *
     * @param symbol
     */
    public String getSymbolInnerString(Symbol symbol) {
        if (symbol.kind == Kinds.TYPE)
            return "&lt;:"; // HTML encoded "<:" symbol
        else
            return super.getSymbolInnerString(symbol);
    }

    /**
     * Prints the name of the given symbol usage.
     *
     * @param symbol
     * @param user
     */
    public SymbolTablePrinter printUsedSymbolName(Symbol symbol, Symbol user) {
        Name name = symbol.name;
        if (!global.debug) name = NameTransformer.decode(name);
        String s = name.toString();
        if (htmlGenerator.isReferenced(symbol))
            page.printAhref(htmlGenerator.ref(symbol, user), s);
	else
	    page.print(s);
        printSymbolUniqueId(symbol);
        return this;
    }

    /**
     * Prints the name of the given symbol definition.
     *
     * @param symbol
     * @param user
     * @param addLink
     */
    public SymbolTablePrinter printDefinedSymbolName(Symbol symbol, Symbol user, boolean addLink) {
        Name name = symbol.name;
        if (!global.debug) name = NameTransformer.decode(name);
        String s = name.toString();
        if (htmlGenerator.isReferenced(symbol))
            if (addLink)
                page.printAhref(htmlGenerator.ref(symbol, user), s);
            else
	        page.printBold(s);
	else
	    page.print(s);
        printSymbolUniqueId(symbol);
        return this;
    }

    /**
     * Prints the type of the given symbol with the given inner string.
     *
     * @param symbol
     * @param inner
     */
    public SymbolTablePrinter printSymbolType(Symbol symbol, String inner, Symbol user) {
        Type type = symbol.rawFirstInfo();
        if (!(type instanceof Type.LazyType)) type = symbol.info();
        boolean star = false;
        if ((symbol.flags & Modifiers.REPEATED) != 0 &&
            type.symbol() == global.definitions.SEQ_CLASS &&
            type.typeArgs().length == 1)
        {
            type = type.typeArgs()[0];
            star = true;
        }
	printType(type, inner, user);
        if (star) print("*");
        return this;
    }

    /**
     * Prints the signature of the given symbol.
     *
     * @param symbol
     * @param addLink
     */
    public SymbolTablePrinter printSignature(Symbol symbol, Symbol user, boolean addLink) {
        String keyword = getSymbolKeyword(symbol);
        if (keyword != null) print(keyword).space();
        String inner = getSymbolInnerString(symbol);
        String name = symbol.nameString();
        if (addLink)
            page.printAhref("#" + name, name);
	else
            page.print(name);
        return printType(symbol.loBound(), ">:", user)
	    .printSymbolType(symbol, inner, user);
    }

    /**
     * Prints the signature of the given symbol.
     *
     * @param symbol
     * @param addLink
     */
    public SymbolTablePrinter printShortSignature(Symbol symbol, Symbol user, boolean addLink) {
        String keyword = getSymbolKeyword(symbol);
        if (keyword != null) print(keyword).space();
        String inner = getSymbolInnerString(symbol);
        String name = symbol.nameString();
        if (addLink)
            page.printAhref("#" + name, name);
        else
            page.print(name);
        return printType(symbol.loBound(), ">:", user);
    }

    /**
     * Prints the signature of a class symbol.
     *
     * @param symbol
     * @param addLink
     * @return the current symbol table printer
     */
    public SymbolTablePrinter printTemplateSignature(Symbol symbol, Symbol user, boolean addLink) {
        // kind
	String keyword = getSymbolKeyword(symbol);
        if (keyword != null) print(keyword).space();
        String inner = getSymbolInnerString(symbol);

	// name
	printDefinedSymbolName(symbol, user, addLink);
	if (symbol.isClass()) {
	    // type parameters
	    Symbol[] tparams = symbol.typeParams();
	    if (tparams.length != 0 || global.debug) {
		print('[');
		for (int i = 0; i < tparams.length; i++) {
		    if (i > 0) print(",");
		    printSignature(tparams[i], user, false);
		}
		print(']');
	    }
	    // value parameters
	    Symbol[] vparams = symbol.valueParams();
	    print('(');
	    for (int i = 0; i < vparams.length; i++) {
		if (i > 0) print(", "); // vincent
		if (vparams[i].isDefParameter()) print("def ");
		htmlGenerator.defString(vparams[i], user, false);
	    }
	    print(')');
	}

	// parents
// 	Type[] parts = symbol.moduleClass().parents();
// 	if (parts.length != 0) space().print(inner).space();
// 	printTypes(parts," with ");
	return this;
    }

    /**
     * Prints the signature of a class symbol.
     *
     * @param symbol
     * @param addLink
     */
    public SymbolTablePrinter printTemplateHtmlSignature(Symbol symbol, Symbol user, boolean addLink) {
	// modifiers
        String mods = Modifiers.Helper.toString(symbol.flags);
	page.printlnOTag("dl");
	page.printlnOTag("dt");
	print(mods).space();

        // kind
	String keyword = getSymbolKeyword(symbol);
        if (keyword != null) print(keyword).space();
        String inner = getSymbolInnerString(symbol);

        // name
	printDefinedSymbolName(symbol, user, addLink);
	if (symbol.isClass()) {
	    // type parameters
	    Symbol[] tparams = symbol.typeParams();
	    if (tparams.length != 0 || global.debug) {
		print('[');
		for (int i = 0; i < tparams.length; i++) {
		    if (i > 0) print(",");
		    printSignature(tparams[i], user, false);
		}
		print(']');
	    }
	    // value parameters
	    Symbol[] vparams = symbol.valueParams();
	    print('(');
	    for (int i = 0; i < vparams.length; i++) {
		if (i > 0) print(", ");
		if (vparams[i].isDefParameter()) print("def ");
		htmlGenerator.defString(vparams[i], user, false);
	    }
	    print(')');
	}

        // parents
        Type[] parts = symbol.moduleClass().parents();
        page.printlnCTag("dt");
        for (int i = 0; i < parts.length; i++) {
            page.printOTag("dd");
            print((i == 0) ? "extends " : "with ");
            printType(parts[i], symbol);
	    page.printlnCTag("dd");
	}
	page.printCTag("dl");
	return this;
    }

    //########################################################################
    // Public Methods - Printing types

    /**
     * Prints the given types separated by infix.
     *
     * @param types
     * @param infix
     */
    public SymbolTablePrinter printTypes(Type[] types, String infix, Symbol user) {
        for (int i = 0; i < types.length; i++) {
            if (i > 0) print(infix);
            printType(types[i], user);
        }
        return this;
    }

    /**
     * Prints the given type.
     *
     * @param type
     * @param user The symbol using the type <code>type</code>
     */
    public SymbolTablePrinter printType(Type type, Symbol user) {
        return printType0(getTypeToPrintForType(type), user);
    }

    /**
     * ..
     *
     * @param type
     * @param user
     */
    public SymbolTablePrinter printType0(Type type, Symbol user) {
        printCommonPart(type, user);
        switch (type) {
        case ThisType(_):
        case SingleType(_,_):
            print(".type");
            return this;
        default:
            return this;
        }
    }

    /**
     * Prints the given type with the given inner string.
     *
     * @param type
     * @param inner
     */
    public SymbolTablePrinter printType(Type type, String inner, Symbol user) {
	if ("<:".equals(inner) && type.symbol() == global.definitions.ANY_CLASS ||
	    ">:".equals(inner) && type.symbol() == global.definitions.ALL_CLASS)
	    return this;
        else
	    return printType0(getTypeToPrintForType(type), inner, user);
    }

    /**
     * ..
     *
     * @param type
     * @param inner
     */
    public SymbolTablePrinter printType0(Type type, String inner, Symbol user) {
        switch (type) {
        case MethodType(Symbol[] vparams, Type result):
            print('(');
            for (int i = 0; i < vparams.length; i++) {
                if (i > 0) print(", ");
		if (vparams[i].isDefParameter()) print("def ");
		htmlGenerator.defString(vparams[i], user, false);
		//print(NameTransformer.decode(vparams[i].name.toString()) + ": ");// vincent
                //printSymbolType(vparams[i], null);
            }
            print(')');
            return printType(result, inner, user);
        case PolyType(Symbol[] tparams, Type result):
            if (tparams.length != 0 || global.debug) {
                print('[');
                for (int i = 0; i < tparams.length; i++) {
                    if (i > 0) print(",");
                    printSignature(tparams[i], user, false);
                }
                print(']');
            }
            return printType(result, inner, user);
        default:
            if (inner != null) {
                if (!inner.startsWith(":")) space();
                print(inner).space();
            }
            return printType0(type, user);
        }
    }

    /**
     * Prints a function type with the given type arguments.
     *
     * @param types
     */
    public SymbolTablePrinter printFunctionType(Type[] types) {
        Type[] args = new Type[types.length - 1];
        for (int i = 0; i < args.length; i++) args[i] = types[i];
        print('(');
        printTypes(args, ", ", Symbol.NONE);
        print(") => ");
        printType(types[types.length - 1], Symbol.NONE);
        return this;
    }

    /**
     * Prints a template type with the given base types.
     *
     * @param types
     */
    public SymbolTablePrinter printTemplateType(Type[] types) {
        print("<template: ");
        printTypes(types, " with ", Symbol.NONE);
        print(" {...}>");
        return this;
    }

    /**
     * Prints the type and prefix common part of the given type.
     *
     * @param type
     * @param user The symbol using the type <code>type</code>
     */
    public SymbolTablePrinter printCommonPart(Type type, Symbol user) {
        switch (type) {
	case ErrorType:
	    print("<error>");
            return this;

	case AnyType:
	    print("<any type>");
            return this;

	case NoType:
	    print("<notype>");
            return this;

	case ThisType(Symbol sym):
            if (sym == Symbol.NONE) print("<local>.this");
            if ((sym.isAnonymousClass() || sym.isCompoundSym()) && !global.debug)
		print("this");
            printUsedSymbolName(sym, user);
            print(".this"); // vincent
            return this;

	case TypeRef(Type pre, Symbol sym, Type[] args):
	    if (!global.debug) {
                if (type.isFunctionType()) {
                    printFunctionType(args);
		    return this;
		}
		if (sym.isAnonymousClass() || sym.isCompoundSym())
                    printTemplateType(pre.memberInfo(sym).parents());
            }
            printPrefix(pre, sym);
            printUsedSymbolName(sym, user);
	    if (args.length != 0) {
                print('[');
                printTypes(args, ",", user);
                print(']');
            }
            return this;

	case SingleType(Type pre, Symbol sym):
            printPrefix(pre, sym);
            printUsedSymbolName(sym, user);
            return this;

	case CompoundType(Type[] parts, Scope members):
	    printTypes(parts," with ", user);
            space();
            return printScope(members, true); // vincent

	case MethodType(_, _):
	    return printType0(type, user);

	case PolyType(_, _):
	    return printType0(type, user);

	case OverloadedType(Symbol[] alts, Type[] alttypes):
            return printTypes(alttypes, " <and> ", user);

	case TypeVar(Type origin, Constraint constr):
            printType(origin, user);
            print("?");
            return this;

	case UnboxedType(int kind):
	    print(type.unboxedName(kind).toString());
            return this;

	case UnboxedArrayType(Type elemtp):
	    printType(elemtp, user);
            print("[]");
            return this;

	case LazyType():
            if (!global.debug) print("?");
            String classname = type.getClass().getName();
            print("<lazy type ").print(classname).print(">");
            return this;

	default:
            String classname = type.getClass().getName();
	    print("<unknown type ").print(classname).print(">");
            return this;
        }
    }

    //########################################################################
    // Public Methods - Printing prefixes

    /**
     * Returns the type to print for the given prefix (transitive).
     *
     * @param prefix
     * @param sym
     */
    public Type getTypeToPrintForPrefix(Type prefix, Symbol sym) {
        while (true) {
            Type result = getTypeToPrintForPrefix0(prefix);
            if (result == prefix || result == null) {
		return
		    htmlGenerator.cleanPrefix(result); // vincent
		// Next lines should replace the previous one in theory.
		// 		    htmlGenerator.isReferenced(sym) ?
		// 		    htmlGenerator.cleanPrefix(result) : // vincent
		// 		    result;
	    }
            prefix = result;
	}
    }

    /**
     * Prints the given type as a prefix.
     *
     * @param prefix
     */
    public SymbolTablePrinter printPrefix(Type prefix, Symbol sym) {
        prefix = getTypeToPrintForPrefix(prefix, sym);
        return prefix == null ? this : printPrefix0(prefix);
    }

    /**
     * Returns the type to print for the given prefix (non-transitive).
     *
     * @param prefix
     */
    public Type getTypeToPrintForPrefix0(Type prefix) {
        if (!global.debug) {
            if (prefix.symbol().kind == Kinds.NONE) return null;
            if (prefix.symbol().isRoot()) return null;
        }
        return getTypeToPrintForType0(prefix);
    }

    /**
     * ..
     */
    public SymbolTablePrinter printPrefix0(Type prefix) {
        printCommonPart(prefix, Symbol.NONE);
        switch (prefix) {
        case ThisType(_):
        case SingleType(_,_):
            print(".");
            return this;
        default:
            print("#");
            return this;
        }
    }

    //########################################################################
    // Public Methods - Converting

    /**
     * Returns the string representation of this.
     */
    public String toString() {
        return toString();
    }

    //########################################################################
}