nobjc.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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).value_str,
  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. move(tstringconstnode(left).value_str^,s[1],len);
  94. CGMessage1(type_e_invalid_objc_selector_name,s);
  95. exit;
  96. end;
  97. end
  98. else
  99. CGMessage(type_e_expected_objc_method);
  100. end;
  101. resultdef:=objc_seltype;
  102. end;
  103. function tobjcselectornode.pass_1: tnode;
  104. begin
  105. result:=nil;
  106. expectloc:=LOC_CREFERENCE;
  107. end;
  108. {*****************************************************************************
  109. TOBJPROTOCOLNODE
  110. *****************************************************************************}
  111. constructor tobjcprotocolnode.create(forprotocol: tnode);
  112. begin
  113. inherited create(objcprotocoln,forprotocol);
  114. end;
  115. function tobjcprotocolnode.pass_typecheck: tnode;
  116. begin
  117. if not(m_objectivec1 in current_settings.modeswitches) then
  118. Message(parser_f_modeswitch_objc_required);
  119. result:=nil;
  120. typecheckpass(left);
  121. if (left.nodetype<>typen) then
  122. MessagePos(left.fileinfo,type_e_type_id_expected)
  123. else if not is_objcprotocol(left.resultdef) then
  124. MessagePos2(left.fileinfo,type_e_incompatible_types,left.resultdef.typename,'ObjCProtocol');
  125. resultdef:=objc_protocoltype;
  126. end;
  127. function tobjcprotocolnode.pass_1: tnode;
  128. begin
  129. result:=ccallnode.createinternresfromunit('OBJC','OBJC_GETPROTOCOL',
  130. ccallparanode.create(cstringconstnode.createstr(tobjectdef(left.resultdef).objextname^),nil),
  131. resultdef
  132. );
  133. typecheckpass(result);
  134. end;
  135. end.