1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
{
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
A graph of declaration overloads.
Create via CodeToolBoss.GatherOverloads(Code,X,Y,Graph).
Add units via Graph.ScanToolForIdentifier.
}
unit FindOverloads;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Laz_AVL_Tree,
// Codetools
FileProcs, BasicCodeTools, CodeTree, CodeGraph,
CodeCache, FindDeclarationTool, FindDeclarationCache, StdCodeTools;
type
TOverloadsGraphEdge = class;
{ TOverloadsGraphNode }
TOverloadsGraphNode = class(TCodeGraphNode)
public
Identifier: string;
Tool: TFindDeclarationTool;
ShortestPathLength: integer;
ShortestPathEdge: TOverloadsGraphEdge;
function AsDebugString: string;
end;
TOverloadsGraphEdgeType = (
ogetParentChild,
ogetAncestorInherited,
ogetAliasOld
);
TOverloadsGraphEdgeTypes = set of TOverloadsGraphEdgeType;
{ TOverloadsGraphEdge }
TOverloadsGraphEdge = class(TCodeGraphEdge)
public
Typ: TOverloadsGraphEdgeType;
function AsDebugString: string;
function Cost: integer;
end;
{ TDeclarationOverloadsGraph }
TDeclarationOverloadsGraph = class
private
FGraph: TCodeGraph;
FIdentifier: string;
FOnGetCodeToolForBuffer: TOnGetCodeToolForBuffer;
FShortestNodes: TAVLTree;
FStartCode: TCodeBuffer;
FStartCodeNode: TCodeTreeNode;
FStartTool: TFindDeclarationTool;
FStartX: integer;
FStartY: integer;
function AddContext(Tool: TFindDeclarationTool;
CodeNode: TCodeTreeNode): TOverloadsGraphNode;
function AddEdge(Typ: TOverloadsGraphEdgeType;
FromNode, ToNode: TCodeTreeNode): TOverloadsGraphEdge;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function Init(Code: TCodeBuffer; X,Y: integer): Boolean;
procedure ScanToolForIdentifier(Tool: TStandardCodeTool;
OnlyInterface: boolean);
procedure ComputeShortestPaths;
public
property Graph: TCodeGraph read FGraph;
property Identifier: string read FIdentifier;
property ShortestNodes: TAVLTree read FShortestNodes;// nodes sorted for ShortestPathLength (after ComputeShortestPaths)
property StartCode: TCodeBuffer read FStartCode;
property StartX: integer read FStartX;
property StartY: integer read FStartY;
property StartTool: TFindDeclarationTool read FStartTool;
property StartCodeNode: TCodeTreeNode read FStartCodeNode;
property OnGetCodeToolForBuffer: TOnGetCodeToolForBuffer
read FOnGetCodeToolForBuffer write FOnGetCodeToolForBuffer;
end;
const
OverloadsGraphEdgeTypeNames: array[TOverloadsGraphEdgeType] of string = (
'Parent-Child',
'Ancestor-Inherited',
'Alias-Old'
);
function CompareOverloadsNodesByPathLen(Node1, Node2: TOverloadsGraphNode): integer;
implementation
function CompareOverloadsNodesByPathLen(Node1, Node2: TOverloadsGraphNode
): integer;
begin
if Node1.ShortestPathLength>Node2.ShortestPathLength then
Result:=1
else if Node1.ShortestPathLength<Node2.ShortestPathLength then
Result:=-1
else
Result:=0;
end;
{ TDeclarationOverloadsGraph }
function TDeclarationOverloadsGraph.AddContext(Tool: TFindDeclarationTool;
CodeNode: TCodeTreeNode): TOverloadsGraphNode;
var
ParentCodeNode: TCodeTreeNode;
ParentGraphNode: TOverloadsGraphNode;
ClassNode: TCodeTreeNode;
Params: TFindDeclarationParams;
ListOfPFindContext: TFPList;
i: Integer;
ContextPtr: PFindContext;
AncestorGraphNode: TOverloadsGraphNode;
Context: TFindContext;
AliasGraphNode: TOverloadsGraphNode;
begin
Result:=TOverloadsGraphNode(Graph.GetGraphNode(CodeNode,false));
if Result<>nil then exit;
// add new node
//DebugLn(['TDeclarationOverloadsGraph.AddContext ',Tool.MainFilename,' ',CodeNode.DescAsString,' "',dbgstr(copy(Tool.Src,CodeNode.StartPos,20)),'"']);
Result:=TOverloadsGraphNode(Graph.GetGraphNode(CodeNode,true));
Result.Tool:=Tool;
if CodeNode.Desc in AllIdentifierDefinitions then
Result.Identifier:=Tool.ExtractDefinitionName(CodeNode)
else begin
case CodeNode.Desc of
ctnEnumIdentifier: Result.Identifier:=GetIdentifier(@Tool.Src[CodeNode.StartPos]);
end;
end;
// add parent nodes to graph
ParentCodeNode:=CodeNode.Parent;
while ParentCodeNode<>nil do begin
//DebugLn(['TDeclarationOverloadsGraph.AddContext ',ParentCodeNode.DescAsString]);
if ParentCodeNode.Desc in
AllSourceTypes+AllClasses
then begin
//DebugLn(['TDeclarationOverloadsGraph.AddContext ADD parent']);
ParentGraphNode:=AddContext(Tool,ParentCodeNode);
AddEdge(ogetParentChild,ParentGraphNode.Node,Result.Node);
break;
end;
if ParentCodeNode.Parent<>nil then
ParentCodeNode:=ParentCodeNode.Parent
else
ParentCodeNode:=ParentCodeNode.PriorBrother;
end;
// add ancestors, interfaces
if (CodeNode.Desc=ctnTypeDefinition)
and (CodeNode.FirstChild<>nil)
and (CodeNode.FirstChild.Desc in AllClasses) then begin
//DebugLn(['TDeclarationOverloadsGraph.AddContext a class or interface']);
// a class or class interface
ClassNode:=CodeNode.FirstChild;
Tool.BuildSubTree(ClassNode);
if (ClassNode.FirstChild<>nil)
and (ClassNode.FirstChild.Desc=ctnClassInheritance) then begin
//DebugLn(['TDeclarationOverloadsGraph.AddContext has ancestor(s)']);
Params:=TFindDeclarationParams.Create;
ListOfPFindContext:=nil;
try
Tool.FindAncestorsOfClass(ClassNode,ListOfPFindContext,Params,false);
//DebugLn(['TDeclarationOverloadsGraph.AddContext ancestors found: ',ListOfPFindContext<>nil]);
if ListOfPFindContext<>nil then begin
for i:=0 to ListOfPFindContext.Count-1 do begin
//DebugLn(['TDeclarationOverloadsGraph.AddContext ancestor #',i]);
ContextPtr:=PFindContext(ListOfPFindContext[i]);
AncestorGraphNode:=AddContext(ContextPtr^.Tool,ContextPtr^.Node);
AddEdge(ogetAncestorInherited,AncestorGraphNode.Node,Result.Node);
end;
end;
finally
FreeListOfPFindContext(ListOfPFindContext);
Params.Free;
end;
end;
end;
// ToDo: add alias
if (CodeNode.Desc=ctnTypeDefinition)
and (CodeNode.FirstChild<>nil)
and (CodeNode.FirstChild.Desc=ctnIdentifier) then begin
//DebugLn(['TDeclarationOverloadsGraph.AddContext alias']);
Params:=TFindDeclarationParams.Create;
try
try
Context:=Tool.FindBaseTypeOfNode(Params,CodeNode);
if Context.Node<>nil then begin
while (Context.Node<>nil)
and (not (Context.Node.Desc in AllIdentifierDefinitions)) do
Context.Node:=Context.Node.Parent;
if Context.Node<>nil then begin
AliasGraphNode:=AddContext(Context.Tool,Context.Node);
AddEdge(ogetAliasOld,AliasGraphNode.Node,Result.Node);
end;
end;
except
end;
finally
Params.Free;
end;
end;
end;
function TDeclarationOverloadsGraph.AddEdge(Typ: TOverloadsGraphEdgeType;
FromNode, ToNode: TCodeTreeNode): TOverloadsGraphEdge;
begin
Result:=TOverloadsGraphEdge(Graph.GetEdge(FromNode,ToNode,false));
if (Result<>nil) then begin
if Result.Typ<>Typ then
RaiseCatchableException('TDeclarationOverloadsGraph.AddEdge Typ conflict');
exit;
end;
// create new edge
Result:=TOverloadsGraphEdge(Graph.GetEdge(FromNode,ToNode,true));
Result.Typ:=Typ;
//DebugLn(['TDeclarationOverloadsGraph.AddEdge ',Result.AsDebugString]);
end;
constructor TDeclarationOverloadsGraph.Create;
begin
FGraph:=TCodeGraph.Create(TOverloadsGraphNode,TOverloadsGraphEdge);
end;
destructor TDeclarationOverloadsGraph.Destroy;
begin
Clear;
FreeAndNil(FGraph);
inherited Destroy;
end;
procedure TDeclarationOverloadsGraph.Clear;
begin
FreeAndNil(FShortestNodes);
Graph.Clear;
FStartCodeNode:=nil;
FStartCode:=nil;
FStartX:=0;
FStartY:=0;
FIdentifier:='';
end;
function TDeclarationOverloadsGraph.Init(Code: TCodeBuffer; X, Y: integer
): Boolean;
var
CleanPos: integer;
begin
Result:=false;
FStartCode:=Code;
FStartX:=X;
FStartY:=Y;
fStartTool:=OnGetCodeToolForBuffer(Self,Code,true);
if fStartTool.CaretToCleanPos(CodeXYPosition(X,Y,Code),CleanPos)<>0 then begin
DebugLn(['TDeclarationOverloadsGraph.Init Tool.CaretToCleanPos failed']);
exit(false);
end;
fStartCodeNode:=fStartTool.FindDeepestNodeAtPos(CleanPos,true);
DebugLn(['TDeclarationOverloadsGraph.Init Add start context ',FStartTool.MainFilename,' ',FStartCodeNode.DescAsString,' ',dbgstr(copy(FStartTool.Src,FStartCodeNode.StartPos,20))]);
AddContext(fStartTool,StartCodeNode);
fIdentifier:='';
if fStartCodeNode.Desc in AllIdentifierDefinitions+[ctnEnumIdentifier] then
fIdentifier:=GetIdentifier(@fStartTool.Src[fStartCodeNode.StartPos]);
Result:=true;
end;
procedure TDeclarationOverloadsGraph.ScanToolForIdentifier(
Tool: TStandardCodeTool; OnlyInterface: boolean);
var
Entry: PInterfaceIdentCacheEntry;
Node: TCodeTreeNode;
begin
if Identifier='' then exit;
if OnlyInterface then begin
// use interface cache
try
Tool.BuildInterfaceIdentifierCache(false);
except
end;
if Tool.InterfaceIdentifierCache<>nil then begin
Entry:=Tool.InterfaceIdentifierCache.FindIdentifier(PChar(Identifier));
while Entry<>nil do begin
if CompareIdentifiers(Entry^.Identifier,PChar(Identifier))=0 then
AddContext(Tool,Entry^.Node);
Entry:=Entry^.NextEntry;
end;
end;
end else begin
// scan whole unit/program
try
Tool.Explore(false);
except
end;
if Tool.Tree=nil then exit;
Node:=Tool.Tree.Root;
while Node<>nil do begin
case Node.Desc of
ctnTypeDefinition,ctnVarDefinition,ctnConstDefinition,ctnGenericType,
ctnEnumIdentifier:
if CompareIdentifiers(@Tool.Src[Node.StartPos],PChar(Identifier))=0
then
AddContext(Tool,Node);
ctnProcedure:
begin
Tool.MoveCursorToProcName(Node,true);
if CompareIdentifiers(@Tool.Src[Tool.CurPos.StartPos],PChar(Identifier))=0
then
AddContext(Tool,Node);
end;
ctnProperty:
begin
Tool.MoveCursorToPropName(Node);
if CompareIdentifiers(@Tool.Src[Tool.CurPos.StartPos],PChar(Identifier))=0
then
AddContext(Tool,Node);
end;
end;
Node:=Node.Next;
end;
end;
end;
procedure TDeclarationOverloadsGraph.ComputeShortestPaths;
var
AVLNode: TAVLTreeNode;
GraphNode: TOverloadsGraphNode;
StartGraphNode: TOverloadsGraphNode;
WorkNodes: TAVLTree;
Edge: TOverloadsGraphEdge;
GraphNode2: TOverloadsGraphNode;
begin
(* Dijkstra-Algorithm:
for v in V do l(v):=inf
l(u) := 0
W:=V
while W not empty do
v := { v in W | l(v) minimal }
W:=W-{v}
for x in Adj(v), x in W do
if l(v)+w(v,x)<l(x) then
l(x):=l(v)+w(v,x)
k(x):=(v,x)
*)
StartGraphNode:=TOverloadsGraphNode(Graph.GetGraphNode(StartCodeNode,false));
if StartGraphNode=nil then exit;
WorkNodes:=nil;
try
// set all ShortestPathLength to infinity (except the start which gets 0)
// and sort all nodes in a tree
WorkNodes:=TAVLTree.Create(TListSortCompare(@CompareOverloadsNodesByPathLen));
AVLNode:=Graph.Nodes.FindLowest;
while AVLNode<>nil do begin
GraphNode:=TOverloadsGraphNode(AVLNode.Data);
GraphNode.ShortestPathEdge:=nil;
if GraphNode.Node=StartCodeNode then
GraphNode.ShortestPathLength:=0
else
GraphNode.ShortestPathLength:=high(integer) div 2;
WorkNodes.Add(GraphNode);
AVLNode:=Graph.Nodes.FindSuccessor(AVLNode);
end;
// for each remaining node that has currently the shortest path ...
while WorkNodes.Count>0 do begin
GraphNode:=TOverloadsGraphNode(WorkNodes.FindLowest.Data);
// this node's ShortestPathLength is final
WorkNodes.Remove(GraphNode);
// update adjacent nodes
AVLNode:=GraphNode.OutTree.FindLowest;
while AVLNode<>nil do begin
Edge:=TOverloadsGraphEdge(AVLNode.Data);
GraphNode2:=TOverloadsGraphNode(Edge.ToNode);
if GraphNode.ShortestPathLength+Edge.Cost<GraphNode2.ShortestPathLength
then begin
GraphNode2.ShortestPathLength:=GraphNode.ShortestPathLength+Edge.Cost;
GraphNode2.ShortestPathEdge:=Edge;
end;
AVLNode:=GraphNode.OutTree.FindSuccessor(AVLNode);
end;
// update incident nodes
AVLNode:=GraphNode.InTree.FindLowest;
while AVLNode<>nil do begin
Edge:=TOverloadsGraphEdge(AVLNode.Data);
GraphNode2:=TOverloadsGraphNode(Edge.FromNode);
if GraphNode.ShortestPathLength+Edge.Cost<GraphNode2.ShortestPathLength
then begin
GraphNode2.ShortestPathLength:=GraphNode.ShortestPathLength+Edge.Cost;
GraphNode2.ShortestPathEdge:=Edge;
end;
AVLNode:=GraphNode.InTree.FindSuccessor(AVLNode);
end;
end;
finally
WorkNodes.Free;
end;
// build ShortestNodes
if FShortestNodes=nil then
FShortestNodes:=TAVLTree.Create(TListSortCompare(@CompareOverloadsNodesByPathLen))
else
FShortestNodes.Clear;
AVLNode:=Graph.Nodes.FindLowest;
while AVLNode<>nil do begin
GraphNode:=TOverloadsGraphNode(AVLNode.Data);
FShortestNodes.Add(GraphNode);
AVLNode:=Graph.Nodes.FindSuccessor(AVLNode);
end;
end;
{ TOverloadsGraphNode }
function TOverloadsGraphNode.AsDebugString: string;
begin
Result:=Identifier;
if Node<>nil then
Result:=Result+' Desc="'+Node.DescAsString+'"';
if (Tool<>nil) and (Node<>nil) and (Identifier='') then begin
Result:=Result+' "'+dbgstr(copy(Tool.Src,Node.StartPos,20))+'"';
end;
end;
{ TOverloadsGraphEdge }
function TOverloadsGraphEdge.AsDebugString: string;
begin
Result:='Typ='+OverloadsGraphEdgeTypeNames[Typ]
+' '+(FromNode as TOverloadsGraphNode).AsDebugString
+'->'
+(ToNode as TOverloadsGraphNode).AsDebugString;
end;
function TOverloadsGraphEdge.Cost: integer;
begin
case Typ of
ogetParentChild: Result:=10;
ogetAncestorInherited: Result:=1;
ogetAliasOld: Result:=1;
else Result:=100;
end;
end;
end.
|