Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 64d073363f30463b42c137fc92c188bfefc10392 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * 	   Sergey Prigogin (Google) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.dom.rewrite;

import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.internal.core.dom.parser.ASTTranslationUnit;
import org.eclipse.cdt.internal.core.dom.parser.SizeofCalculator;
import org.eclipse.cdt.internal.core.dom.parser.SizeofCalculator.SizeAndAlignment;
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.TypeTraits;

/**
 * A collection of static methods related to types.
 * @since 5.4
 */
public class TypeHelper {
	// Do not instantiate - all methods are static.
	private TypeHelper() {
	}

	/**
	 * Returns <code>true</code> if it is preferable to pass parameters of the given type to methods
	 * by reference, not by value. A parameter should be passed by reference if it is
	 * a <code>class</code>, <code>struct</code>, or <code>union</code>, and either has a nontrivial
	 * copy constructor or nontrivial destructor, or is larger than pointer. 
	 * 
	 * @param type the type in question. 
	 * @param ast the AST used as a context. 
	 * @return <code>true</code> is passing by reverence is preferable.
	 */
	public static boolean shouldBePassedByReference(IType type, IASTTranslationUnit ast) {
		type = SemanticUtil.getNestedType(type, SemanticUtil.CVTYPE | SemanticUtil.TDEF);
		if (type instanceof ICompositeType) {
			if (type instanceof ICPPClassType) {
				ICPPClassType classType = ((ICPPClassType) type);
				if (!TypeTraits.hasTrivialCopyCtor(classType, ast) ||
						!TypeTraits.hasTrivialDestructor(classType, ast)) {
					return true;
				}
			}
			SizeofCalculator calc = ((ASTTranslationUnit) ast).getSizeofCalculator();
			SizeAndAlignment sizeofPointer = calc.sizeAndAlignmentOfPointer();
			long maxSize = sizeofPointer != null ? sizeofPointer.size : 4;
			SizeAndAlignment sizeofType = calc.sizeAndAlignment(type);
			if (sizeofType == null || sizeofType.size > maxSize)
				return true;
		}
		return false;
	}

	public static IType createType(IASTDeclarator declarator) {
		if (declarator.getTranslationUnit() instanceof ICPPASTTranslationUnit) {
			return CPPVisitor.createType(declarator);
		} else {
			return CVisitor.createType(declarator);
		}
	}
}

Back to the top