mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-08 10:16:03 +02:00
Bug 319279. Passing parameter and return value by reference.
This commit is contained in:
parent
693a79de5e
commit
253ba9ddef
5 changed files with 107 additions and 282 deletions
|
@ -14,7 +14,9 @@ import java.io.IOException;
|
||||||
|
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IVariable;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
||||||
|
import org.eclipse.cdt.core.parser.util.ASTPrinter;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.ClassTypeHelper;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.ClassTypeHelper;
|
||||||
import org.eclipse.cdt.internal.core.parser.ParserException;
|
import org.eclipse.cdt.internal.core.parser.ParserException;
|
||||||
|
|
||||||
|
@ -39,6 +41,13 @@ public class ClassTypeHelperTests extends AST2BaseTest {
|
||||||
return new BindingAssertionHelper(code, true);
|
return new BindingAssertionHelper(code, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// int a;
|
||||||
|
// const int& b;
|
||||||
|
public void testTemp() throws Exception {
|
||||||
|
BindingAssertionHelper helper = getAssertionHelper();
|
||||||
|
ASTPrinter.print(helper.getTranslationUnit());
|
||||||
|
}
|
||||||
|
|
||||||
// struct A {
|
// struct A {
|
||||||
// A(const A& a);
|
// A(const A& a);
|
||||||
// };
|
// };
|
||||||
|
|
|
@ -52,10 +52,9 @@ public class TypeHelper {
|
||||||
}
|
}
|
||||||
SizeofCalculator calc = ((ASTTranslationUnit) ast).getSizeofCalculator();
|
SizeofCalculator calc = ((ASTTranslationUnit) ast).getSizeofCalculator();
|
||||||
SizeAndAlignment sizeofPointer = calc.sizeAndAlignmentOfPointer();
|
SizeAndAlignment sizeofPointer = calc.sizeAndAlignmentOfPointer();
|
||||||
if (sizeofPointer == null)
|
long maxSize = sizeofPointer != null ? sizeofPointer.size : 4;
|
||||||
return true;
|
|
||||||
SizeAndAlignment sizeofType = calc.sizeAndAlignment(type);
|
SizeAndAlignment sizeofType = calc.sizeAndAlignment(type);
|
||||||
if (sizeofType == null || sizeofType.size > sizeofPointer.size)
|
if (sizeofType == null || sizeofType.size > maxSize)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -30,7 +30,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
//=
|
//=
|
||||||
#ifndef C_H_
|
#ifndef C_H_
|
||||||
#define C_H_
|
#define C_H_
|
||||||
|
@ -70,7 +70,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -97,7 +97,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
//!Generate Getters and Setters One Getter Selection with Namespace
|
//!Generate Getters and Setters One Getter Selection with Namespace
|
||||||
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
||||||
//@.config
|
//@.config
|
||||||
|
@ -132,7 +132,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -157,7 +157,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
|
||||||
//=
|
//=
|
||||||
#ifndef C_H_
|
#ifndef C_H_
|
||||||
|
@ -177,7 +177,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -206,7 +206,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
|
||||||
//!Generate Getters and Setters One Setter Selection
|
//!Generate Getters and Setters One Setter Selection
|
||||||
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
||||||
|
@ -240,7 +240,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -264,7 +264,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
//=
|
//=
|
||||||
#ifndef C_H_
|
#ifndef C_H_
|
||||||
#define C_H_
|
#define C_H_
|
||||||
|
@ -281,7 +281,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -309,7 +309,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
//!Generate Getters and Setters Getter and Setter Selection
|
//!Generate Getters and Setters Getter and Setter Selection
|
||||||
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
||||||
//@.config
|
//@.config
|
||||||
|
@ -343,7 +343,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -367,7 +367,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
//=
|
//=
|
||||||
#ifndef C_H_
|
#ifndef C_H_
|
||||||
#define C_H_
|
#define C_H_
|
||||||
|
@ -384,7 +384,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -416,7 +416,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
//!Generate Getters and Setters Multiple Selection
|
//!Generate Getters and Setters Multiple Selection
|
||||||
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
||||||
//@.config
|
//@.config
|
||||||
|
@ -450,7 +450,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -465,7 +465,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
|
||||||
//=
|
//=
|
||||||
#ifndef C_H_
|
#ifndef C_H_
|
||||||
|
@ -499,7 +499,7 @@ public:
|
||||||
this->systemId = systemId;
|
this->systemId = systemId;
|
||||||
}
|
}
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -514,7 +514,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
|
||||||
//!Generate Getters and Setters Visibility order 1
|
//!Generate Getters and Setters Visibility order 1
|
||||||
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
||||||
|
@ -596,7 +596,7 @@ private:
|
||||||
int /*$*/id/*$$*/;
|
int /*$*/id/*$$*/;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
|
||||||
//=
|
//=
|
||||||
#ifndef C_H_
|
#ifndef C_H_
|
||||||
|
@ -616,7 +616,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
|
||||||
//!Generate Getters and Setters no Methods
|
//!Generate Getters and Setters no Methods
|
||||||
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
||||||
|
@ -704,53 +704,37 @@ class test {
|
||||||
|
|
||||||
#endif /* TEST_H_ */
|
#endif /* TEST_H_ */
|
||||||
|
|
||||||
//!Generate Getters and Setters One Getter Selection Separate Definition
|
//!Generate Getters and Setters, Pass by Reference, Separate Definition
|
||||||
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
||||||
//@.config
|
//@.config
|
||||||
filename=C.h
|
filename=C.h
|
||||||
getters=name
|
getters=name
|
||||||
|
setters=name
|
||||||
definitionSeparate=true
|
definitionSeparate=true
|
||||||
//@C.cpp
|
|
||||||
#include "C.h"
|
|
||||||
|
|
||||||
int Person::SocSecNo() {
|
|
||||||
return socSecNo;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
|
||||||
}
|
|
||||||
|
|
||||||
//=
|
|
||||||
#include "C.h"
|
|
||||||
|
|
||||||
char* Person::getName() const {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Person::SocSecNo() {
|
|
||||||
return socSecNo;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
|
||||||
}
|
|
||||||
|
|
||||||
//@C.h
|
//@C.h
|
||||||
#ifndef C_H_
|
#ifndef C_H_
|
||||||
#define C_H_
|
#define C_H_
|
||||||
|
|
||||||
|
struct FullName {
|
||||||
|
const char* first;
|
||||||
|
const char* last;
|
||||||
|
FullName(const FullName& other);
|
||||||
|
~FullName();
|
||||||
|
};
|
||||||
|
|
||||||
class Person {
|
class Person {
|
||||||
private:
|
private:
|
||||||
int systemId;
|
int systemId;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
char* name;
|
FullName name;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
const int socSecNo;
|
const int socSecNo;
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -769,25 +753,33 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
//=
|
//=
|
||||||
#ifndef C_H_
|
#ifndef C_H_
|
||||||
#define C_H_
|
#define C_H_
|
||||||
|
|
||||||
|
struct FullName {
|
||||||
|
const char* first;
|
||||||
|
const char* last;
|
||||||
|
FullName(const FullName& other);
|
||||||
|
~FullName();
|
||||||
|
};
|
||||||
|
|
||||||
class Person {
|
class Person {
|
||||||
private:
|
private:
|
||||||
int systemId;
|
int systemId;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
char* name;
|
FullName name;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
const int socSecNo;
|
const int socSecNo;
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
char* getName() const;
|
const FullName& getName() const;
|
||||||
|
void setName(const FullName& name);
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -806,7 +798,35 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
//@C.cpp
|
||||||
|
#include "C.h"
|
||||||
|
|
||||||
|
int Person::SocSecNo() {
|
||||||
|
return socSecNo;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
}
|
||||||
|
|
||||||
|
//=
|
||||||
|
#include "C.h"
|
||||||
|
|
||||||
|
const FullName& Person::getName() const {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Person::setName(const FullName& name) {
|
||||||
|
this->name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Person::SocSecNo() {
|
||||||
|
return socSecNo;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
}
|
||||||
|
|
||||||
//!Generate Getters and Setters One Getter Selection with Namespace Separate Definition
|
//!Generate Getters and Setters One Getter Selection with Namespace Separate Definition
|
||||||
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
||||||
//@.config
|
//@.config
|
||||||
|
@ -863,7 +883,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -888,7 +908,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
|
||||||
//=
|
//=
|
||||||
#ifndef C_H_
|
#ifndef C_H_
|
||||||
|
@ -908,7 +928,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -934,7 +954,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
|
||||||
//!Generate Getters and Setters One Setter Selection Separate Definition
|
//!Generate Getters and Setters One Setter Selection Separate Definition
|
||||||
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
||||||
|
@ -984,7 +1004,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -1008,7 +1028,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
//=
|
//=
|
||||||
#ifndef C_H_
|
#ifndef C_H_
|
||||||
#define C_H_
|
#define C_H_
|
||||||
|
@ -1025,7 +1045,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -1050,7 +1070,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
//!Getter and Setter Selection Separate Definition
|
//!Getter and Setter Selection Separate Definition
|
||||||
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
||||||
//@.config
|
//@.config
|
||||||
|
@ -1102,7 +1122,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -1126,7 +1146,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
|
||||||
//=
|
//=
|
||||||
#ifndef C_H_
|
#ifndef C_H_
|
||||||
|
@ -1144,7 +1164,7 @@ public:
|
||||||
|
|
||||||
Person myFriend;
|
Person myFriend;
|
||||||
|
|
||||||
Person(int socSecNo); // contructor
|
Person(int socSecNo); // constructor
|
||||||
|
|
||||||
~Person(); // destructor
|
~Person(); // destructor
|
||||||
|
|
||||||
|
@ -1170,7 +1190,7 @@ public:
|
||||||
|
|
||||||
int gooo = 1;
|
int gooo = 1;
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
|
||||||
//!Generate Getters and Setters no Methods Separate Definition
|
//!Generate Getters and Setters no Methods Separate Definition
|
||||||
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
||||||
|
@ -1188,7 +1208,7 @@ private:
|
||||||
int /*$*/id/*$$*/;
|
int /*$*/id/*$$*/;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
|
||||||
//=
|
//=
|
||||||
#ifndef C_H_
|
#ifndef C_H_
|
||||||
|
@ -1212,7 +1232,7 @@ inline void Person::setId(int id) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif /*C_H_*/
|
#endif /* C_H_ */
|
||||||
|
|
||||||
//!No Methods Separate Definition
|
//!No Methods Separate Definition
|
||||||
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
|
||||||
|
|
|
@ -1,203 +0,0 @@
|
||||||
/*******************************************************************************
|
|
||||||
* Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik
|
|
||||||
* Rapperswil, University of applied sciences 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:
|
|
||||||
* Institute for Software - initial API and implementation
|
|
||||||
* Sergey Prigogin (Google)
|
|
||||||
*******************************************************************************/
|
|
||||||
package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
|
|
||||||
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.CopyStyle;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
|
||||||
import org.eclipse.cdt.core.parser.Keywords;
|
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTCompoundStatement;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTExpressionStatement;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFieldReference;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDefinition;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTParameterDeclaration;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReturnStatement;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclaration;
|
|
||||||
|
|
||||||
public class FunctionFactory {
|
|
||||||
|
|
||||||
public static IASTFunctionDefinition createGetterDefinition(IASTName fieldName,
|
|
||||||
IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) {
|
|
||||||
IASTFunctionDefinition getter = new CPPASTFunctionDefinition();
|
|
||||||
|
|
||||||
getter.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy(CopyStyle.withLocations));
|
|
||||||
IASTDeclarator getterDeclarator = getGetterDeclarator(fieldName, fieldDeclaration, name);
|
|
||||||
// IASTFunctionDefinition. expects the outermost IASTFunctionDeclarator in declarator hierarchy
|
|
||||||
while (!(getterDeclarator instanceof IASTFunctionDeclarator)) {
|
|
||||||
getterDeclarator = getterDeclarator.getNestedDeclarator();
|
|
||||||
}
|
|
||||||
getter.setDeclarator((IASTFunctionDeclarator) getterDeclarator);
|
|
||||||
getter.setBody(getGetterBody(fieldName));
|
|
||||||
return getter;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static CPPASTCompoundStatement getGetterBody(IASTName fieldName) {
|
|
||||||
CPPASTCompoundStatement compound = new CPPASTCompoundStatement();
|
|
||||||
CPPASTReturnStatement returnStatement = new CPPASTReturnStatement();
|
|
||||||
CPPASTIdExpression idExpr = new CPPASTIdExpression();
|
|
||||||
CPPASTName returnVal = new CPPASTName();
|
|
||||||
returnVal.setName(fieldName.toCharArray());
|
|
||||||
idExpr.setName(returnVal);
|
|
||||||
returnStatement.setReturnValue(idExpr);
|
|
||||||
compound.addStatement(returnStatement);
|
|
||||||
return compound;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IASTDeclarator getGetterDeclarator(IASTName fieldName,
|
|
||||||
IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) {
|
|
||||||
CPPASTName getterName = new CPPASTName();
|
|
||||||
getterName.setName(GetterSetterNameGenerator.generateGetterName(fieldName).toCharArray());
|
|
||||||
|
|
||||||
// copy declarator hierarchy
|
|
||||||
IASTDeclarator topDeclarator = fieldDeclaration.getDeclarators()[0].copy(CopyStyle.withLocations);
|
|
||||||
|
|
||||||
// find the innermost declarator in hierarchy
|
|
||||||
IASTDeclarator innermost = topDeclarator;
|
|
||||||
while (innermost.getNestedDeclarator() != null) {
|
|
||||||
innermost = innermost.getNestedDeclarator();
|
|
||||||
}
|
|
||||||
|
|
||||||
// create a new innermost function declarator basing on the field declarator
|
|
||||||
CPPASTFunctionDeclarator functionDeclarator = new CPPASTFunctionDeclarator();
|
|
||||||
functionDeclarator.setConst(true);
|
|
||||||
if (name != null) {
|
|
||||||
name.addName(getterName);
|
|
||||||
functionDeclarator.setName(name);
|
|
||||||
} else {
|
|
||||||
functionDeclarator.setName(getterName);
|
|
||||||
}
|
|
||||||
for (IASTPointerOperator pointer : innermost.getPointerOperators()){
|
|
||||||
functionDeclarator.addPointerOperator(pointer.copy(CopyStyle.withLocations));
|
|
||||||
}
|
|
||||||
|
|
||||||
// replace innermost with functionDeclarator and return the whole declarator tree
|
|
||||||
if (innermost == topDeclarator) {
|
|
||||||
// no tree
|
|
||||||
return functionDeclarator;
|
|
||||||
} else {
|
|
||||||
IASTDeclarator parent = (IASTDeclarator) innermost.getParent();
|
|
||||||
parent.setNestedDeclarator(functionDeclarator);
|
|
||||||
return topDeclarator;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IASTFunctionDefinition createSetterDefinition(IASTName fieldName,
|
|
||||||
IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) {
|
|
||||||
IASTFunctionDefinition setter = new CPPASTFunctionDefinition();
|
|
||||||
setter.setDeclSpecifier(getVoidDeclSpec());
|
|
||||||
setter.setDeclarator(getSetterDeclarator(fieldName, fieldDeclaration, name));
|
|
||||||
setter.setBody(getSetterBody(fieldDeclaration));
|
|
||||||
return setter;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static CPPASTCompoundStatement getSetterBody(IASTSimpleDeclaration fieldDeclaration) {
|
|
||||||
CPPASTCompoundStatement compound = new CPPASTCompoundStatement();
|
|
||||||
CPPASTExpressionStatement exprStmt = new CPPASTExpressionStatement();
|
|
||||||
CPPASTBinaryExpression binExpr = new CPPASTBinaryExpression();
|
|
||||||
IASTDeclarator innerDeclarator = fieldDeclaration.getDeclarators()[0];
|
|
||||||
while (innerDeclarator.getNestedDeclarator() != null) {
|
|
||||||
innerDeclarator = innerDeclarator.getNestedDeclarator();
|
|
||||||
}
|
|
||||||
IASTName fieldName = innerDeclarator.getName();
|
|
||||||
CPPASTName parameterName = getSetterParameterName(fieldName);
|
|
||||||
if (Arrays.equals(fieldName.getSimpleID(), parameterName.getSimpleID())) {
|
|
||||||
CPPASTFieldReference fieldRef = new CPPASTFieldReference();
|
|
||||||
CPPASTLiteralExpression litExpr = new CPPASTLiteralExpression();
|
|
||||||
litExpr.setValue(Keywords.cTHIS);
|
|
||||||
fieldRef.setFieldOwner(litExpr);
|
|
||||||
fieldRef.setIsPointerDereference(true);
|
|
||||||
fieldRef.setFieldName(fieldName.copy(CopyStyle.withLocations));
|
|
||||||
binExpr.setOperand1(fieldRef);
|
|
||||||
} else {
|
|
||||||
CPPASTIdExpression idExpr = new CPPASTIdExpression(fieldName.copy(CopyStyle.withLocations));
|
|
||||||
binExpr.setOperand1(idExpr);
|
|
||||||
}
|
|
||||||
binExpr.setOperator(IASTBinaryExpression.op_assign);
|
|
||||||
CPPASTIdExpression idExpr = new CPPASTIdExpression(parameterName);
|
|
||||||
binExpr.setOperand2(idExpr);
|
|
||||||
exprStmt.setExpression(binExpr);
|
|
||||||
compound.addStatement(exprStmt);
|
|
||||||
return compound;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static CPPASTFunctionDeclarator getSetterDeclarator(IASTName fieldName,
|
|
||||||
IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) {
|
|
||||||
CPPASTName setterName = new CPPASTName();
|
|
||||||
setterName.setName(GetterSetterNameGenerator.generateSetterName(fieldName).toCharArray());
|
|
||||||
CPPASTFunctionDeclarator declarator = new CPPASTFunctionDeclarator();
|
|
||||||
if (name != null) {
|
|
||||||
name.addName(setterName);
|
|
||||||
declarator.setName(name);
|
|
||||||
} else {
|
|
||||||
declarator.setName(setterName);
|
|
||||||
}
|
|
||||||
CPPASTParameterDeclaration parameterDeclaration = new CPPASTParameterDeclaration();
|
|
||||||
IASTDeclarator parameterDeclarator = fieldDeclaration.getDeclarators()[0].copy(CopyStyle.withLocations);
|
|
||||||
parameterDeclarator.setName(getSetterParameterName(fieldName));
|
|
||||||
parameterDeclaration.setDeclarator(parameterDeclarator);
|
|
||||||
parameterDeclaration.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy(
|
|
||||||
CopyStyle.withLocations));
|
|
||||||
declarator.addParameterDeclaration(parameterDeclaration.copy(CopyStyle.withLocations));
|
|
||||||
return declarator;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static CPPASTName getSetterParameterName(IASTName fieldName) {
|
|
||||||
String parameterName = GetterSetterNameGenerator.generateSetterParameterName(fieldName);
|
|
||||||
return new CPPASTName(parameterName.toCharArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static CPPASTSimpleDeclSpecifier getVoidDeclSpec() {
|
|
||||||
CPPASTSimpleDeclSpecifier declSpecifier = new CPPASTSimpleDeclSpecifier();
|
|
||||||
declSpecifier.setType(IASTSimpleDeclSpecifier.t_void);
|
|
||||||
return declSpecifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IASTSimpleDeclaration createGetterDeclaration(IASTName fieldName,
|
|
||||||
IASTSimpleDeclaration fieldDeclaration) {
|
|
||||||
IASTSimpleDeclaration getter = new CPPASTSimpleDeclaration();
|
|
||||||
IASTDeclSpecifier declSpec = fieldDeclaration.getDeclSpecifier();
|
|
||||||
getter.setDeclSpecifier(declSpec.copy(CopyStyle.withLocations));
|
|
||||||
// TODO(sprigogin): Implement return by reference
|
|
||||||
// IType type = CPPVisitor.createType(declSpec);
|
|
||||||
// if (TypeHelper.shouldBePassedByReference(type, fieldDeclaration.getTranslationUnit())) {
|
|
||||||
// declSpec.s
|
|
||||||
// }
|
|
||||||
getter.addDeclarator(getGetterDeclarator(fieldName, fieldDeclaration, null));
|
|
||||||
return getter;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IASTSimpleDeclaration createSetterDeclaration(IASTName fieldName,
|
|
||||||
IASTSimpleDeclaration fieldDeclaration) {
|
|
||||||
IASTSimpleDeclaration setter = new CPPASTSimpleDeclaration();
|
|
||||||
setter.setDeclSpecifier(getVoidDeclSpec());
|
|
||||||
setter.addDeclarator(getSetterDeclarator(fieldName, fieldDeclaration, null));
|
|
||||||
return setter;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -31,14 +31,14 @@ public class GetterSetterInsertEditProvider implements Comparable<GetterSetterIn
|
||||||
|
|
||||||
private IASTSimpleDeclaration functionDeclaration;
|
private IASTSimpleDeclaration functionDeclaration;
|
||||||
private AccessorKind kind;
|
private AccessorKind kind;
|
||||||
private IASTName fieldName;
|
|
||||||
private IASTSimpleDeclaration fieldDeclaration;
|
private IASTSimpleDeclaration fieldDeclaration;
|
||||||
|
private GetterSetterFactory getterSetterFactory;
|
||||||
|
|
||||||
public GetterSetterInsertEditProvider(IASTName fieldName, IASTSimpleDeclaration fieldDeclaration,
|
public GetterSetterInsertEditProvider(IASTName fieldName, IASTSimpleDeclaration fieldDeclaration,
|
||||||
AccessorKind kind) {
|
AccessorKind kind) {
|
||||||
this.kind = kind;
|
this.kind = kind;
|
||||||
this.fieldName = fieldName;
|
|
||||||
this.fieldDeclaration = fieldDeclaration;
|
this.fieldDeclaration = fieldDeclaration;
|
||||||
|
this.getterSetterFactory = new GetterSetterFactory(fieldName, fieldDeclaration);
|
||||||
|
|
||||||
createFunctionDeclaration();
|
createFunctionDeclaration();
|
||||||
}
|
}
|
||||||
|
@ -46,10 +46,10 @@ public class GetterSetterInsertEditProvider implements Comparable<GetterSetterIn
|
||||||
public void createFunctionDeclaration() {
|
public void createFunctionDeclaration() {
|
||||||
switch (this.kind) {
|
switch (this.kind) {
|
||||||
case GETTER:
|
case GETTER:
|
||||||
this.functionDeclaration = FunctionFactory.createGetterDeclaration(fieldName, fieldDeclaration);
|
this.functionDeclaration = getterSetterFactory.createGetterDeclaration();
|
||||||
break;
|
break;
|
||||||
case SETTER:
|
case SETTER:
|
||||||
this.functionDeclaration = FunctionFactory.createSetterDeclaration(fieldName, fieldDeclaration);
|
this.functionDeclaration = getterSetterFactory.createSetterDeclaration();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,23 +67,23 @@ public class GetterSetterInsertEditProvider implements Comparable<GetterSetterIn
|
||||||
IASTFunctionDefinition definition = null;
|
IASTFunctionDefinition definition = null;
|
||||||
ICPPASTQualifiedName qname;
|
ICPPASTQualifiedName qname;
|
||||||
if (qualifedName) {
|
if (qualifedName) {
|
||||||
qname = getClassname();
|
qname = getClassName();
|
||||||
} else {
|
} else {
|
||||||
qname = null;
|
qname = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
case GETTER:
|
case GETTER:
|
||||||
definition = FunctionFactory.createGetterDefinition(fieldName, fieldDeclaration, qname);
|
definition = getterSetterFactory.createGetterDefinition(qname);
|
||||||
break;
|
break;
|
||||||
case SETTER:
|
case SETTER:
|
||||||
definition = FunctionFactory.createSetterDefinition(fieldName, fieldDeclaration, qname);
|
definition = getterSetterFactory.createSetterDefinition(qname);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return definition;
|
return definition;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ICPPASTQualifiedName getClassname() {
|
private ICPPASTQualifiedName getClassName() {
|
||||||
IASTNode node = fieldDeclaration.getParent();
|
IASTNode node = fieldDeclaration.getParent();
|
||||||
while (!(node instanceof IASTCompositeTypeSpecifier)) {
|
while (!(node instanceof IASTCompositeTypeSpecifier)) {
|
||||||
node = node.getParent();
|
node = node.getParent();
|
||||||
|
|
Loading…
Add table
Reference in a new issue