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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
/*******************************************************************************
* Copyright (c) 2000, 2011 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
*
* Contributors:
* QNX Software Systems - Initial API and implementation
* Markus Schorn (Wind River Systems)
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.model;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICElementVisitor;
import org.eclipse.cdt.core.model.ICModel;
import org.eclipse.cdt.core.model.ICModelStatusConstants;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IOpenable;
import org.eclipse.cdt.core.model.IParent;
import org.eclipse.cdt.core.model.ISourceRange;
import org.eclipse.cdt.core.model.ISourceReference;
import org.eclipse.cdt.core.model.ISourceRoot;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.internal.core.util.MementoTokenizer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourceAttributes;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.PlatformObject;
public abstract class CElement extends PlatformObject implements ICElement {
public static final char CEM_ESCAPE = '\\';
public static final char CEM_CPROJECT = '=';
public static final char CEM_SOURCEROOT = '/';
public static final char CEM_SOURCEFOLDER = '<';
public static final char CEM_TRANSLATIONUNIT = '{';
public static final char CEM_SOURCEELEMENT = '[';
public static final char CEM_PARAMETER = '(';
public static final char CEM_ELEMENTTYPE = '#';
protected static final CElement[] NO_ELEMENTS = new CElement[0];
protected int fType;
protected ICElement fParent;
protected String fName;
protected CElement(ICElement parent, String name, int type) {
fParent= parent;
fName= name;
fType= type;
}
/* (non-Javadoc)
* @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Object getAdapter(Class adapter) {
// handle all kinds of resources
if (IResource.class.isAssignableFrom(adapter)) {
IResource r= getResource();
if (r != null && adapter.isAssignableFrom(r.getClass())) {
return r;
}
}
return super.getAdapter(adapter);
}
// setters
public void setElementType (int type) {
fType= type;
}
public void setElementName(String name) {
fName = name;
}
public void setParent (ICElement parent) {
fParent = parent;
}
// getters
public int getElementType() {
return fType;
}
public String getElementName() {
return fName;
}
public ICElement getParent() {
return fParent;
}
public IPath getPath() {
IResource res = getUnderlyingResource();
if (res != null)
return res.getFullPath();
return new Path(getElementName());
}
public URI getLocationURI() {
IResource res = getUnderlyingResource();
if(res != null) {
return res.getLocationURI();
}
else {
return null;
}
}
public boolean exists() {
try {
return getElementInfo() != null;
} catch (CModelException e) {
// Do not log it, it will fil the .log alarming the user.
//CCorePlugin.log(e);
return false;
}
}
/**
* Returns the element that is located at the given source offset
* in this element. This is a helper method for <code>ITranslationUnit#getElementAtOffset</code>,
* and only works on compilation units and types. The offset given is
* known to be within this element's source range already, and if no finer
* grained element is found at the offset, this element is returned.
*/
protected ICElement getSourceElementAtOffset(int offset) throws CModelException {
if (this instanceof ISourceReference && this instanceof Parent) {
ICElement[] children = ((Parent)this).getChildren();
for (ICElement aChild : children) {
if (aChild instanceof ISourceReference) {
ISourceReference child = (ISourceReference) aChild;
ISourceRange range = child.getSourceRange();
int startPos = range.getStartPos();
int endPos = startPos + range.getLength();
if (offset < endPos && offset >= startPos) {
if (child instanceof Parent) {
return ((Parent)child).getSourceElementAtOffset(offset);
}
return (ICElement)child;
}
}
}
} else {
// should not happen
//Assert.isTrue(false);
}
return this;
}
/**
* Returns the elements that are located at the given source offset
* in this element. This is a helper method for <code>ITranslationUnit#getElementAtOffset</code>,
* and only works on compilation units and types. The offset given is
* known to be within this element's source range already, and if no finer
* grained element is found at the offset, this element is returned.
*/
protected ICElement[] getSourceElementsAtOffset(int offset) throws CModelException {
if (this instanceof ISourceReference && this instanceof Parent) {
ArrayList<Object> list = new ArrayList<Object>();
ICElement[] children = ((Parent)this).getChildren();
for (ICElement aChild : children) {
if (aChild instanceof ISourceReference) {
ISourceReference child = (ISourceReference) aChild;
ISourceRange range = child.getSourceRange();
int startPos = range.getStartPos();
int endPos = startPos + range.getLength();
if (offset < endPos && offset >= startPos) {
if (child instanceof Parent) {
ICElement[] elements = ((Parent)child).getSourceElementsAtOffset(offset);
list.addAll(Arrays.asList(elements));
}
list.add(child);
}
}
}
children = new ICElement[list.size()];
list.toArray(children);
return children;
}
return new ICElement[]{this};
}
public boolean isReadOnly () {
IResource r = getUnderlyingResource();
if (r != null) {
ResourceAttributes attributes = r.getResourceAttributes();
if (attributes != null) {
return attributes.isReadOnly();
}
}
return false;
}
public boolean isStructureKnown() throws CModelException {
return getElementInfo().isStructureKnown();
}
public ICModel getCModel () {
ICElement current = this;
do {
if (current instanceof ICModel) return (ICModel) current;
} while ((current = current.getParent()) != null);
return null;
}
public ICProject getCProject() {
ICElement current = this;
do {
if (current instanceof ICProject) return (ICProject) current;
} while ((current = current.getParent()) != null);
return null;
}
protected void addChild(ICElement e) throws CModelException {
}
public IResource getUnderlyingResource() {
IResource res = getResource();
if (res == null) {
ICElement p = getParent();
if (p != null) {
res = p.getUnderlyingResource();
}
}
return res;
}
public abstract IResource getResource() ;
protected abstract CElementInfo createElementInfo();
/**
* Tests if an element has the same name, type and an equal parent.
*/
@Override
public boolean equals (Object o) {
if (this == o)
return true;
if (o instanceof ICElement) {
return equals(this, (ICElement) o);
}
return false;
}
public static boolean equals(ICElement lhs, ICElement rhs) {
if (lhs == rhs) {
return true;
}
if (lhs.getElementType() != rhs.getElementType()) {
return false;
}
String lhsName= lhs.getElementName();
String rhsName= rhs.getElementName();
if( lhsName == null || rhsName == null || lhsName.length() != rhsName.length() ||
!lhsName.equals(rhsName)) {
return false;
}
if (lhs instanceof ISourceReference && rhs instanceof ISourceReference) {
if (((ISourceReference) lhs).getIndex() != ((ISourceReference) rhs).getIndex()) {
return false;
}
}
ICElement lhsParent= lhs.getParent();
ICElement rhsParent= rhs.getParent();
if (lhsParent == rhsParent) {
return true;
}
return lhsParent != null && lhsParent.equals(rhsParent);
}
public CElementInfo getElementInfo() throws CModelException {
return getElementInfo(null);
}
public CElementInfo getElementInfo (IProgressMonitor monitor) throws CModelException {
CModelManager manager = CModelManager.getDefault();
CElementInfo info = (CElementInfo)manager.getInfo(this);
if (info != null) {
return info;
}
info = createElementInfo();
openWhenClosed(info, monitor);
return info;
}
@Override
public String toString() {
return getElementName();
}
public String toDebugString() {
return getElementName() + " " + getTypeString(); //$NON-NLS-1$
}
// util
public String getTypeString() {
switch (getElementType()) {
case C_MODEL:
return "CMODEL"; //$NON-NLS-1$
case C_PROJECT:
return "CPROJECT"; //$NON-NLS-1$
case C_CCONTAINER:
if (this instanceof ISourceRoot) {
return "SOURCE_ROOT"; //$NON-NLS-1$
}
return "CCONTAINER"; //$NON-NLS-1$
case C_UNIT:
if (this instanceof IWorkingCopy) {
return "WORKING_UNIT"; //$NON-NLS-1$
}
return "TRANSLATION_UNIT"; //$NON-NLS-1$
case C_FUNCTION:
return "C_FUNCTION"; //$NON-NLS-1$
case C_FUNCTION_DECLARATION:
return "C_FUNCTION_DECLARATION"; //$NON-NLS-1$
case C_VARIABLE:
return "C_VARIABLE"; //$NON-NLS-1$
case C_VARIABLE_DECLARATION:
return "C_VARIABLE_DECLARATION"; //$NON-NLS-1$
case C_INCLUDE:
return "C_INCLUDE"; //$NON-NLS-1$
case C_MACRO:
return "C_MACRO"; //$NON-NLS-1$
case C_STRUCT:
return "C_STRUCT"; //$NON-NLS-1$
case C_CLASS:
return "C_CLASS"; //$NON-NLS-1$
case C_UNION:
return "C_UNION"; //$NON-NLS-1$
case C_FIELD:
return "C_FIELD"; //$NON-NLS-1$
case C_METHOD:
return "C_METHOD"; //$NON-NLS-1$
case C_NAMESPACE:
return "C_NAMESPACE"; //$NON-NLS-1$
case C_USING:
return "C_USING"; //$NON-NLS-1$
case C_VCONTAINER:
return "C_CONTAINER"; //$NON-NLS-1$
case C_BINARY:
return "C_BINARY"; //$NON-NLS-1$
case C_ARCHIVE:
return "C_ARCHIVE"; //$NON-NLS-1$
default:
return "UNKNOWN"; //$NON-NLS-1$
}
}
/**
* Close the C Element
* @throws CModelException
*/
public void close() throws CModelException {
CModelManager.getDefault().releaseCElement(this);
}
/**
* This element is being closed. Do any necessary cleanup.
*/
protected void closing(Object info) throws CModelException {
}
/**
* This element has just been opened. Do any necessary setup.
*/
protected void opening(Object info) {
}
/**
* Return the first instance of IOpenable in the parent
* hierarchy of this element.
*
* <p>Subclasses that are not IOpenable's must override this method.
*/
public IOpenable getOpenableParent() {
if (fParent instanceof IOpenable) {
return (IOpenable)fParent;
}
return null;
}
/**
* Builds this element's structure and properties in the given
* info object, based on this element's current contents (i.e. buffer
* contents if this element has an open buffer, or resource contents
* if this element does not have an open buffer). Children
* are placed in the given newElements table (note, this element
* has already been placed in the newElements table). Returns true
* if successful, or false if an error is encountered while determining
* the structure of this element.
*/
protected abstract void generateInfos(CElementInfo info, Map<ICElement, CElementInfo> newElements, IProgressMonitor monitor) throws CModelException;
/**
* Open a <code>IOpenable</code> that is known to be closed (no check for
* <code>isOpen()</code>).
*/
protected void openWhenClosed(CElementInfo info, IProgressMonitor pm) throws CModelException {
CModelManager manager = CModelManager.getDefault();
boolean hadTemporaryCache = manager.hasTemporaryCache();
try {
Map<ICElement, CElementInfo> newElements = manager.getTemporaryCache();
generateInfos(info, newElements, pm);
if (info == null) {
info = newElements.get(this);
}
if (info == null) { // a source ref element could not be opened
// close any buffer that was opened for the openable parent
Iterator<ICElement> iterator = newElements.keySet().iterator();
while (iterator.hasNext()) {
ICElement element = iterator.next();
if (element instanceof Openable) {
((Openable)element).closeBuffer();
}
}
throw newNotPresentException();
}
if (!hadTemporaryCache) {
manager.putInfos(this, newElements);
}
} finally {
if (!hadTemporaryCache) {
manager.resetTemporaryCache();
}
}
}
/**
* @see ICElement
*/
public ICElement getAncestor(int ancestorType) {
ICElement element = this;
while (element != null) {
if (element.getElementType() == ancestorType) {
return element;
}
element= element.getParent();
}
return null;
}
/**
* Returns true if this element is an ancestor of the given element,
* otherwise false.
*/
public boolean isAncestorOf(ICElement e) {
ICElement parent= e.getParent();
while (parent != null && !parent.equals(this)) {
parent= parent.getParent();
}
return parent != null;
}
/**
* Creates and returns and not present exception for this element.
*/
protected CModelException newNotPresentException() {
return new CModelException(new CModelStatus(ICModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this));
}
/**
* Returns the hash code for this Java element. By default,
* the hash code for an element is a combination of its name
* and parent's hash code. Elements with other requirements must
* override this method.
*/
// CHECKPOINT: making not equal objects seem equal
// What elements should override this?
@Override
public int hashCode() {
return hashCode(this);
}
public static int hashCode(ICElement elem) {
ICElement parent= elem.getParent();
if (parent == null) {
return System.identityHashCode(elem);
}
return Util.combineHashCodes(elem.getElementName().hashCode(), parent.hashCode());
}
/*
* Test to see if two objects are identical
* Subclasses should override accordingly
*/
public boolean isIdentical(CElement otherElement){
return this.equals(otherElement);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.model.ICElement#accept(org.eclipse.cdt.core.model.ICElementVisitor)
*/
public void accept(ICElementVisitor visitor) throws CoreException {
// Visit me, return right away if the visitor doesn't want to visit my children
if (!visitor.visit(this))
return;
// If I am a Parent, visit my children
if (this instanceof IParent) {
ICElement [] children = ((IParent)this).getChildren();
for (int i = 0; i < children.length; ++i)
children[i].accept(visitor);
}
}
/*
* @see org.eclipse.cdt.core.model.ICElement#getHandleIdentifier()
*/
public String getHandleIdentifier() {
return getHandleMemento();
}
/**
* Builds a string representation of this element.
*
* @return the string representation
*/
public String getHandleMemento(){
StringBuilder buff = new StringBuilder();
getHandleMemento(buff);
return buff.toString();
}
/**
* Append this elements memento string to the given buffer.
*
* @param buff the buffer building the memento string
*/
public void getHandleMemento(StringBuilder buff) {
((CElement)getParent()).getHandleMemento(buff);
buff.append(getHandleMementoDelimiter());
escapeMementoName(buff, getElementName());
}
/**
* Returns the <code>char</code> that marks the start of this handles
* contribution to a memento.
*/
protected abstract char getHandleMementoDelimiter();
/**
* Creates a C element handle from the given memento.
*
* @param memento the memento tokenizer
*/
public ICElement getHandleFromMemento(MementoTokenizer memento) {
if (!memento.hasMoreTokens()) return this;
String token = memento.nextToken();
return getHandleFromMemento(token, memento);
}
/**
* Creates a C element handle from the given memento.
* The given token is the current delimiter indicating the type of the next token(s).
*
* @param token the curren memento token
* @param memento the memento tokenizer
*/
public abstract ICElement getHandleFromMemento(String token, MementoTokenizer memento);
/**
* Escape special characters in the given name and append the result to buffer.
*
* @param buffer the buffer to build the memento string
* @param mementoName the name to escape
*/
public static void escapeMementoName(StringBuilder buffer, String mementoName) {
for (int i = 0, length = mementoName.length(); i < length; i++) {
char character = mementoName.charAt(i);
switch (character) {
case CEM_ESCAPE:
case CEM_CPROJECT:
case CEM_TRANSLATIONUNIT:
case CEM_SOURCEROOT:
case CEM_SOURCEFOLDER:
case CEM_SOURCEELEMENT:
case CEM_ELEMENTTYPE:
case CEM_PARAMETER:
buffer.append(CEM_ESCAPE);
}
buffer.append(character);
}
}
}
|