nobjc.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. globtype,globals,
  47. verbose,pass_1,
  48. symdef,symconst,
  49. ncon,ncal,
  50. objcutil,
  51. cgbase;
  52. {*****************************************************************************
  53. TOBJCSELECTORNODE
  54. *****************************************************************************}
  55. constructor tobjcselectornode.create(formethod: tnode);
  56. begin
  57. inherited create(objcselectorn,formethod);
  58. end;
  59. function tobjcselectornode.pass_typecheck: tnode;
  60. var
  61. len: longint;
  62. s: shortstring;
  63. begin
  64. s:='';
  65. if not(m_objectivec1 in current_settings.modeswitches) then
  66. Message(parser_f_modeswitch_objc_required);
  67. result:=nil;
  68. typecheckpass(left);
  69. { argument can be
  70. a) an objc method
  71. b) a pchar, zero-based chararray or ansistring
  72. }
  73. case left.nodetype of
  74. loadn:
  75. begin
  76. if (left.resultdef.typ=procdef) and
  77. (po_objc in tprocdef(left.resultdef).procoptions) then
  78. begin
  79. { ok }
  80. end
  81. else
  82. CGMessage1(type_e_expected_objc_method_but_got,left.resultdef.typename);
  83. end;
  84. stringconstn:
  85. begin
  86. if not objcvalidselectorname(tstringconstnode(left).asconstpchar,
  87. tstringconstnode(left).len) then
  88. begin
  89. len:=tstringconstnode(left).len;
  90. if (len>255) then
  91. len:=255;
  92. setlength(s,len);
  93. if len>0 then
  94. move(tstringconstnode(left).valueas[0],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. if not(m_objectivec1 in current_settings.modeswitches) then
  119. Message(parser_f_modeswitch_objc_required);
  120. result:=nil;
  121. typecheckpass(left);
  122. if (left.nodetype<>typen) then
  123. MessagePos(left.fileinfo,type_e_type_id_expected)
  124. else if not is_objcprotocol(left.resultdef) then
  125. MessagePos2(left.fileinfo,type_e_incompatible_types,left.resultdef.typename,'ObjCProtocol');
  126. resultdef:=objc_protocoltype;
  127. end;
  128. function tobjcprotocolnode.pass_1: tnode;
  129. begin
  130. result:=ccallnode.createinternresfromunit('OBJC','OBJC_GETPROTOCOL',
  131. ccallparanode.create(cstringconstnode.createstr(tobjectdef(left.resultdef).objextname^),nil),
  132. resultdef
  133. );
  134. typecheckpass(result);
  135. end;
  136. end.