Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.event.types; | |
14 | ||
15 | /** | |
d37aaa7f FC |
16 | * A CTF array declaration |
17 | * | |
9639bf3a MK |
18 | * Arrays are fixed-length. Their length is declared in the type declaration |
19 | * within the meta-data. They contain an array of "inner type" elements, which | |
20 | * can refer to any type not containing the type of the array being declared (no | |
21 | * circular dependency). The length is the number of elements in an array. | |
d37aaa7f FC |
22 | * |
23 | * @version 1.0 | |
24 | * @author Matthew Khouzam | |
25 | * @author Simon Marchi | |
866e5b51 FC |
26 | */ |
27 | public class ArrayDeclaration implements IDeclaration { | |
28 | ||
29 | // ------------------------------------------------------------------------ | |
30 | // Attributes | |
31 | // ------------------------------------------------------------------------ | |
32 | ||
33 | private final int length; | |
34 | private final IDeclaration elemType; | |
35 | ||
36 | // ------------------------------------------------------------------------ | |
37 | // Constructors | |
38 | // ------------------------------------------------------------------------ | |
39 | ||
9ac2eb62 MK |
40 | /** |
41 | * Constructor | |
9639bf3a MK |
42 | * |
43 | * @param length | |
44 | * how many elements in the array | |
45 | * @param elemType | |
46 | * what type of element is in the array | |
9ac2eb62 | 47 | */ |
866e5b51 FC |
48 | public ArrayDeclaration(int length, IDeclaration elemType) { |
49 | this.length = length; | |
50 | this.elemType = elemType; | |
51 | } | |
52 | ||
53 | // ------------------------------------------------------------------------ | |
54 | // Getters/Setters/Predicates | |
55 | // ------------------------------------------------------------------------ | |
56 | ||
9ac2eb62 MK |
57 | /** |
58 | * | |
59 | * @return the type of element in the array | |
60 | */ | |
866e5b51 FC |
61 | public IDeclaration getElementType() { |
62 | return elemType; | |
63 | } | |
64 | ||
9ac2eb62 MK |
65 | /** |
66 | * | |
67 | * @return how many elements in the array | |
68 | */ | |
866e5b51 FC |
69 | public int getLength() { |
70 | return length; | |
71 | } | |
72 | ||
9639bf3a MK |
73 | /** |
74 | * Sometimes, strings are encoded as an array of 1-byte integers (each one | |
75 | * being an UTF-8 byte). | |
76 | * | |
77 | * @return true if this array is in fact an UTF-8 string. false if it's a | |
78 | * "normal" array of generic Definition's. | |
79 | * @since 3.0 | |
80 | */ | |
81 | public boolean isString() { | |
82 | if (elemType instanceof IntegerDeclaration) { | |
83 | /* | |
84 | * If the first byte is a "character", we'll consider the whole | |
85 | * array a character string. | |
86 | */ | |
87 | IntegerDeclaration elemInt = (IntegerDeclaration) elemType; | |
88 | if (elemInt.isCharacter()) { | |
89 | return true; | |
90 | } | |
91 | } | |
92 | return false; | |
93 | } | |
94 | ||
fd74e6c1 MK |
95 | @Override |
96 | public long getAlignment() { | |
0594c61c | 97 | return getElementType().getAlignment(); |
fd74e6c1 | 98 | } |
9639bf3a | 99 | |
866e5b51 FC |
100 | // ------------------------------------------------------------------------ |
101 | // Operations | |
102 | // ------------------------------------------------------------------------ | |
103 | ||
104 | @Override | |
105 | public ArrayDefinition createDefinition(IDefinitionScope definitionScope, | |
106 | String fieldName) { | |
107 | return new ArrayDefinition(this, definitionScope, fieldName); | |
108 | } | |
109 | ||
110 | @Override | |
111 | public String toString() { | |
112 | /* Only used for debugging */ | |
113 | return "[declaration] array[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
114 | } | |
115 | ||
116 | } |