4c5b042f81201da03ee9c4dfaeba0cfa5fdad47b
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / ClassLiteralAccess.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v0.5 
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v05.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
12
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.codegen.CodeStream;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
17 import net.sourceforge.phpdt.internal.compiler.lookup.ArrayBinding;
18 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
19 import net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.SourceTypeBinding;
21 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
22
23 public class ClassLiteralAccess extends Expression {
24         
25         public TypeReference type;
26         public TypeBinding targetType;
27         FieldBinding syntheticField;
28
29         public ClassLiteralAccess(int sourceEnd, TypeReference t) {
30                 type = t;
31                 this.sourceStart = t.sourceStart;
32                 this.sourceEnd = sourceEnd;
33         }
34
35         public FlowInfo analyseCode(
36                 BlockScope currentScope,
37                 FlowContext flowContext,
38                 FlowInfo flowInfo) {
39
40                 // if reachable, request the addition of a synthetic field for caching the class descriptor
41                 SourceTypeBinding sourceType =
42                         currentScope.outerMostMethodScope().enclosingSourceType();
43                 if (!(sourceType.isInterface()
44                         // no field generated in interface case (would'nt verify) see 1FHHEZL
45                         || sourceType.isBaseType())) {
46                         syntheticField = sourceType.addSyntheticField(targetType, currentScope);
47                 }
48                 return flowInfo;
49         }
50
51         /**
52          * MessageSendDotClass code generation
53          *
54          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
55          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
56          * @param valueRequired boolean
57          */
58         public void generateCode(
59                 BlockScope currentScope,
60                 CodeStream codeStream,
61                 boolean valueRequired) {
62                 int pc = codeStream.position;
63
64                 // in interface case, no caching occurs, since cannot make a cache field for interface
65                 if (valueRequired)
66                         codeStream.generateClassLiteralAccessForType(type.binding, syntheticField);
67                 codeStream.recordPositionsFrom(pc, this.sourceStart);
68         }
69
70         public TypeBinding resolveType(BlockScope scope) {
71
72                 constant = NotAConstant;
73                 if ((targetType = type.resolveType(scope)) == null)
74                         return null;
75
76                 if (targetType.isArrayType()
77                         && ((ArrayBinding) targetType).leafComponentType == VoidBinding) {
78                         scope.problemReporter().cannotAllocateVoidArray(this);
79                         return null;
80                 }
81
82                 return scope.getJavaLangClass();
83         }
84
85         public String toStringExpression() {
86
87                 String s = ""; //$NON-NLS-1$
88                 s = s + type.toString(0) + ".class"; //$NON-NLS-1$
89                 return s;
90         }
91
92         public void traverse(
93                 IAbstractSyntaxTreeVisitor visitor,
94                 BlockScope blockScope) {
95
96                 if (visitor.visit(this, blockScope)) {
97                         type.traverse(visitor, blockScope);
98                 }
99                 visitor.endVisit(this, blockScope);
100         }
101 }