Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8184f4ab8da2530885213e1f06915320d1845157 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*******************************************************************************
 * Copyright (c) 2006, 2009 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.dom.parser.upc;


import static org.eclipse.cdt.internal.core.dom.parser.upc.UPCParsersym.*;
import lpg.lpgjavaruntime.IToken;

import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.c.ICASTDeclSpecifier;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.action.ISecondaryParserFactory;
import org.eclipse.cdt.core.dom.lrparser.action.ScopedStack;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99BuildASTParserAction;
import org.eclipse.cdt.core.dom.upc.ast.IUPCASTDeclSpecifier;
import org.eclipse.cdt.core.dom.upc.ast.IUPCASTForallStatement;
import org.eclipse.cdt.core.dom.upc.ast.IUPCASTKeywordExpression;
import org.eclipse.cdt.core.dom.upc.ast.IUPCASTSynchronizationStatement;


/**
 * Extension to the C99ParserAction that adds support fot building
 * an AST with UPC specific nodes.
 */
public class UPCParserAction extends C99BuildASTParserAction {
	
	private UPCASTNodeFactory nodeFactory;
	
	
	/**
	 * TODO why is the nodeFactory passed as an argument
	 * @param nodeFactory
	 * @param parser
	 * @param tu
	 */
	public UPCParserAction(IParserActionTokenProvider parser, IASTTranslationUnit tu, ScopedStack<Object> astStack, UPCASTNodeFactory nodeFactory, ISecondaryParserFactory parserFactory) {
		super(parser, tu, astStack, nodeFactory, parserFactory);
		this.nodeFactory = nodeFactory;
		nodeFactory.setUseC99SizeofExpressions();
	}
	
	
	@Override 
	protected boolean isCompletionToken(IToken token) {
		return token.getKind() == TK_Completion;
	}
		
	


	/**************************************************************************************
	 * Semantic actions
	 **************************************************************************************/
	

	public void consumeExpressionUnarySizeofOperator(int upcOp) {
		nodeFactory.setUseUPCSizeofExpressions(upcOp);
		super.consumeExpressionUnaryOperator(IASTUnaryExpression.op_sizeof);
		nodeFactory.setUseC99SizeofExpressions();
	}
	
	
	public void consumeExpressionSizeofTypeId(int upcOp) {
		nodeFactory.setUseUPCSizeofExpressions(upcOp);
		super.consumeExpressionTypeId(IASTTypeIdExpression.op_sizeof);
		nodeFactory.setUseC99SizeofExpressions();
	}

	
	
	/**
	 * constant ::= 'MYTHREAD' | 'THREADS' | 'UPC_MAX_BLOCKSIZE'
	 */
	public void consumeKeywordExpression(int keywordKind) {
		IUPCASTKeywordExpression expr = nodeFactory.newKeywordExpression(keywordKind);
		setOffsetAndLength(expr);
		astStack.push(expr);
	}
	
	
	/**
	 * synchronization_statement ::= 'upc_notify' expression ';'
     *                             | 'upc_notify' ';'
     *                             | 'upc_wait' expression ';'
     *                             | 'upc_wait' ';'
     *                             | 'upc_barrier' expression ';'
     *                             | 'upc_barrier' ';'
     *                             | 'upc_fence' ';'
	 */
	public void consumeStatementSynchronizationStatement(int statementKind, boolean hasBarrierExpr) {
		IASTExpression barrierExpression = hasBarrierExpr ? (IASTExpression) astStack.pop() : null;
		IUPCASTSynchronizationStatement statement = nodeFactory.newSyncronizationStatment(barrierExpression, statementKind);
		setOffsetAndLength(statement);
		astStack.push(statement);
	}
	
	
	/**
	 * iteration_statement
     *     ::= 'upc_forall' '(' expression ';' expression ';' expression ';' affinity ')' statement
     *       | 'upc_forall' '(' declaration expression ';' expression ';' affinity ')' statement
	 */
	public void consumeStatementUPCForallLoop(boolean hasExpr1, boolean hasExpr2, boolean hasExpr3, boolean hasAffinity) {
		IASTStatement body = (IASTStatement) astStack.pop();
		
		boolean affinityContinue = false;
		IASTExpression affinity = null;
		if(hasAffinity) {
			Object o = astStack.pop();
			if(o instanceof IASTExpression)
				affinity = (IASTExpression)o;
			else if(o instanceof IToken)
				affinityContinue = true;
		}
		
		IASTExpression expr3 = hasExpr3 ? (IASTExpression) astStack.pop() : null;
		IASTExpression expr2 = hasExpr2 ? (IASTExpression) astStack.pop() : null;
		
		IASTStatement initializer = nodeFactory.newNullStatement();
		if(hasExpr1) { // may be an expression or a declaration
			Object node = astStack.pop();
			if(node instanceof IASTExpression)
				initializer = nodeFactory.newExpressionStatement((IASTExpression)node);
			else if(node instanceof IASTDeclaration)
				initializer = nodeFactory.newDeclarationStatement((IASTDeclaration)node);
		}
		
		IUPCASTForallStatement forStat = nodeFactory.newForallStatement(initializer, expr2, expr3, body, affinity);
		forStat.setAffinityContinue(affinityContinue);
		setOffsetAndLength(forStat);
		astStack.push(forStat);
	}
	
	
	/**
	 * Temporary object used during the parsing of UPC declaration specifiers.
	 * Stored temporarily on the astStack, but does not become part of the AST.
	 * Makes parsing of layout qualifiers easier.
	 * 
	 * @author Mike
	 */
	private static class UPCParserActionLayoutQualifier {
		public boolean hasStar = false;
		public IASTExpression expression = null;
	}

	
	/**
	 * layout_qualifier ::= '[' constant_expression ']'
     *                    | '[' '*' ']'
     *                    | '[' ']'
	 */
	public void consumeLayoutQualifier(boolean hasExpression, boolean hasStar) {
		UPCParserActionLayoutQualifier layoutQualifier = new UPCParserActionLayoutQualifier();
		layoutQualifier.hasStar = hasStar;
		if(hasExpression) {
			layoutQualifier.expression = (IASTExpression) astStack.pop();
		}
		astStack.push(layoutQualifier);
	}
	
	
	
	/**
	 * Overrides setSpecifier to add support for temporary layout qualifier nodes.
	 */
	@Override
	protected void setSpecifier(ICASTDeclSpecifier declSpec, Object specifier) {
		if(specifier instanceof IToken)
			setTokenSpecifier((IUPCASTDeclSpecifier)declSpec, (IToken)specifier);
		else 
			setLayoutQualifier((IUPCASTDeclSpecifier)declSpec, (UPCParserActionLayoutQualifier) specifier);
	}
	
	
	/**
	 * Support for new declaration specifier keywords.
	 * 
	 * 'shared' without [] is handled here
	 * 'shared' with [] is handled in setLayoutQualifier().
	 */
	protected void setTokenSpecifier(IUPCASTDeclSpecifier node, IToken token) {
		switch(token.getKind()) {
			case TK_relaxed:
				node.setReferenceType(IUPCASTDeclSpecifier.rt_relaxed);
				break;
			case TK_strict:
				node.setReferenceType(IUPCASTDeclSpecifier.rt_strict);
				break;
			case TK_shared:
				node.setSharedQualifier(IUPCASTDeclSpecifier.sh_shared_default_block_size);
				break;
			default:
				super.setSpecifier(node, token);
		}
	}
	
	
	/**
	 * Handles layout qualifiers with block size specified.
	 */
	protected void setLayoutQualifier(IUPCASTDeclSpecifier node, UPCParserActionLayoutQualifier layoutQualifier) {
		if(layoutQualifier.hasStar) {
			node.setSharedQualifier(IUPCASTDeclSpecifier.sh_shared_pure_allocation);
		}
		else if(layoutQualifier.expression != null) {
			node.setSharedQualifier(IUPCASTDeclSpecifier.sh_shared_constant_expression);
			node.setBlockSizeExpression(layoutQualifier.expression);
		}
		else {
			node.setSharedQualifier(IUPCASTDeclSpecifier.sh_shared_indefinite_allocation);
		}
	}
	
}


Back to the top