1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Patch for Hoda Amer:

This patch updates the create methods of the CModelBuilder:
-changes visibility to protected.
-returns the created element.
The new CModelBuilder returns a map of the created elements and their infos.
This commit is contained in:
Doug Schaefer 2003-04-15 18:37:32 +00:00
parent e6d765afcd
commit f3b8a1b3ea
3 changed files with 78 additions and 48 deletions

View file

@ -203,8 +203,8 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
} }
return sourceManipulationInfo; return sourceManipulationInfo;
} }
protected void parse(InputStream in) { protected Map parse(InputStream in) {
getTranslationUnitInfo().parse(in); return (getTranslationUnitInfo().parse(in));
} }
protected CElementInfo createElementInfo () { protected CElementInfo createElementInfo () {
@ -300,12 +300,14 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
// put the info now, because getting the contents requires it // put the info now, because getting the contents requires it
CModelManager.getDefault().putInfo(this, info); CModelManager.getDefault().putInfo(this, info);
TranslationUnitInfo unitInfo = (TranslationUnitInfo) info; TranslationUnitInfo unitInfo = (TranslationUnitInfo) info;
// generate structure // generate structure
this.parse(); newElements = null;
newElements = this.parse();
// this is temporary until the New Model Builder is implemented // this is temporary until the New Model Builder is implemented
getNewElements(newElements, this); if(newElements == null)
getNewElements(newElements, this);
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
if (isWorkingCopy()) { if (isWorkingCopy()) {
@ -455,11 +457,12 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
/** /**
* Parse the buffer contents of this element. * Parse the buffer contents of this element.
*/ */
public void parse(){ public Map parse(){
try{ try{
getTranslationUnitInfo().parse(this.getBuffer().getContents()); return (getTranslationUnitInfo().parse(this.getBuffer().getContents()));
} catch (CModelException e){ } catch (CModelException e){
// error getting the buffer // error getting the buffer
return null;
} }
} }

View file

@ -7,6 +7,7 @@ package org.eclipse.cdt.internal.core.model;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringBufferInputStream; import java.io.StringBufferInputStream;
import java.util.Map;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
@ -38,31 +39,34 @@ class TranslationUnitInfo extends OpenableInfo {
return fChildren; return fChildren;
} }
protected void parse(InputStream in) { protected Map parse(InputStream in) {
try { try {
removeChildren(); removeChildren();
if (CCorePlugin.getDefault().useNewParser()) { if (CCorePlugin.getDefault().useNewParser()) {
// new parser // new parser
CModelBuilder modelBuilder = new CModelBuilder((TranslationUnit)getElement()); CModelBuilder modelBuilder = new CModelBuilder((TranslationUnit)getElement());
modelBuilder.parse(); return (modelBuilder.parse());
} else { } else {
// cdt 1.0 parser // cdt 1.0 parser
ModelBuilder modelBuilder= new ModelBuilder((TranslationUnit)getElement()); ModelBuilder modelBuilder= new ModelBuilder((TranslationUnit)getElement());
CStructurizer.getCStructurizer().parse(modelBuilder, in); CStructurizer.getCStructurizer().parse(modelBuilder, in);
} return null;
}
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); System.out.println(e);
return null;
} }
} }
protected void parse(String buf) { protected Map parse(String buf) {
// CHECKPOINT: Parsing a string using the StringBufferInputStream // CHECKPOINT: Parsing a string using the StringBufferInputStream
// FIXME: quick fix for the IBinary which uses fake translationUnit // FIXME: quick fix for the IBinary which uses fake translationUnit
if (buf != null) { if (buf != null) {
StringBufferInputStream in = new StringBufferInputStream (buf); StringBufferInputStream in = new StringBufferInputStream (buf);
parse (in); return (parse (in));
} }
return null;
} }
/* Overide the SourceManipulation for the range. */ /* Overide the SourceManipulation for the range. */

View file

@ -12,8 +12,10 @@ package org.eclipse.cdt.internal.core.model;
* Rational Software - initial implementation * Rational Software - initial implementation
******************************************************************************/ ******************************************************************************/
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
@ -49,12 +51,15 @@ import org.eclipse.core.resources.IProject;
public class CModelBuilder { public class CModelBuilder {
org.eclipse.cdt.internal.core.model.TranslationUnit translationUnit; protected org.eclipse.cdt.internal.core.model.TranslationUnit translationUnit;
protected Map newElements;
public CModelBuilder(org.eclipse.cdt.internal.core.model.TranslationUnit tu) { public CModelBuilder(org.eclipse.cdt.internal.core.model.TranslationUnit tu) {
this.translationUnit = tu ; this.translationUnit = tu ;
this.newElements = new HashMap();
} }
public TranslationUnit parse() throws Exception { public Map parse() throws Exception {
DOMBuilder domBuilder = new DOMBuilder(); DOMBuilder domBuilder = new DOMBuilder();
String code = translationUnit.getBuffer().getContents(); String code = translationUnit.getBuffer().getContents();
Parser parser = new Parser(code, domBuilder, true); Parser parser = new Parser(code, domBuilder, true);
@ -68,7 +73,7 @@ public class CModelBuilder {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
generateModelElements(domBuilder.getTranslationUnit()); generateModelElements(domBuilder.getTranslationUnit());
System.out.println("CModel build: "+ ( System.currentTimeMillis() - startTime ) + "ms" ); System.out.println("CModel build: "+ ( System.currentTimeMillis() - startTime ) + "ms" );
return domBuilder.getTranslationUnit(); return this.newElements;
} }
protected void generateModelElements(TranslationUnit tu){ protected void generateModelElements(TranslationUnit tu){
@ -165,7 +170,7 @@ public class CModelBuilder {
} }
private void createElement(Parent parent, SimpleDeclaration simpleDeclaration, Declarator declarator){ protected void createElement(Parent parent, SimpleDeclaration simpleDeclaration, Declarator declarator){
// typedef // typedef
if(simpleDeclaration.getDeclSpecifier().isTypedef()){ if(simpleDeclaration.getDeclSpecifier().isTypedef()){
createTypeDef(parent, declarator, simpleDeclaration); createTypeDef(parent, declarator, simpleDeclaration);
@ -187,7 +192,7 @@ public class CModelBuilder {
} }
} }
private void createTemplateElement(Parent parent, TemplateDeclaration templateDeclaration, SimpleDeclaration simpleDeclaration, Declarator declarator){ protected void createTemplateElement(Parent parent, TemplateDeclaration templateDeclaration, SimpleDeclaration simpleDeclaration, Declarator declarator){
ParameterDeclarationClause pdc = declarator.getParms(); ParameterDeclarationClause pdc = declarator.getParms();
if (pdc != null){ if (pdc != null){
// template of function or method // template of function or method
@ -196,7 +201,7 @@ public class CModelBuilder {
template.setTemplateParameterTypes(parameterTypes); template.setTemplateParameterTypes(parameterTypes);
} }
} }
private void createInclusion(Parent parent, Inclusion inclusion){ protected Include createInclusion(Parent parent, Inclusion inclusion){
// create element // create element
Include element = new Include((CElement)parent, inclusion.getName()); Include element = new Include((CElement)parent, inclusion.getName());
// add to parent // add to parent
@ -204,9 +209,11 @@ public class CModelBuilder {
// set position // set position
element.setIdPos(inclusion.getNameOffset(), inclusion.getNameLength()); element.setIdPos(inclusion.getNameOffset(), inclusion.getNameLength());
element.setPos(inclusion.getStartingOffset(), inclusion.getTotalLength()); element.setPos(inclusion.getStartingOffset(), inclusion.getTotalLength());
this.newElements.put(element, element.getElementInfo());
return element;
} }
private void createMacro(Parent parent, Macro macro){ protected org.eclipse.cdt.internal.core.model.Macro createMacro(Parent parent, Macro macro){
// create element // create element
org.eclipse.cdt.internal.core.model.Macro element = new org.eclipse.cdt.internal.core.model.Macro(parent, macro.getName()); org.eclipse.cdt.internal.core.model.Macro element = new org.eclipse.cdt.internal.core.model.Macro(parent, macro.getName());
// add to parent // add to parent
@ -214,10 +221,11 @@ public class CModelBuilder {
// set position // set position
element.setIdPos(macro.getNameOffset(), macro.getNameLength()); element.setIdPos(macro.getNameOffset(), macro.getNameLength());
element.setPos(macro.getStartingOffset(), macro.getTotalLength()); element.setPos(macro.getStartingOffset(), macro.getTotalLength());
this.newElements.put(element, element.getElementInfo());
return element;
} }
private IParent createNamespace(Parent parent, NamespaceDefinition nsDef){ protected Namespace createNamespace(Parent parent, NamespaceDefinition nsDef){
// create element // create element
String nsName = (nsDef.getName() == null ) ? "" : nsDef.getName().toString(); String nsName = (nsDef.getName() == null ) ? "" : nsDef.getName().toString();
Namespace element = new Namespace ((ICElement)parent, nsName ); Namespace element = new Namespace ((ICElement)parent, nsName );
@ -232,41 +240,49 @@ public class CModelBuilder {
element.setPos(nsDef.getStartingOffset(), nsDef.getTotalLength()); element.setPos(nsDef.getStartingOffset(), nsDef.getTotalLength());
element.setTypeName(nsDef.getStartToken().getImage()); element.setTypeName(nsDef.getStartToken().getImage());
return (IParent)element; this.newElements.put(element, element.getElementInfo());
return element;
} }
private IParent createEnumeration(Parent parent, EnumerationSpecifier enumSpecifier){ protected Enumeration createEnumeration(Parent parent, EnumerationSpecifier enumSpecifier){
// create element // create element
String enumName = (enumSpecifier.getName() == null ) ? "" : enumSpecifier.getName().toString(); String enumName = (enumSpecifier.getName() == null ) ? "" : enumSpecifier.getName().toString();
Enumeration enum = new Enumeration ((ICElement)parent, enumName ); Enumeration element = new Enumeration ((ICElement)parent, enumName );
// add to parent // add to parent
parent.addChild((ICElement)enum); parent.addChild((ICElement)element);
List enumItems = enumSpecifier.getEnumeratorDefinitions(); List enumItems = enumSpecifier.getEnumeratorDefinitions();
Iterator i = enumItems.iterator(); Iterator i = enumItems.iterator();
while (i.hasNext()){ while (i.hasNext()){
// create sub element // create sub element
EnumeratorDefinition enumDef = (EnumeratorDefinition) i.next(); EnumeratorDefinition enumDef = (EnumeratorDefinition) i.next();
Enumerator element = new Enumerator (enum, enumDef.getName().toString()); createEnumerator(element, enumDef);
// add to parent
enum.addChild(element);
// set enumerator position
element.setIdPos(enumDef.getName().getStartOffset(), enumDef.getName().length());
element.setPos(enumDef.getStartingOffset(), enumDef.getTotalLength());
} }
// set enumeration position // set enumeration position
if(enumSpecifier.getName() != null ){ if(enumSpecifier.getName() != null ){
enum.setIdPos(enumSpecifier.getName().getStartOffset(), enumSpecifier.getName().length()); element.setIdPos(enumSpecifier.getName().getStartOffset(), enumSpecifier.getName().length());
}else { }else {
enum.setIdPos(enumSpecifier.getStartToken().getOffset(), enumSpecifier.getStartToken().getLength()); element.setIdPos(enumSpecifier.getStartToken().getOffset(), enumSpecifier.getStartToken().getLength());
} }
enum.setPos(enumSpecifier.getStartingOffset(), enumSpecifier.getTotalLength()); element.setPos(enumSpecifier.getStartingOffset(), enumSpecifier.getTotalLength());
enum.setTypeName(enumSpecifier.getStartToken().getImage()); element.setTypeName(enumSpecifier.getStartToken().getImage());
return (IParent)enum; this.newElements.put(element, element.getElementInfo());
return element;
} }
private IParent createClass(Parent parent, SimpleDeclaration simpleDeclaration, ClassSpecifier classSpecifier, boolean isTemplate){ protected Enumerator createEnumerator(Parent enum, EnumeratorDefinition enumDef){
Enumerator element = new Enumerator (enum, enumDef.getName().toString());
// add to parent
enum.addChild(element);
// set enumerator position
element.setIdPos(enumDef.getName().getStartOffset(), enumDef.getName().length());
element.setPos(enumDef.getStartingOffset(), enumDef.getTotalLength());
this.newElements.put(element, element.getElementInfo());
return element;
}
protected Structure createClass(Parent parent, SimpleDeclaration simpleDeclaration, ClassSpecifier classSpecifier, boolean isTemplate){
// create element // create element
String className = (classSpecifier.getName() == null ) ? "" : classSpecifier.getName().toString(); String className = (classSpecifier.getName() == null ) ? "" : classSpecifier.getName().toString();
int kind; int kind;
@ -311,26 +327,29 @@ public class CModelBuilder {
element.setTypeName( type ); element.setTypeName( type );
element.setPos(classSpecifier.getStartingOffset(), classSpecifier.getTotalLength()); element.setPos(classSpecifier.getStartingOffset(), classSpecifier.getTotalLength());
this.newElements.put(element, element.getElementInfo());
return element; return element;
} }
private void createTypeDef(Parent parent, Declarator declarator, SimpleDeclaration simpleDeclaration){ protected TypeDef createTypeDef(Parent parent, Declarator declarator, SimpleDeclaration simpleDeclaration){
// create the element // create the element
String declaratorName = declarator.getName().toString(); String declaratorName = declarator.getName().toString();
TypeDef typedef = new TypeDef( parent, declaratorName ); TypeDef element = new TypeDef( parent, declaratorName );
String type = getType(simpleDeclaration, declarator); String type = getType(simpleDeclaration, declarator);
typedef.setTypeName(type); element.setTypeName(type);
// add to parent // add to parent
parent.addChild((CElement)typedef); parent.addChild((CElement)element);
// set positions // set positions
typedef.setIdPos(declarator.getName().getStartOffset(), declarator.getName().length()); element.setIdPos(declarator.getName().getStartOffset(), declarator.getName().length());
typedef.setPos(simpleDeclaration.getStartingOffset(), simpleDeclaration.getTotalLength()); element.setPos(simpleDeclaration.getStartingOffset(), simpleDeclaration.getTotalLength());
this.newElements.put(element, element.getElementInfo());
return element;
} }
private VariableDeclaration createVariableSpecification(Parent parent, SimpleDeclaration simpleDeclaration, Declarator declarator){ protected VariableDeclaration createVariableSpecification(Parent parent, SimpleDeclaration simpleDeclaration, Declarator declarator){
String declaratorName = declarator.getName().toString(); String declaratorName = declarator.getName().toString();
DeclSpecifier declSpecifier = simpleDeclaration.getDeclSpecifier(); DeclSpecifier declSpecifier = simpleDeclaration.getDeclSpecifier();
@ -365,10 +384,11 @@ public class CModelBuilder {
element.setIdPos( declarator.getName().getStartOffset(), declarator.getName().length() ); element.setIdPos( declarator.getName().getStartOffset(), declarator.getName().length() );
element.setPos(simpleDeclaration.getStartingOffset(), simpleDeclaration.getTotalLength()); element.setPos(simpleDeclaration.getStartingOffset(), simpleDeclaration.getTotalLength());
this.newElements.put(element, element.getElementInfo());
return element; return element;
} }
private FunctionDeclaration createFunctionSpecification(Parent parent, SimpleDeclaration simpleDeclaration, Declarator declarator, ParameterDeclarationClause pdc, boolean isTemplate){ protected FunctionDeclaration createFunctionSpecification(Parent parent, SimpleDeclaration simpleDeclaration, Declarator declarator, ParameterDeclarationClause pdc, boolean isTemplate){
String declaratorName = declarator.getName().toString(); String declaratorName = declarator.getName().toString();
DeclSpecifier declSpecifier = simpleDeclaration.getDeclSpecifier(); DeclSpecifier declSpecifier = simpleDeclaration.getDeclSpecifier();
// getParameterTypes // getParameterTypes
@ -442,10 +462,11 @@ public class CModelBuilder {
// hook up the offsets // hook up the offsets
element.setIdPos( declarator.getName().getStartOffset(), declarator.getName().length() ); element.setIdPos( declarator.getName().getStartOffset(), declarator.getName().length() );
element.setPos(simpleDeclaration.getStartingOffset(), simpleDeclaration.getTotalLength()); element.setPos(simpleDeclaration.getStartingOffset(), simpleDeclaration.getTotalLength());
this.newElements.put(element, element.getElementInfo());
return element; return element;
} }
private VariableDeclaration createPointerToFunction(Parent parent, SimpleDeclaration simpleDeclaration, Declarator declarator, ParameterDeclarationClause pdc, boolean isTemplate){ protected VariableDeclaration createPointerToFunction(Parent parent, SimpleDeclaration simpleDeclaration, Declarator declarator, ParameterDeclarationClause pdc, boolean isTemplate){
String declaratorName = declarator.getDeclarator().getName().toString(); String declaratorName = declarator.getDeclarator().getName().toString();
DeclSpecifier declSpecifier = simpleDeclaration.getDeclSpecifier(); DeclSpecifier declSpecifier = simpleDeclaration.getDeclSpecifier();
// getParameterTypes // getParameterTypes
@ -489,8 +510,10 @@ public class CModelBuilder {
// hook up the offsets // hook up the offsets
element.setIdPos( declarator.getDeclarator().getName().getStartOffset(), declarator.getDeclarator().getName().length() ); element.setIdPos( declarator.getDeclarator().getName().getStartOffset(), declarator.getDeclarator().getName().length() );
element.setPos(simpleDeclaration.getStartingOffset(), simpleDeclaration.getTotalLength()); element.setPos(simpleDeclaration.getStartingOffset(), simpleDeclaration.getTotalLength());
this.newElements.put(element, element.getElementInfo());
return element; return element;
} }
private String[] getTemplateParameters(ITemplateParameterListOwner templateDeclaration){ private String[] getTemplateParameters(ITemplateParameterListOwner templateDeclaration){
// add the parameters // add the parameters
List templateParameters = templateDeclaration.getTemplateParms().getDeclarations(); List templateParameters = templateDeclaration.getTemplateParms().getDeclarations();