Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 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 | ||
f357bcd4 | 13 | package org.eclipse.tracecompass.ctf.core.event.types; |
866e5b51 | 14 | |
a4fa4e36 MK |
15 | import java.util.Collections; |
16 | import java.util.LinkedList; | |
2b7f6f09 | 17 | import java.util.List; |
843f986b | 18 | import java.util.Map; |
866e5b51 | 19 | |
a4fa4e36 | 20 | import org.eclipse.jdt.annotation.NonNull; |
f357bcd4 | 21 | import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope; |
fbe6fa6f | 22 | import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope; |
a4fa4e36 MK |
23 | |
24 | import com.google.common.base.Joiner; | |
25 | import com.google.common.collect.ImmutableList; | |
26 | import com.google.common.collect.ImmutableMap; | |
27 | import com.google.common.collect.ImmutableMap.Builder; | |
866e5b51 FC |
28 | |
29 | /** | |
d37aaa7f | 30 | * A CTF structure definition (similar to a C structure). |
486efb2e | 31 | * |
d37aaa7f FC |
32 | * A structure is similar to a C structure, it is a compound data type that |
33 | * contains other datatypes in fields. they are stored in an hashmap and indexed | |
34 | * by names which are strings. | |
35 | * | |
6c7592e1 MK |
36 | * TODO: move me to internal |
37 | * | |
d37aaa7f FC |
38 | * @version 1.0 |
39 | * @author Matthew Khouzam | |
40 | * @author Simon Marchi | |
866e5b51 | 41 | */ |
009883d7 | 42 | public final class StructDefinition extends ScopedDefinition implements ICompositeDefinition { |
866e5b51 FC |
43 | |
44 | // ------------------------------------------------------------------------ | |
45 | // Attributes | |
46 | // ------------------------------------------------------------------------ | |
47 | ||
a4fa4e36 MK |
48 | private final ImmutableList<String> fFieldNames; |
49 | private final Definition[] fDefinitions; | |
50 | private Map<String, Definition> fDefinitionsMap = null; | |
866e5b51 FC |
51 | |
52 | // ------------------------------------------------------------------------ | |
53 | // Constructors | |
54 | // ------------------------------------------------------------------------ | |
55 | ||
9ac2eb62 MK |
56 | /** |
57 | * Constructor | |
58 | * | |
59 | * @param declaration | |
60 | * the parent declaration | |
61 | * @param definitionScope | |
62 | * the parent scope | |
be6df2d8 | 63 | * @param structFieldName |
9ac2eb62 | 64 | * the field name |
a4fa4e36 MK |
65 | * @param fieldNames |
66 | * the list of fields | |
67 | * @param definitions | |
68 | * the definitions | |
9ac2eb62 | 69 | */ |
a4fa4e36 | 70 | public StructDefinition(@NonNull StructDeclaration declaration, |
2db699c2 AM |
71 | IDefinitionScope definitionScope, |
72 | @NonNull String structFieldName, | |
73 | Iterable<String> fieldNames, | |
74 | Definition[] definitions) { | |
a4fa4e36 MK |
75 | super(declaration, definitionScope, structFieldName); |
76 | fFieldNames = ImmutableList.copyOf(fieldNames); | |
77 | fDefinitions = definitions; | |
70f60307 MK |
78 | if (fFieldNames.isEmpty()) { |
79 | fDefinitionsMap = Collections.EMPTY_MAP; | |
80 | } | |
81 | } | |
82 | ||
83 | /** | |
84 | * Constructor This one takes the scope and thus speeds up definition | |
85 | * creation | |
86 | * | |
87 | * @param declaration | |
88 | * the parent declaration | |
89 | * @param definitionScope | |
90 | * the parent scope | |
91 | * @param scope | |
92 | * the scope of this variable | |
93 | * @param structFieldName | |
94 | * the field name | |
95 | * @param fieldNames | |
96 | * the list of fields | |
97 | * @param definitions | |
98 | * the definitions | |
fbe6fa6f | 99 | * @since 1.0 |
70f60307 MK |
100 | */ |
101 | public StructDefinition(@NonNull StructDeclaration declaration, | |
fbe6fa6f | 102 | IDefinitionScope definitionScope, @NonNull ILexicalScope scope, |
70f60307 MK |
103 | @NonNull String structFieldName, @NonNull Iterable<String> fieldNames, Definition[] definitions) { |
104 | super(declaration, definitionScope, structFieldName, scope); | |
105 | fFieldNames = ImmutableList.copyOf(fieldNames); | |
106 | fDefinitions = definitions; | |
009883d7 | 107 | if (fFieldNames.isEmpty()) { |
a4fa4e36 | 108 | fDefinitionsMap = Collections.EMPTY_MAP; |
866e5b51 FC |
109 | } |
110 | } | |
111 | ||
112 | // ------------------------------------------------------------------------ | |
113 | // Getters/Setters/Predicates | |
114 | // ------------------------------------------------------------------------ | |
115 | ||
009883d7 | 116 | @Override |
a4fa4e36 MK |
117 | public Definition getDefinition(String fieldName) { |
118 | if (fDefinitionsMap == null) { | |
2db699c2 AM |
119 | /* Build the definitions map */ |
120 | Builder<String, Definition> mapBuilder = new ImmutableMap.Builder<>(); | |
121 | for (int i = 0; i < fFieldNames.size(); i++) { | |
122 | if (fDefinitions[i] != null) { | |
123 | mapBuilder.put(fFieldNames.get(i), fDefinitions[i]); | |
124 | } | |
a4fa4e36 | 125 | } |
2db699c2 | 126 | fDefinitionsMap = mapBuilder.build(); |
a4fa4e36 | 127 | } |
2db699c2 | 128 | return fDefinitionsMap.get(fieldName); |
a4fa4e36 MK |
129 | } |
130 | ||
009883d7 | 131 | @Override |
a4fa4e36 MK |
132 | public List<String> getFieldNames() { |
133 | return fFieldNames; | |
866e5b51 FC |
134 | } |
135 | ||
9ac2eb62 | 136 | @Override |
866e5b51 | 137 | public StructDeclaration getDeclaration() { |
a4fa4e36 | 138 | return (StructDeclaration) super.getDeclaration(); |
866e5b51 FC |
139 | } |
140 | ||
141 | // ------------------------------------------------------------------------ | |
142 | // Operations | |
143 | // ------------------------------------------------------------------------ | |
144 | ||
866e5b51 FC |
145 | @Override |
146 | public Definition lookupDefinition(String lookupPath) { | |
147 | /* | |
148 | * The fields are created in order of appearance, so if a variant or | |
149 | * sequence refers to a field that is after it, the field's definition | |
150 | * will not be there yet in the hashmap. | |
151 | */ | |
a4fa4e36 MK |
152 | int val = fFieldNames.indexOf(lookupPath); |
153 | if (val != -1) { | |
154 | return fDefinitions[val]; | |
91847dfc | 155 | } |
a4fa4e36 MK |
156 | String lookupUnderscored = "_" + lookupPath; //$NON-NLS-1$ |
157 | val = fFieldNames.indexOf(lookupUnderscored); | |
158 | if (val != -1) { | |
159 | return fDefinitions[val]; | |
160 | } | |
161 | return null; | |
866e5b51 FC |
162 | } |
163 | ||
866e5b51 FC |
164 | @Override |
165 | public String toString() { | |
166 | StringBuilder builder = new StringBuilder(); | |
167 | ||
419f09a8 | 168 | builder.append("{ "); //$NON-NLS-1$ |
866e5b51 | 169 | |
a4fa4e36 MK |
170 | if (fFieldNames != null) { |
171 | List<String> fields = new LinkedList<>(); | |
172 | for (String field : fFieldNames) { | |
173 | String appendee = field + " = " + lookupDefinition(field).toString(); //$NON-NLS-1$ | |
174 | fields.add(appendee); | |
866e5b51 | 175 | } |
a4fa4e36 MK |
176 | Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$ |
177 | builder.append(joiner.join(fields)); | |
866e5b51 FC |
178 | } |
179 | ||
419f09a8 | 180 | builder.append(" }"); //$NON-NLS-1$ |
866e5b51 FC |
181 | |
182 | return builder.toString(); | |
183 | } | |
a4fa4e36 | 184 | |
866e5b51 | 185 | } |