tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/engine/rules/AutoAddReturnParameterRule.java
changeset 56 aa2539c91954
parent 54 a151135b0cf9
child 60 e54443a6878c
child 62 1c2bb2fc7c87
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
     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 * Trace rule for automatically adding function return value to exit trace
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.engine.rules;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 
       
    23 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorMessages;
       
    24 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals;
       
    25 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorCodes.TraceCompilerErrorCode;
       
    26 import com.nokia.tracecompiler.engine.utils.TraceMultiplierRule;
       
    27 import com.nokia.tracecompiler.model.Trace;
       
    28 import com.nokia.tracecompiler.model.TraceCompilerException;
       
    29 import com.nokia.tracecompiler.model.TraceConstantTable;
       
    30 import com.nokia.tracecompiler.model.TraceObjectRuleCreateObject;
       
    31 import com.nokia.tracecompiler.model.TraceObjectRuleRemoveOnCreate;
       
    32 import com.nokia.tracecompiler.model.TraceParameter;
       
    33 import com.nokia.tracecompiler.source.SourceContext;
       
    34 import com.nokia.tracecompiler.source.SourceParserException;
       
    35 import com.nokia.tracecompiler.source.SourceReturn;
       
    36 import com.nokia.tracecompiler.source.SourceUtils;
       
    37 import com.nokia.tracecompiler.source.TypeMapping;
       
    38 
       
    39 /**
       
    40  * Trace rule for automatically adding function return value to exit trace
       
    41  * 
       
    42  */
       
    43 public final class AutoAddReturnParameterRule extends RuleBase implements
       
    44 		TraceObjectRuleCreateObject, TraceObjectRuleRemoveOnCreate {
       
    45 
       
    46 	/**
       
    47 	 * Parameter name
       
    48 	 */
       
    49 	static final String PARAMETER_NAME = "retval"; //$NON-NLS-1$
       
    50 
       
    51 	/**
       
    52 	 * The number of handled return statements
       
    53 	 */
       
    54 	private static int numberOfHandledReturnStatements = 0;
       
    55 
       
    56 	/*
       
    57 	 * (non-Javadoc)
       
    58 	 * 
       
    59 	 * @see com.nokia.tracecompiler.model.TraceObjectRuleCreateObject#
       
    60 	 *      createObject(com.nokia.tracecompiler.model.TraceObject)
       
    61 	 */
       
    62 	public void createObject() throws TraceCompilerException {
       
    63 		// If owner has a multiplier (entry trace), the trace is not added to it
       
    64 		Trace owner = (Trace) getOwner();
       
    65 		if (owner.getExtension(TraceMultiplierRule.class) == null) {
       
    66 			SourceContext context = TraceCompilerEngineGlobals
       
    67 					.getSourceContextManager().getContext();
       
    68 			if (!context.isVoid()) {
       
    69 				TraceConstantTable table = RuleUtils.findConstantTableByType(
       
    70 						owner.getModel(), context);
       
    71 				if (table != null) {
       
    72 					createParameter(owner, table);
       
    73 				} else {
       
    74 					createParameter(owner, context);
       
    75 				}
       
    76 			}
       
    77 		}
       
    78 	}
       
    79 
       
    80 	/*
       
    81 	 * (non-Javadoc)
       
    82 	 * 
       
    83 	 * @see com.nokia.tracecompiler.model.TraceObjectRuleRemoveOnCreate#canBeRemoved()
       
    84 	 */
       
    85 	public boolean canBeRemoved() {
       
    86 		// If the owner has a multiplier, this needs to be moved to it using the
       
    87 		// CopyAndRemoveExtensionRule
       
    88 		return getOwner().getExtension(TraceMultiplierRule.class) == null;
       
    89 	}
       
    90 
       
    91 	/**
       
    92 	 * Creates a return parameter that was not associated with a constant table
       
    93 	 * 
       
    94 	 * @param owner
       
    95 	 *            the owner for the parameter
       
    96 	 * @param context
       
    97 	 *            the context specifying the parameter type 
       
    98 	 */
       
    99 	private void createParameter(Trace owner, SourceContext context) {
       
   100 		TypeMapping type = SourceUtils.mapSymbianTypeToParameterType(context);
       
   101 		String return_value_name = PARAMETER_NAME;
       
   102 		ArrayList<SourceReturn> returnList = new ArrayList<SourceReturn>();
       
   103 
       
   104 		// Find out return value name
       
   105 		try {
       
   106 			context.parseReturnValues(returnList);
       
   107 			if (numberOfHandledReturnStatements <= (returnList.size() - 1)) {
       
   108 				return_value_name = returnList.get(
       
   109 						numberOfHandledReturnStatements).getReturnStatement();
       
   110 			}
       
   111 
       
   112 			numberOfHandledReturnStatements++;
       
   113 		} catch (SourceParserException e) {
       
   114 			String msg = Messages
       
   115 					.getString("RuleUtils.FailedToParseReturnValues"); //$NON-NLS-1$
       
   116 			String cname = context.getClassName();
       
   117 			String source;
       
   118 			if (cname != null) {
       
   119 				source = cname + "::" + context.getFunctionName(); //$NON-NLS-1$;
       
   120 			} else {
       
   121 				source = context.getFunctionName();
       
   122 			}
       
   123 			TraceCompilerEngineGlobals.getEvents().postErrorMessage(msg, source, true);
       
   124 		}
       
   125 
       
   126 		try {
       
   127 			if (!type.needsCasting) {
       
   128 				// If an extension header is generated, the parameter needs to
       
   129 				// be cast, since the return statement may contain anything.
       
   130 				type.needsCasting = type.type != TraceParameter.HEX32
       
   131 						&& type.type != TraceParameter.SDEC32
       
   132 						&& type.type != TraceParameter.UDEC32;
       
   133 			}
       
   134 			RuleUtils.createParameterFromType(owner, return_value_name, type);
       
   135 
       
   136 		} catch (TraceCompilerException e) {
       
   137 
       
   138 			if (e.getErrorCode() == TraceCompilerErrorCode.INVALID_PARAMETER_NAME) {
       
   139 				String msg = TraceCompilerEngineErrorMessages
       
   140 						.getErrorMessage(
       
   141 								TraceCompilerErrorCode.INVALID_PARAMETER_NAME_IN_RETURN_VALUE,
       
   142 								null);
       
   143 
       
   144 				TraceCompilerEngineGlobals.getEvents().postWarningMessage(msg, owner);
       
   145 			} else {
       
   146 				TraceCompilerException exception = new TraceCompilerException(e.getErrorCode(), e
       
   147 						.getErrorParameters(), null);
       
   148 				TraceCompilerEngineGlobals.getEvents().postError(
       
   149 						exception);
       
   150 			}
       
   151 		}
       
   152 	}
       
   153 
       
   154 	/**
       
   155 	 * Creates a return parameter that was associated with a constant table
       
   156 	 * 
       
   157 	 * @param owner
       
   158 	 *            the owner for the parameter
       
   159 	 * @param table
       
   160 	 *            the constant table the parameter was associated to
       
   161 	 */
       
   162 	private void createParameter(Trace owner, TraceConstantTable table) {
       
   163 		try {
       
   164 			RuleUtils.createParameterFromConstantTable(owner, PARAMETER_NAME,
       
   165 					table);
       
   166 		} catch (TraceCompilerException e) {
       
   167 			TraceCompilerException exception = new TraceCompilerException(e.getErrorCode(), e
       
   168 					.getErrorParameters(), null);
       
   169 			TraceCompilerEngineGlobals.getEvents().postError(
       
   170 					exception);
       
   171 		}
       
   172 	}
       
   173 
       
   174 	/**
       
   175 	 * Reset static variables
       
   176 	 */
       
   177 	static void resetNumberOfHandledReturnStatements() {
       
   178 		numberOfHandledReturnStatements = 0;
       
   179 	}
       
   180 
       
   181 	/**
       
   182 	 * Increase number of handled return statements
       
   183 	 */
       
   184 	static void increaseNumberOfHandledReturnStatements() {
       
   185 		numberOfHandledReturnStatements++;
       
   186 	}
       
   187 
       
   188 }