symsym.pas 62 KB

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