2
0

h2ptypes.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. unit h2ptypes;
  2. // {$mode ObjFPC}
  3. {$inline on}
  4. {$modeswitch result}
  5. interface
  6. uses
  7. Classes, SysUtils;
  8. type
  9. Char=system.ansichar;
  10. ttyp = (
  11. t_id,
  12. { p contains the string }
  13. t_arraydef,
  14. { }
  15. t_pointerdef,
  16. { p1 contains the definition
  17. if in type overrider
  18. or nothing for args
  19. }
  20. t_addrdef,
  21. t_void,
  22. { no field }
  23. t_dec,
  24. { }
  25. t_declist,
  26. { p1 is t_dec
  27. next if exists }
  28. t_memberdec,
  29. { p1 is type specifier
  30. p2 is declarator_list }
  31. t_structdef,
  32. { }
  33. t_memberdeclist,
  34. { p1 is memberdec
  35. next is next if it exist }
  36. t_procdef,
  37. { }
  38. t_uniondef,
  39. { }
  40. t_enumdef,
  41. { }
  42. t_enumlist,
  43. { }
  44. t_preop,
  45. { p contains the operator string
  46. p1 contains the right expr }
  47. t_bop,
  48. { p contains the operator string
  49. p1 contains the left expr
  50. p2 contains the right expr }
  51. t_arrayop,
  52. {
  53. p1 contains the array expr
  54. p2 contains the index expressions }
  55. t_callop,
  56. {
  57. p1 contains the proc expr
  58. p2 contains the index expressions }
  59. t_arg,
  60. {
  61. p1 contain the typedef
  62. p2 the declarator (t_dec)
  63. }
  64. t_arglist,
  65. { }
  66. t_funexprlist,
  67. { }
  68. t_exprlist,
  69. { p1 contains the expr
  70. next contains the next if it exists }
  71. t_ifexpr,
  72. { p1 contains the condition expr
  73. p2 contains the if branch
  74. p3 contains the else branch }
  75. t_funcname,
  76. { p1 contains the function dname
  77. p2 contains the funexprlist
  78. p3 possibly contains the return type }
  79. t_typespec,
  80. { p1 is the type itself
  81. p2 the typecast expr }
  82. t_size_specifier,
  83. { p1 expr for size }
  84. t_default_value,
  85. { p1 expr for value }
  86. t_statement_list,
  87. { p1 is the statement
  88. next is next if it exist }
  89. t_whilenode,
  90. t_fornode,
  91. t_dowhilenode,
  92. t_switchnode,
  93. t_gotonode,
  94. t_continuenode,
  95. t_breaknode
  96. );
  97. const
  98. ttypstr: array[ttyp] of string =
  99. (
  100. 't_id',
  101. 't_arraydef',
  102. 't_pointerdef',
  103. 't_addrdef',
  104. 't_void',
  105. 't_dec',
  106. 't_declist',
  107. 't_memberdec',
  108. 't_structdef',
  109. 't_memberdeclist',
  110. 't_procdef',
  111. 't_uniondef',
  112. 't_enumdef',
  113. 't_enumlist',
  114. 't_preop',
  115. 't_bop',
  116. 't_arrayop',
  117. 't_callop',
  118. 't_arg',
  119. 't_arglist',
  120. 't_funexprlist',
  121. 't_exprlist',
  122. 't_ifexpr',
  123. 't_funcname',
  124. 't_typespec',
  125. 't_size_specifier',
  126. 't_default_value',
  127. 't_statement_list',
  128. 't_whilenode',
  129. 't_fornode',
  130. 't_dowhilenode',
  131. 't_switchnode',
  132. 't_gotonode',
  133. 't_continuenode',
  134. 't_breaknode'
  135. );
  136. type
  137. presobject = ^tresobject;
  138. tresobject = object
  139. typ : ttyp;
  140. p : pansichar;
  141. next : presobject;
  142. p1,p2,p3 : presobject;
  143. { name of int/real, then no T prefix is required }
  144. skiptprefix : boolean;
  145. constructor init_no(t : ttyp);
  146. constructor init_one(t : ttyp;_p1 : presobject);
  147. constructor init_two(t : ttyp;_p1,_p2 : presobject);
  148. constructor init_three(t : ttyp;_p1,_p2,_p3 : presobject);
  149. constructor init_id(const s : string);
  150. constructor init_intid(const s : string);
  151. constructor init_bop(const s : string;_p1,_p2 : presobject);
  152. constructor init_preop(const s : string;_p1 : presobject);
  153. procedure setstr(const s:string);
  154. function str : string;
  155. function strlength : byte;
  156. function get_copy : presobject;
  157. { can this ve considered as a constant ? }
  158. function is_const : boolean;
  159. destructor done;
  160. end;
  161. tblocktype = (bt_type,bt_const,bt_var,bt_func,bt_no);
  162. Function NewUnaryOp(const aop : ansistring; aright : presobject) : presobject; inline;
  163. Function NewBinaryOp(const aop : ansistring; aleft,aright : presobject) : presobject; inline;
  164. Function NewVoid : presobject; inline;
  165. Function NewID(const aID : ansistring) : presobject; inline;
  166. Function NewType1(aType : ttyp; aID : presobject) : presobject; inline;
  167. Function NewType2(aType : ttyp; aID,aID2 : presobject) : presobject; inline;
  168. Function NewType3(aType : ttyp; aID,aID2,aID3 : presobject) : presobject; inline;
  169. Function NewIntID(const aPascalType : ansistring) : presobject; inline;
  170. function strpnew(const s : ansistring) : pansichar; inline;
  171. implementation
  172. uses h2poptions, strings;
  173. Function NewVoid : presobject;
  174. begin
  175. Result:=new(presobject,init_no(t_void));
  176. end;
  177. Function NewBinaryOp(const aop : ansistring; aleft,aright : presobject) : presobject;
  178. begin
  179. Result:=new(presobject,init_bop(aop,aleft,aright));
  180. end;
  181. Function NewUnaryOp(const aop : ansistring; aright : presobject) : presobject; inline;
  182. begin
  183. Result:=new(presobject,init_preop(aop,aright));
  184. end;
  185. Function NewID(const aID : ansistring) : presobject;
  186. begin
  187. if useansichar and (aId='char') then
  188. Result:=new(presobject,init_id('AnsiChar'))
  189. else
  190. Result:=new(presobject,init_id(aID));
  191. end;
  192. Function NewIntID(const aPascalType : ansistring) : presobject;
  193. begin
  194. Result:=new(presobject,init_intid(aPascalType));
  195. end;
  196. Function NewType1(aType : ttyp; aID : presobject) : presobject; inline;
  197. begin
  198. Result:=new(presobject,init_one(atype,aID));
  199. end;
  200. Function NewType2(aType : ttyp; aID,aID2 : presobject) : presobject; inline;
  201. begin
  202. Result:=new(presobject,init_two(atype,aID,aID2));
  203. end;
  204. Function NewType3(aType : ttyp; aID,aID2,aID3 : presobject) : presobject; inline;
  205. begin
  206. Result:=new(presobject,init_three(atype,aID,aID2,aID3));
  207. end;
  208. function strpnew(const s : ansistring) : pansichar;
  209. var
  210. p : pansichar;
  211. begin
  212. getmem(p,length(s)+1);
  213. strpcopy(p,s);
  214. strpnew:=p;
  215. end;
  216. constructor tresobject.init_preop(const s : string;_p1 : presobject);
  217. begin
  218. typ:=t_preop;
  219. p:=strpnew(s);
  220. p1:=_p1;
  221. p2:=nil;
  222. p3:=nil;
  223. next:=nil;
  224. skiptprefix:=false;
  225. end;
  226. constructor tresobject.init_bop(const s : string;_p1,_p2 : presobject);
  227. begin
  228. typ:=t_bop;
  229. p:=strpnew(s);
  230. p1:=_p1;
  231. p2:=_p2;
  232. p3:=nil;
  233. next:=nil;
  234. skiptprefix:=false;
  235. end;
  236. constructor tresobject.init_id(const s : string);
  237. begin
  238. typ:=t_id;
  239. p:=strpnew(s);
  240. p1:=nil;
  241. p2:=nil;
  242. p3:=nil;
  243. next:=nil;
  244. skiptprefix:=false;
  245. end;
  246. constructor tresobject.init_intid(const s : string);
  247. begin
  248. typ:=t_id;
  249. if useansichar and (s='char') then
  250. p:=strpnew('ansichar')
  251. else
  252. p:=strpnew(s);
  253. p1:=nil;
  254. p2:=nil;
  255. p3:=nil;
  256. next:=nil;
  257. skiptprefix:=true;
  258. end;
  259. constructor tresobject.init_two(t : ttyp;_p1,_p2 : presobject);
  260. begin
  261. typ:=t;
  262. p1:=_p1;
  263. p2:=_p2;
  264. p3:=nil;
  265. p:=nil;
  266. next:=nil;
  267. skiptprefix:=false;
  268. end;
  269. constructor tresobject.init_three(t : ttyp;_p1,_p2,_p3 : presobject);
  270. begin
  271. typ:=t;
  272. p1:=_p1;
  273. p2:=_p2;
  274. p3:=_p3;
  275. p:=nil;
  276. next:=nil;
  277. skiptprefix:=false;
  278. end;
  279. constructor tresobject.init_one(t : ttyp;_p1 : presobject);
  280. begin
  281. typ:=t;
  282. p1:=_p1;
  283. p2:=nil;
  284. p3:=nil;
  285. next:=nil;
  286. p:=nil;
  287. skiptprefix:=false;
  288. end;
  289. constructor tresobject.init_no(t : ttyp);
  290. begin
  291. typ:=t;
  292. p:=nil;
  293. p1:=nil;
  294. p2:=nil;
  295. p3:=nil;
  296. next:=nil;
  297. skiptprefix:=false;
  298. end;
  299. procedure tresobject.setstr(const s : string);
  300. begin
  301. if assigned(p) then
  302. strdispose(p);
  303. p:=strpnew(s);
  304. end;
  305. function tresobject.str : string;
  306. begin
  307. str:=strpas(p);
  308. end;
  309. function tresobject.strlength : byte;
  310. begin
  311. if assigned(p) then
  312. strlength:=strlen(p)
  313. else
  314. strlength:=0;
  315. end;
  316. { can this ve considered as a constant ? }
  317. function tresobject.is_const : boolean;
  318. begin
  319. case typ of
  320. t_id,t_void :
  321. is_const:=true;
  322. t_preop :
  323. is_const:= ((str='-') or (str=' not ')) and p1^.is_const;
  324. t_bop :
  325. is_const:= p2^.is_const and p1^.is_const;
  326. else
  327. is_const:=false;
  328. end;
  329. end;
  330. function tresobject.get_copy : presobject;
  331. var
  332. newres : presobject;
  333. begin
  334. newres:=new(presobject,init_no(typ));
  335. newres^.skiptprefix:=skiptprefix;
  336. if assigned(p) then
  337. newres^.p:=strnew(p);
  338. if assigned(p1) then
  339. newres^.p1:=p1^.get_copy;
  340. if assigned(p2) then
  341. newres^.p2:=p2^.get_copy;
  342. if assigned(p3) then
  343. newres^.p3:=p3^.get_copy;
  344. if assigned(next) then
  345. newres^.next:=next^.get_copy;
  346. get_copy:=newres;
  347. end;
  348. destructor tresobject.done;
  349. begin
  350. (* writeln('disposing ',byte(typ)); *)
  351. if assigned(p)then strdispose(p);
  352. if assigned(p1) then
  353. dispose(p1,done);
  354. if assigned(p2) then
  355. dispose(p2,done);
  356. if assigned(p3) then
  357. dispose(p3,done);
  358. if assigned(next) then
  359. dispose(next,done);
  360. end;
  361. end.