nobjc.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. tobjcprotocolnode = class(tunarynode)
  35. public
  36. constructor create(forprotocol: tnode);
  37. function pass_typecheck: tnode;override;
  38. function pass_1: tnode;override;
  39. end;
  40. tobjcprotocolnodeclass = class of tobjcprotocolnode;
  41. var
  42. cobjcselectornode : tobjcselectornodeclass;
  43. cobjcprotocolnode : tobjcprotocolnodeclass;
  44. implementation
  45. uses
  46. sysutils,
  47. globtype,cclasses,systems,
  48. verbose,pass_1,
  49. defutil,
  50. symtype,symtable,symdef,symconst,symsym,
  51. paramgr,
  52. nutils,
  53. nbas,nld,ncnv,ncon,ncal,nmem,
  54. objcutil,
  55. cgbase;
  56. {*****************************************************************************
  57. TOBJCSELECTORNODE
  58. *****************************************************************************}
  59. constructor tobjcselectornode.create(formethod: tnode);
  60. begin
  61. inherited create(objcselectorn,formethod);
  62. end;
  63. function tobjcselectornode.pass_typecheck: tnode;
  64. var
  65. len: longint;
  66. s: shortstring;
  67. begin
  68. result:=nil;
  69. typecheckpass(left);
  70. { argument can be
  71. a) an objc method
  72. b) a pchar, zero-based chararray or ansistring
  73. }
  74. case left.nodetype of
  75. loadn:
  76. begin
  77. if (left.resultdef.typ=procdef) and
  78. (po_objc in tprocdef(left.resultdef).procoptions) then
  79. begin
  80. { ok }
  81. end
  82. else
  83. CGMessage1(type_e_expected_objc_method_but_got,left.resultdef.typename);
  84. end;
  85. stringconstn:
  86. begin
  87. if not objcvalidselectorname(tstringconstnode(left).value_str,
  88. tstringconstnode(left).len) then
  89. begin
  90. len:=tstringconstnode(left).len;
  91. if (len>255) then
  92. len:=255;
  93. setlength(s,len);
  94. move(tstringconstnode(left).value_str^,s[1],len);
  95. CGMessage1(type_e_invalid_objc_selector_name,s);
  96. exit;
  97. end;
  98. end
  99. else
  100. CGMessage(type_e_expected_objc_method);
  101. end;
  102. resultdef:=objc_seltype;
  103. end;
  104. function tobjcselectornode.pass_1: tnode;
  105. begin
  106. result:=nil;
  107. expectloc:=LOC_CREFERENCE;
  108. end;
  109. {*****************************************************************************
  110. TOBJPROTOCOLNODE
  111. *****************************************************************************}
  112. constructor tobjcprotocolnode.create(forprotocol: tnode);
  113. begin
  114. inherited create(objcprotocoln,forprotocol);
  115. end;
  116. function tobjcprotocolnode.pass_typecheck: tnode;
  117. begin
  118. result:=nil;
  119. typecheckpass(left);
  120. if (left.nodetype<>typen) then
  121. MessagePos(left.fileinfo,type_e_type_id_expected)
  122. else if not is_objcprotocol(left.resultdef) then
  123. MessagePos2(left.fileinfo,type_e_incompatible_types,left.resultdef.typename,'ObjCProtocol');
  124. resultdef:=objc_protocoltype;
  125. end;
  126. function tobjcprotocolnode.pass_1: tnode;
  127. begin
  128. result:=ccallnode.createinternresfromunit('OBJC','OBJC_GETPROTOCOL',
  129. ccallparanode.create(cstringconstnode.createstr(tobjectdef(left.resultdef).objextname^),nil),
  130. resultdef
  131. );
  132. typecheckpass(result);
  133. end;
  134. end.