symsym.pas 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. Implementation for the symbols types of the symtable
  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. unit symsym;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,
  23. { target }
  24. globtype,globals,widestr,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,symdef,defcmp,
  27. { ppu }
  28. ppu,finput,
  29. cclasses,symnot,
  30. { aasm }
  31. aasmbase,
  32. cpuinfo,cpubase,cgbase,cgutils,parabase
  33. ;
  34. type
  35. { this class is the base for all symbol objects }
  36. tstoredsym = class(tsym)
  37. public
  38. constructor create(st:tsymtyp;const n : string);
  39. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  40. destructor destroy;override;
  41. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  42. end;
  43. tlabelsym = class(tstoredsym)
  44. used,
  45. defined,
  46. nonlocal : boolean;
  47. { points to the matching node, only valid resultdef pass is run and
  48. the goto<->label relation in the node tree is created, should
  49. be a tnode }
  50. code : pointer;
  51. { when the label is defined in an asm block, this points to the
  52. generated asmlabel }
  53. asmblocklabel : tasmlabel;
  54. constructor create(const n : string);
  55. constructor ppuload(ppufile:tcompilerppufile);
  56. procedure ppuwrite(ppufile:tcompilerppufile);override;
  57. function mangledname:string;override;
  58. end;
  59. tunitsym = class(Tstoredsym)
  60. module : tobject; { tmodule }
  61. constructor create(const n : string;amodule : tobject);
  62. constructor ppuload(ppufile:tcompilerppufile);
  63. destructor destroy;override;
  64. procedure ppuwrite(ppufile:tcompilerppufile);override;
  65. end;
  66. terrorsym = class(Tsym)
  67. constructor create;
  68. end;
  69. { tprocsym }
  70. tprocsym = class(tstoredsym)
  71. protected
  72. FProcdefList : TFPObjectList;
  73. FProcdefDerefList : TFPList;
  74. public
  75. constructor create(const n : string);
  76. constructor ppuload(ppufile:tcompilerppufile);
  77. destructor destroy;override;
  78. { writes all declarations except the specified one }
  79. procedure write_parameter_lists(skipdef:tprocdef);
  80. { tests, if all procedures definitions are defined and not }
  81. { only forward }
  82. procedure check_forward;
  83. procedure ppuwrite(ppufile:tcompilerppufile);override;
  84. procedure buildderef;override;
  85. procedure deref;override;
  86. function find_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  87. function find_procdef_bypara(para:TFPObjectList;retdef:tdef;cpoptions:tcompare_paras_options):Tprocdef;
  88. function find_procdef_byoptions(ops:tprocoptions): Tprocdef;
  89. function find_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  90. function find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  91. function find_procdef_enumerator_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  92. property ProcdefList:TFPObjectList read FProcdefList;
  93. end;
  94. ttypesym = class(Tstoredsym)
  95. typedef : tdef;
  96. typedefderef : tderef;
  97. constructor create(const n : string;def:tdef);
  98. constructor ppuload(ppufile:tcompilerppufile);
  99. procedure ppuwrite(ppufile:tcompilerppufile);override;
  100. procedure buildderef;override;
  101. procedure deref;override;
  102. end;
  103. tabstractvarsym = class(tstoredsym)
  104. varoptions : tvaroptions;
  105. notifications : Tlinkedlist;
  106. varspez : tvarspez; { sets the type of access }
  107. varregable : tvarregable;
  108. varstate : tvarstate;
  109. { Has the address of this variable potentially escaped the }
  110. { block in which is was declared? }
  111. { could also be part of tabstractnormalvarsym, but there's }
  112. { one byte left here till the next 4 byte alignment }
  113. addr_taken : boolean;
  114. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  115. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  116. destructor destroy;override;
  117. procedure ppuwrite(ppufile:tcompilerppufile);override;
  118. procedure buildderef;override;
  119. procedure deref;override;
  120. function getsize : aint;
  121. function getpackedbitsize : longint;
  122. function is_regvar(refpara: boolean):boolean;
  123. procedure trigger_notifications(what:Tnotification_flag);
  124. function register_notification(flags:Tnotification_flags;
  125. callback:Tnotification_callback):cardinal;
  126. procedure unregister_notification(id:cardinal);
  127. private
  128. _vardef : tdef;
  129. vardefderef : tderef;
  130. procedure setvardef(def:tdef);
  131. public
  132. property vardef: tdef read _vardef write setvardef;
  133. end;
  134. tfieldvarsym = class(tabstractvarsym)
  135. fieldoffset : aint; { offset in record/object }
  136. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  137. constructor ppuload(ppufile:tcompilerppufile);
  138. procedure ppuwrite(ppufile:tcompilerppufile);override;
  139. function mangledname:string;override;
  140. end;
  141. tabstractnormalvarsym = class(tabstractvarsym)
  142. defaultconstsym : tsym;
  143. defaultconstsymderef : tderef;
  144. localloc : TLocation; { register/reference for local var }
  145. initialloc : TLocation; { initial location so it can still be initialized later after the location was changed by SSA }
  146. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  147. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  148. procedure ppuwrite(ppufile:tcompilerppufile);override;
  149. procedure buildderef;override;
  150. procedure deref;override;
  151. end;
  152. tlocalvarsym = class(tabstractnormalvarsym)
  153. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  154. constructor ppuload(ppufile:tcompilerppufile);
  155. procedure ppuwrite(ppufile:tcompilerppufile);override;
  156. end;
  157. tparavarsym = class(tabstractnormalvarsym)
  158. paraloc : array[tcallercallee] of TCGPara;
  159. paranr : word; { position of this parameter }
  160. {$ifdef EXTDEBUG}
  161. eqval : tequaltype;
  162. {$endif EXTDEBUG}
  163. constructor create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  164. constructor ppuload(ppufile:tcompilerppufile);
  165. destructor destroy;override;
  166. procedure ppuwrite(ppufile:tcompilerppufile);override;
  167. end;
  168. tstaticvarsym = class(tabstractnormalvarsym)
  169. private
  170. _mangledname : pshortstring;
  171. public
  172. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  173. constructor create_dll(const n : string;vsp:tvarspez;def:tdef);
  174. constructor create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  175. constructor ppuload(ppufile:tcompilerppufile);
  176. destructor destroy;override;
  177. procedure ppuwrite(ppufile:tcompilerppufile);override;
  178. function mangledname:string;override;
  179. procedure set_mangledname(const s:string);
  180. end;
  181. tabsolutevarsym = class(tabstractvarsym)
  182. public
  183. abstyp : absolutetyp;
  184. {$ifdef i386}
  185. absseg : boolean;
  186. {$endif i386}
  187. asmname : pshortstring;
  188. addroffset : aword;
  189. ref : tpropaccesslist;
  190. constructor create(const n : string;def:tdef);
  191. constructor create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  192. destructor destroy;override;
  193. constructor ppuload(ppufile:tcompilerppufile);
  194. procedure buildderef;override;
  195. procedure deref;override;
  196. function mangledname : string;override;
  197. procedure ppuwrite(ppufile:tcompilerppufile);override;
  198. end;
  199. tpropaccesslisttypes=(palt_none,palt_read,palt_write,palt_stored);
  200. tpropertysym = class(Tstoredsym)
  201. propoptions : tpropertyoptions;
  202. overriddenpropsym : tpropertysym;
  203. overriddenpropsymderef : tderef;
  204. propdef : tdef;
  205. propdefderef : tderef;
  206. indexdef : tdef;
  207. indexdefderef : tderef;
  208. index,
  209. default : longint;
  210. propaccesslist : array[tpropaccesslisttypes] of tpropaccesslist;
  211. constructor create(const n : string);
  212. destructor destroy;override;
  213. constructor ppuload(ppufile:tcompilerppufile);
  214. function getsize : aint;
  215. procedure ppuwrite(ppufile:tcompilerppufile);override;
  216. procedure buildderef;override;
  217. procedure deref;override;
  218. end;
  219. tconstvalue = record
  220. case integer of
  221. 0: (valueord : tconstexprint);
  222. 1: (valueordptr : tconstptruint);
  223. 2: (valueptr : pointer; len : longint);
  224. end;
  225. tconstsym = class(tstoredsym)
  226. constdef : tdef;
  227. constdefderef : tderef;
  228. consttyp : tconsttyp;
  229. value : tconstvalue;
  230. constructor create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  231. constructor create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  232. constructor create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  233. constructor create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  234. constructor create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  235. constructor ppuload(ppufile:tcompilerppufile);
  236. destructor destroy;override;
  237. procedure buildderef;override;
  238. procedure deref;override;
  239. procedure ppuwrite(ppufile:tcompilerppufile);override;
  240. end;
  241. tenumsym = class(Tstoredsym)
  242. value : longint;
  243. definition : tenumdef;
  244. definitionderef : tderef;
  245. nextenum : tenumsym;
  246. constructor create(const n : string;def : tenumdef;v : longint);
  247. constructor ppuload(ppufile:tcompilerppufile);
  248. procedure ppuwrite(ppufile:tcompilerppufile);override;
  249. procedure buildderef;override;
  250. procedure deref;override;
  251. procedure order;
  252. end;
  253. tsyssym = class(Tstoredsym)
  254. number : longint;
  255. constructor create(const n : string;l : longint);
  256. constructor ppuload(ppufile:tcompilerppufile);
  257. destructor destroy;override;
  258. procedure ppuwrite(ppufile:tcompilerppufile);override;
  259. end;
  260. const
  261. maxmacrolen=16*1024;
  262. type
  263. pmacrobuffer = ^tmacrobuffer;
  264. tmacrobuffer = array[0..maxmacrolen-1] of char;
  265. tmacro = class(tstoredsym)
  266. {Normally true, but false when a previously defined macro is undef-ed}
  267. defined : boolean;
  268. {True if this is a mac style compiler variable, in which case no macro
  269. substitutions shall be done.}
  270. is_compiler_var : boolean;
  271. {Whether the macro was used. NOTE: A use of a macro which was never defined}
  272. {e. g. an IFDEF which returns false, will not be registered as used,}
  273. {since there is no place to register its use. }
  274. is_used : boolean;
  275. buftext : pchar;
  276. buflen : longint;
  277. constructor create(const n : string);
  278. constructor ppuload(ppufile:tcompilerppufile);
  279. procedure ppuwrite(ppufile:tcompilerppufile);override;
  280. destructor destroy;override;
  281. function GetCopy:tmacro;
  282. end;
  283. var
  284. generrorsym : tsym;
  285. implementation
  286. uses
  287. { global }
  288. verbose,
  289. { target }
  290. systems,
  291. { symtable }
  292. defutil,symtable,
  293. fmodule,
  294. { tree }
  295. node,
  296. { aasm }
  297. aasmtai,aasmdata,
  298. { codegen }
  299. paramgr,
  300. procinfo
  301. ;
  302. {****************************************************************************
  303. Helpers
  304. ****************************************************************************}
  305. {****************************************************************************
  306. TSYM (base for all symtypes)
  307. ****************************************************************************}
  308. constructor tstoredsym.create(st:tsymtyp;const n : string);
  309. begin
  310. inherited create(st,n);
  311. { Register in current_module }
  312. if assigned(current_module) then
  313. begin
  314. current_module.symlist.Add(self);
  315. SymId:=current_module.symlist.Count-1;
  316. end;
  317. end;
  318. constructor tstoredsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  319. begin
  320. SymId:=ppufile.getlongint;
  321. inherited Create(st,ppufile.getstring);
  322. { Register symbol }
  323. current_module.symlist[SymId]:=self;
  324. ppufile.getposinfo(fileinfo);
  325. visibility:=tvisibility(ppufile.getbyte);
  326. ppufile.getsmallset(symoptions);
  327. if sp_has_deprecated_msg in symoptions then
  328. deprecatedmsg:=stringdup(ppufile.getstring)
  329. else
  330. deprecatedmsg:=nil;
  331. end;
  332. procedure tstoredsym.ppuwrite(ppufile:tcompilerppufile);
  333. begin
  334. ppufile.putlongint(SymId);
  335. ppufile.putstring(realname);
  336. ppufile.putposinfo(fileinfo);
  337. ppufile.putbyte(byte(visibility));
  338. ppufile.putsmallset(symoptions);
  339. if sp_has_deprecated_msg in symoptions then
  340. ppufile.putstring(deprecatedmsg^);
  341. end;
  342. destructor tstoredsym.destroy;
  343. begin
  344. inherited destroy;
  345. end;
  346. {****************************************************************************
  347. TLABELSYM
  348. ****************************************************************************}
  349. constructor tlabelsym.create(const n : string);
  350. begin
  351. inherited create(labelsym,n);
  352. used:=false;
  353. defined:=false;
  354. nonlocal:=false;
  355. code:=nil;
  356. end;
  357. constructor tlabelsym.ppuload(ppufile:tcompilerppufile);
  358. begin
  359. inherited ppuload(labelsym,ppufile);
  360. code:=nil;
  361. used:=false;
  362. nonlocal:=false;
  363. defined:=true;
  364. end;
  365. procedure tlabelsym.ppuwrite(ppufile:tcompilerppufile);
  366. begin
  367. if owner.symtabletype=globalsymtable then
  368. Message(sym_e_ill_label_decl)
  369. else
  370. begin
  371. inherited ppuwrite(ppufile);
  372. ppufile.writeentry(iblabelsym);
  373. end;
  374. end;
  375. function tlabelsym.mangledname:string;
  376. begin
  377. if not(defined) then
  378. begin
  379. defined:=true;
  380. if nonlocal then
  381. current_asmdata.getglobaljumplabel(asmblocklabel)
  382. else
  383. current_asmdata.getjumplabel(asmblocklabel);
  384. end;
  385. result:=asmblocklabel.name;
  386. end;
  387. {****************************************************************************
  388. TUNITSYM
  389. ****************************************************************************}
  390. constructor tunitsym.create(const n : string;amodule : tobject);
  391. begin
  392. inherited create(unitsym,n);
  393. module:=amodule;
  394. end;
  395. constructor tunitsym.ppuload(ppufile:tcompilerppufile);
  396. begin
  397. inherited ppuload(unitsym,ppufile);
  398. module:=nil;
  399. end;
  400. destructor tunitsym.destroy;
  401. begin
  402. inherited destroy;
  403. end;
  404. procedure tunitsym.ppuwrite(ppufile:tcompilerppufile);
  405. begin
  406. inherited ppuwrite(ppufile);
  407. ppufile.writeentry(ibunitsym);
  408. end;
  409. {****************************************************************************
  410. TPROCSYM
  411. ****************************************************************************}
  412. constructor tprocsym.create(const n : string);
  413. begin
  414. inherited create(procsym,n);
  415. FProcdefList:=TFPObjectList.Create(false);
  416. FProcdefderefList:=nil;
  417. { the tprocdef have their own symoptions, make the procsym
  418. always visible }
  419. visibility:=vis_public;
  420. end;
  421. constructor tprocsym.ppuload(ppufile:tcompilerppufile);
  422. var
  423. pdderef : tderef;
  424. i,
  425. pdcnt : longint;
  426. begin
  427. inherited ppuload(procsym,ppufile);
  428. FProcdefList:=TFPObjectList.Create(false);
  429. FProcdefDerefList:=TFPList.Create;
  430. pdcnt:=ppufile.getword;
  431. for i:=1 to pdcnt do
  432. begin
  433. ppufile.getderef(pdderef);
  434. FProcdefDerefList.Add(Pointer(PtrInt(pdderef.dataidx)));
  435. end;
  436. end;
  437. destructor tprocsym.destroy;
  438. begin
  439. FProcdefList.Free;
  440. if assigned(FProcdefDerefList) then
  441. FProcdefDerefList.Free;
  442. inherited destroy;
  443. end;
  444. procedure tprocsym.ppuwrite(ppufile:tcompilerppufile);
  445. var
  446. i : longint;
  447. d : tderef;
  448. begin
  449. inherited ppuwrite(ppufile);
  450. ppufile.putword(FProcdefDerefList.Count);
  451. for i:=0 to FProcdefDerefList.Count-1 do
  452. begin
  453. d.dataidx:=PtrInt(FProcdefDerefList[i]);
  454. ppufile.putderef(d);
  455. end;
  456. ppufile.writeentry(ibprocsym);
  457. end;
  458. procedure tprocsym.write_parameter_lists(skipdef:tprocdef);
  459. var
  460. i : longint;
  461. pd : tprocdef;
  462. begin
  463. for i:=0 to ProcdefList.Count-1 do
  464. begin
  465. pd:=tprocdef(ProcdefList[i]);
  466. if pd<>skipdef then
  467. MessagePos1(pd.fileinfo,sym_h_param_list,pd.fullprocname(false));
  468. end;
  469. end;
  470. procedure tprocsym.check_forward;
  471. var
  472. i : longint;
  473. pd : tprocdef;
  474. begin
  475. for i:=0 to ProcdefList.Count-1 do
  476. begin
  477. pd:=tprocdef(ProcdefList[i]);
  478. if (pd.owner=owner) and (pd.forwarddef) then
  479. begin
  480. { For mode macpas. Make implicit externals (procedures declared in the interface
  481. section which do not have a counterpart in the implementation)
  482. to be an imported procedure }
  483. if (m_mac in current_settings.modeswitches) and
  484. (pd.interfacedef) then
  485. begin
  486. pd.setmangledname(target_info.CPrefix+tprocdef(pd).procsym.realname);
  487. if (not current_module.interface_only) then
  488. MessagePos1(pd.fileinfo,sym_w_forward_not_resolved,pd.fullprocname(false));
  489. end
  490. else
  491. begin
  492. MessagePos1(pd.fileinfo,sym_e_forward_not_resolved,pd.fullprocname(false));
  493. end;
  494. { Turn further error messages off }
  495. pd.forwarddef:=false;
  496. end;
  497. end;
  498. end;
  499. procedure tprocsym.buildderef;
  500. var
  501. i : longint;
  502. pd : tprocdef;
  503. d : tderef;
  504. begin
  505. if not assigned(FProcdefDerefList) then
  506. FProcdefDerefList:=TFPList.Create
  507. else
  508. FProcdefDerefList.Clear;
  509. for i:=0 to ProcdefList.Count-1 do
  510. begin
  511. pd:=tprocdef(ProcdefList[i]);
  512. { only write the proc definitions that belong
  513. to this procsym and are in the global symtable }
  514. if pd.owner=owner then
  515. begin
  516. d.build(pd);
  517. FProcdefDerefList.Add(Pointer(PtrInt(d.dataidx)));
  518. end;
  519. end;
  520. end;
  521. procedure tprocsym.deref;
  522. var
  523. i : longint;
  524. pd : tprocdef;
  525. d : tderef;
  526. begin
  527. { Clear all procdefs }
  528. ProcdefList.Clear;
  529. if not assigned(FProcdefDerefList) then
  530. internalerror(200611031);
  531. for i:=0 to FProcdefDerefList.Count-1 do
  532. begin
  533. d.dataidx:=PtrInt(FProcdefDerefList[i]);
  534. pd:=tprocdef(d.resolve);
  535. ProcdefList.Add(pd);
  536. end;
  537. end;
  538. function Tprocsym.Find_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  539. var
  540. i : longint;
  541. pd : tprocdef;
  542. begin
  543. result:=nil;
  544. for i:=0 to ProcdefList.Count-1 do
  545. begin
  546. pd:=tprocdef(ProcdefList[i]);
  547. if pd.proctypeoption=pt then
  548. begin
  549. result:=pd;
  550. exit;
  551. end;
  552. end;
  553. end;
  554. function Tprocsym.Find_procdef_bypara(para:TFPObjectList;retdef:tdef;
  555. cpoptions:tcompare_paras_options):Tprocdef;
  556. var
  557. i : longint;
  558. pd : tprocdef;
  559. eq : tequaltype;
  560. begin
  561. result:=nil;
  562. for i:=0 to ProcdefList.Count-1 do
  563. begin
  564. pd:=tprocdef(ProcdefList[i]);
  565. if assigned(retdef) then
  566. eq:=compare_defs(retdef,pd.returndef,nothingn)
  567. else
  568. eq:=te_equal;
  569. if (eq>=te_equal) or
  570. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  571. begin
  572. eq:=compare_paras(para,pd.paras,cp_value_equal_const,cpoptions);
  573. if (eq>=te_equal) or
  574. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  575. begin
  576. result:=pd;
  577. exit;
  578. end;
  579. end;
  580. end;
  581. end;
  582. function tprocsym.find_procdef_byoptions(ops: tprocoptions): Tprocdef;
  583. var
  584. i : longint;
  585. pd : tprocdef;
  586. begin
  587. result:=nil;
  588. for i:=0 to ProcdefList.Count-1 do
  589. begin
  590. pd:=tprocdef(ProcdefList[i]);
  591. if ops * pd.procoptions = ops then
  592. begin
  593. result:=pd;
  594. exit;
  595. end;
  596. end;
  597. end;
  598. function Tprocsym.Find_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  599. var
  600. i : longint;
  601. bestpd,
  602. pd : tprocdef;
  603. eq,besteq : tequaltype;
  604. begin
  605. { This function will return the pprocdef of pprocsym that
  606. is the best match for procvardef. When there are multiple
  607. matches it returns nil.}
  608. result:=nil;
  609. bestpd:=nil;
  610. besteq:=te_incompatible;
  611. for i:=0 to ProcdefList.Count-1 do
  612. begin
  613. pd:=tprocdef(ProcdefList[i]);
  614. eq:=proc_to_procvar_equal(pd,d);
  615. if eq>=te_equal then
  616. begin
  617. { multiple procvars with the same equal level }
  618. if assigned(bestpd) and
  619. (besteq=eq) then
  620. exit;
  621. if eq>besteq then
  622. begin
  623. besteq:=eq;
  624. bestpd:=pd;
  625. end;
  626. end;
  627. end;
  628. result:=bestpd;
  629. end;
  630. function Tprocsym.Find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  631. var
  632. paraidx, realparamcount,
  633. i, j : longint;
  634. bestpd,
  635. hpd,
  636. pd : tprocdef;
  637. convtyp : tconverttype;
  638. eq : tequaltype;
  639. begin
  640. { This function will return the pprocdef of pprocsym that
  641. is the best match for fromdef and todef. }
  642. result:=nil;
  643. bestpd:=nil;
  644. besteq:=te_incompatible;
  645. for i:=0 to ProcdefList.Count-1 do
  646. begin
  647. pd:=tprocdef(ProcdefList[i]);
  648. if (equal_defs(todef,pd.returndef) or
  649. { shortstrings of different lengths are ok as result }
  650. (is_shortstring(todef) and is_shortstring(pd.returndef))) and
  651. { the result type must be always really equal and not an alias,
  652. if you mess with this code, check tw4093 }
  653. ((todef=pd.returndef) or
  654. (
  655. not(df_unique in todef.defoptions) and
  656. not(df_unique in pd.returndef.defoptions)
  657. )
  658. ) then
  659. begin
  660. paraidx:=0;
  661. { ignore vs_hidden parameters }
  662. while (paraidx<pd.paras.count) and
  663. assigned(pd.paras[paraidx]) and
  664. (vo_is_hidden_para in tparavarsym(pd.paras[paraidx]).varoptions) do
  665. inc(paraidx);
  666. realparamcount:=0;
  667. for j := 0 to pd.paras.Count-1 do
  668. if assigned(pd.paras[j]) and not (vo_is_hidden_para in tparavarsym(pd.paras[j]).varoptions) then
  669. inc(realparamcount);
  670. if (paraidx<pd.paras.count) and
  671. assigned(pd.paras[paraidx]) and
  672. (realparamcount = 1) then
  673. begin
  674. eq:=compare_defs_ext(fromdef,tparavarsym(pd.paras[paraidx]).vardef,nothingn,convtyp,hpd,[]);
  675. { alias? if yes, only l1 choice,
  676. if you mess with this code, check tw4093 }
  677. if (eq=te_exact) and
  678. (fromdef<>tparavarsym(pd.paras[paraidx]).vardef) and
  679. ((df_unique in fromdef.defoptions) or
  680. (df_unique in tparavarsym(pd.paras[paraidx]).vardef.defoptions)) then
  681. eq:=te_convert_l1;
  682. if eq=te_exact then
  683. begin
  684. besteq:=eq;
  685. result:=pd;
  686. exit;
  687. end;
  688. if eq>besteq then
  689. begin
  690. bestpd:=pd;
  691. besteq:=eq;
  692. end;
  693. end;
  694. end;
  695. end;
  696. result:=bestpd;
  697. end;
  698. function Tprocsym.find_procdef_enumerator_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  699. var
  700. paraidx, realparamcount,
  701. i, j : longint;
  702. bestpd,
  703. hpd,
  704. pd : tprocdef;
  705. current : tpropertysym;
  706. convtyp : tconverttype;
  707. eq : tequaltype;
  708. begin
  709. { This function will return the pprocdef of pprocsym that
  710. is the best match for fromdef and todef. }
  711. result:=nil;
  712. bestpd:=nil;
  713. besteq:=te_incompatible;
  714. for i:=0 to ProcdefList.Count-1 do
  715. begin
  716. pd:=tprocdef(ProcdefList[i]);
  717. if not is_class_or_interface_or_object(pd.returndef) then
  718. continue;
  719. current := tpropertysym(tobjectdef(pd.returndef).search_enumerator_current);
  720. if (current = nil) then
  721. continue;
  722. // compare current result def with the todef
  723. if (equal_defs(todef, current.propdef) or
  724. { shortstrings of different lengths are ok as result }
  725. (is_shortstring(todef) and is_shortstring(current.propdef))) and
  726. { the result type must be always really equal and not an alias,
  727. if you mess with this code, check tw4093 }
  728. ((todef=current.propdef) or
  729. (
  730. not(df_unique in todef.defoptions) and
  731. not(df_unique in current.propdef.defoptions)
  732. )
  733. ) then
  734. begin
  735. paraidx:=0;
  736. { ignore vs_hidden parameters }
  737. while (paraidx<pd.paras.count) and
  738. assigned(pd.paras[paraidx]) and
  739. (vo_is_hidden_para in tparavarsym(pd.paras[paraidx]).varoptions) do
  740. inc(paraidx);
  741. realparamcount:=0;
  742. for j := 0 to pd.paras.Count-1 do
  743. if assigned(pd.paras[j]) and not (vo_is_hidden_para in tparavarsym(pd.paras[j]).varoptions) then
  744. inc(realparamcount);
  745. if (paraidx<pd.paras.count) and
  746. assigned(pd.paras[paraidx]) and
  747. (realparamcount = 1) then
  748. begin
  749. eq:=compare_defs_ext(fromdef,tparavarsym(pd.paras[paraidx]).vardef,nothingn,convtyp,hpd,[]);
  750. { alias? if yes, only l1 choice,
  751. if you mess with this code, check tw4093 }
  752. if (eq=te_exact) and
  753. (fromdef<>tparavarsym(pd.paras[paraidx]).vardef) and
  754. ((df_unique in fromdef.defoptions) or
  755. (df_unique in tparavarsym(pd.paras[paraidx]).vardef.defoptions)) then
  756. eq:=te_convert_l1;
  757. if eq=te_exact then
  758. begin
  759. besteq:=eq;
  760. result:=pd;
  761. exit;
  762. end;
  763. if eq>besteq then
  764. begin
  765. bestpd:=pd;
  766. besteq:=eq;
  767. end;
  768. end;
  769. end;
  770. end;
  771. result:=bestpd;
  772. end;
  773. {****************************************************************************
  774. TERRORSYM
  775. ****************************************************************************}
  776. constructor terrorsym.create;
  777. begin
  778. inherited create(errorsym,'');
  779. end;
  780. {****************************************************************************
  781. TPROPERTYSYM
  782. ****************************************************************************}
  783. constructor tpropertysym.create(const n : string);
  784. var
  785. pap : tpropaccesslisttypes;
  786. begin
  787. inherited create(propertysym,n);
  788. propoptions:=[];
  789. index:=0;
  790. default:=0;
  791. propdef:=nil;
  792. indexdef:=nil;
  793. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  794. propaccesslist[pap]:=tpropaccesslist.create;
  795. end;
  796. constructor tpropertysym.ppuload(ppufile:tcompilerppufile);
  797. var
  798. pap : tpropaccesslisttypes;
  799. begin
  800. inherited ppuload(propertysym,ppufile);
  801. ppufile.getsmallset(propoptions);
  802. ppufile.getderef(overriddenpropsymderef);
  803. ppufile.getderef(propdefderef);
  804. index:=ppufile.getlongint;
  805. default:=ppufile.getlongint;
  806. ppufile.getderef(indexdefderef);
  807. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  808. propaccesslist[pap]:=ppufile.getpropaccesslist;
  809. end;
  810. destructor tpropertysym.destroy;
  811. var
  812. pap : tpropaccesslisttypes;
  813. begin
  814. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  815. propaccesslist[pap].free;
  816. inherited destroy;
  817. end;
  818. procedure tpropertysym.buildderef;
  819. var
  820. pap : tpropaccesslisttypes;
  821. begin
  822. overriddenpropsymderef.build(overriddenpropsym);
  823. propdefderef.build(propdef);
  824. indexdefderef.build(indexdef);
  825. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  826. propaccesslist[pap].buildderef;
  827. end;
  828. procedure tpropertysym.deref;
  829. var
  830. pap : tpropaccesslisttypes;
  831. begin
  832. overriddenpropsym:=tpropertysym(overriddenpropsymderef.resolve);
  833. indexdef:=tdef(indexdefderef.resolve);
  834. propdef:=tdef(propdefderef.resolve);
  835. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  836. propaccesslist[pap].resolve;
  837. end;
  838. function tpropertysym.getsize : aint;
  839. begin
  840. getsize:=0;
  841. end;
  842. procedure tpropertysym.ppuwrite(ppufile:tcompilerppufile);
  843. var
  844. pap : tpropaccesslisttypes;
  845. begin
  846. inherited ppuwrite(ppufile);
  847. ppufile.putsmallset(propoptions);
  848. ppufile.putderef(overriddenpropsymderef);
  849. ppufile.putderef(propdefderef);
  850. ppufile.putlongint(index);
  851. ppufile.putlongint(default);
  852. ppufile.putderef(indexdefderef);
  853. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  854. ppufile.putpropaccesslist(propaccesslist[pap]);
  855. ppufile.writeentry(ibpropertysym);
  856. end;
  857. {****************************************************************************
  858. TABSTRACTVARSYM
  859. ****************************************************************************}
  860. constructor tabstractvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  861. begin
  862. inherited create(st,n);
  863. vardef:=def;
  864. varspez:=vsp;
  865. varstate:=vs_declared;
  866. varoptions:=vopts;
  867. end;
  868. constructor tabstractvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  869. begin
  870. inherited ppuload(st,ppufile);
  871. varstate:=vs_readwritten;
  872. varspez:=tvarspez(ppufile.getbyte);
  873. varregable:=tvarregable(ppufile.getbyte);
  874. addr_taken:=boolean(ppufile.getbyte);
  875. ppufile.getderef(vardefderef);
  876. ppufile.getsmallset(varoptions);
  877. end;
  878. destructor tabstractvarsym.destroy;
  879. begin
  880. if assigned(notifications) then
  881. notifications.destroy;
  882. inherited destroy;
  883. end;
  884. procedure tabstractvarsym.buildderef;
  885. begin
  886. vardefderef.build(vardef);
  887. end;
  888. procedure tabstractvarsym.deref;
  889. var
  890. oldvarregable: tvarregable;
  891. begin
  892. { setting the vardef also updates varregable. We just loaded this }
  893. { value from a ppu, so it must not be changed (e.g. tw7817a.pp/ }
  894. { tw7817b.pp: the address is taken of a local variable in an }
  895. { inlined procedure -> must remain non-regable when inlining) }
  896. oldvarregable:=varregable;
  897. vardef:=tdef(vardefderef.resolve);
  898. varregable:=oldvarregable;
  899. end;
  900. procedure tabstractvarsym.ppuwrite(ppufile:tcompilerppufile);
  901. var
  902. oldintfcrc : boolean;
  903. begin
  904. inherited ppuwrite(ppufile);
  905. ppufile.putbyte(byte(varspez));
  906. oldintfcrc:=ppufile.do_crc;
  907. ppufile.do_crc:=false;
  908. ppufile.putbyte(byte(varregable));
  909. ppufile.putbyte(byte(addr_taken));
  910. ppufile.do_crc:=oldintfcrc;
  911. ppufile.putderef(vardefderef);
  912. ppufile.putsmallset(varoptions);
  913. end;
  914. function tabstractvarsym.getsize : aint;
  915. begin
  916. if assigned(vardef) and
  917. ((vardef.typ<>arraydef) or
  918. is_dynamic_array(vardef) or
  919. (tarraydef(vardef).highrange>=tarraydef(vardef).lowrange)) then
  920. result:=vardef.size
  921. else
  922. result:=0;
  923. end;
  924. function tabstractvarsym.getpackedbitsize : longint;
  925. begin
  926. { bitpacking is only done for ordinals }
  927. if not is_ordinal(vardef) then
  928. internalerror(2006082010);
  929. result:=vardef.packedbitsize;
  930. end;
  931. function tabstractvarsym.is_regvar(refpara: boolean):boolean;
  932. begin
  933. { Register variables are not allowed in the following cases:
  934. - regvars are disabled
  935. - exceptions are used (after an exception is raised the contents of the
  936. registers is not valid anymore)
  937. - it has a local copy
  938. - the value needs to be in memory (i.e. reference counted) }
  939. result:=(cs_opt_regvar in current_settings.optimizerswitches) and
  940. not(pi_has_assembler_block in current_procinfo.flags) and
  941. not(pi_uses_exceptions in current_procinfo.flags) and
  942. not(vo_has_local_copy in varoptions) and
  943. ((refpara and
  944. (varregable <> vr_none)) or
  945. (not refpara and
  946. not(varregable in [vr_none,vr_addr])))
  947. {$if not defined(powerpc) and not defined(powerpc64)}
  948. and ((vardef.typ <> recorddef) or
  949. (varregable = vr_addr) or
  950. not(varstate in [vs_written,vs_readwritten]));
  951. {$endif}
  952. end;
  953. procedure tabstractvarsym.trigger_notifications(what:Tnotification_flag);
  954. var n:Tnotification;
  955. begin
  956. if assigned(notifications) then
  957. begin
  958. n:=Tnotification(notifications.first);
  959. while assigned(n) do
  960. begin
  961. if what in n.flags then
  962. n.callback(what,self);
  963. n:=Tnotification(n.next);
  964. end;
  965. end;
  966. end;
  967. function Tabstractvarsym.register_notification(flags:Tnotification_flags;callback:
  968. Tnotification_callback):cardinal;
  969. var n:Tnotification;
  970. begin
  971. if not assigned(notifications) then
  972. notifications:=Tlinkedlist.create;
  973. n:=Tnotification.create(flags,callback);
  974. register_notification:=n.id;
  975. notifications.concat(n);
  976. end;
  977. procedure Tabstractvarsym.unregister_notification(id:cardinal);
  978. var n:Tnotification;
  979. begin
  980. if not assigned(notifications) then
  981. internalerror(200212311)
  982. else
  983. begin
  984. n:=Tnotification(notifications.first);
  985. while assigned(n) do
  986. begin
  987. if n.id=id then
  988. begin
  989. notifications.remove(n);
  990. n.destroy;
  991. exit;
  992. end;
  993. n:=Tnotification(n.next);
  994. end;
  995. internalerror(200212311)
  996. end;
  997. end;
  998. procedure tabstractvarsym.setvardef(def:tdef);
  999. begin
  1000. _vardef := def;
  1001. { can we load the value into a register ? }
  1002. if not assigned(owner) or
  1003. (owner.symtabletype in [localsymtable,parasymtable]) or
  1004. (
  1005. (owner.symtabletype=staticsymtable) and
  1006. not(cs_create_pic in current_settings.moduleswitches)
  1007. ) then
  1008. begin
  1009. if tstoreddef(vardef).is_intregable then
  1010. varregable:=vr_intreg
  1011. else
  1012. { $warning TODO: no fpu regvar in staticsymtable yet, need initialization with 0 }
  1013. if {(
  1014. not assigned(owner) or
  1015. (owner.symtabletype<>staticsymtable)
  1016. ) and }
  1017. tstoreddef(vardef).is_fpuregable then
  1018. begin
  1019. {$ifdef x86}
  1020. if use_sse(vardef) then
  1021. varregable:=vr_mmreg
  1022. else
  1023. {$else x86}
  1024. varregable:=vr_fpureg;
  1025. {$endif x86}
  1026. end;
  1027. end;
  1028. end;
  1029. {****************************************************************************
  1030. TFIELDVARSYM
  1031. ****************************************************************************}
  1032. constructor tfieldvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1033. begin
  1034. inherited create(fieldvarsym,n,vsp,def,vopts);
  1035. fieldoffset:=-1;
  1036. end;
  1037. constructor tfieldvarsym.ppuload(ppufile:tcompilerppufile);
  1038. begin
  1039. inherited ppuload(fieldvarsym,ppufile);
  1040. fieldoffset:=ppufile.getaint;
  1041. end;
  1042. procedure tfieldvarsym.ppuwrite(ppufile:tcompilerppufile);
  1043. begin
  1044. inherited ppuwrite(ppufile);
  1045. ppufile.putaint(fieldoffset);
  1046. ppufile.writeentry(ibfieldvarsym);
  1047. end;
  1048. function tfieldvarsym.mangledname:string;
  1049. var
  1050. srsym : tsym;
  1051. srsymtable : tsymtable;
  1052. begin
  1053. if sp_static in symoptions then
  1054. begin
  1055. if searchsym(lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1056. result:=srsym.mangledname
  1057. { when generating the debug info for the module in which the }
  1058. { symbol is defined, the localsymtable of that module is }
  1059. { already popped from the symtablestack }
  1060. else if searchsym_in_module(current_module,lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1061. result:=srsym.mangledname
  1062. else
  1063. internalerror(2007012501);
  1064. end
  1065. else
  1066. result:=inherited mangledname;
  1067. end;
  1068. {****************************************************************************
  1069. TABSTRACTNORMALVARSYM
  1070. ****************************************************************************}
  1071. constructor tabstractnormalvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1072. begin
  1073. inherited create(st,n,vsp,def,vopts);
  1074. fillchar(localloc,sizeof(localloc),0);
  1075. fillchar(initialloc,sizeof(initialloc),0);
  1076. defaultconstsym:=nil;
  1077. end;
  1078. constructor tabstractnormalvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  1079. begin
  1080. inherited ppuload(st,ppufile);
  1081. fillchar(localloc,sizeof(localloc),0);
  1082. fillchar(initialloc,sizeof(initialloc),0);
  1083. ppufile.getderef(defaultconstsymderef);
  1084. end;
  1085. procedure tabstractnormalvarsym.buildderef;
  1086. begin
  1087. inherited buildderef;
  1088. defaultconstsymderef.build(defaultconstsym);
  1089. end;
  1090. procedure tabstractnormalvarsym.deref;
  1091. begin
  1092. inherited deref;
  1093. defaultconstsym:=tsym(defaultconstsymderef.resolve);
  1094. end;
  1095. procedure tabstractnormalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1096. begin
  1097. inherited ppuwrite(ppufile);
  1098. ppufile.putderef(defaultconstsymderef);
  1099. end;
  1100. {****************************************************************************
  1101. Tstaticvarsym
  1102. ****************************************************************************}
  1103. constructor tstaticvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1104. begin
  1105. inherited create(staticvarsym,n,vsp,def,vopts);
  1106. _mangledname:=nil;
  1107. end;
  1108. constructor tstaticvarsym.create_dll(const n : string;vsp:tvarspez;def:tdef);
  1109. begin
  1110. tstaticvarsym(self).create(n,vsp,def,[vo_is_dll_var]);
  1111. end;
  1112. constructor tstaticvarsym.create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  1113. begin
  1114. tstaticvarsym(self).create(n,vsp,def,[]);
  1115. set_mangledname(mangled);
  1116. end;
  1117. constructor tstaticvarsym.ppuload(ppufile:tcompilerppufile);
  1118. begin
  1119. inherited ppuload(staticvarsym,ppufile);
  1120. if vo_has_mangledname in varoptions then
  1121. _mangledname:=stringdup(ppufile.getstring)
  1122. else
  1123. _mangledname:=nil;
  1124. end;
  1125. destructor tstaticvarsym.destroy;
  1126. begin
  1127. if assigned(_mangledname) then
  1128. begin
  1129. {$ifdef MEMDEBUG}
  1130. memmanglednames.start;
  1131. {$endif MEMDEBUG}
  1132. stringdispose(_mangledname);
  1133. {$ifdef MEMDEBUG}
  1134. memmanglednames.stop;
  1135. {$endif MEMDEBUG}
  1136. end;
  1137. inherited destroy;
  1138. end;
  1139. procedure tstaticvarsym.ppuwrite(ppufile:tcompilerppufile);
  1140. begin
  1141. inherited ppuwrite(ppufile);
  1142. if vo_has_mangledname in varoptions then
  1143. ppufile.putstring(_mangledname^);
  1144. ppufile.writeentry(ibstaticvarsym);
  1145. end;
  1146. function tstaticvarsym.mangledname:string;
  1147. var
  1148. prefix : string[2];
  1149. begin
  1150. if not assigned(_mangledname) then
  1151. begin
  1152. if (vo_is_typed_const in varoptions) then
  1153. prefix:='TC'
  1154. else
  1155. prefix:='U';
  1156. {$ifdef compress}
  1157. _mangledname:=stringdup(minilzw_encode(make_mangledname(prefix,owner,name)));
  1158. {$else}
  1159. _mangledname:=stringdup(make_mangledname(prefix,owner,name));
  1160. {$endif}
  1161. end;
  1162. result:=_mangledname^;
  1163. end;
  1164. procedure tstaticvarsym.set_mangledname(const s:string);
  1165. begin
  1166. stringdispose(_mangledname);
  1167. {$ifdef compress}
  1168. _mangledname:=stringdup(minilzw_encode(s));
  1169. {$else}
  1170. _mangledname:=stringdup(s);
  1171. {$endif}
  1172. include(varoptions,vo_has_mangledname);
  1173. end;
  1174. {****************************************************************************
  1175. TLOCALVARSYM
  1176. ****************************************************************************}
  1177. constructor tlocalvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1178. begin
  1179. inherited create(localvarsym,n,vsp,def,vopts);
  1180. end;
  1181. constructor tlocalvarsym.ppuload(ppufile:tcompilerppufile);
  1182. begin
  1183. inherited ppuload(localvarsym,ppufile);
  1184. end;
  1185. procedure tlocalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1186. begin
  1187. inherited ppuwrite(ppufile);
  1188. ppufile.writeentry(iblocalvarsym);
  1189. end;
  1190. {****************************************************************************
  1191. TPARAVARSYM
  1192. ****************************************************************************}
  1193. constructor tparavarsym.create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1194. begin
  1195. inherited create(paravarsym,n,vsp,def,vopts);
  1196. if (vsp in [vs_var,vs_value,vs_const]) then
  1197. varstate := vs_initialised;
  1198. paranr:=nr;
  1199. paraloc[calleeside].init;
  1200. paraloc[callerside].init;
  1201. end;
  1202. destructor tparavarsym.destroy;
  1203. begin
  1204. paraloc[calleeside].done;
  1205. paraloc[callerside].done;
  1206. inherited destroy;
  1207. end;
  1208. constructor tparavarsym.ppuload(ppufile:tcompilerppufile);
  1209. var
  1210. b : byte;
  1211. begin
  1212. inherited ppuload(paravarsym,ppufile);
  1213. paranr:=ppufile.getword;
  1214. { The var state of parameter symbols is fixed after writing them so
  1215. we write them to the unit file.
  1216. This enables constant folding for inline procedures loaded from units
  1217. }
  1218. varstate:=tvarstate(ppufile.getbyte);
  1219. paraloc[calleeside].init;
  1220. paraloc[callerside].init;
  1221. if vo_has_explicit_paraloc in varoptions then
  1222. begin
  1223. paraloc[callerside].alignment:=ppufile.getbyte;
  1224. b:=ppufile.getbyte;
  1225. if b<>sizeof(paraloc[callerside].location^) then
  1226. internalerror(200411154);
  1227. ppufile.getdata(paraloc[callerside].add_location^,sizeof(paraloc[callerside].location^));
  1228. paraloc[callerside].size:=paraloc[callerside].location^.size;
  1229. paraloc[callerside].intsize:=tcgsize2size[paraloc[callerside].size];
  1230. end;
  1231. end;
  1232. procedure tparavarsym.ppuwrite(ppufile:tcompilerppufile);
  1233. var
  1234. oldintfcrc : boolean;
  1235. begin
  1236. inherited ppuwrite(ppufile);
  1237. ppufile.putword(paranr);
  1238. { The var state of parameter symbols is fixed after writing them so
  1239. we write them to the unit file.
  1240. This enables constant folding for inline procedures loaded from units
  1241. }
  1242. oldintfcrc:=ppufile.do_crc;
  1243. ppufile.do_crc:=false;
  1244. ppufile.putbyte(ord(varstate));
  1245. ppufile.do_crc:=oldintfcrc;
  1246. if vo_has_explicit_paraloc in varoptions then
  1247. begin
  1248. paraloc[callerside].check_simple_location;
  1249. ppufile.putbyte(sizeof(paraloc[callerside].alignment));
  1250. ppufile.putbyte(sizeof(paraloc[callerside].location^));
  1251. ppufile.putdata(paraloc[callerside].location^,sizeof(paraloc[callerside].location^));
  1252. end;
  1253. ppufile.writeentry(ibparavarsym);
  1254. end;
  1255. {****************************************************************************
  1256. TABSOLUTEVARSYM
  1257. ****************************************************************************}
  1258. constructor tabsolutevarsym.create(const n : string;def:tdef);
  1259. begin
  1260. inherited create(absolutevarsym,n,vs_value,def,[]);
  1261. ref:=nil;
  1262. end;
  1263. constructor tabsolutevarsym.create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  1264. begin
  1265. inherited create(absolutevarsym,n,vs_value,def,[]);
  1266. ref:=_ref;
  1267. end;
  1268. destructor tabsolutevarsym.destroy;
  1269. begin
  1270. if assigned(ref) then
  1271. ref.free;
  1272. inherited destroy;
  1273. end;
  1274. constructor tabsolutevarsym.ppuload(ppufile:tcompilerppufile);
  1275. begin
  1276. inherited ppuload(absolutevarsym,ppufile);
  1277. ref:=nil;
  1278. asmname:=nil;
  1279. abstyp:=absolutetyp(ppufile.getbyte);
  1280. {$ifdef i386}
  1281. absseg:=false;
  1282. {$endif i386}
  1283. case abstyp of
  1284. tovar :
  1285. ref:=ppufile.getpropaccesslist;
  1286. toasm :
  1287. asmname:=stringdup(ppufile.getstring);
  1288. toaddr :
  1289. begin
  1290. addroffset:=ppufile.getaword;
  1291. {$ifdef i386}
  1292. absseg:=boolean(ppufile.getbyte);
  1293. {$endif i386}
  1294. end;
  1295. end;
  1296. end;
  1297. procedure tabsolutevarsym.ppuwrite(ppufile:tcompilerppufile);
  1298. begin
  1299. inherited ppuwrite(ppufile);
  1300. ppufile.putbyte(byte(abstyp));
  1301. case abstyp of
  1302. tovar :
  1303. ppufile.putpropaccesslist(ref);
  1304. toasm :
  1305. ppufile.putstring(asmname^);
  1306. toaddr :
  1307. begin
  1308. ppufile.putaword(addroffset);
  1309. {$ifdef i386}
  1310. ppufile.putbyte(byte(absseg));
  1311. {$endif i386}
  1312. end;
  1313. end;
  1314. ppufile.writeentry(ibabsolutevarsym);
  1315. end;
  1316. procedure tabsolutevarsym.buildderef;
  1317. begin
  1318. inherited buildderef;
  1319. if (abstyp=tovar) then
  1320. ref.buildderef;
  1321. end;
  1322. procedure tabsolutevarsym.deref;
  1323. begin
  1324. inherited deref;
  1325. { own absolute deref }
  1326. if (abstyp=tovar) then
  1327. ref.resolve;
  1328. end;
  1329. function tabsolutevarsym.mangledname : string;
  1330. begin
  1331. case abstyp of
  1332. toasm :
  1333. mangledname:=asmname^;
  1334. toaddr :
  1335. mangledname:='$'+tostr(addroffset);
  1336. else
  1337. internalerror(200411062);
  1338. end;
  1339. end;
  1340. {****************************************************************************
  1341. TCONSTSYM
  1342. ****************************************************************************}
  1343. constructor tconstsym.create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  1344. begin
  1345. inherited create(constsym,n);
  1346. fillchar(value, sizeof(value), #0);
  1347. consttyp:=t;
  1348. value.valueord:=v;
  1349. constdef:=def;
  1350. end;
  1351. constructor tconstsym.create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  1352. begin
  1353. inherited create(constsym,n);
  1354. fillchar(value, sizeof(value), #0);
  1355. consttyp:=t;
  1356. value.valueordptr:=v;
  1357. constdef:=def;
  1358. end;
  1359. constructor tconstsym.create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  1360. begin
  1361. inherited create(constsym,n);
  1362. fillchar(value, sizeof(value), #0);
  1363. consttyp:=t;
  1364. value.valueptr:=v;
  1365. constdef:=def;
  1366. end;
  1367. constructor tconstsym.create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  1368. begin
  1369. inherited create(constsym,n);
  1370. fillchar(value, sizeof(value), #0);
  1371. consttyp:=t;
  1372. value.valueptr:=str;
  1373. constdef:=nil;
  1374. value.len:=l;
  1375. end;
  1376. constructor tconstsym.create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  1377. begin
  1378. inherited create(constsym,n);
  1379. fillchar(value, sizeof(value), #0);
  1380. consttyp:=t;
  1381. pcompilerwidestring(value.valueptr):=pw;
  1382. constdef:=nil;
  1383. value.len:=getlengthwidestring(pw);
  1384. end;
  1385. constructor tconstsym.ppuload(ppufile:tcompilerppufile);
  1386. var
  1387. pd : pbestreal;
  1388. ps : pnormalset;
  1389. pc : pchar;
  1390. pw : pcompilerwidestring;
  1391. i : longint;
  1392. begin
  1393. inherited ppuload(constsym,ppufile);
  1394. constdef:=nil;
  1395. consttyp:=tconsttyp(ppufile.getbyte);
  1396. fillchar(value, sizeof(value), #0);
  1397. case consttyp of
  1398. constord :
  1399. begin
  1400. ppufile.getderef(constdefderef);
  1401. value.valueord:=ppufile.getexprint;
  1402. end;
  1403. constpointer :
  1404. begin
  1405. ppufile.getderef(constdefderef);
  1406. value.valueordptr:=ppufile.getptruint;
  1407. end;
  1408. constwstring :
  1409. begin
  1410. initwidestring(pw);
  1411. setlengthwidestring(pw,ppufile.getlongint);
  1412. { don't use getdata, because the compilerwidechars may have to
  1413. be byteswapped
  1414. }
  1415. {$if sizeof(tcompilerwidechar) = 2}
  1416. for i:=0 to pw^.len-1 do
  1417. pw^.data[i]:=ppufile.getword;
  1418. {$elseif sizeof(tcompilerwidechar) = 4}
  1419. for i:=0 to pw^.len-1 do
  1420. pw^.data[i]:=cardinal(ppufile.getlongint);
  1421. {$else}
  1422. {$error Unsupported tcompilerwidechar size}
  1423. {$endif}
  1424. pcompilerwidestring(value.valueptr):=pw;
  1425. end;
  1426. conststring,
  1427. constresourcestring :
  1428. begin
  1429. value.len:=ppufile.getlongint;
  1430. getmem(pc,value.len+1);
  1431. ppufile.getdata(pc^,value.len);
  1432. pc[value.len]:=#0;
  1433. value.valueptr:=pc;
  1434. end;
  1435. constreal :
  1436. begin
  1437. new(pd);
  1438. pd^:=ppufile.getreal;
  1439. value.valueptr:=pd;
  1440. end;
  1441. constset :
  1442. begin
  1443. ppufile.getderef(constdefderef);
  1444. new(ps);
  1445. ppufile.getnormalset(ps^);
  1446. value.valueptr:=ps;
  1447. end;
  1448. constguid :
  1449. begin
  1450. new(pguid(value.valueptr));
  1451. ppufile.getdata(value.valueptr^,sizeof(tguid));
  1452. end;
  1453. constnil : ;
  1454. else
  1455. Message1(unit_f_ppu_invalid_entry,tostr(ord(consttyp)));
  1456. end;
  1457. end;
  1458. destructor tconstsym.destroy;
  1459. begin
  1460. case consttyp of
  1461. conststring,
  1462. constresourcestring :
  1463. freemem(pchar(value.valueptr),value.len+1);
  1464. constwstring :
  1465. donewidestring(pcompilerwidestring(value.valueptr));
  1466. constreal :
  1467. dispose(pbestreal(value.valueptr));
  1468. constset :
  1469. dispose(pnormalset(value.valueptr));
  1470. constguid :
  1471. dispose(pguid(value.valueptr));
  1472. end;
  1473. inherited destroy;
  1474. end;
  1475. procedure tconstsym.buildderef;
  1476. begin
  1477. if consttyp in [constord,constpointer,constset] then
  1478. constdefderef.build(constdef);
  1479. end;
  1480. procedure tconstsym.deref;
  1481. begin
  1482. if consttyp in [constord,constpointer,constset] then
  1483. constdef:=tdef(constdefderef.resolve);
  1484. end;
  1485. procedure tconstsym.ppuwrite(ppufile:tcompilerppufile);
  1486. begin
  1487. inherited ppuwrite(ppufile);
  1488. ppufile.putbyte(byte(consttyp));
  1489. case consttyp of
  1490. constnil : ;
  1491. constord :
  1492. begin
  1493. ppufile.putderef(constdefderef);
  1494. ppufile.putexprint(value.valueord);
  1495. end;
  1496. constpointer :
  1497. begin
  1498. ppufile.putderef(constdefderef);
  1499. ppufile.putptruint(value.valueordptr);
  1500. end;
  1501. constwstring :
  1502. begin
  1503. ppufile.putlongint(getlengthwidestring(pcompilerwidestring(value.valueptr)));
  1504. ppufile.putdata(pcompilerwidestring(value.valueptr)^.data^,pcompilerwidestring(value.valueptr)^.len*sizeof(tcompilerwidechar));
  1505. end;
  1506. conststring,
  1507. constresourcestring :
  1508. begin
  1509. ppufile.putlongint(value.len);
  1510. ppufile.putdata(pchar(value.valueptr)^,value.len);
  1511. end;
  1512. constreal :
  1513. ppufile.putreal(pbestreal(value.valueptr)^);
  1514. constset :
  1515. begin
  1516. ppufile.putderef(constdefderef);
  1517. ppufile.putnormalset(value.valueptr^);
  1518. end;
  1519. constguid :
  1520. ppufile.putdata(value.valueptr^,sizeof(tguid));
  1521. else
  1522. internalerror(13);
  1523. end;
  1524. ppufile.writeentry(ibconstsym);
  1525. end;
  1526. {****************************************************************************
  1527. TENUMSYM
  1528. ****************************************************************************}
  1529. constructor tenumsym.create(const n : string;def : tenumdef;v : longint);
  1530. begin
  1531. inherited create(enumsym,n);
  1532. definition:=def;
  1533. value:=v;
  1534. { First entry? Then we need to set the minval }
  1535. if def.firstenum=nil then
  1536. begin
  1537. if v>0 then
  1538. def.has_jumps:=true;
  1539. def.setmin(v);
  1540. def.setmax(v);
  1541. end
  1542. else
  1543. begin
  1544. { check for jumps }
  1545. if v>def.max+1 then
  1546. def.has_jumps:=true;
  1547. { update low and high }
  1548. if def.min>v then
  1549. def.setmin(v);
  1550. if def.max<v then
  1551. def.setmax(v);
  1552. end;
  1553. order;
  1554. end;
  1555. constructor tenumsym.ppuload(ppufile:tcompilerppufile);
  1556. begin
  1557. inherited ppuload(enumsym,ppufile);
  1558. ppufile.getderef(definitionderef);
  1559. value:=ppufile.getlongint;
  1560. nextenum := Nil;
  1561. end;
  1562. procedure tenumsym.buildderef;
  1563. begin
  1564. definitionderef.build(definition);
  1565. end;
  1566. procedure tenumsym.deref;
  1567. begin
  1568. definition:=tenumdef(definitionderef.resolve);
  1569. order;
  1570. end;
  1571. procedure tenumsym.order;
  1572. var
  1573. sym : tenumsym;
  1574. begin
  1575. sym := tenumsym(definition.firstenum);
  1576. if sym = nil then
  1577. begin
  1578. definition.firstenum := self;
  1579. nextenum := nil;
  1580. exit;
  1581. end;
  1582. { reorder the symbols in increasing value }
  1583. if value < sym.value then
  1584. begin
  1585. nextenum := sym;
  1586. definition.firstenum := self;
  1587. end
  1588. else
  1589. begin
  1590. while (sym.value <= value) and assigned(sym.nextenum) do
  1591. sym := sym.nextenum;
  1592. nextenum := sym.nextenum;
  1593. sym.nextenum := self;
  1594. end;
  1595. end;
  1596. procedure tenumsym.ppuwrite(ppufile:tcompilerppufile);
  1597. begin
  1598. inherited ppuwrite(ppufile);
  1599. ppufile.putderef(definitionderef);
  1600. ppufile.putlongint(value);
  1601. ppufile.writeentry(ibenumsym);
  1602. end;
  1603. {****************************************************************************
  1604. TTYPESYM
  1605. ****************************************************************************}
  1606. constructor ttypesym.create(const n : string;def:tdef);
  1607. begin
  1608. inherited create(typesym,n);
  1609. typedef:=def;
  1610. { register the typesym for the definition }
  1611. if assigned(typedef) and
  1612. (typedef.typ<>errordef) and
  1613. not(assigned(typedef.typesym)) then
  1614. typedef.typesym:=self;
  1615. end;
  1616. constructor ttypesym.ppuload(ppufile:tcompilerppufile);
  1617. begin
  1618. inherited ppuload(typesym,ppufile);
  1619. ppufile.getderef(typedefderef);
  1620. end;
  1621. procedure ttypesym.buildderef;
  1622. begin
  1623. typedefderef.build(typedef);
  1624. end;
  1625. procedure ttypesym.deref;
  1626. begin
  1627. typedef:=tdef(typedefderef.resolve);
  1628. end;
  1629. procedure ttypesym.ppuwrite(ppufile:tcompilerppufile);
  1630. begin
  1631. inherited ppuwrite(ppufile);
  1632. ppufile.putderef(typedefderef);
  1633. ppufile.writeentry(ibtypesym);
  1634. end;
  1635. {****************************************************************************
  1636. TSYSSYM
  1637. ****************************************************************************}
  1638. constructor tsyssym.create(const n : string;l : longint);
  1639. begin
  1640. inherited create(syssym,n);
  1641. number:=l;
  1642. end;
  1643. constructor tsyssym.ppuload(ppufile:tcompilerppufile);
  1644. begin
  1645. inherited ppuload(syssym,ppufile);
  1646. number:=ppufile.getlongint;
  1647. end;
  1648. destructor tsyssym.destroy;
  1649. begin
  1650. inherited destroy;
  1651. end;
  1652. procedure tsyssym.ppuwrite(ppufile:tcompilerppufile);
  1653. begin
  1654. inherited ppuwrite(ppufile);
  1655. ppufile.putlongint(number);
  1656. ppufile.writeentry(ibsyssym);
  1657. end;
  1658. {*****************************************************************************
  1659. TMacro
  1660. *****************************************************************************}
  1661. constructor tmacro.create(const n : string);
  1662. begin
  1663. inherited create(macrosym,n);
  1664. owner:=nil;
  1665. defined:=false;
  1666. is_used:=false;
  1667. is_compiler_var:=false;
  1668. buftext:=nil;
  1669. buflen:=0;
  1670. end;
  1671. constructor tmacro.ppuload(ppufile:tcompilerppufile);
  1672. begin
  1673. inherited ppuload(macrosym,ppufile);
  1674. defined:=boolean(ppufile.getbyte);
  1675. is_compiler_var:=boolean(ppufile.getbyte);
  1676. is_used:=false;
  1677. buflen:= ppufile.getlongint;
  1678. if buflen > 0 then
  1679. begin
  1680. getmem(buftext, buflen);
  1681. ppufile.getdata(buftext^, buflen)
  1682. end
  1683. else
  1684. buftext:=nil;
  1685. end;
  1686. destructor tmacro.destroy;
  1687. begin
  1688. if assigned(buftext) then
  1689. freemem(buftext);
  1690. inherited destroy;
  1691. end;
  1692. procedure tmacro.ppuwrite(ppufile:tcompilerppufile);
  1693. begin
  1694. inherited ppuwrite(ppufile);
  1695. ppufile.putbyte(byte(defined));
  1696. ppufile.putbyte(byte(is_compiler_var));
  1697. ppufile.putlongint(buflen);
  1698. if buflen > 0 then
  1699. ppufile.putdata(buftext^,buflen);
  1700. ppufile.writeentry(ibmacrosym);
  1701. end;
  1702. function tmacro.GetCopy:tmacro;
  1703. var
  1704. p : tmacro;
  1705. begin
  1706. p:=tmacro.create(realname);
  1707. p.defined:=defined;
  1708. p.is_used:=is_used;
  1709. p.is_compiler_var:=is_compiler_var;
  1710. p.buflen:=buflen;
  1711. if assigned(buftext) then
  1712. begin
  1713. getmem(p.buftext,buflen);
  1714. move(buftext^,p.buftext^,buflen);
  1715. end;
  1716. Result:=p;
  1717. end;
  1718. end.