Method.pm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package Logos::Generator::Base::Method;
  2. use strict;
  3. use Logos::Util;
  4. sub originalFunctionName {
  5. my $self = shift;
  6. my $method = shift;
  7. return Logos::sigil(($method->scope eq "+" ? "meta_" : "")."orig").$method->groupIdentifier."\$".$method->class->name."\$".$method->_new_selector;
  8. }
  9. sub newFunctionName {
  10. my $self = shift;
  11. my $method = shift;
  12. return Logos::sigil(($method->scope eq "+" ? "meta_" : "")."method").$method->groupIdentifier."\$".$method->class->name."\$".$method->_new_selector;
  13. }
  14. sub definition {
  15. ::fileError(-1, "generator does not implement Method::definition");
  16. }
  17. sub originalCall {
  18. ::fileError(-1, "generator does not implement Method::originalCall");
  19. }
  20. sub selectorRef {
  21. my $self = shift;
  22. my $selector = shift;
  23. if ($selector eq "dealloc") {
  24. return "sel_registerName(\"".$selector."\")";
  25. }
  26. return "\@selector(".$selector.")";
  27. }
  28. sub selfTypeForMethod {
  29. my $self = shift;
  30. my $method = shift;
  31. if($method->scope eq "+") {
  32. return "_LOGOS_SELF_TYPE_NORMAL Class _LOGOS_SELF_CONST";
  33. }
  34. if($method->methodFamily eq "init") {
  35. return "_LOGOS_SELF_TYPE_INIT ".$method->class->type;
  36. }
  37. return "_LOGOS_SELF_TYPE_NORMAL ".$method->class->type." _LOGOS_SELF_CONST";
  38. }
  39. sub returnTypeForMethod {
  40. my $self = shift;
  41. my $method = shift;
  42. if($method->methodFamily ne "") {
  43. return $method->class->type;
  44. }
  45. my $result = $method->return;
  46. if ($result eq "instancetype") {
  47. return $method->class->type;
  48. }
  49. return $result;
  50. }
  51. sub functionAttributesForMethod {
  52. my $self = shift;
  53. my $method = shift;
  54. if($method->methodFamily ne "") {
  55. return " _LOGOS_RETURN_RETAINED";
  56. }
  57. return "";
  58. }
  59. sub buildLogCall {
  60. my $self = shift;
  61. my $method = shift;
  62. my $args = shift;
  63. # Log preamble
  64. my $build = "HBLogDebug(\@\"".$method->scope."[<".$method->class->name.": %p>";
  65. my $argnamelist = "";
  66. if($method->numArgs > 0) {
  67. # For each argument, add its keyword and a format char to the log string.
  68. map $build .= " ".$method->selectorParts->[$_].":".Logos::Method::formatCharForArgType($method->argtypes->[$_]), (0..$method->numArgs - 1);
  69. # This builds a list of args by making sure the format char isn't -- (or, what we're using for non-operational types)
  70. # Map (in list context) "format char == -- ? nothing : arg name" over the indices of the arg list.
  71. my @newarglist = map(Logos::Method::printArgForArgType($method->argtypes->[$_], $method->argnames->[$_]), (0..$method->numArgs - 1));
  72. my @existingargs = grep(defined($_), @newarglist);
  73. if(scalar(@existingargs) > 0) {
  74. $argnamelist = ", ".join(", ", grep(defined($_), @existingargs));
  75. }
  76. } else {
  77. # Space and then the only keyword in the selector.
  78. $build .= " ".$method->selector;
  79. }
  80. my @extraFormatSpecifiers;
  81. my @extraArguments;
  82. for(Logos::Util::smartSplit(qr/\s*,\s*/, $args)) {
  83. my ($popen, $pclose) = matchedParenthesisSet($_);
  84. my $type = "id";
  85. if(defined $popen) {
  86. $type = substr($_, $popen, $pclose-$popen-1);
  87. }
  88. push(@extraFormatSpecifiers, Logos::Method::formatCharForArgType($type));
  89. my $n = Logos::Method::printArgForArgType($type, "($_)");
  90. push(@extraArguments, $n) if $n;
  91. }
  92. # Log postamble
  93. $build .= "]";
  94. $build .= ": ".join(", ", @extraFormatSpecifiers) if @extraFormatSpecifiers > 0;
  95. $build .= "\", self".$argnamelist;
  96. $build .= ", ".join(", ", @extraArguments) if @extraArguments > 0;
  97. $build .= ")";
  98. }
  99. sub declarations {
  100. ::fileError(-1, "generator does not implement Method::declarations");
  101. }
  102. sub initializers {
  103. ::fileError(-1, "generator does not implement Method::initializers");
  104. }
  105. 1;