From 70de988f839fcc2d1bc90caa257c803673cd65d4 Mon Sep 17 00:00:00 2001 From: mattias Date: Tue, 18 Mar 2008 17:35:31 +0000 Subject: [PATCH] codetools: h2p: implemented typedef function pointers git-svn-id: trunk@14577 - --- components/codetools/ccodeparsertool.pas | 33 +++++++++++++--- .../codetools/examples/scanexamples/test.h | 2 +- components/codetools/h2pastool.pas | 38 +++++++++++++++---- 3 files changed, 60 insertions(+), 13 deletions(-) diff --git a/components/codetools/ccodeparsertool.pas b/components/codetools/ccodeparsertool.pas index 171bef23cb..9f963192cc 100644 --- a/components/codetools/ccodeparsertool.pas +++ b/components/codetools/ccodeparsertool.pas @@ -182,7 +182,8 @@ type function ExtractFunctionType(FuncNode: TCodeTreeNode; WithDirectives: boolean = false): string; function ExtractFunctionResultType(FuncNode: TCodeTreeNode; - WithDirectives: boolean = false): string; + WithDirectives: boolean = false; + WithBrackets: boolean = true): string; function IsPointerToFunction(FuncNode: TCodeTreeNode): boolean; function ExtractParameterName(ParamNode: TCodeTreeNode): string; function ExtractParameterType(ParamNode: TCodeTreeNode; @@ -1384,9 +1385,11 @@ begin end; function TCCodeParserTool.ExtractFunctionResultType(FuncNode: TCodeTreeNode; - WithDirectives: boolean): string; + WithDirectives: boolean; WithBrackets: boolean): string; var NameNode: TCodeTreeNode; + p: Integer; + CurAtomStart: integer; begin NameNode:=GetFirstNameNode(FuncNode); if (NameNode=nil) then begin @@ -1414,6 +1417,22 @@ begin Result:=Result+ExtractCode(NameNode.EndPos,FuncNode.EndPos, WithDirectives); end; + + if not WithBrackets then begin + p:=1; + repeat + ReadRawNextCAtom(Result,p,CurAtomStart); + if CurAtomStart>length(Result) then break; + if Result[CurAtomStart]='(' then begin + p:=CurAtomStart; + if ReadTilCBracketClose(Result,p) then begin + Result:=copy(Result,1,CurAtomStart-1) + +copy(Result,p,length(Result)); + end; + break; + end; + until false; + end; end; function TCCodeParserTool.IsPointerToFunction(FuncNode: TCodeTreeNode @@ -1553,9 +1572,13 @@ begin Node:=TypedefNode.LastChild; while (Node<>nil) and (Node.Desc<>ccnName) do Node:=Node.PriorBrother; - if Node=nil then - Result:='' - else + if Node=nil then begin + if (TypedefNode.FirstChild<>nil) + and (TypedefNode.FirstChild.Desc=ccnFunction) then + Result:=ExtractFunctionName(TypedefNode.FirstChild) + else + Result:=''; + end else Result:=GetIdentifier(@Src[Node.StartPos]); end; diff --git a/components/codetools/examples/scanexamples/test.h b/components/codetools/examples/scanexamples/test.h index b0ec18adef..2c431b9fe0 100644 --- a/components/codetools/examples/scanexamples/test.h +++ b/components/codetools/examples/scanexamples/test.h @@ -118,7 +118,7 @@ struct struct1 { void *data; }; -typedef void(*sdp_list_func_t)(void *, void *); +typedef void(*procedure_type)(void *, void *); complex operator+(complex, complex); diff --git a/components/codetools/h2pastool.pas b/components/codetools/h2pastool.pas index c4228c5276..67ca010afa 100644 --- a/components/codetools/h2pastool.pas +++ b/components/codetools/h2pastool.pas @@ -314,8 +314,10 @@ begin if CNode.FirstChild<>nil then begin CurName:=CTool.ExtractTypedefName(CNode); DebugLn(['TH2PasTool.BuildH2PTree Typedef name="',CurName,'"']); - case CNode.FirstChild.Desc of - ccnStruct: + ChildNode:=CNode.FirstChild; + case ChildNode.Desc of + + ccnStruct: // typedef struct begin ChildNode:=CNode.FirstChild.FirstChild; if (ChildNode<>nil) @@ -333,11 +335,33 @@ begin BuildH2PTree(TypeH2PNode,ChildNode); end; end; - ccnVariable: + + ccnFunction: // typedef function begin - + CurName:=CTool.ExtractFunctionName(ChildNode); + CurType:=CTool.ExtractFunctionResultType(ChildNode,false,false); + IsPointerToFunction:=CTool.IsPointerToFunction(ChildNode); + SimpleType:=GetSimplePascalResultTypeOfCFunction(ChildNode); + if IsPointerToFunction and (SimpleType='') then begin + // this function has a complex result type + TypeH2PNode:=GetH2PNodeForComplexType(ChildNode); + if TypeH2PNode<>nil then + SimpleType:=TypeH2PNode.PascalName; + end; + if IsPointerToFunction and (SimpleType<>'') then begin + H2PNode:=CreateH2PNode(CurName,CurName,CNode,ctnProcedureType,SimpleType, + nil,true); + DebugLn(['TH2PasTool.BuildH2PTree function type added: ',H2PNode.DescAsString]); + // build recursively + if ChildNode.FirstChild<>nil then + BuildH2PTree(H2PNode,ChildNode.FirstChild); + end else begin + DebugLn(['TH2PasTool.BuildH2PTree typdef function CurName=',CurName,' CurType=',CTool.ExtractFunctionResultType(ChildNode),' SimpleType=',SimpleType]); + DebugLn(['TH2PasTool.BuildH2PTree SKIPPING typedef ',CCNodeDescAsString(ChildNode.Desc),' at ',CTool.CleanPosToStr(CNode.StartPos)]); + end; end; - else + + else // typedef DebugLn(['TH2PasTool.BuildH2PTree SKIPPING typedef ',CCNodeDescAsString(CNode.FirstChild.Desc),' at ',CTool.CleanPosToStr(CNode.StartPos)]); end; end; @@ -418,7 +442,7 @@ begin if Ok then begin H2PNode:=CreateH2PNode(CurName,CurName,CNode,ctnProcedure,SimpleType, - nil,ParentNode=nil); + nil,false); DebugLn(['TH2PasTool.BuildH2PTree function added: ',H2PNode.DescAsString]); // build recursively BuildH2PTree(H2PNode); @@ -528,7 +552,7 @@ end; function TH2PasTool.GetSimplePascalResultTypeOfCFunction( CFuncNode: TCodeTreeNode): string; begin - Result:=CTool.ExtractFunctionResultType(CFuncNode); + Result:=CTool.ExtractFunctionResultType(CFuncNode,false,false); if Result='' then exit; Result:=ConvertSimpleCTypeToPascalType(Result,true); end;