symsym.pas 59 KB

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