symsym.pas 59 KB

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