2
0

nobjc.pas 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. {
  2. Copyright (c) 2009 by Jonas Maebe
  3. This unit implements Objective-C nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { @abstract(This unit implements Objective-C nodes)
  18. This unit contains various nodes to implement Objective-Pascal and to
  19. interface with the Objective-C runtime.
  20. }
  21. unit nobjc;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. node;
  26. type
  27. tobjcselectornode = class(tunarynode)
  28. public
  29. constructor create(formethod: tnode);
  30. function pass_typecheck:tnode;override;
  31. function pass_1 : tnode;override;
  32. end;
  33. tobjcselectornodeclass = class of tobjcselectornode;
  34. var
  35. cobjcselectornode : tobjcselectornodeclass;
  36. implementation
  37. uses
  38. sysutils,
  39. cclasses,
  40. verbose,pass_1,
  41. defutil,symtable,symdef,symconst,
  42. ncnv,ncon,ncal,nmem;
  43. {*****************************************************************************
  44. TOBJCSELECTORNODE
  45. *****************************************************************************}
  46. constructor tobjcselectornode.create(formethod: tnode);
  47. begin
  48. inherited create(objcselectorn,formethod);
  49. end;
  50. function validselectorname(value_str: pchar; len: longint): boolean;
  51. var
  52. i : longint;
  53. gotcolon : boolean;
  54. begin
  55. result:=false;
  56. { empty name is not allowed }
  57. if (len=0) then
  58. exit;
  59. gotcolon:=false;
  60. { if the first character is a colon, all of them must be colons }
  61. if (value_str[0] = ':') then
  62. begin
  63. for i:=1 to len-1 do
  64. if (value_str[i]<>':') then
  65. exit;
  66. end
  67. else
  68. begin
  69. { no special characters other than ':'
  70. (already checked character 0, so start checking from 1)
  71. }
  72. for i:=1 to len-1 do
  73. if (value_str[i] = ':') then
  74. gotcolon:=true
  75. else if not(value_str[i] in ['_','A'..'Z','a'..'z','0'..'9',':']) then
  76. exit;
  77. { if there is at least one colon, the final character must
  78. also be a colon (in case it's only one character that is
  79. a colon, this was already checked before the above loop)
  80. }
  81. if gotcolon and
  82. (value_str[len-1] <> ':') then
  83. exit;
  84. end;
  85. result:=true;
  86. end;
  87. function tobjcselectornode.pass_typecheck: tnode;
  88. begin
  89. result:=nil;
  90. typecheckpass(left);
  91. { argument can be
  92. a) an objc method
  93. b) a pchar, zero-based chararray or ansistring
  94. }
  95. case left.nodetype of
  96. loadn:
  97. begin
  98. if (left.resultdef.typ=procdef) and
  99. (po_objc in tprocdef(left.resultdef).procoptions) then
  100. begin
  101. { ok }
  102. end
  103. else
  104. CGMessage1(type_e_expected_objc_method_but_got,left.resultdef.typename);
  105. end;
  106. stringconstn:
  107. begin
  108. if not validselectorname(tstringconstnode(left).value_str,
  109. tstringconstnode(left).len) then
  110. begin
  111. CGMessage(type_e_invalid_objc_selector_name);
  112. exit;
  113. end;
  114. end
  115. else
  116. CGMessage(type_e_expected_objc_method);
  117. end;
  118. resultdef:=search_system_type('SEL').typedef;
  119. end;
  120. function tobjcselectornode.pass_1: tnode;
  121. begin
  122. result:=nil;
  123. end;
  124. end.