nobjc.pas 4.5 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. 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. if not(m_objectivec1 in current_settings.modeswitches) then
  65. Message(parser_f_modeswitch_objc_required);
  66. result:=nil;
  67. typecheckpass(left);
  68. { argument can be
  69. a) an objc method
  70. b) a pchar, zero-based chararray or ansistring
  71. }
  72. case left.nodetype of
  73. loadn:
  74. begin
  75. if (left.resultdef.typ=procdef) and
  76. (po_objc in tprocdef(left.resultdef).procoptions) then
  77. begin
  78. { ok }
  79. end
  80. else
  81. CGMessage1(type_e_expected_objc_method_but_got,left.resultdef.typename);
  82. end;
  83. stringconstn:
  84. begin
  85. if not objcvalidselectorname(tstringconstnode(left).value_str,
  86. tstringconstnode(left).len) then
  87. begin
  88. len:=tstringconstnode(left).len;
  89. if (len>255) then
  90. len:=255;
  91. setlength(s,len);
  92. move(tstringconstnode(left).value_str^,s[1],len);
  93. CGMessage1(type_e_invalid_objc_selector_name,s);
  94. exit;
  95. end;
  96. end
  97. else
  98. CGMessage(type_e_expected_objc_method);
  99. end;
  100. resultdef:=objc_seltype;
  101. end;
  102. function tobjcselectornode.pass_1: tnode;
  103. begin
  104. result:=nil;
  105. expectloc:=LOC_CREFERENCE;
  106. end;
  107. {*****************************************************************************
  108. TOBJPROTOCOLNODE
  109. *****************************************************************************}
  110. constructor tobjcprotocolnode.create(forprotocol: tnode);
  111. begin
  112. inherited create(objcprotocoln,forprotocol);
  113. end;
  114. function tobjcprotocolnode.pass_typecheck: tnode;
  115. begin
  116. if not(m_objectivec1 in current_settings.modeswitches) then
  117. Message(parser_f_modeswitch_objc_required);
  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.