PageRenderTime 32ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/ABB.SrcML.Data-Monolithic/Archive.cs

https://github.com/nkcsgexi/SrcML.NET
C# | 242 lines | 161 code | 26 blank | 55 comment | 54 complexity | 8606a08cf1c9665babf2da51f155dc95 MD5 | raw file
  1. /******************************************************************************
  2. * Copyright (c) 2011 ABB Group
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Vinay Augustine (ABB Group) - initial API, implementation, & documentation
  10. *****************************************************************************/
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Xml.Linq;
  16. using System.Data.Linq;
  17. namespace ABB.SrcML.Data
  18. {
  19. /// <summary>
  20. /// Class representing a SrcML archive from the database
  21. /// </summary>
  22. partial class Archive
  23. {
  24. private static HashSet<XName> ScopeContainers = new HashSet<XName>(ContainerNames.All);
  25. private SrcMLFile _document;
  26. /// <summary>
  27. /// Gets the document.
  28. /// </summary>
  29. public SrcMLFile Document
  30. {
  31. get
  32. {
  33. if (null == this._document)
  34. this._document = new SrcMLFile(this.Path);
  35. return this._document;
  36. }
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="Archive"/> class.
  40. /// </summary>
  41. /// <param name="document">The document.</param>
  42. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
  43. public Archive(SrcMLFile document)
  44. : this()
  45. {
  46. if (null == document)
  47. throw new ArgumentNullException("document");
  48. this.Path = document.FileName;
  49. this.LastUpdated = System.IO.File.GetLastWriteTime(this.Path);
  50. }
  51. /// <summary>
  52. /// Gets the definitions from archive.
  53. /// </summary>
  54. /// <returns>all the definitions associated with this archive</returns>
  55. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
  56. public IEnumerable<Definition> GetDefinitionsFromArchive()
  57. {
  58. var definitions = from unit in this.Document.FileUnits
  59. from definition in GetDefinitionsFromFileUnit(unit)
  60. select definition;
  61. return definitions;
  62. }
  63. /// <summary>
  64. /// Creates definitions from this file unit
  65. /// </summary>
  66. /// <param name="fileUnit">The file unit.</param>
  67. /// <returns>an enumerable of definition objects to be inserted into the database</returns>
  68. public IEnumerable<Definition> GetDefinitionsFromFileUnit(XElement fileUnit)
  69. {
  70. if (fileUnit == null)
  71. throw new ArgumentNullException("fileUnit");
  72. var fileName = fileUnit.Attribute("filename").Value;
  73. var definitions = from child in fileUnit.DescendantsAndSelf()
  74. where Definition.ValidNames.Contains(child.Name)
  75. let defs = Definition.CreateFromElement(child, fileName, this.Id)
  76. from def in defs
  77. select def;
  78. return definitions;
  79. }
  80. /// <summary>
  81. /// Locates possible variable declarations that match the given name element.
  82. /// </summary>
  83. /// <param name="db">The db.</param>
  84. /// <param name="element">The element.</param>
  85. /// <returns>A collection of declarations that match this element</returns>
  86. public IEnumerable<VariableDeclaration> GetDeclarationForVariable(SrcMLDataContext db, XElement element)
  87. {
  88. if (null == db)
  89. throw new ArgumentNullException("db");
  90. if (null == element)
  91. throw new ArgumentNullException("element");
  92. //SrcMLHelper.ThrowExceptionOnInvalidName(element, SRC.Name);
  93. var parentContainer = (from container in element.Ancestors()
  94. where ScopeContainers.Contains(container.Name)
  95. select container).FirstOrDefault();
  96. if (null != parentContainer)
  97. {
  98. var parentXPath = parentContainer.GetXPath(false);
  99. return GetDeclarationForVariable(db, element, parentXPath);
  100. }
  101. return null;
  102. }
  103. /// <summary>
  104. /// Locates possible variable declarations that match the given name element.
  105. /// </summary>
  106. /// <param name="db">The db.</param>
  107. /// <param name="element">The element.</param>
  108. /// <param name="containerPath">The container path.</param>
  109. /// <returns>A collection of declarations that match this element</returns>
  110. public IEnumerable<VariableDeclaration> GetDeclarationForVariable(SrcMLDataContext db, XElement element, string containerPath)
  111. {
  112. if (null == db)
  113. throw new ArgumentNullException("db");
  114. var declarations = from declaration in db.Definitions.OfType<VariableDeclaration>()
  115. where declaration.ArchiveId == this.Id
  116. where declaration.DeclarationName == element.Value
  117. where declaration.ValidScopes.Any(vs => vs.XPath == containerPath)
  118. select declaration;
  119. if (!declarations.Any())
  120. {
  121. declarations = from declaration in db.Definitions.OfType<VariableDeclaration>()
  122. where declaration.Archive == this
  123. where declaration.DeclarationName == element.Value
  124. where declaration.IsGlobal ?? false
  125. select declaration;
  126. }
  127. return declarations;
  128. }
  129. /// <summary>
  130. /// Locates the ty
  131. /// </summary>
  132. /// <param name="db">The db.</param>
  133. /// <param name="typeElement">The type element.</param>
  134. /// <returns>a collection of type definitions that match the given element.</returns>
  135. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
  136. public IEnumerable<TypeDefinition> GetTypeForVariableName(SrcMLDataContext db, XElement typeElement)
  137. {
  138. if (null == db)
  139. throw new ArgumentNullException("db");
  140. if (null == typeElement)
  141. throw new ArgumentNullException("typeElement");
  142. SrcMLHelper.ThrowExceptionOnInvalidName(typeElement, SRC.Type);
  143. var typeName = typeElement.Elements(SRC.Name).Last().Value;
  144. var typeCandidates = from typeDefinition in db.Definitions.OfType<TypeDefinition>()
  145. where typeDefinition.Archive == this
  146. where typeDefinition.TypeName == typeName
  147. select typeDefinition;
  148. return typeCandidates;
  149. }
  150. /// <summary>
  151. /// Gets a collection of method definitions that match the given call element.
  152. /// </summary>
  153. /// <param name="db">The db.</param>
  154. /// <param name="call">The call.</param>
  155. /// <returns>A collection of method definitions that match</returns>
  156. public IEnumerable<MethodDefinition> GetMethodForCall(SrcMLDataContext db, XElement call)
  157. {
  158. if (null == db)
  159. throw new ArgumentNullException("db");
  160. if (null == call)
  161. throw new ArgumentNullException("call");
  162. SrcMLHelper.ThrowExceptionOnInvalidName(call, SRC.Call);
  163. var containingFunction = (from parent in call.Ancestors()
  164. where ContainerNames.MethodDefinitions.Any(n => n == parent.Name)
  165. select parent).First();
  166. var className = SrcMLHelper.GetClassNameForMethod(containingFunction);
  167. TypeDefinition classDef = null;
  168. if (null != className)
  169. {
  170. var candidates = from ce in db.Definitions.OfType<TypeDefinition>()
  171. where ce.TypeName == className.Value
  172. select ce;
  173. if (candidates.Any())
  174. {
  175. classDef = candidates.First();
  176. }
  177. }
  178. var precedingElements = call.ElementsBeforeSelf();
  179. if (precedingElements.Any())
  180. {
  181. var last = precedingElements.Last();
  182. var count = precedingElements.Count();
  183. if (last.Name == OP.Operator && count > 1 && (last.Value == "." || last.Value == "->"))
  184. {
  185. var callingObject = precedingElements.Take(count - 1).Last();
  186. if("this" != callingObject.Value)
  187. {
  188. var candidateDeclarations = this.GetDeclarationForVariable(db, callingObject);
  189. if (candidateDeclarations.Any())
  190. {
  191. var declaration = candidateDeclarations.First();
  192. var possibleTypes = from ce in db.Definitions.OfType<TypeDefinition>()
  193. where ce.TypeName == declaration.VariableTypeName
  194. select ce;
  195. if (possibleTypes.Any())
  196. {
  197. classDef = possibleTypes.First();
  198. }
  199. }
  200. }
  201. }
  202. }
  203. var numArguments = call.Element(SRC.ArgumentList).Elements(SRC.Argument).Count();
  204. string typeName = null;
  205. if (null != classDef)
  206. typeName = classDef.TypeName;
  207. var methods = from method in db.Definitions.OfType<MethodDefinition>()
  208. where method.MethodName == call.Element(SRC.Name).Value
  209. where (typeName == null ? true : typeName == method.MethodClassName)
  210. let minNumberOfParameters = method.NumberOfMethodParameters - method.NumberOfMethodParametersWithDefaults
  211. where numArguments >= minNumberOfParameters && numArguments <= method.NumberOfMethodParameters
  212. select method;
  213. return methods;
  214. }
  215. }
  216. }