symsym.pas 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  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. {$ifdef x86}
  1005. if use_sse(vardef) then
  1006. varregable:=vr_mmreg
  1007. else
  1008. {$else x86}
  1009. varregable:=vr_fpureg;
  1010. {$endif x86}
  1011. end;
  1012. end;
  1013. end;
  1014. {****************************************************************************
  1015. TFIELDVARSYM
  1016. ****************************************************************************}
  1017. constructor tfieldvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1018. begin
  1019. inherited create(fieldvarsym,n,vsp,def,vopts);
  1020. fieldoffset:=-1;
  1021. end;
  1022. constructor tfieldvarsym.ppuload(ppufile:tcompilerppufile);
  1023. begin
  1024. inherited ppuload(fieldvarsym,ppufile);
  1025. fieldoffset:=ppufile.getaint;
  1026. end;
  1027. procedure tfieldvarsym.ppuwrite(ppufile:tcompilerppufile);
  1028. begin
  1029. inherited ppuwrite(ppufile);
  1030. ppufile.putaint(fieldoffset);
  1031. ppufile.writeentry(ibfieldvarsym);
  1032. end;
  1033. function tfieldvarsym.mangledname:string;
  1034. var
  1035. srsym : tsym;
  1036. srsymtable : tsymtable;
  1037. begin
  1038. if sp_static in symoptions then
  1039. begin
  1040. if searchsym(lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1041. result:=srsym.mangledname
  1042. { when generating the debug info for the module in which the }
  1043. { symbol is defined, the localsymtable of that module is }
  1044. { already popped from the symtablestack }
  1045. else if searchsym_in_module(current_module,lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1046. result:=srsym.mangledname
  1047. else
  1048. internalerror(2007012501);
  1049. end
  1050. else if is_objcclass(tdef(owner.defowner)) then
  1051. begin
  1052. if assigned(objcoffsetmangledname) then
  1053. result:=objcoffsetmangledname^
  1054. else
  1055. begin
  1056. result:=target_info.cprefix+'OBJC_IVAR_$_'+tobjectdef(owner.defowner).objextname^+'.'+RealName;
  1057. objcoffsetmangledname:=stringdup(result);
  1058. end;
  1059. end
  1060. else
  1061. result:=inherited mangledname;
  1062. end;
  1063. destructor tfieldvarsym.destroy;
  1064. begin
  1065. stringdispose(objcoffsetmangledname);
  1066. inherited destroy;
  1067. end;
  1068. {****************************************************************************
  1069. TABSTRACTNORMALVARSYM
  1070. ****************************************************************************}
  1071. constructor tabstractnormalvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1072. begin
  1073. inherited create(st,n,vsp,def,vopts);
  1074. fillchar(localloc,sizeof(localloc),0);
  1075. fillchar(initialloc,sizeof(initialloc),0);
  1076. defaultconstsym:=nil;
  1077. end;
  1078. constructor tabstractnormalvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  1079. begin
  1080. inherited ppuload(st,ppufile);
  1081. fillchar(localloc,sizeof(localloc),0);
  1082. fillchar(initialloc,sizeof(initialloc),0);
  1083. ppufile.getderef(defaultconstsymderef);
  1084. end;
  1085. procedure tabstractnormalvarsym.buildderef;
  1086. begin
  1087. inherited buildderef;
  1088. defaultconstsymderef.build(defaultconstsym);
  1089. end;
  1090. procedure tabstractnormalvarsym.deref;
  1091. begin
  1092. inherited deref;
  1093. defaultconstsym:=tsym(defaultconstsymderef.resolve);
  1094. end;
  1095. procedure tabstractnormalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1096. begin
  1097. inherited ppuwrite(ppufile);
  1098. ppufile.putderef(defaultconstsymderef);
  1099. end;
  1100. {****************************************************************************
  1101. Tstaticvarsym
  1102. ****************************************************************************}
  1103. constructor tstaticvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1104. begin
  1105. inherited create(staticvarsym,n,vsp,def,vopts);
  1106. _mangledname:=nil;
  1107. end;
  1108. constructor tstaticvarsym.create_dll(const n : string;vsp:tvarspez;def:tdef);
  1109. begin
  1110. tstaticvarsym(self).create(n,vsp,def,[vo_is_dll_var]);
  1111. end;
  1112. constructor tstaticvarsym.create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  1113. begin
  1114. tstaticvarsym(self).create(n,vsp,def,[]);
  1115. set_mangledname(mangled);
  1116. end;
  1117. constructor tstaticvarsym.ppuload(ppufile:tcompilerppufile);
  1118. begin
  1119. inherited ppuload(staticvarsym,ppufile);
  1120. if vo_has_mangledname in varoptions then
  1121. _mangledname:=stringdup(ppufile.getstring)
  1122. else
  1123. _mangledname:=nil;
  1124. end;
  1125. destructor tstaticvarsym.destroy;
  1126. begin
  1127. if assigned(_mangledname) then
  1128. begin
  1129. {$ifdef MEMDEBUG}
  1130. memmanglednames.start;
  1131. {$endif MEMDEBUG}
  1132. stringdispose(_mangledname);
  1133. {$ifdef MEMDEBUG}
  1134. memmanglednames.stop;
  1135. {$endif MEMDEBUG}
  1136. end;
  1137. inherited destroy;
  1138. end;
  1139. procedure tstaticvarsym.ppuwrite(ppufile:tcompilerppufile);
  1140. begin
  1141. inherited ppuwrite(ppufile);
  1142. if vo_has_mangledname in varoptions then
  1143. ppufile.putstring(_mangledname^);
  1144. ppufile.writeentry(ibstaticvarsym);
  1145. end;
  1146. function tstaticvarsym.mangledname:string;
  1147. var
  1148. prefix : string[2];
  1149. begin
  1150. if not assigned(_mangledname) then
  1151. begin
  1152. if (vo_is_typed_const in varoptions) then
  1153. prefix:='TC'
  1154. else
  1155. prefix:='U';
  1156. {$ifdef compress}
  1157. _mangledname:=stringdup(minilzw_encode(make_mangledname(prefix,owner,name)));
  1158. {$else}
  1159. _mangledname:=stringdup(make_mangledname(prefix,owner,name));
  1160. {$endif}
  1161. end;
  1162. result:=_mangledname^;
  1163. end;
  1164. procedure tstaticvarsym.set_mangledname(const s:string);
  1165. begin
  1166. stringdispose(_mangledname);
  1167. {$ifdef compress}
  1168. _mangledname:=stringdup(minilzw_encode(s));
  1169. {$else}
  1170. _mangledname:=stringdup(s);
  1171. {$endif}
  1172. include(varoptions,vo_has_mangledname);
  1173. end;
  1174. {****************************************************************************
  1175. TLOCALVARSYM
  1176. ****************************************************************************}
  1177. constructor tlocalvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1178. begin
  1179. inherited create(localvarsym,n,vsp,def,vopts);
  1180. end;
  1181. constructor tlocalvarsym.ppuload(ppufile:tcompilerppufile);
  1182. begin
  1183. inherited ppuload(localvarsym,ppufile);
  1184. end;
  1185. procedure tlocalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1186. begin
  1187. inherited ppuwrite(ppufile);
  1188. ppufile.writeentry(iblocalvarsym);
  1189. end;
  1190. {****************************************************************************
  1191. TPARAVARSYM
  1192. ****************************************************************************}
  1193. constructor tparavarsym.create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1194. begin
  1195. inherited create(paravarsym,n,vsp,def,vopts);
  1196. if (vsp in [vs_var,vs_value,vs_const]) then
  1197. varstate := vs_initialised;
  1198. paranr:=nr;
  1199. paraloc[calleeside].init;
  1200. paraloc[callerside].init;
  1201. end;
  1202. destructor tparavarsym.destroy;
  1203. begin
  1204. paraloc[calleeside].done;
  1205. paraloc[callerside].done;
  1206. inherited destroy;
  1207. end;
  1208. constructor tparavarsym.ppuload(ppufile:tcompilerppufile);
  1209. var
  1210. b : byte;
  1211. begin
  1212. inherited ppuload(paravarsym,ppufile);
  1213. paranr:=ppufile.getword;
  1214. { The var state of parameter symbols is fixed after writing them so
  1215. we write them to the unit file.
  1216. This enables constant folding for inline procedures loaded from units
  1217. }
  1218. varstate:=tvarstate(ppufile.getbyte);
  1219. paraloc[calleeside].init;
  1220. paraloc[callerside].init;
  1221. if vo_has_explicit_paraloc in varoptions then
  1222. begin
  1223. paraloc[callerside].alignment:=ppufile.getbyte;
  1224. b:=ppufile.getbyte;
  1225. if b<>sizeof(paraloc[callerside].location^) then
  1226. internalerror(200411154);
  1227. ppufile.getdata(paraloc[callerside].add_location^,sizeof(paraloc[callerside].location^));
  1228. paraloc[callerside].size:=paraloc[callerside].location^.size;
  1229. paraloc[callerside].intsize:=tcgsize2size[paraloc[callerside].size];
  1230. end;
  1231. end;
  1232. procedure tparavarsym.ppuwrite(ppufile:tcompilerppufile);
  1233. var
  1234. oldintfcrc : boolean;
  1235. begin
  1236. inherited ppuwrite(ppufile);
  1237. ppufile.putword(paranr);
  1238. { The var state of parameter symbols is fixed after writing them so
  1239. we write them to the unit file.
  1240. This enables constant folding for inline procedures loaded from units
  1241. }
  1242. oldintfcrc:=ppufile.do_crc;
  1243. ppufile.do_crc:=false;
  1244. ppufile.putbyte(ord(varstate));
  1245. ppufile.do_crc:=oldintfcrc;
  1246. if vo_has_explicit_paraloc in varoptions then
  1247. begin
  1248. paraloc[callerside].check_simple_location;
  1249. ppufile.putbyte(sizeof(paraloc[callerside].alignment));
  1250. ppufile.putbyte(sizeof(paraloc[callerside].location^));
  1251. ppufile.putdata(paraloc[callerside].location^,sizeof(paraloc[callerside].location^));
  1252. end;
  1253. ppufile.writeentry(ibparavarsym);
  1254. end;
  1255. {****************************************************************************
  1256. TABSOLUTEVARSYM
  1257. ****************************************************************************}
  1258. constructor tabsolutevarsym.create(const n : string;def:tdef);
  1259. begin
  1260. inherited create(absolutevarsym,n,vs_value,def,[]);
  1261. ref:=nil;
  1262. end;
  1263. constructor tabsolutevarsym.create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  1264. begin
  1265. inherited create(absolutevarsym,n,vs_value,def,[]);
  1266. ref:=_ref;
  1267. end;
  1268. destructor tabsolutevarsym.destroy;
  1269. begin
  1270. if assigned(ref) then
  1271. ref.free;
  1272. inherited destroy;
  1273. end;
  1274. constructor tabsolutevarsym.ppuload(ppufile:tcompilerppufile);
  1275. begin
  1276. inherited ppuload(absolutevarsym,ppufile);
  1277. ref:=nil;
  1278. asmname:=nil;
  1279. abstyp:=absolutetyp(ppufile.getbyte);
  1280. {$ifdef i386}
  1281. absseg:=false;
  1282. {$endif i386}
  1283. case abstyp of
  1284. tovar :
  1285. ref:=ppufile.getpropaccesslist;
  1286. toasm :
  1287. asmname:=stringdup(ppufile.getstring);
  1288. toaddr :
  1289. begin
  1290. addroffset:=ppufile.getaword;
  1291. {$ifdef i386}
  1292. absseg:=boolean(ppufile.getbyte);
  1293. {$endif i386}
  1294. end;
  1295. end;
  1296. end;
  1297. procedure tabsolutevarsym.ppuwrite(ppufile:tcompilerppufile);
  1298. begin
  1299. inherited ppuwrite(ppufile);
  1300. ppufile.putbyte(byte(abstyp));
  1301. case abstyp of
  1302. tovar :
  1303. ppufile.putpropaccesslist(ref);
  1304. toasm :
  1305. ppufile.putstring(asmname^);
  1306. toaddr :
  1307. begin
  1308. ppufile.putaword(addroffset);
  1309. {$ifdef i386}
  1310. ppufile.putbyte(byte(absseg));
  1311. {$endif i386}
  1312. end;
  1313. end;
  1314. ppufile.writeentry(ibabsolutevarsym);
  1315. end;
  1316. procedure tabsolutevarsym.buildderef;
  1317. begin
  1318. inherited buildderef;
  1319. if (abstyp=tovar) then
  1320. ref.buildderef;
  1321. end;
  1322. procedure tabsolutevarsym.deref;
  1323. begin
  1324. inherited deref;
  1325. { own absolute deref }
  1326. if (abstyp=tovar) then
  1327. ref.resolve;
  1328. end;
  1329. function tabsolutevarsym.mangledname : string;
  1330. begin
  1331. case abstyp of
  1332. toasm :
  1333. mangledname:=asmname^;
  1334. toaddr :
  1335. mangledname:='$'+tostr(addroffset);
  1336. else
  1337. internalerror(200411062);
  1338. end;
  1339. end;
  1340. {****************************************************************************
  1341. TCONSTSYM
  1342. ****************************************************************************}
  1343. constructor tconstsym.create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  1344. begin
  1345. inherited create(constsym,n);
  1346. fillchar(value, sizeof(value), #0);
  1347. consttyp:=t;
  1348. value.valueord:=v;
  1349. constdef:=def;
  1350. end;
  1351. constructor tconstsym.create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  1352. begin
  1353. inherited create(constsym,n);
  1354. fillchar(value, sizeof(value), #0);
  1355. consttyp:=t;
  1356. value.valueordptr:=v;
  1357. constdef:=def;
  1358. end;
  1359. constructor tconstsym.create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  1360. begin
  1361. inherited create(constsym,n);
  1362. fillchar(value, sizeof(value), #0);
  1363. consttyp:=t;
  1364. value.valueptr:=v;
  1365. constdef:=def;
  1366. end;
  1367. constructor tconstsym.create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  1368. begin
  1369. inherited create(constsym,n);
  1370. fillchar(value, sizeof(value), #0);
  1371. consttyp:=t;
  1372. value.valueptr:=str;
  1373. constdef:=nil;
  1374. value.len:=l;
  1375. end;
  1376. constructor tconstsym.create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  1377. begin
  1378. inherited create(constsym,n);
  1379. fillchar(value, sizeof(value), #0);
  1380. consttyp:=t;
  1381. pcompilerwidestring(value.valueptr):=pw;
  1382. constdef:=nil;
  1383. value.len:=getlengthwidestring(pw);
  1384. end;
  1385. constructor tconstsym.ppuload(ppufile:tcompilerppufile);
  1386. var
  1387. pd : pbestreal;
  1388. ps : pnormalset;
  1389. pc : pchar;
  1390. pw : pcompilerwidestring;
  1391. i : longint;
  1392. begin
  1393. inherited ppuload(constsym,ppufile);
  1394. constdef:=nil;
  1395. consttyp:=tconsttyp(ppufile.getbyte);
  1396. fillchar(value, sizeof(value), #0);
  1397. case consttyp of
  1398. constord :
  1399. begin
  1400. ppufile.getderef(constdefderef);
  1401. value.valueord:=ppufile.getexprint;
  1402. end;
  1403. constpointer :
  1404. begin
  1405. ppufile.getderef(constdefderef);
  1406. value.valueordptr:=ppufile.getptruint;
  1407. end;
  1408. constwstring :
  1409. begin
  1410. initwidestring(pw);
  1411. setlengthwidestring(pw,ppufile.getlongint);
  1412. pw^.len:=pw^.maxlen;
  1413. { don't use getdata, because the compilerwidechars may have to
  1414. be byteswapped
  1415. }
  1416. {$if sizeof(tcompilerwidechar) = 2}
  1417. for i:=0 to pw^.len-1 do
  1418. pw^.data[i]:=ppufile.getword;
  1419. {$elseif sizeof(tcompilerwidechar) = 4}
  1420. for i:=0 to pw^.len-1 do
  1421. pw^.data[i]:=cardinal(ppufile.getlongint);
  1422. {$else}
  1423. {$error Unsupported tcompilerwidechar size}
  1424. {$endif}
  1425. pcompilerwidestring(value.valueptr):=pw;
  1426. end;
  1427. conststring,
  1428. constresourcestring :
  1429. begin
  1430. value.len:=ppufile.getlongint;
  1431. getmem(pc,value.len+1);
  1432. ppufile.getdata(pc^,value.len);
  1433. pc[value.len]:=#0;
  1434. value.valueptr:=pc;
  1435. end;
  1436. constreal :
  1437. begin
  1438. new(pd);
  1439. pd^:=ppufile.getreal;
  1440. value.valueptr:=pd;
  1441. end;
  1442. constset :
  1443. begin
  1444. ppufile.getderef(constdefderef);
  1445. new(ps);
  1446. ppufile.getnormalset(ps^);
  1447. value.valueptr:=ps;
  1448. end;
  1449. constguid :
  1450. begin
  1451. new(pguid(value.valueptr));
  1452. ppufile.getdata(value.valueptr^,sizeof(tguid));
  1453. end;
  1454. constnil : ;
  1455. else
  1456. Message1(unit_f_ppu_invalid_entry,tostr(ord(consttyp)));
  1457. end;
  1458. end;
  1459. destructor tconstsym.destroy;
  1460. begin
  1461. case consttyp of
  1462. conststring,
  1463. constresourcestring :
  1464. freemem(pchar(value.valueptr),value.len+1);
  1465. constwstring :
  1466. donewidestring(pcompilerwidestring(value.valueptr));
  1467. constreal :
  1468. dispose(pbestreal(value.valueptr));
  1469. constset :
  1470. dispose(pnormalset(value.valueptr));
  1471. constguid :
  1472. dispose(pguid(value.valueptr));
  1473. end;
  1474. inherited destroy;
  1475. end;
  1476. procedure tconstsym.buildderef;
  1477. begin
  1478. if consttyp in [constord,constpointer,constset] then
  1479. constdefderef.build(constdef);
  1480. end;
  1481. procedure tconstsym.deref;
  1482. begin
  1483. if consttyp in [constord,constpointer,constset] then
  1484. constdef:=tdef(constdefderef.resolve);
  1485. end;
  1486. procedure tconstsym.ppuwrite(ppufile:tcompilerppufile);
  1487. begin
  1488. inherited ppuwrite(ppufile);
  1489. ppufile.putbyte(byte(consttyp));
  1490. case consttyp of
  1491. constnil : ;
  1492. constord :
  1493. begin
  1494. ppufile.putderef(constdefderef);
  1495. ppufile.putexprint(value.valueord);
  1496. end;
  1497. constpointer :
  1498. begin
  1499. ppufile.putderef(constdefderef);
  1500. ppufile.putptruint(value.valueordptr);
  1501. end;
  1502. constwstring :
  1503. begin
  1504. ppufile.putlongint(getlengthwidestring(pcompilerwidestring(value.valueptr)));
  1505. ppufile.putdata(pcompilerwidestring(value.valueptr)^.data^,pcompilerwidestring(value.valueptr)^.len*sizeof(tcompilerwidechar));
  1506. end;
  1507. conststring,
  1508. constresourcestring :
  1509. begin
  1510. ppufile.putlongint(value.len);
  1511. ppufile.putdata(pchar(value.valueptr)^,value.len);
  1512. end;
  1513. constreal :
  1514. ppufile.putreal(pbestreal(value.valueptr)^);
  1515. constset :
  1516. begin
  1517. ppufile.putderef(constdefderef);
  1518. ppufile.putnormalset(value.valueptr^);
  1519. end;
  1520. constguid :
  1521. ppufile.putdata(value.valueptr^,sizeof(tguid));
  1522. else
  1523. internalerror(13);
  1524. end;
  1525. ppufile.writeentry(ibconstsym);
  1526. end;
  1527. {****************************************************************************
  1528. TENUMSYM
  1529. ****************************************************************************}
  1530. constructor tenumsym.create(const n : string;def : tenumdef;v : longint);
  1531. begin
  1532. inherited create(enumsym,n);
  1533. definition:=def;
  1534. value:=v;
  1535. { First entry? Then we need to set the minval }
  1536. if def.firstenum=nil then
  1537. begin
  1538. if v>0 then
  1539. def.has_jumps:=true;
  1540. def.setmin(v);
  1541. def.setmax(v);
  1542. end
  1543. else
  1544. begin
  1545. { check for jumps }
  1546. if v>def.max+1 then
  1547. def.has_jumps:=true;
  1548. { update low and high }
  1549. if def.min>v then
  1550. def.setmin(v);
  1551. if def.max<v then
  1552. def.setmax(v);
  1553. end;
  1554. order;
  1555. end;
  1556. constructor tenumsym.ppuload(ppufile:tcompilerppufile);
  1557. begin
  1558. inherited ppuload(enumsym,ppufile);
  1559. ppufile.getderef(definitionderef);
  1560. value:=ppufile.getlongint;
  1561. nextenum := Nil;
  1562. end;
  1563. procedure tenumsym.buildderef;
  1564. begin
  1565. definitionderef.build(definition);
  1566. end;
  1567. procedure tenumsym.deref;
  1568. begin
  1569. definition:=tenumdef(definitionderef.resolve);
  1570. order;
  1571. end;
  1572. procedure tenumsym.order;
  1573. var
  1574. sym : tenumsym;
  1575. begin
  1576. sym := tenumsym(definition.firstenum);
  1577. if sym = nil then
  1578. begin
  1579. definition.firstenum := self;
  1580. nextenum := nil;
  1581. exit;
  1582. end;
  1583. { reorder the symbols in increasing value }
  1584. if value < sym.value then
  1585. begin
  1586. nextenum := sym;
  1587. definition.firstenum := self;
  1588. end
  1589. else
  1590. begin
  1591. while (sym.value <= value) and assigned(sym.nextenum) do
  1592. sym := sym.nextenum;
  1593. nextenum := sym.nextenum;
  1594. sym.nextenum := self;
  1595. end;
  1596. end;
  1597. procedure tenumsym.ppuwrite(ppufile:tcompilerppufile);
  1598. begin
  1599. inherited ppuwrite(ppufile);
  1600. ppufile.putderef(definitionderef);
  1601. ppufile.putlongint(value);
  1602. ppufile.writeentry(ibenumsym);
  1603. end;
  1604. {****************************************************************************
  1605. TTYPESYM
  1606. ****************************************************************************}
  1607. constructor ttypesym.create(const n : string;def:tdef);
  1608. begin
  1609. inherited create(typesym,n);
  1610. typedef:=def;
  1611. { register the typesym for the definition }
  1612. if assigned(typedef) and
  1613. (typedef.typ<>errordef) and
  1614. not(assigned(typedef.typesym)) then
  1615. typedef.typesym:=self;
  1616. end;
  1617. constructor ttypesym.ppuload(ppufile:tcompilerppufile);
  1618. begin
  1619. inherited ppuload(typesym,ppufile);
  1620. ppufile.getderef(typedefderef);
  1621. end;
  1622. procedure ttypesym.buildderef;
  1623. begin
  1624. typedefderef.build(typedef);
  1625. end;
  1626. procedure ttypesym.deref;
  1627. begin
  1628. typedef:=tdef(typedefderef.resolve);
  1629. end;
  1630. procedure ttypesym.ppuwrite(ppufile:tcompilerppufile);
  1631. begin
  1632. inherited ppuwrite(ppufile);
  1633. ppufile.putderef(typedefderef);
  1634. ppufile.writeentry(ibtypesym);
  1635. end;
  1636. {****************************************************************************
  1637. TSYSSYM
  1638. ****************************************************************************}
  1639. constructor tsyssym.create(const n : string;l : longint);
  1640. begin
  1641. inherited create(syssym,n);
  1642. number:=l;
  1643. end;
  1644. constructor tsyssym.ppuload(ppufile:tcompilerppufile);
  1645. begin
  1646. inherited ppuload(syssym,ppufile);
  1647. number:=ppufile.getlongint;
  1648. end;
  1649. destructor tsyssym.destroy;
  1650. begin
  1651. inherited destroy;
  1652. end;
  1653. procedure tsyssym.ppuwrite(ppufile:tcompilerppufile);
  1654. begin
  1655. inherited ppuwrite(ppufile);
  1656. ppufile.putlongint(number);
  1657. ppufile.writeentry(ibsyssym);
  1658. end;
  1659. {*****************************************************************************
  1660. TMacro
  1661. *****************************************************************************}
  1662. constructor tmacro.create(const n : string);
  1663. begin
  1664. inherited create(macrosym,n);
  1665. owner:=nil;
  1666. defined:=false;
  1667. is_used:=false;
  1668. is_compiler_var:=false;
  1669. buftext:=nil;
  1670. buflen:=0;
  1671. end;
  1672. constructor tmacro.ppuload(ppufile:tcompilerppufile);
  1673. begin
  1674. inherited ppuload(macrosym,ppufile);
  1675. defined:=boolean(ppufile.getbyte);
  1676. is_compiler_var:=boolean(ppufile.getbyte);
  1677. is_used:=false;
  1678. buflen:= ppufile.getlongint;
  1679. if buflen > 0 then
  1680. begin
  1681. getmem(buftext, buflen);
  1682. ppufile.getdata(buftext^, buflen)
  1683. end
  1684. else
  1685. buftext:=nil;
  1686. end;
  1687. destructor tmacro.destroy;
  1688. begin
  1689. if assigned(buftext) then
  1690. freemem(buftext);
  1691. inherited destroy;
  1692. end;
  1693. procedure tmacro.ppuwrite(ppufile:tcompilerppufile);
  1694. begin
  1695. inherited ppuwrite(ppufile);
  1696. ppufile.putbyte(byte(defined));
  1697. ppufile.putbyte(byte(is_compiler_var));
  1698. ppufile.putlongint(buflen);
  1699. if buflen > 0 then
  1700. ppufile.putdata(buftext^,buflen);
  1701. ppufile.writeentry(ibmacrosym);
  1702. end;
  1703. function tmacro.GetCopy:tmacro;
  1704. var
  1705. p : tmacro;
  1706. begin
  1707. p:=tmacro.create(realname);
  1708. p.defined:=defined;
  1709. p.is_used:=is_used;
  1710. p.is_compiler_var:=is_compiler_var;
  1711. p.buflen:=buflen;
  1712. if assigned(buftext) then
  1713. begin
  1714. getmem(p.buftext,buflen);
  1715. move(buftext^,p.buftext^,buflen);
  1716. end;
  1717. Result:=p;
  1718. end;
  1719. end.