Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 210d4ddc55312e5f0134febb1482c593c83dcf9b (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (c) 2013, 2015 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 */

package org.eclipse.cdt.internal.qt.core;

import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;

/**
 * Declares constants related to tokens that are special in Qt applications.
 */
public class QtKeywords {
	public static final String CONNECT = "connect";
	public static final String DISCONNECT = "disconnect";
	public static final String Q_CLASSINFO = "Q_CLASSINFO";
	public static final String Q_DECLARE_FLAGS = "Q_DECLARE_FLAGS";
	public static final String Q_ENUMS = "Q_ENUMS";
	public static final String Q_FLAGS = "Q_FLAGS";
	public static final String Q_GADGET = "Q_GADGET";
	public static final String Q_INVOKABLE = "Q_INVOKABLE";
	public static final String Q_OBJECT = "Q_OBJECT";
	public static final String Q_PROPERTY = "Q_PROPERTY";
	public static final String Q_REVISION = "Q_REVISION";
	public static final String Q_SIGNAL = "Q_SIGNAL";
	public static final String Q_SIGNALS = "Q_SIGNALS";
	public static final String Q_SLOT = "Q_SLOT";
	public static final String Q_SLOTS = "Q_SLOTS";
	public static final String QMETAMETHOD = "QMetaMethod";
	public static final String QML_ATTACHED_PROPERTIES = "qmlAttachedProperties";
	public static final String QML_REGISTER_TYPE = "qmlRegisterType";
	public static final String QML_REGISTER_UNCREATABLE_TYPE = "qmlRegisterUncreatableType";
	public static final String QOBJECT = "QObject";
	public static final String SIGNAL = "SIGNAL";
	public static final String SIGNALS = "signals";
	public static final String SLOT = "SLOT";
	public static final String SLOTS = "slots";

	/**
	 * Returns true if the argument type is for Qt's QObject class and false otherwise.
	 */
	public static boolean isQObject(IType type) {
		if (!(type instanceof ICPPClassType))
			return false;

		ICPPClassType clsType = (ICPPClassType)type;
		return QtKeywords.QOBJECT.equals(clsType.getName());
	}

	/**
	 * Returns true if the argument type is for Qt's QMetaMethod class and false otherwise.
	 */
	public static boolean isQMetaMethod(IType type) {
		if (!(type instanceof ICPPClassType))
			return false;

		ICPPClassType clsType = (ICPPClassType)type;
		return QMETAMETHOD.equals(clsType.getName());
	}

	/**
	 * Returns true if the argument binding is for the QObject::connect function
	 * and false otherwise.
	 */
	public static boolean is_QObject_connect(IBinding binding) {
		String[] qualName = getFunctionQualifiedName(binding);
		return qualName != null
			&& qualName.length == 2
			&& QOBJECT.equals(qualName[0])
			&& CONNECT.equals(qualName[1]);
	}

	/**
	 * Returns true if the argument binding is for the QObject::disconnect function
	 * and false otherwise.
	 */
	public static boolean is_QObject_disconnect(IBinding binding) {
		String[] qualName = getFunctionQualifiedName(binding);
		return qualName != null
			&& qualName.length == 2
			&& QOBJECT.equals(qualName[0])
			&& DISCONNECT.equals(qualName[1]);
	}

	/**
	 * Returns true if the given binding will register a type with the QML type system and false
	 * otherwise.
	 */
	public static boolean is_QmlType(IBinding binding) {
		String[] qualName = getFunctionQualifiedName(binding);
		return qualName != null
			&& qualName.length == 1
			&& (QML_REGISTER_TYPE.equals(qualName[0])
			 || QML_REGISTER_UNCREATABLE_TYPE.equals(qualName[0]));
	}

	private static String[] getFunctionQualifiedName(IBinding binding) {
		// IBinding#getAdapter returns null when binding is an instance of
		// PDOMCPPMethod.
		if (binding instanceof ICPPFunction)
			try {
				return ((ICPPFunction) binding).getQualifiedName();
			} catch (DOMException e) {
				Activator.log(e);
			}
		return null;
	}
}

Back to the top