Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 19fd43da082941142d5e7b55f14a44a657153285 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*******************************************************************************
 * Copyright (c) 2007 Wind River Systems, 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:
 *    Markus Schorn - initial API and implementation
 *******************************************************************************/ 
package org.eclipse.cdt.internal.core.parser.scanner;

import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.core.parser.OffsetLimitReachedException;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.Linkage;
import org.eclipse.cdt.internal.core.parser.scanner.Lexer.LexerOptions;

/**
 * Models macros used by the preprocessor
 * @since 5.0
 */
abstract class PreprocessorMacro implements IMacroBinding {
	final private char[] fName;

	public PreprocessorMacro(char[] name) {
		fName= name;
	}
	
	final public ILinkage getLinkage() {
		return Linkage.NO_LINKAGE;
	}

	final public char[] getNameCharArray() {
		return fName;
	}

	final public String getName() {
		return new String(fName);
	}

	public IScope getScope() {
		return null;
	}

	public boolean isFunctionStyle() {
		return false;
	}
	
	public char[][] getParameterList() {
		return null;
	}
	
	public char[][] getParameterPlaceholderList() {
		return null;
	}

	public Object getAdapter(Class clazz) {
		return null;
	}

	public int hasVarArgs() {
		return FunctionStyleMacro.NO_VAARGS;
	}

	public String toString() {
		char[][] p= getParameterList();
		if (p == null) {
			return getName();
		}
		StringBuffer buf= new StringBuffer();
		buf.append(getNameCharArray());
		buf.append('(');
		for (int i = 0; i < p.length; i++) {
			if (i>0) {
				buf.append(',');
			}
			buf.append(p[i]);
		}
		buf.append(')');
		return buf.toString();
	}
	public abstract TokenList getTokens(MacroDefinitionParser parser, LexerOptions lexOptions);
}

abstract class DynamicStyleMacro extends PreprocessorMacro {

	public DynamicStyleMacro(char[] name) {
		super(name);
	}
	public char[] getExpansion() {
		return getExpansionImage();
	}
	public char[] getExpansionImage() {
		return execute().getCharImage();
	}
	public abstract Token execute();

	public TokenList getTokens(MacroDefinitionParser mdp, LexerOptions lexOptions) {
		TokenList result= new TokenList();
		result.append(execute());
		return result;
	}
}

class ObjectStyleMacro extends PreprocessorMacro {
	private final char[] fExpansion;
	final int fExpansionOffset;
	final int fEndOffset;
	private TokenList fExpansionTokens;
	
	public ObjectStyleMacro(char[] name, char[] expansion) {
		this(name, 0, expansion.length, null, expansion);
	}

	public ObjectStyleMacro(char[] name, int expansionOffset, int endOffset, TokenList expansion, char[] source) {
		super(name);
		fExpansionOffset= expansionOffset;
		fEndOffset= endOffset;
		fExpansion= source;
		fExpansionTokens= expansion;
		if (expansion != null) {
			setSource(expansion.first());
		}
	}

	private void setSource(Token t) {
		while (t != null) {
			t.fSource= this;
			t= (Token) t.getNext();
		}
	}

	public char[] getExpansion() {
		TokenList tl= getTokens(new MacroDefinitionParser(), new LexerOptions());
		StringBuffer buf= new StringBuffer();
		Token t= tl.first();
		if (t == null) {
			return CharArrayUtils.EMPTY;
		}
		int endOffset= t.getOffset();
		for (; t != null; t= (Token) t.getNext()) {
			if (endOffset < t.getOffset()) {
				buf.append(' ');
			}
			buf.append(t.getCharImage());
			endOffset= t.getEndOffset();
		}
		final int length= buf.length(); 
		final char[] expansion= new char[length];
		buf.getChars(0, length, expansion, 0);
		return expansion;
	}

	public char[] getExpansionImage() {
		final int length = fEndOffset - fExpansionOffset;
		if (length == fExpansion.length) {
			return fExpansion;
		}
		char[] result= new char[length];
		System.arraycopy(fExpansion, fExpansionOffset, result, 0, length);
		return result;
	}
	
	public TokenList getTokens(MacroDefinitionParser mdp, LexerOptions lexOptions) {
		if (fExpansionTokens == null) {
			fExpansionTokens= new TokenList();
			Lexer lex= new Lexer(fExpansion, fExpansionOffset, fEndOffset, lexOptions, ILexerLog.NULL, this);
			try {
				mdp.parseExpansion(lex, ILexerLog.NULL, getNameCharArray(), getParameterPlaceholderList(), fExpansionTokens);
			} catch (OffsetLimitReachedException e) {
			}
		}
		return fExpansionTokens;
	}
}


class FunctionStyleMacro extends ObjectStyleMacro {
	public static final int NO_VAARGS 	= 0;    // M(a)
	public static final int VAARGS	  	= 1;	// M(...)
	public static final int NAMED_VAARGS= 2;	// M(a...)
	
	final private char[][] fParamList;
	final private int fHasVarArgs;
	private char[] fSignature;
	
	public FunctionStyleMacro(char[] name, char[][] paramList, int hasVarArgs, char[] expansion) {
		super(name, expansion);
		fParamList = paramList;
		fHasVarArgs= hasVarArgs;
	}

	public FunctionStyleMacro(char[] name, char[][] paramList, int hasVarArgs, int expansionFileOffset, int endFileOffset, 
			TokenList expansion, char[] source) {
		super(name, expansionFileOffset, endFileOffset, expansion, source);
		fParamList = paramList;
		fHasVarArgs= hasVarArgs;
	}
	
	public char[][] getParameterList() {
		final int length = fParamList.length;
		if (fHasVarArgs == NO_VAARGS || length==0) {
			return fParamList;
		}
		char[][] result= new char[length][];
		System.arraycopy(fParamList, 0, result, 0, length-1);
		if (fHasVarArgs == VAARGS) {
			result[length-1] = Keywords.cVA_ARGS;
		}
		else {
			final char[] param= fParamList[length-1];
			final int plen= param.length;
			final int elen = Keywords.cpELLIPSIS.length;
			final char[] rp= new char[plen+elen];
			System.arraycopy(param, 0, rp, 0, plen);
			System.arraycopy(Keywords.cpELLIPSIS, 0, rp, plen, elen);
			result[length-1]= rp;
		}
		return result;
	}

	public char[][] getParameterPlaceholderList() {
		return fParamList;
	}

	public char[] getSignature() {
	    if (fSignature != null) {
	        return fSignature;
	    }
	    
	    StringBuffer result= new StringBuffer();
	    result.append(getName());
	    result.append('(');
	    
	    final int lastIdx= fParamList.length-1;
	    if (lastIdx >= 0) {
	    	for (int i = 0; i < lastIdx; i++) {
	    		result.append(fParamList[i]);
	    		result.append(',');
	    	}
	    	switch(fHasVarArgs) {
	    	case VAARGS:
	    		result.append(Keywords.cpELLIPSIS);
	    		break;
	    	case NAMED_VAARGS:
	    		result.append(fParamList[lastIdx]);
	    		result.append(Keywords.cpELLIPSIS);
	    		break;
	    	default:
	    		result.append(fParamList[lastIdx]);
	    		break;
	    	}
	    }
	    result.append(')');
	    final int len= result.length();
	    final char[] sig= new char[len];
	    result.getChars(0, len, sig, 0);
	    fSignature= sig;
	    return sig;
	}
		
	/**
	 * Returns one of {@link #NO_VAARGS}, {@link #VAARGS} or {@link #NAMED_VAARGS}.
	 */
	public int hasVarArgs() {
		return fHasVarArgs;
	}
		
	public boolean isFunctionStyle() {
		return true;
	}
}

Back to the top