removed Makefile; lifted repo/org.ibex.tool/src/ to src/
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / CharLiteral.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
12
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.impl.*;
15 import org.eclipse.jdt.internal.compiler.codegen.*;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
17
18 public class CharLiteral extends NumberLiteral {
19         char value;
20 public CharLiteral(char[] token, int s, int e) {
21         super(token, s, e);
22         computeValue();
23 }
24 public void computeConstant() {
25         //The source is a  char[3] first and last char are '
26         //This is true for both regular char AND unicode char
27         //BUT not for escape char like '\b' which are char[4]....
28
29         constant = Constant.fromValue(value);
30 }
31 private void computeValue() {
32         //The source is a  char[3] first and last char are '
33         //This is true for both regular char AND unicode char
34         //BUT not for escape char like '\b' which are char[4]....
35
36         if ((value = source[1]) != '\\')
37                 return;
38         char digit;
39         switch (digit = source[2]) {
40                 case 'b' :
41                         value = '\b';
42                         break;
43                 case 't' :
44                         value = '\t';
45                         break;
46                 case 'n' :
47                         value = '\n';
48                         break;
49                 case 'f' :
50                         value = '\f';
51                         break;
52                 case 'r' :
53                         value = '\r';
54                         break;
55                 case '\"' :
56                         value = '\"';
57                         break;
58                 case '\'' :
59                         value = '\'';
60                         break;
61                 case '\\' :
62                         value = '\\';
63                         break;
64                 default : //octal (well-formed: ended by a ' )
65                         int number = Character.getNumericValue(digit);
66                         if ((digit = source[3]) != '\'')
67                                 number = (number * 8) + Character.getNumericValue(digit);
68                         else {
69                                 constant = Constant.fromValue(value = (char) number);
70                                 break;
71                         }
72                         if ((digit = source[4]) != '\'')
73                                 number = (number * 8) + Character.getNumericValue(digit);
74                         value = (char) number;
75                         break;
76         }
77 }
78 /**
79  * CharLiteral code generation
80  *
81  * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
82  * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
83  * @param valueRequired boolean
84  */
85 public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
86         int pc = codeStream.position;
87         if (valueRequired)
88                 if ((implicitConversion >> 4) == T_char)
89                         codeStream.generateInlinedValue(value);
90                 else
91                         codeStream.generateConstant(constant, implicitConversion);
92         codeStream.recordPositionsFrom(pc, this.sourceStart);
93 }
94 public TypeBinding literalType(BlockScope scope) {
95         return CharBinding;
96 }
97 public void traverse(ASTVisitor visitor, BlockScope blockScope) {
98         visitor.visit(this, blockScope);
99         visitor.endVisit(this, blockScope);
100 }
101 }