Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 626e82056ae37bee6b8f4e2ab0e176e962fc490c (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
/*
 * Copyright (c) 2013 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.parser;

import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypeId;
import org.eclipse.cdt.core.dom.parser.cpp.GPPParserExtensionConfiguration;
import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.internal.core.dom.parser.BacktrackException;
import org.eclipse.cdt.internal.core.dom.parser.DeclarationOptions;
import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPSourceParser;

/**
 * A parser that use a special StringScanner to extract small sections of C++ syntax that
 * are used in Qt macro expansions.
 *
 * @see StringScanner
 */
@SuppressWarnings("restriction")
public class QtParser extends GNUCPPSourceParser {

	private QtParser(String str) {
		super(new StringScanner(str), ParserMode.QUICK_PARSE, new NullLogService(), GPPParserExtensionConfiguration.getInstance());
	}

	/**
	 * The argument String is the expansion parameter for SIGNAL and SLOT macros.  The text
	 * is parsed and the function declarator is returned if possible.  Returns null if the
	 * string is not a valid function declarator reference.
	 */
	public static ICPPASTFunctionDeclarator parseQtMethodReference(String str) {
		// Reject strings that have embedded line terminators.  This is needed to properly check that
		// one that is about to be added.
		if (str == null
		 || str.contains(";"))
			return null;

		QtParser parser = new QtParser(str + ';');
		try {
			IASTDeclarator declarator
				= parser.declarator(GNUCPPSourceParser.DtorStrategy.PREFER_FUNCTION, DeclarationOptions.CPP_MEMBER);
			if (!(declarator instanceof ICPPASTFunctionDeclarator))
				return null;

			// JI 439374: Make sure the ; was the last token read to prevent errors where extra strings
			//            appear in the expansion parameter.
			if (parser.lastTokenFromScanner == null
			 || parser.lastTokenFromScanner.getType() != IToken.tSEMI)
				return null;

			// JI 439374: Make sure the ; was the last token read to prevent errors where extra strings
			//            appear in the expansion parameter.
			if (parser.lastTokenFromScanner == null
			 || parser.lastTokenFromScanner.getType() != IToken.tSEMI)
				return null;

			// make sure this is a legal declarator for a Qt method reference
			ICPPASTFunctionDeclarator function = (ICPPASTFunctionDeclarator) declarator;

			// 1) parameters must not have names
			for(ICPPASTParameterDeclaration param : function.getParameters()) {
				ICPPASTDeclarator decltor = param.getDeclarator();
				if (decltor == null)
					continue;

				IASTName paramName = decltor.getName();
				if (paramName == null)
					continue;

				char[] name = paramName.getSimpleID();
				if (name == null
				 || name.length <= 0)
					continue;

				// The Qt normalization code treats a reference with a trailing const as a special case (this
				// seems to be a bug in the way they normalize const pointers.  We could support this case by
				// allowing reference parameters to be named 'const'.  However, since this seems to be a bug
				// in Qt they will likely fix it at some point, and there doesn't seem to be a case where the
				// user would need to reference the Qt method in this way.

				// the parameter has a non-empty name, so reject the declarator
				return null;
			}

			// All tests have passed, so return this declarator.
			return function;
		} catch(BacktrackException e) {
			return null;
		} catch(EndOfFileException e) {
			return null;
		}
	}

	public static ICPPASTTypeId parseTypeId(String str) {
		QtParser parser = new QtParser(str);
		try {
			return parser.typeId(new DeclarationOptions(DeclarationOptions.NO_INITIALIZER));
		} catch(BacktrackException e) {
			return null;
		} catch(EndOfFileException e) {
			return null;
		}
	}
}

Back to the top