Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e0309e78031d1d312ff7f5c75be99efe7dd45cb4 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*******************************************************************************
 * Copyright (c) 2004, 2013 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 Rational Software - Initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;

import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.AttributeUtil;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.Linkage;
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
import org.eclipse.core.runtime.PlatformObject;

/**
 * Represents a function.
 */
public class CFunction extends PlatformObject implements IFunction, ICInternalFunction {
	private IASTDeclarator[] declarators;
	private IASTFunctionDeclarator definition;

	private static final int FULLY_RESOLVED         = 1;
	private static final int RESOLUTION_IN_PROGRESS = 1 << 1;
	private int bits;

	protected IFunctionType type;

	public CFunction(IASTDeclarator declarator) {
		storeDeclarator(declarator);
	}

	private void storeDeclarator(IASTDeclarator declarator) {
		if (declarator != null) {
			if (declarator instanceof ICASTKnRFunctionDeclarator) {
				definition = (IASTFunctionDeclarator) declarator;
			} else if (declarator instanceof IASTFunctionDeclarator
					&& ASTQueries.findOutermostDeclarator(declarator).getParent() instanceof IASTFunctionDefinition) {
				definition = (IASTFunctionDeclarator) declarator;
			} else {
				declarators = ArrayUtil.append(IASTDeclarator.class, declarators, declarator);
			}
		}
	}

	@Override
	public IASTDeclarator getPhysicalNode() {
		if (definition != null) {
			return definition;
		} else if (declarators != null && declarators.length > 0) {
			return declarators[0];
		}
		return null;
	}

	@Override
	public void addDeclarator(IASTDeclarator fnDeclarator) {
		if (!fnDeclarator.isActive())
			return;

		if (fnDeclarator instanceof IASTFunctionDeclarator) {
			updateParameterBindings((IASTFunctionDeclarator) fnDeclarator);
		}
		storeDeclarator(fnDeclarator);
	}

    protected IASTTranslationUnit getTranslationUnit() {
		if (definition != null) {
            return definition.getTranslationUnit();
		} else if (declarators != null) {
            return declarators[0].getTranslationUnit();
		}
		return null;
    }

    private void resolveAllDeclarations() {
	    if ((bits & (FULLY_RESOLVED | RESOLUTION_IN_PROGRESS)) == 0) {
	        bits |= RESOLUTION_IN_PROGRESS;
		    IASTTranslationUnit tu = getTranslationUnit();
	        if (tu != null) {
	            CVisitor.getDeclarations(tu, this);
	        }
			declarators = ArrayUtil.trim(IASTDeclarator.class, declarators);
	        bits |= FULLY_RESOLVED;
	        bits &= ~RESOLUTION_IN_PROGRESS;
	    }
	}

	@Override
	public IParameter[] getParameters() {
		int j= -1;
		int len = declarators != null ? declarators.length : 0;
		for (IASTDeclarator dtor = definition; j < len; j++) {
			if (j >= 0) {
				dtor = declarators[j];
			}
			if (dtor instanceof IASTStandardFunctionDeclarator) {
				IASTParameterDeclaration[] params = ((IASTStandardFunctionDeclarator) dtor).getParameters();
				int size = params.length;
				IParameter[] result = new IParameter[size];
				if (size > 0) {
					for (int i = 0; i < size; i++) {
						IASTParameterDeclaration p = params[i];
						result[i] = (IParameter) ASTQueries.findInnermostDeclarator(p.getDeclarator())
								.getName().resolveBinding();
					}

					if (result.length == 1 && SemanticUtil.isVoidType(result[0].getType()))
						return IParameter.EMPTY_PARAMETER_ARRAY;
				}
				return result;
			}
			if (dtor instanceof ICASTKnRFunctionDeclarator) {
				IASTName[] names = ((ICASTKnRFunctionDeclarator) dtor).getParameterNames();
				IParameter[] result = new IParameter[names.length];
				if (names.length > 0) {
					// Ensures that the list of parameters is created in the same order as
					// the K&R C parameter names
					for (int i = 0; i < names.length; i++) {
						IASTDeclarator decl = CVisitor.getKnRParameterDeclarator(
								(ICASTKnRFunctionDeclarator) dtor, names[i]);
						if (decl != null) {
							result[i] = (IParameter) decl.getName().resolveBinding();
						} else {
							result[i] = new CParameter.CParameterProblem(names[i],
									IProblemBinding.SEMANTIC_KNR_PARAMETER_DECLARATION_NOT_FOUND,
									names[i].toCharArray());
						}
					}
				}
				return result;
			}
		}

		if ((bits & (FULLY_RESOLVED | RESOLUTION_IN_PROGRESS)) == 0) {
			resolveAllDeclarations();
			return getParameters();
		}

		return CBuiltinParameter.createParameterList(getType());
	}

	@Override
	public String getName() {
		return getASTName().toString();
	}

	@Override
	public char[] getNameCharArray() {
		return getASTName().toCharArray();
	}

	private IASTName getASTName() {
		return ASTQueries.findInnermostDeclarator(getPhysicalNode()).getName();
	}

	@Override
	public IScope getScope() {
	    IASTDeclarator dtor = getPhysicalNode();
		if (dtor != null)
			return CVisitor.getContainingScope(ASTQueries.findOutermostDeclarator(dtor).getParent());
	    return null;
	}

	@Override
	public IScope getFunctionScope() {
		if (definition != null) {
			IASTFunctionDefinition def = (IASTFunctionDefinition) definition.getParent();
			return def.getScope();
		}
		return null;
	}

    @Override
	public IFunctionType getType() {
		if (type == null) {
			type = createType();
		}
        return type;
    }

	protected IFunctionType createType() {
		IASTDeclarator declarator = getPhysicalNode();
		if (declarator == null && (bits & FULLY_RESOLVED) == 0) {
			resolveAllDeclarations();
			declarator = getPhysicalNode();
		}
		if (declarator != null) {
			IType tempType = CVisitor.unwrapTypedefs(CVisitor.createType(declarator));
			if (tempType instanceof IFunctionType)
				return (IFunctionType) tempType;
		}
		return null;
	}

    public IBinding resolveParameter(IASTName paramName) {
    	if (paramName.getBinding() != null)
    	    return paramName.getBinding();

    	IBinding binding = null;
    	int idx = 0;
    	IASTNode parent = paramName.getParent();
    	while (parent instanceof IASTDeclarator && !(parent instanceof ICASTKnRFunctionDeclarator))
    	    parent = parent.getParent();

    	ICASTKnRFunctionDeclarator fKnRDtor = null;
    	IASTDeclarator knrParamDtor = null;
    	if (parent instanceof IASTParameterDeclaration) {
    	    IASTStandardFunctionDeclarator fdtor = (IASTStandardFunctionDeclarator) parent.getParent();
    	    IASTParameterDeclaration[] ps = fdtor.getParameters();
        	for (; idx < ps.length; idx++) {
        		if (parent == ps[idx])
        			break;
        	}
    	} else if (parent instanceof IASTSimpleDeclaration) {
    	    //KnR: name in declaration list
    	    fKnRDtor = (ICASTKnRFunctionDeclarator) parent.getParent();
    	    IASTName[] ps = fKnRDtor.getParameterNames();
    	    char[] n = paramName.toCharArray();
        	for (; idx < ps.length; idx++) {
        		if (CharArrayUtils.equals(ps[idx].toCharArray(), n))
        			break;
        	}
    	} else {
    	    //KnR: name in name list
    	    fKnRDtor = (ICASTKnRFunctionDeclarator) parent;
    	    IASTName[] ps = fKnRDtor.getParameterNames();
        	for (; idx < ps.length; idx++) {
        		if (ps[idx] == paramName)
        			break;
        	}
        	knrParamDtor = CVisitor.getKnRParameterDeclarator(fKnRDtor, paramName);
            if (knrParamDtor != null)
                paramName = knrParamDtor.getName();
    	}

    	//create a new binding and set it for the corresponding parameter in all known defns and decls
    	binding = new CParameter(paramName);
    	IASTParameterDeclaration temp = null;
    	if (definition != null) {
    	    if (definition instanceof IASTStandardFunctionDeclarator) {
    	    	IASTParameterDeclaration[] parameters = ((IASTStandardFunctionDeclarator) definition).getParameters();
    	    	if (parameters.length > idx) {
	    	        temp = parameters[idx];
	    	        ASTQueries.findInnermostDeclarator(temp.getDeclarator()).getName().setBinding(binding);
    	    	}
    	    } else if (definition instanceof ICASTKnRFunctionDeclarator) {
    	    	fKnRDtor = (ICASTKnRFunctionDeclarator) definition;
    	    	IASTName[] parameterNames = fKnRDtor.getParameterNames();
    	    	if (parameterNames.length > idx) {
	    	        IASTName n = parameterNames[idx];
	    	        n.setBinding(binding);
	    	        IASTDeclarator dtor = CVisitor.getKnRParameterDeclarator(fKnRDtor, n);
	    	        if (dtor != null) {
	    	            dtor.getName().setBinding(binding);
	    	        }
    	    	}
    	    }
    	}
    	if (declarators != null) {
    		for (IASTDeclarator dtor : declarators) {
    			if (dtor instanceof IASTStandardFunctionDeclarator) {
    				IASTStandardFunctionDeclarator fdtor= (IASTStandardFunctionDeclarator) dtor;
    				if (fdtor.getParameters().length > idx) {
    					temp = fdtor.getParameters()[idx];
    					ASTQueries.findInnermostDeclarator(temp.getDeclarator()).getName().setBinding(binding);
    				}
    			}
    		}
    	}
    	return binding;
    }

    protected void updateParameterBindings(IASTFunctionDeclarator fdtor) {
        IParameter[] params = getParameters();
        if (fdtor instanceof IASTStandardFunctionDeclarator) {
        	IASTParameterDeclaration[] nps = ((IASTStandardFunctionDeclarator) fdtor).getParameters();
        	if (params.length < nps.length)
        	    return;
        	for (int i = 0; i < nps.length; i++) {
        		IASTName name = ASTQueries.findInnermostDeclarator(nps[i].getDeclarator()).getName();
        		name.setBinding(params[i]);
        		if (params[i] instanceof CParameter)
        			((CParameter) params[i]).addDeclaration(name);
        	}
        } else {
            IASTName[] ns = ((ICASTKnRFunctionDeclarator) fdtor).getParameterNames();
            if (params.length > 0 && params.length != ns.length)
                return; //problem

            for (int i = 0; i < params.length; i++) {
            	IASTName name = ns[i];
            	name.setBinding(params[i]);
            	IASTDeclarator dtor = CVisitor.getKnRParameterDeclarator((ICASTKnRFunctionDeclarator) fdtor, name);
    			if (dtor != null) {
    			    dtor.getName().setBinding(params[i]);
    			    if (params[i] instanceof CParameter)
    			    	((CParameter) params[i]).addDeclaration(dtor.getName());
    			}
        	}
        }
    }

    @Override
	public boolean isStatic() {
    	return isStatic(true);
    }

    @Override
	public boolean isStatic(boolean resolveAll) {
        if (resolveAll && (bits & FULLY_RESOLVED) == 0) {
            resolveAllDeclarations();
        }
		return hasStorageClass(IASTDeclSpecifier.sc_static);
    }

	public boolean hasStorageClass(int storage) {
	    IASTDeclarator dtor = definition;
	    IASTDeclarator[] ds = declarators;

        int i = -1;
        do {
            if (dtor != null) {
	            IASTNode parent = dtor.getParent();
	            while (!(parent instanceof IASTDeclaration))
	                parent = parent.getParent();

	            IASTDeclSpecifier declSpec = null;
	            if (parent instanceof IASTSimpleDeclaration) {
	                declSpec = ((IASTSimpleDeclaration) parent).getDeclSpecifier();
	            } else if (parent instanceof IASTFunctionDefinition)
	                declSpec = ((IASTFunctionDefinition) parent).getDeclSpecifier();

	            if (declSpec != null && declSpec.getStorageClass() == storage) {
	            	return true;
	            }
            }

            if (ds != null && ++i < ds.length) {
                dtor = ds[i];
            } else {
            	break;
            }
        } while (dtor != null);
        return false;
	}

    @Override
	public boolean isExtern() {
    	return isExtern(true);
    }

    public boolean isExtern(boolean resolveAll) {
        if (resolveAll && (bits & FULLY_RESOLVED) == 0) {
            resolveAllDeclarations();
        }
        return hasStorageClass(IASTDeclSpecifier.sc_extern);
    }

    @Override
	public boolean isAuto() {
        if ((bits & FULLY_RESOLVED) == 0) {
            resolveAllDeclarations();
        }
        return hasStorageClass(IASTDeclSpecifier.sc_auto);
    }

    @Override
	public boolean isRegister() {
        if ((bits & FULLY_RESOLVED) == 0) {
            resolveAllDeclarations();
        }
        return hasStorageClass(IASTDeclSpecifier.sc_register);
    }

    @Override
	public boolean isInline() {
        if ((bits & FULLY_RESOLVED) == 0) {
            resolveAllDeclarations();
        }
	    IASTDeclarator dtor = definition;
	    IASTDeclarator[] ds = declarators;
        int i = -1;
        do {
            if (dtor != null) {
	            IASTNode parent = dtor.getParent();
	            while (!(parent instanceof IASTDeclaration)) {
	                parent = parent.getParent();
	            }

	            IASTDeclSpecifier declSpec = null;
	            if (parent instanceof IASTSimpleDeclaration) {
	                declSpec = ((IASTSimpleDeclaration) parent).getDeclSpecifier();
	            } else if (parent instanceof IASTFunctionDefinition) {
	                declSpec = ((IASTFunctionDefinition) parent).getDeclSpecifier();
	            }

	            if (declSpec != null && declSpec.isInline())
	                return true;
            }
            if (ds != null && ++i < ds.length) {
                dtor = ds[i];
            } else {
                break;
            }
        } while (dtor != null);

        return false;
    }

	@Override
	public boolean takesVarArgs() {
		if ((bits & FULLY_RESOLVED) == 0) {
			resolveAllDeclarations();
		}

		if (definition != null) {
			if (definition instanceof IASTStandardFunctionDeclarator)
				return ((IASTStandardFunctionDeclarator) definition).takesVarArgs();
			return false;
		}

		if (declarators != null) {
			for (IASTDeclarator dtor : declarators) {
				if (dtor instanceof IASTStandardFunctionDeclarator) {
					return ((IASTStandardFunctionDeclarator) dtor).takesVarArgs();
				}
			}
		}
		return false;
	}

	@Override
	public void setFullyResolved(boolean resolved) {
		if (resolved) {
			bits |= FULLY_RESOLVED;
		} else {
			bits &= ~FULLY_RESOLVED;
		}
	}

	@Override
	public ILinkage getLinkage() {
		return Linkage.C_LINKAGE;
	}

	@Override
	public IASTDeclarator[] getDeclarations() {
		return declarators;
	}

	@Override
	public IASTFunctionDeclarator getDefinition() {
		return definition;
	}

	@Override
	public IBinding getOwner() {
		return null;
	}

	@Override
	public boolean isNoReturn() {
		IASTFunctionDeclarator dtor = getPreferredDtor();
		return dtor != null && AttributeUtil.hasNoreturnAttribute(dtor);
	}

	protected IASTFunctionDeclarator getPreferredDtor() {
		IASTFunctionDeclarator dtor = getDefinition();
        if (dtor != null)
        	return dtor;

        IASTDeclarator[] dtors = getDeclarations();
        if (dtors != null) {
        	for (IASTDeclarator declarator : dtors) {
        		if (declarator instanceof IASTFunctionDeclarator)
        			return (IASTFunctionDeclarator) declarator;
        	}
        }
        return null;
	}

	/** For debugging only. */
	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		result.append(getName());
		IFunctionType t = getType();
		result.append(t != null ? ASTTypeUtil.getParameterTypeStringAndQualifiers(t) : "()"); //$NON-NLS-1$
		return result.toString();
	}
}

Back to the top