codetools: h2p: implemented typedef function pointers

git-svn-id: trunk@14577 -
This commit is contained in:
mattias 2008-03-18 17:35:31 +00:00
parent ad20039e2e
commit 70de988f83
3 changed files with 60 additions and 13 deletions

View File

@ -182,7 +182,8 @@ type
function ExtractFunctionType(FuncNode: TCodeTreeNode; function ExtractFunctionType(FuncNode: TCodeTreeNode;
WithDirectives: boolean = false): string; WithDirectives: boolean = false): string;
function ExtractFunctionResultType(FuncNode: TCodeTreeNode; function ExtractFunctionResultType(FuncNode: TCodeTreeNode;
WithDirectives: boolean = false): string; WithDirectives: boolean = false;
WithBrackets: boolean = true): string;
function IsPointerToFunction(FuncNode: TCodeTreeNode): boolean; function IsPointerToFunction(FuncNode: TCodeTreeNode): boolean;
function ExtractParameterName(ParamNode: TCodeTreeNode): string; function ExtractParameterName(ParamNode: TCodeTreeNode): string;
function ExtractParameterType(ParamNode: TCodeTreeNode; function ExtractParameterType(ParamNode: TCodeTreeNode;
@ -1384,9 +1385,11 @@ begin
end; end;
function TCCodeParserTool.ExtractFunctionResultType(FuncNode: TCodeTreeNode; function TCCodeParserTool.ExtractFunctionResultType(FuncNode: TCodeTreeNode;
WithDirectives: boolean): string; WithDirectives: boolean; WithBrackets: boolean): string;
var var
NameNode: TCodeTreeNode; NameNode: TCodeTreeNode;
p: Integer;
CurAtomStart: integer;
begin begin
NameNode:=GetFirstNameNode(FuncNode); NameNode:=GetFirstNameNode(FuncNode);
if (NameNode=nil) then begin if (NameNode=nil) then begin
@ -1414,6 +1417,22 @@ begin
Result:=Result+ExtractCode(NameNode.EndPos,FuncNode.EndPos, Result:=Result+ExtractCode(NameNode.EndPos,FuncNode.EndPos,
WithDirectives); WithDirectives);
end; 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; end;
function TCCodeParserTool.IsPointerToFunction(FuncNode: TCodeTreeNode function TCCodeParserTool.IsPointerToFunction(FuncNode: TCodeTreeNode
@ -1553,9 +1572,13 @@ begin
Node:=TypedefNode.LastChild; Node:=TypedefNode.LastChild;
while (Node<>nil) and (Node.Desc<>ccnName) do while (Node<>nil) and (Node.Desc<>ccnName) do
Node:=Node.PriorBrother; Node:=Node.PriorBrother;
if Node=nil then if Node=nil then begin
Result:='' if (TypedefNode.FirstChild<>nil)
else and (TypedefNode.FirstChild.Desc=ccnFunction) then
Result:=ExtractFunctionName(TypedefNode.FirstChild)
else
Result:='';
end else
Result:=GetIdentifier(@Src[Node.StartPos]); Result:=GetIdentifier(@Src[Node.StartPos]);
end; end;

View File

@ -118,7 +118,7 @@ struct struct1 {
void *data; void *data;
}; };
typedef void(*sdp_list_func_t)(void *, void *); typedef void(*procedure_type)(void *, void *);
complex operator+(complex, complex); complex operator+(complex, complex);

View File

@ -314,8 +314,10 @@ begin
if CNode.FirstChild<>nil then begin if CNode.FirstChild<>nil then begin
CurName:=CTool.ExtractTypedefName(CNode); CurName:=CTool.ExtractTypedefName(CNode);
DebugLn(['TH2PasTool.BuildH2PTree Typedef name="',CurName,'"']); DebugLn(['TH2PasTool.BuildH2PTree Typedef name="',CurName,'"']);
case CNode.FirstChild.Desc of ChildNode:=CNode.FirstChild;
ccnStruct: case ChildNode.Desc of
ccnStruct: // typedef struct
begin begin
ChildNode:=CNode.FirstChild.FirstChild; ChildNode:=CNode.FirstChild.FirstChild;
if (ChildNode<>nil) if (ChildNode<>nil)
@ -333,11 +335,33 @@ begin
BuildH2PTree(TypeH2PNode,ChildNode); BuildH2PTree(TypeH2PNode,ChildNode);
end; end;
end; end;
ccnVariable:
ccnFunction: // typedef function
begin 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; end;
else
else // typedef
DebugLn(['TH2PasTool.BuildH2PTree SKIPPING typedef ',CCNodeDescAsString(CNode.FirstChild.Desc),' at ',CTool.CleanPosToStr(CNode.StartPos)]); DebugLn(['TH2PasTool.BuildH2PTree SKIPPING typedef ',CCNodeDescAsString(CNode.FirstChild.Desc),' at ',CTool.CleanPosToStr(CNode.StartPos)]);
end; end;
end; end;
@ -418,7 +442,7 @@ begin
if Ok then begin if Ok then begin
H2PNode:=CreateH2PNode(CurName,CurName,CNode,ctnProcedure,SimpleType, H2PNode:=CreateH2PNode(CurName,CurName,CNode,ctnProcedure,SimpleType,
nil,ParentNode=nil); nil,false);
DebugLn(['TH2PasTool.BuildH2PTree function added: ',H2PNode.DescAsString]); DebugLn(['TH2PasTool.BuildH2PTree function added: ',H2PNode.DescAsString]);
// build recursively // build recursively
BuildH2PTree(H2PNode); BuildH2PTree(H2PNode);
@ -528,7 +552,7 @@ end;
function TH2PasTool.GetSimplePascalResultTypeOfCFunction( function TH2PasTool.GetSimplePascalResultTypeOfCFunction(
CFuncNode: TCodeTreeNode): string; CFuncNode: TCodeTreeNode): string;
begin begin
Result:=CTool.ExtractFunctionResultType(CFuncNode); Result:=CTool.ExtractFunctionResultType(CFuncNode,false,false);
if Result='' then exit; if Result='' then exit;
Result:=ConvertSimpleCTypeToPascalType(Result,true); Result:=ConvertSimpleCTypeToPascalType(Result,true);
end; end;