You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

VarEntity.java 3.3 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. MIT License
  3. Copyright (c) 2018-2019 Gang ZHANG
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. package depends.entity;
  21. import depends.relations.IBindingResolver;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. public class VarEntity extends ContainerEntity {
  25. private GenericName rawType;
  26. private TypeEntity type;
  27. private List<FunctionCall> functionCalls;
  28. public VarEntity() {
  29. }
  30. public VarEntity(GenericName simpleName, GenericName rawType, Entity parent, int id) {
  31. super(simpleName, parent,id);
  32. this.rawType = rawType;
  33. }
  34. public void setRawType(GenericName rawType) {
  35. this.rawType =rawType;
  36. }
  37. public GenericName getRawType() {
  38. return rawType;
  39. }
  40. @Override
  41. public TypeEntity getType() {
  42. return type;
  43. }
  44. public void setType(TypeEntity type) {
  45. this.type = type;
  46. }
  47. @Override
  48. public void inferLocalLevelEntities(IBindingResolver bindingResolver) {
  49. super.inferLocalLevelEntities(bindingResolver);
  50. Entity entity = bindingResolver.resolveName(this, rawType, true);
  51. if (entity!=null) {
  52. this.setActualReferTo(entity);
  53. type = entity.getType();
  54. if (type==null) {
  55. if (((ContainerEntity)getParent()).isGenericTypeParameter(rawType)) {
  56. type = TypeEntity.genericParameterType;
  57. }
  58. }
  59. }
  60. if (type==null) {
  61. fillCandidateTypes(bindingResolver);
  62. }
  63. }
  64. public List<FunctionCall> getCalledFunctions() {
  65. if (this.functionCalls!=null)
  66. return functionCalls;
  67. return new ArrayList<>();
  68. }
  69. public void addFunctionCall(GenericName fname) {
  70. if (this.functionCalls==null)
  71. {
  72. functionCalls = new ArrayList<>();
  73. }
  74. this.functionCalls.add(new FunctionCall(fname));
  75. }
  76. public void fillCandidateTypes(IBindingResolver bindingResolver) {
  77. if (!bindingResolver.isEagerExpressionResolve()) return;
  78. if (type!=null && !(type instanceof CandidateTypes)) return ; //it is a strong type lang, do not need deduce candidate types
  79. if (functionCalls==null) return;
  80. if (functionCalls.size()==0) return; //no information avaliable for type deduction
  81. if (this.rawType==null) {
  82. List<TypeEntity> candidateTypes = bindingResolver.calculateCandidateTypes(this,this.functionCalls);
  83. if (candidateTypes.size()>0) {
  84. this.type = new CandidateTypes(candidateTypes, bindingResolver.getRepo().generateId());
  85. bindingResolver.getRepo().add(this.type);
  86. }
  87. }
  88. }
  89. }