| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others |
| 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 | import org.eclipse.jdt.annotation.NonNull; |
| 16 | import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; |
| 17 | import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; |
| 18 | |
| 19 | /** |
| 20 | * A CTF definition |
| 21 | * |
| 22 | * A definition is like an object of a declaration class. It fills the |
| 23 | * declaration with values. <br> |
| 24 | * An example: <br> |
| 25 | * int i = 0; <br> |
| 26 | * <b>int</b> is the declaration.<br> |
| 27 | * <b>i</b> is the definition.<br> |
| 28 | * <b>0</b> is the value assigned to the definition, not the declaration.<br> |
| 29 | * |
| 30 | * @version 1.0 |
| 31 | * @author Matthew Khouzam |
| 32 | * @author Simon Marchi |
| 33 | */ |
| 34 | public abstract class Definition implements IDefinition { |
| 35 | |
| 36 | // ------------------------------------------------------------------------ |
| 37 | // Attributes |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | private final String fFieldName; |
| 41 | |
| 42 | /** The complete path of this field */ |
| 43 | private final @NonNull LexicalScope fPath; |
| 44 | |
| 45 | private final IDefinitionScope fDefinitionScope; |
| 46 | |
| 47 | @NonNull |
| 48 | private final IDeclaration fDeclaration; |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | // Constructors |
| 52 | // ------------------------------------------------------------------------ |
| 53 | |
| 54 | /** |
| 55 | * Constructor |
| 56 | * |
| 57 | * @param declaration |
| 58 | * the event declaration |
| 59 | * @param definitionScope |
| 60 | * the definition is in a scope, (normally a struct) what is it? |
| 61 | * @param fieldName |
| 62 | * the name of the definition. (it is a field in the parent |
| 63 | * scope) |
| 64 | * @since 3.0 |
| 65 | */ |
| 66 | public Definition(@NonNull IDeclaration declaration, IDefinitionScope definitionScope, @NonNull String fieldName) { |
| 67 | this(declaration, definitionScope, fieldName, declaration.getPath(definitionScope, fieldName)); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Constructor This one takes the scope and thus speeds up definition |
| 72 | * creation |
| 73 | * |
| 74 | * |
| 75 | * @param declaration |
| 76 | * the event declaration |
| 77 | * |
| 78 | * @param definitionScope |
| 79 | * the definition is in a scope, (normally a struct) what is it? |
| 80 | * |
| 81 | * @param fieldName |
| 82 | * the name of the defintions. it is a field in the parent scope. |
| 83 | * |
| 84 | * @param scope |
| 85 | * the scope |
| 86 | * @since 3.1 |
| 87 | */ |
| 88 | public Definition(@NonNull IDeclaration declaration, IDefinitionScope definitionScope, @NonNull String fieldName, @NonNull LexicalScope scope) { |
| 89 | fDeclaration = declaration; |
| 90 | fDefinitionScope = definitionScope; |
| 91 | fFieldName = fieldName; |
| 92 | fPath = scope; |
| 93 | } |
| 94 | |
| 95 | // ------------------------------------------------------------------------ |
| 96 | // Getters |
| 97 | // ------------------------------------------------------------------------ |
| 98 | |
| 99 | /** |
| 100 | * Get the field name in its container. |
| 101 | * |
| 102 | * @return The field name |
| 103 | * @since 2.0 |
| 104 | */ |
| 105 | protected String getFieldName() { |
| 106 | return fFieldName; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public LexicalScope getScopePath() { |
| 111 | return fPath; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get the definition scope in which this definition is found. |
| 116 | * |
| 117 | * The complete path of a definition is thus the path of the definition |
| 118 | * scope DOT the name of the definition (name of the field in its container) |
| 119 | * |
| 120 | * @return The definition scope |
| 121 | * @since 3.0 |
| 122 | */ |
| 123 | protected IDefinitionScope getDefinitionScope() { |
| 124 | return fDefinitionScope; |
| 125 | } |
| 126 | |
| 127 | // ------------------------------------------------------------------------ |
| 128 | // Operations |
| 129 | // ------------------------------------------------------------------------ |
| 130 | |
| 131 | @Override |
| 132 | public IDeclaration getDeclaration() { |
| 133 | return fDeclaration; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public String toString() { |
| 138 | return fPath.toString() + '[' + Integer.toHexString(hashCode()) + ']'; |
| 139 | } |
| 140 | } |