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.

RelationCounter.java 9.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.relations;
  21. import depends.deptypes.DependencyType;
  22. import depends.entity.*;
  23. import depends.entity.repo.EntityRepo;
  24. import depends.extractor.AbstractLangProcessor;
  25. import java.util.Collection;
  26. import java.util.List;
  27. import java.util.stream.Collectors;
  28. public class RelationCounter {
  29. private Collection<Entity> entities;
  30. private IBindingResolver bindingResolver;
  31. private EntityRepo repo;
  32. private boolean callAsImpl;
  33. private AbstractLangProcessor langProcessor;
  34. public RelationCounter(EntityRepo repo, AbstractLangProcessor langProcessor, IBindingResolver bindingResolver) {
  35. this.entities = repo.getFileEntities();
  36. this.bindingResolver = bindingResolver;
  37. this.repo = repo;
  38. this.callAsImpl = langProcessor.supportCallAsImpl();
  39. this.langProcessor = langProcessor;
  40. }
  41. public void computeRelations() {
  42. entities.forEach(entity->
  43. computeRelationOf(entity));
  44. }
  45. private void computeRelationOf(Entity entity) {
  46. if (!entity.inScope())
  47. return;
  48. if (entity instanceof FileEntity) {
  49. computeImports((FileEntity)entity);
  50. }
  51. else if (entity instanceof FunctionEntity) {
  52. computeFunctionRelations((FunctionEntity)entity);
  53. }
  54. else if (entity instanceof TypeEntity) {
  55. computeTypeRelations((TypeEntity)entity);
  56. }
  57. if (entity instanceof ContainerEntity) {
  58. computeContainerRelations((ContainerEntity)entity);
  59. }
  60. entity.getChildren().forEach(child->computeRelationOf(child));
  61. }
  62. private void computeContainerRelations(ContainerEntity entity) {
  63. for (VarEntity var:entity.getVars()) {
  64. if (var.getType()!=null)
  65. entity.addRelation(buildRelation(entity,DependencyType.CONTAIN,var.getType(),var.getLocation()));
  66. for (Entity type:var.getResolvedTypeParameters()) {
  67. var.addRelation(buildRelation(var, DependencyType.PARAMETER,type,type.getLocation()));
  68. }
  69. }
  70. for (Entity type:entity.getResolvedAnnotations()) {
  71. entity.addRelation(buildRelation(entity,DependencyType.ANNOTATION,type));
  72. }
  73. for (Entity type:entity.getResolvedTypeParameters()) {
  74. entity.addRelation(buildRelation(entity,DependencyType.USE,type));
  75. }
  76. for (ContainerEntity mixin:entity.getResolvedMixins()) {
  77. entity.addRelation(buildRelation(entity,DependencyType.MIXIN,mixin));
  78. }
  79. entity.reloadExpression(repo);
  80. if (!bindingResolver.isEagerExpressionResolve())
  81. {
  82. entity.resolveExpressions(bindingResolver);
  83. }
  84. for (Expression expression:entity.expressionList()){
  85. if (expression.isStatement()) {
  86. continue;
  87. }
  88. Entity referredEntity = expression.getReferredEntity();
  89. addRelationFromExpression(entity, expression, referredEntity);
  90. }
  91. entity.clearExpressions();
  92. }
  93. private void addRelationFromExpression(ContainerEntity entity, Expression expression, Entity referredEntity) {
  94. if (referredEntity==null) {
  95. return;
  96. }
  97. if (referredEntity.getId()<0){
  98. return;
  99. }
  100. if (referredEntity instanceof MultiDeclareEntities) {
  101. for (Entity e:((MultiDeclareEntities)referredEntity).getEntities()) {
  102. addRelationFromExpression(entity,expression,e);
  103. }
  104. return;
  105. }
  106. boolean matched = false;
  107. if (expression.isCall()) {
  108. /* if it is a FunctionEntityProto, add Relation to all Impl Entities*/
  109. if (callAsImpl && referredEntity instanceof FunctionEntityProto) {
  110. if (entity.getAncestorOfType(FileEntity.class).getId().equals(referredEntity.getAncestorOfType(FileEntity.class).getId())){
  111. entity.addRelation(buildRelation(entity,DependencyType.CALL,referredEntity,expression.getLocation()));
  112. }else {
  113. Entity multiDeclare = repo.getEntity(referredEntity.getQualifiedName());
  114. if (multiDeclare instanceof MultiDeclareEntities) {
  115. MultiDeclareEntities m = (MultiDeclareEntities) multiDeclare;
  116. List<Entity> entities = m.getEntities().stream().filter(item -> (item instanceof FunctionEntityImpl))
  117. .collect(Collectors.toList());
  118. for (Entity e : entities) {
  119. entity.addRelation(expression, buildRelation(entity, DependencyType.IMPLLINK, e, expression.getLocation()));
  120. matched = true;
  121. }
  122. }
  123. }
  124. }
  125. entity.addRelation(buildRelation(entity,DependencyType.CALL,referredEntity,expression.getLocation()));
  126. matched = true;
  127. }
  128. if (expression.isCreate()) {
  129. entity.addRelation(buildRelation(entity,DependencyType.CREATE,referredEntity,expression.getLocation()));
  130. matched = true;
  131. }
  132. if (expression.isThrow()) {
  133. entity.addRelation(buildRelation(entity,DependencyType.THROW,referredEntity,expression.getLocation()));
  134. matched = true;
  135. }
  136. if (expression.isCast()) {
  137. entity.addRelation(buildRelation(entity,DependencyType.CAST,referredEntity,expression.getLocation()));
  138. matched = true;
  139. }
  140. if (!matched) {
  141. if (callAsImpl && repo.getEntity(referredEntity.getQualifiedName()) instanceof MultiDeclareEntities &&
  142. (referredEntity instanceof VarEntity ||referredEntity instanceof FunctionEntity)) {
  143. if (entity.getAncestorOfType(FileEntity.class).getId().equals(referredEntity.getAncestorOfType(FileEntity.class).getId())){
  144. entity.addRelation(buildRelation(entity,DependencyType.USE,referredEntity,expression.getLocation()));
  145. }else {
  146. MultiDeclareEntities m = (MultiDeclareEntities) (repo.getEntity(referredEntity.getQualifiedName()));
  147. for (Entity e : m.getEntities()) {
  148. if (e == referredEntity) {
  149. entity.addRelation(expression, buildRelation(entity, DependencyType.USE, e, expression.getLocation()));
  150. } else {
  151. entity.addRelation(expression, buildRelation(entity, DependencyType.IMPLLINK, e, expression.getLocation()));
  152. }
  153. matched = true;
  154. }
  155. }
  156. }
  157. else {
  158. entity.addRelation(expression,buildRelation(entity,DependencyType.USE,referredEntity,expression.getLocation()));
  159. }
  160. }
  161. }
  162. private Relation buildRelation(Entity from, String type, Entity referredEntity) {
  163. return buildRelation(from,type,referredEntity,from.getLocation());
  164. }
  165. private Relation buildRelation(Entity from, String type, Entity referredEntity,Location location) {
  166. if (referredEntity instanceof AliasEntity) {
  167. if (from.getAncestorOfType(FileEntity.class).equals(referredEntity.getAncestorOfType(FileEntity.class))) {
  168. AliasEntity alias = ((AliasEntity) referredEntity);
  169. if (alias.deepResolve()!=null) {
  170. referredEntity = alias.deepResolve();
  171. }
  172. }
  173. }
  174. if (this.langProcessor==null)
  175. return new Relation(type,referredEntity,location);
  176. return new Relation(langProcessor.getRelationMapping(type),referredEntity,location);
  177. }
  178. private void computeTypeRelations(TypeEntity type) {
  179. for (TypeEntity superType:type.getInheritedTypes()) {
  180. type.addRelation(buildRelation(type,DependencyType.INHERIT,superType));
  181. }
  182. for (TypeEntity interfaceType:type.getImplementedTypes()) {
  183. type.addRelation(buildRelation(type,DependencyType.IMPLEMENT,interfaceType));
  184. }
  185. }
  186. private void computeFunctionRelations(FunctionEntity func) {
  187. for (Entity returnType:func.getReturnTypes()) {
  188. func.addRelation(buildRelation(func,DependencyType.RETURN,returnType.getActualReferTo()));
  189. }
  190. for (VarEntity parameter:func.getParameters()) {
  191. if (parameter.getType()!=null)
  192. func.addRelation(buildRelation(func,DependencyType.PARAMETER,parameter.getActualReferTo()));
  193. }
  194. for (Entity throwType:func.getThrowTypes()) {
  195. func.addRelation(buildRelation(func,DependencyType.THROW,throwType));
  196. }
  197. for (Entity type:func.getResolvedTypeParameters()) {
  198. func.addRelation(buildRelation(func,DependencyType.PARAMETER,type));
  199. }
  200. if (func instanceof FunctionEntityImpl) {
  201. FunctionEntityImpl funcImpl = (FunctionEntityImpl)func;
  202. if(funcImpl.getImplemented()!=null) {
  203. func.addRelation(buildRelation(func,DependencyType.IMPLEMENT,funcImpl.getImplemented()));
  204. }
  205. }
  206. }
  207. private void computeImports(FileEntity file) {
  208. Collection<Entity> imports = file.getImportedRelationEntities();
  209. if (imports==null) return;
  210. for (Entity imported:imports) {
  211. if (imported instanceof FileEntity)
  212. {
  213. if (((FileEntity)imported).isInProjectScope())
  214. file.addRelation(buildRelation(file,DependencyType.IMPORT,imported));
  215. }else {
  216. file.addRelation(buildRelation(file,DependencyType.IMPORT,imported));
  217. }
  218. }
  219. }
  220. }

人工智能研发终端

Contributors (2)