changeset 714:d5bd1c640db0

impl name space at gears
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Fri, 28 Aug 2020 12:24:42 +0900
parents 843a94916d8a
children fd9b9fa4ec98
files src/parallel_execution/generate_stub.pl
diffstat 1 files changed, 75 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/parallel_execution/generate_stub.pl	Fri Aug 28 11:42:31 2020 +0900
+++ b/src/parallel_execution/generate_stub.pl	Fri Aug 28 12:24:42 2020 +0900
@@ -71,8 +71,9 @@
 my %filename2EachCodeGearArgs;
 my %stub;
 
-my $implInterfaceInfo = {isImpl => undef, implementation => undef, interface => undef};
-my $generateHaveOutputStub = { counter => {}, list => {} };
+my $implInterfaceInfo = {isImpl => undef, implementation => undef, interface => undef, parsedInterfaceInfo => undef, genConstructor => undef};
+my $generateHaveOutputStub = { counter => undef, list => undef };
+my $replaceCodeGearNames = {};
 
 
 # this for statement is main routine
@@ -143,8 +144,10 @@
                 $implInterfaceInfo->{implementation} = $3;
                 my $cbc_source_path = $searchCbCFromCodeGearNameWCurrentFrileName->($interfaceName, $filename);
                 if ($cbc_source_path) {
+                  # Probably not executed
                     &getDataGear($cbc_source_path);
                 }
+                $implInterfaceInfo->{genConstructor} = 0;
             } elsif(/^(.*)par goto (\w+)\((.*)\)/) {
                 my $codeGearName = $2;
                 my $cbc_source_path = $searchCbCFromCodeGearNameWCurrentFrileName->($codeGearName, $filename);
@@ -158,32 +161,59 @@
                 $interfaceHeader =~ m|(\w+)\.\w+$|; #remove filename extention
                 my $interfaceName = $1;
                 includeInterface(\%call_interfaces, $filename, $interfaceName, $headerNameToInfo);
+            } elsif(/^#impl "(.*)"/) {
+                # use interface
+                my $interfaceHeader = $1;
+                $interfaceHeader =~ m|(\w+)\.\w+$|; #remove filename extention
+                my $interfaceName = $1;
+                includeInterface(\%call_interfaces, $filename, $interfaceName, $headerNameToInfo);
+                my $interfacePATH = $headerNameToInfo->{$interfaceName}->{path};
+
+                my $implName = basename $filename;
+                $implName =~ s/\.cbc$//;
+
+                $implInterfaceInfo->{isImpl} = 1;
+                $implInterfaceInfo->{interface} = $interfaceName;
+                $implInterfaceInfo->{implementation} = $implName;
+                $implInterfaceInfo->{genConstructor} = 1;
+                $implInterfaceInfo->{parsedInterfaceInfo} = Gears::Interface->separate_code_and_data_gear_after_parse($interfacePATH);
+
             } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) {
                 my $codeGearName = $1;
+                my $args = $2;
+
                 if ($codeGearName =~ /_stub$/) {
                   $stub{$codeGearName}->{static} = 1;
-                  $stub{$codeGearName}->{wrote} = 1;
+                  $stub{$codeGearName}->{wrote}  = 1;
                   $currentCodeGear = undef;
                   next;
                 }
-                my $args = $2;
+
+                if ($implInterfaceInfo->{parsedInterfaceInfo}) {
+                  if (exists_codegear_in_interface({codeGearName => $codeGearName, parsedInfo => $implInterfaceInfo->{parsedInterfaceInfo}})) {
+                    my $replaceCodeGear = "${codeGearName}$implInterfaceInfo->{implementation}"; #${pop}SingleLinkedStack
+                    $replaceCodeGearNames->{$codeGearName} = $replaceCodeGear;
+                    $codeGearName = $replaceCodeGear;
+                  }
+                }
+
                 my $cbc_source_path = $searchCbCFromCodeGearNameWCurrentFrileName->($codeGearName, $filename);
                 if ($cbc_source_path) {
-                    &getCodeGear($cbc_source_path);
+                  &getCodeGear($cbc_source_path);
                 }
                 my $vname2type = Gears::Util->parseCodeGearDeclarationArg($args);
                 for my $vname (keys %$vname2type) {
                   $codeGearInfo->{$codeGearName}->{arg}->{$vname} = $vname2type->{$vname};
                 }
                 $currentCodeGear = $codeGearName;
-            } elsif ((/^\s*(union|struct|const|enum)?\s*(\w+)(\*)\s+(\w+)\s+=/) && $currentCodeGear) { # collect local variables
+              } elsif ((/^\s*(union|struct|const|enum)?\s*(\w+)(\*)\s+(\w+)\s+=/) && $currentCodeGear) { # collect local variables
                 my $structType = $1;
                 my $interfaceName = $2;
                 my $instance = $4;
                 $codeGearInfo->{$currentCodeGear}->{localVar}->{$instance} = $interfaceName;
-            } elsif (/^(.*)goto (\w+)\-\>(\w+)\((.*)\);/) {
+              } elsif (/^(.*)goto (\w+)\-\>(\w+)\((.*)\);/) {
                 # handling goto statement
-                # convert it to the meta call form with two arugments, that is context and enum Code
+                # determine the interface you are using, and in the case of a goto CodeGear with output, create a special stub flag
                 my $prev = $1;
                 my $instance = $2;
                 my $method = $3;
@@ -206,6 +236,14 @@
                   includeInterface(\%call_interfaces, $filename, $typeName, $headerNameToInfo);
                 }
 
+
+                if ($implInterfaceInfo->{parsedInterfaceInfo}) {
+                  if (exists_codegear_in_interface({codeGearName => $tmpArgs, parsedInfo => $implInterfaceInfo->{parsedInterfaceInfo}})) {
+                    my $replaceCodeGear = "${tmpArgs}$implInterfaceInfo->{implementation}"; #${pop}SingleLinkedStack
+                    $tmpArgs = $replaceCodeGear;
+                  }
+                }
+
                 my $nextOutPutArgs = findExistsOutputDataGear($typeName, $method);
                 my $outputStubElem = { modifyEnumCode => $currentCodeGear, createStubName => $tmpArgs };
 
@@ -557,6 +595,12 @@
                 $codeGearName = $1;
                 my $args = $2;
                 my $tail = $3;
+
+                #replace Code Gear Name to Implemenatation
+                if (exists $replaceCodeGearNames->{$codeGearName}) {
+                    $codeGearName = $replaceCodeGearNames->{$codeGearName};
+                }
+
                 if ($codeGearName =~ /_stub$/) {
                     # don't touch already existing stub
                     $inStub = 1;
@@ -706,6 +750,13 @@
                             my $hasGotoArgOrLocalVar = undef;
                             my $outputStubElem = $generateHaveOutputStub->{list}->{$codeGearName};
 
+                            if ($implInterfaceInfo->{parsedInterfaceInfo}) {
+                              if (exists_codegear_in_interface({codeGearName => $arg, parsedInfo => $implInterfaceInfo->{parsedInterfaceInfo}})) {
+                                my $replaceCodeGear = "${arg}$implInterfaceInfo->{implementation}"; #${pop}SingleLinkedStack
+                                $arg = $replaceCodeGear;
+                              }
+                            }
+
                             if ($outputStubElem && !$stub{$outputStubElem->{createStubName}."_stub"}->{static}) {
                               my $pick_next = "$outputStubElem->{createStubName}_$outputStubElem->{counter}";
                               print $fd "${indent}Gearef(${context_name}, $ntype)->$pName = C_$pick_next;\n";
@@ -724,7 +775,7 @@
                             # inteface case
 
                             if ($arg =~ /->/) {
-                              print $fd "${indent}Gearef(${context_name}, $ntype)->$pName = $arg;\n"; #Gearef->()->next = next;
+                              print $fd "${indent}Gearef(${context_name}, $ntype)->$pName = $arg;\n"; #Gearef->()->next = bar->baz;
                               $i++;
                               next;
                             }
@@ -735,7 +786,6 @@
                               next;
                             }
 
-                            #TODO: goto to special stub
                             print $fd "${indent}Gearef(${context_name}, $ntype)->$pName = C_$arg;\n";
                             $i++;
                             next;
@@ -895,13 +945,13 @@
     }
 
 
+
     #Create a stub when the output is a different interface
     for my $modifyEnumCodeCodeGear (keys %{$generateHaveOutputStub->{list}})  {
-      my $outputStubElem = $generateHaveOutputStub->{list}->{$modifyEnumCodeCodeGear};
-      my $targetStubName = $outputStubElem->{createStubName};
-      my $createStubName = "$outputStubElem->{createStubName}_$outputStubElem->{counter}";
-      my $replaceArgs    = $outputStubElem->{args};
-
+      my $outputStubElem      = $generateHaveOutputStub->{list}->{$modifyEnumCodeCodeGear};
+      my $targetStubName      = $outputStubElem->{createStubName};
+      my $createStubName      = "$outputStubElem->{createStubName}_$outputStubElem->{counter}";
+      my $replaceArgs         = $outputStubElem->{args};
       my $replaceStubContents = $dataGearName{$targetStubName};
 
       #If the stub was handwritten, skip
@@ -911,12 +961,10 @@
 
       for my $arg (keys %$replaceArgs) {
         my $interface = $replaceArgs->{$arg};
-        print "replace $arg $interface\n";
-        $replaceStubContents =~ s/,(.*)\)->$arg/,$interface)->$arg/;
+        $replaceStubContents =~ s/,(.*)\)->$arg/, $interface)->$arg/;
       }
 
       generateStub($fd,$createStubName,$replaceStubContents);
-
     }
 
 }
@@ -1002,3 +1050,12 @@
   return "";
 }
 
+sub exists_codegear_in_interface {
+  my $arg = shift;
+
+  my $codeGearName        = $arg->{codeGearName};
+  my $parsedInterfaceInfo = $arg->{parsedInfo};
+  my @res = grep { $_->{name} eq $codeGearName } @{$parsedInterfaceInfo->{codes}};
+  return @res;
+}
+