Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a7cfd0f647106bc7dcb87438d576fcf8771e6ffb (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (c) 2014 QNX Software Systems 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
 */
package org.eclipse.cdt.internal.qt.core.pdom;

import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
import org.eclipse.cdt.internal.qt.core.ASTUtil;
import org.eclipse.cdt.qt.core.QtKeywords;
import org.eclipse.cdt.qt.core.index.IQmlRegistration;
import org.eclipse.core.runtime.CoreException;

public class QmlTypeRegistration extends ASTDelegatedName implements IQtASTName {

	private final ICPPTemplateInstance functionInstanceBinding;
	private final IASTFunctionCallExpression fnCall;
	private final IQmlRegistration.Kind kind;
	private char[] simpleID;

	public QmlTypeRegistration(IASTName ast, ICPPTemplateInstance functionInstanceBinding, IASTFunctionCallExpression fnCall) {
		super(ast);
		this.functionInstanceBinding = functionInstanceBinding;
		this.fnCall = fnCall;

		if (QtKeywords.QML_REGISTER_UNCREATABLE_TYPE.equals(functionInstanceBinding.getName()))
			this.kind = IQmlRegistration.Kind.Uncreatable;
		else
			this.kind = IQmlRegistration.Kind.Type;
	}

	@Override
	public char[] getSimpleID() {
		if (simpleID == null) {
			IASTInitializerClause[] args = fnCall.getArguments();
			simpleID = (functionInstanceBinding.getName()
					 + ASTTypeUtil.getArgumentListString(functionInstanceBinding.getTemplateArguments(), true)
					 + "\0("
					 + asStringForName(args, 0) + ','
					 + asStringForName(args, 1) + ','
					 + asStringForName(args, 2) + ','
					 + asStringForName(args, 3) + ')'
					 ).toCharArray();
		}

		return simpleID;
	}

	@Override
	public QtPDOMBinding createPDOMBinding(QtPDOMLinkage linkage) throws CoreException {
		switch(kind) {
		case Type:
			return new QtPDOMQmlRegistration(linkage, this, delegate);
		case Uncreatable:
			return new QtPDOMQmlUncreatable(linkage, this, delegate);
		}
		return null;
	}

	public String getQObjectName() {
		ICPPTemplateArgument[] args = functionInstanceBinding.getTemplateArguments();
		if (args.length < 1)
			return null;

		IType type = args[0].getTypeValue();
		return type instanceof ICPPBinding ? ASTUtil.getFullyQualifiedName((ICPPBinding) type) : null;
	}

	public Long getVersion() {
		ICPPTemplateArgument[] args = functionInstanceBinding.getTemplateArguments();
		if (args.length < 2)
			return null;

		 IValue val = args[1].getNonTypeValue();
		 return val == null ? null : val.numericalValue();
	}

	public String getUri() {
		return getArgAsStringOrNull(0);
	}

	public Long getMajor() {
		return getArgAsLongOrNull(1);
	}

	public Long getMinor() {
		return getArgAsLongOrNull(2);
	}

	public String getQmlName() {
		return getArgAsStringOrNull(3);
	}

	public String getReason() {
		return getArgAsStringOrNull(4);
	}

	private String asStringForName(IASTInitializerClause[] args, int index) {
		String arg = args.length <= index ? null : asString(args[index]);
		return arg == null ? "" : arg;
	}

	private String getArgAsStringOrNull(int index) {
		IASTInitializerClause[] args = fnCall.getArguments();
		if (args.length <= index)
			return null;

		return asString(args[index]);
	}

	private Long getArgAsLongOrNull(int index) {
		IASTInitializerClause[] args = fnCall.getArguments();
		if (args.length <= index)
			return null;

		String str = asString(args[index]);
		if (str != null)
			try {
				return Long.parseLong(str);
			} catch(NumberFormatException e) {
				// This is caused by invalid user code, do not log it
			}

		return null;
	}

	private static String asString(IASTInitializerClause init) {
		if (init instanceof IASTLiteralExpression) {
			IASTLiteralExpression literal = (IASTLiteralExpression) init;
			switch(literal.getKind()) {
			case IASTLiteralExpression.lk_integer_constant:
				return new String(literal.getValue());
			case IASTLiteralExpression.lk_string_literal:
				char[] value = literal.getValue();
				return new String(value, 1, value.length - 2);
			}
		}
		return null;
	}
}

Back to the top