Makefile fixup
[org.ibex.tool.git] / repo / org.ibex.tool / src / org / eclipse / jdt / internal / compiler / ast / StringLiteralConcatenation.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.lookup.BlockScope;
15
16 /**
17  * Flatten string literal
18  */
19 public class StringLiteralConcatenation extends StringLiteral {
20         private static final int INITIAL_SIZE = 5;
21         public StringLiteral[] literals;
22         public int counter;
23         /**     
24          * Build a two-strings literal
25          * */
26         public StringLiteralConcatenation(StringLiteral str1, StringLiteral str2) {
27                 super(str1.sourceStart, str1.sourceEnd);
28                 this.source = str1.source;
29                 this.literals = new StringLiteral[INITIAL_SIZE];
30                 this.counter = 0;
31                 this.literals[this.counter++] = str1;
32                 extendsWith(str2);
33         }
34
35         /**
36          *  Add the lit source to mine, just as if it was mine
37          */
38         public StringLiteralConcatenation extendsWith(StringLiteral lit) {
39                 this.sourceEnd = lit.sourceEnd;
40                 final int literalsLength = this.literals.length;
41                 if (this.counter == literalsLength) {
42                         // resize
43                         System.arraycopy(this.literals, 0, this.literals = new StringLiteral[literalsLength + INITIAL_SIZE], 0, literalsLength);
44                 }
45                 //uddate the source
46                 int length = source.length;
47                 System.arraycopy(
48                         source,
49                         0,
50                         source = new char[length + lit.source.length],
51                         0,
52                         length);
53                 System.arraycopy(lit.source, 0, source, length, lit.source.length);
54                 this.literals[this.counter++] = lit;            
55                 return this;
56         }
57         
58         public StringBuffer printExpression(int indent, StringBuffer output) {
59                 output.append("StringLiteralConcatenation{"); //$NON-NLS-1$
60                 for (int i = 0, max = this.counter; i < max; i++) {
61                         this.literals[i].printExpression(indent, output);
62                         output.append("+\n");//$NON-NLS-1$
63                 }
64                 return output.append('}');
65         }
66         
67         public char[] source() {
68                 return source;
69         }
70
71         public void traverse(ASTVisitor visitor, BlockScope scope) {
72                 if (visitor.visit(this, scope)) {
73                         for (int i = 0, max = this.counter; i < max; i++) {
74                                 this.literals[i].traverse(visitor, scope);
75                         }                       
76                 }
77                 visitor.endVisit(this, scope);
78         }
79 }