symsym.pas 60 KB

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