symsym.pas 62 KB

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