001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.compiler.ast;
011:
012: import org.eclipse.jdt.internal.compiler.ASTVisitor;
013: import org.eclipse.jdt.internal.compiler.impl.*;
014: import org.eclipse.jdt.internal.compiler.codegen.*;
015: import org.eclipse.jdt.internal.compiler.lookup.*;
016: import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
017:
018: public class CharLiteral extends NumberLiteral {
019: char value;
020:
021: public CharLiteral(char[] token, int s, int e) {
022: super (token, s, e);
023: computeValue();
024: }
025:
026: public void computeConstant() {
027: //The source is a char[3] first and last char are '
028: //This is true for both regular char AND unicode char
029: //BUT not for escape char like '\b' which are char[4]....
030:
031: constant = CharConstant.fromValue(value);
032: }
033:
034: private void computeValue() {
035: //The source is a char[3] first and last char are '
036: //This is true for both regular char AND unicode char
037: //BUT not for escape char like '\b' which are char[4]....
038:
039: if ((value = source[1]) != '\\')
040: return;
041: char digit;
042: switch (digit = source[2]) {
043: case 'b':
044: value = '\b';
045: break;
046: case 't':
047: value = '\t';
048: break;
049: case 'n':
050: value = '\n';
051: break;
052: case 'f':
053: value = '\f';
054: break;
055: case 'r':
056: value = '\r';
057: break;
058: case '\"':
059: value = '\"';
060: break;
061: case '\'':
062: value = '\'';
063: break;
064: case '\\':
065: value = '\\';
066: break;
067: default: //octal (well-formed: ended by a ' )
068: int number = ScannerHelper.getNumericValue(digit);
069: if ((digit = source[3]) != '\'')
070: number = (number * 8)
071: + ScannerHelper.getNumericValue(digit);
072: else {
073: constant = CharConstant
074: .fromValue(value = (char) number);
075: break;
076: }
077: if ((digit = source[4]) != '\'')
078: number = (number * 8)
079: + ScannerHelper.getNumericValue(digit);
080: value = (char) number;
081: break;
082: }
083: }
084:
085: /**
086: * CharLiteral code generation
087: *
088: * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
089: * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
090: * @param valueRequired boolean
091: */
092: public void generateCode(BlockScope currentScope,
093: CodeStream codeStream, boolean valueRequired) {
094: int pc = codeStream.position;
095: if (valueRequired) {
096: codeStream.generateConstant(constant, implicitConversion);
097: }
098: codeStream.recordPositionsFrom(pc, this .sourceStart);
099: }
100:
101: public TypeBinding literalType(BlockScope scope) {
102: return TypeBinding.CHAR;
103: }
104:
105: public void traverse(ASTVisitor visitor, BlockScope blockScope) {
106: visitor.visit(this, blockScope);
107: visitor.endVisit(this, blockScope);
108: }
109: }
|