tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/source/SourceContext.java
branchRCL_3
changeset 20 ca8a1b6995f6
equal deleted inserted replaced
19:07b41fa8d1dd 20:ca8a1b6995f6
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 * Source context is a representation of a range of source code
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.source;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 import java.util.Collections;
       
    23 import java.util.Iterator;
       
    24 import java.util.List;
       
    25 
       
    26 /**
       
    27  * Source context is a representation of a range of source code. For example a
       
    28  * function in source code could be represented with a context that has the name
       
    29  * of the function
       
    30  * 
       
    31  */
       
    32 public class SourceContext extends SourceLocationBase implements ParsedType {
       
    33 
       
    34 	/**
       
    35 	 * No proposal
       
    36 	 */
       
    37 	public static final int NONE = 0; // CodForChk_Dis_Magic
       
    38 
       
    39 	/**
       
    40 	 * Start of function proposal type
       
    41 	 */
       
    42 	public static final int START_OF_FUNCTION = 1; // CodForChk_Dis_Magic
       
    43 
       
    44 	/**
       
    45 	 * End of function proposal type
       
    46 	 */
       
    47 	public static final int END_OF_FUNCTION = 2; // CodForChk_Dis_Magic
       
    48 
       
    49 	/**
       
    50 	 * Full selection proposal type
       
    51 	 */
       
    52 	public static final int FULL_SELECTION = 3; // CodForChk_Dis_Magic
       
    53 
       
    54 	/**
       
    55 	 * Class name
       
    56 	 */
       
    57 	private String className;
       
    58 
       
    59 	/**
       
    60 	 * Function name
       
    61 	 */
       
    62 	private String functionName;
       
    63 
       
    64 	/**
       
    65 	 * Index to start of parameters
       
    66 	 */
       
    67 	private int parameterStart;
       
    68 
       
    69 	/**
       
    70 	 * List of return types
       
    71 	 */
       
    72 	private ArrayList<String> returnTypes;
       
    73 
       
    74 	/**
       
    75 	 * Creates a new SourceContext starting from given offset
       
    76 	 * 
       
    77 	 * @param parser
       
    78 	 *            the parser owning this context
       
    79 	 * @param offset
       
    80 	 *            the offset
       
    81 	 */
       
    82 	SourceContext(SourceParser parser, int offset) {
       
    83 		super(parser, offset);
       
    84 	}
       
    85 
       
    86 	/**
       
    87 	 * Gets the class name of this context
       
    88 	 * 
       
    89 	 * @return the class name
       
    90 	 */
       
    91 	public String getClassName() {
       
    92 		return className;
       
    93 	}
       
    94 
       
    95 	/**
       
    96 	 * Gets the function name of this context
       
    97 	 * 
       
    98 	 * @return the function name
       
    99 	 */
       
   100 	public String getFunctionName() {
       
   101 		return functionName;
       
   102 	}
       
   103 
       
   104 	/**
       
   105 	 * Sets the class name. If the class name contains pointer or reference
       
   106 	 * characters, they are added to the return statements list
       
   107 	 * 
       
   108 	 * @param name
       
   109 	 *            the new class name
       
   110 	 */
       
   111 	void setClassName(String name) {
       
   112 		className = removePtrAndRef(name);
       
   113 	}
       
   114 
       
   115 	/**
       
   116 	 * Sets the function name. If the function name contains pointer or
       
   117 	 * reference characters, they are added to the return statements list
       
   118 	 * 
       
   119 	 * @param name
       
   120 	 *            the new function name
       
   121 	 */
       
   122 	void setFunctionName(String name) {
       
   123 		functionName = removePtrAndRef(name);
       
   124 	}
       
   125 
       
   126 	/**
       
   127 	 * Removes the * and & characters from the given tag and adds them as return
       
   128 	 * modifiers
       
   129 	 * 
       
   130 	 * @param name
       
   131 	 *            the name
       
   132 	 * @return the name with * and & removed
       
   133 	 */
       
   134 	private String removePtrAndRef(String name) {
       
   135 		String retval = name;
       
   136 		if (name != null) {
       
   137 			int start = 0;
       
   138 			int end = name.length();
       
   139 			boolean found = true;
       
   140 			while (start < end && found) {
       
   141 				char c = name.charAt(start);
       
   142 				if (c == '*' || c == '&') {
       
   143 					addReturnModifier(c);
       
   144 					start++;
       
   145 				} else {
       
   146 					found = false;
       
   147 				}
       
   148 			}
       
   149 			found = true;
       
   150 			while (end > start && found) {
       
   151 				char c = name.charAt(end - 1);
       
   152 				if (c == '*' || c == '&') {
       
   153 					addReturnModifier(c);
       
   154 					end--;
       
   155 				} else {
       
   156 					found = false;
       
   157 				}
       
   158 			}
       
   159 			retval = name.substring(start, end);
       
   160 		}
       
   161 		return retval;
       
   162 	}
       
   163 
       
   164 	/**
       
   165 	 * Sets the index for start of parameters
       
   166 	 * 
       
   167 	 * @param index
       
   168 	 *            the index
       
   169 	 */
       
   170 	void setParametersStartIndex(int index) {
       
   171 		parameterStart = index;
       
   172 	}
       
   173 
       
   174 	/**
       
   175 	 * Adds a return type string to the context
       
   176 	 * 
       
   177 	 * @param type
       
   178 	 *            the return type
       
   179 	 */
       
   180 	void addReturnType(String type) {
       
   181 		// Constructors and destructor do not have return types
       
   182 		if (className != null && functionName!= null && !className.equals(functionName)
       
   183 				&& !functionName.equals(SourceConstants.TILDE + className)) {
       
   184 			type = removePtrAndRef(type);
       
   185 			if (type.length() > 0) {
       
   186 				if (returnTypes == null) {
       
   187 					returnTypes = new ArrayList<String>();
       
   188 				}
       
   189 				returnTypes.add(type);
       
   190 			}
       
   191 		}
       
   192 	}
       
   193 
       
   194 	/**
       
   195 	 * Adds a return type modifier to the list of return types
       
   196 	 * 
       
   197 	 * @param c
       
   198 	 *            the modifier
       
   199 	 */
       
   200 	private void addReturnModifier(char c) {
       
   201 		if (returnTypes == null) {
       
   202 			returnTypes = new ArrayList<String>();
       
   203 		}
       
   204 		returnTypes.add(String.valueOf(c));
       
   205 	}
       
   206 
       
   207 	/**
       
   208 	 * Gets the return types of this context
       
   209 	 * 
       
   210 	 * @return the return types
       
   211 	 */
       
   212 	Iterator<String> getReturnTypes() {
       
   213 		List<String> list;
       
   214 		if (returnTypes != null) {
       
   215 			list = returnTypes;
       
   216 		} else {
       
   217 			list = Collections.emptyList();
       
   218 		}
       
   219 		return list.iterator();
       
   220 	}
       
   221 
       
   222 	/*
       
   223 	 * (non-Javadoc)
       
   224 	 * 
       
   225 	 * @see com.nokia.tracecompiler.source.ParsedType#hasQualifier(java.lang.String)
       
   226 	 */
       
   227 	public boolean hasQualifier(String type) {
       
   228 		// All types and qualifiers are in the returnTypes list
       
   229 		return typeEquals(type);
       
   230 	}
       
   231 
       
   232 	/*
       
   233 	 * (non-Javadoc)
       
   234 	 * 
       
   235 	 * @see com.nokia.tracecompiler.source.ParsedType#typeEquals(java.lang.String)
       
   236 	 */
       
   237 	public boolean typeEquals(String type) {
       
   238 		return (returnTypes != null && returnTypes.contains(type));
       
   239 	}
       
   240 
       
   241 	/*
       
   242 	 * (non-Javadoc)
       
   243 	 * 
       
   244 	 * @see com.nokia.tracecompiler.source.ParsedType#isPointer()
       
   245 	 */
       
   246 	public boolean isPointer() {
       
   247 		boolean retval = false;
       
   248 		if (returnTypes != null) {
       
   249 			for (int i = 0; i < returnTypes.size() && !retval; i++) {
       
   250 				if (returnTypes.get(i).equals(SourceConstants.ASTERISK)) {
       
   251 					retval = true;
       
   252 				}
       
   253 			}
       
   254 		}
       
   255 		return retval;
       
   256 	}
       
   257 
       
   258 	/**
       
   259 	 * Checks if return types list contains "void"
       
   260 	 * 
       
   261 	 * @return true if void, false if not
       
   262 	 */
       
   263 	public boolean isVoid() {
       
   264 		return (returnTypes == null || returnTypes
       
   265 				.contains(SourceConstants.VOID));
       
   266 	}
       
   267 
       
   268 	/**
       
   269 	 * Parses the parameters of this source context
       
   270 	 * 
       
   271 	 * @param parameterList
       
   272 	 *            the list where the parameters are stored
       
   273 	 * @throws SourceParserException
       
   274 	 *             if parsing fails
       
   275 	 */
       
   276 	public void parseParameters(List<SourceParameter> parameterList)
       
   277 			throws SourceParserException {
       
   278 		if (getParser() != null) {
       
   279 			getParser().parseFunctionParameters(parameterStart, parameterList);
       
   280 		} else {
       
   281 			throw new SourceParserException(
       
   282 					SourceErrorCodes.CONTEXT_MISSING_OWNER);
       
   283 		}
       
   284 	}
       
   285 
       
   286 	/**
       
   287 	 * Parses the return statements of this source context
       
   288 	 * 
       
   289 	 * @param returnList
       
   290 	 *            the list where the return values are stored
       
   291 	 * @throws SourceParserException
       
   292 	 *             if parsing fails
       
   293 	 */
       
   294 	public void parseReturnValues(List<SourceReturn> returnList)
       
   295 			throws SourceParserException {
       
   296 		if (getParser() != null) {
       
   297 			getParser().parseReturnValues(this, returnList);
       
   298 		} else {
       
   299 			throw new SourceParserException(
       
   300 					SourceErrorCodes.CONTEXT_MISSING_OWNER);
       
   301 		}
       
   302 	}
       
   303 
       
   304 	/*
       
   305 	 * (non-Javadoc)
       
   306 	 * 
       
   307 	 * @see java.lang.Object#toString()
       
   308 	 */
       
   309 	@Override
       
   310 	public String toString() {
       
   311 		StringBuffer sb = new StringBuffer();
       
   312 		if (className != null) {
       
   313 			sb.append(getClassName());
       
   314 		}
       
   315 		sb.append("::"); //$NON-NLS-1$
       
   316 		sb.append(getFunctionName());
       
   317 		return sb.toString();
       
   318 	}
       
   319 
       
   320 }