pmodules.pas 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005
  1. {
  2. Copyright (c) 1998-2008 by Florian Klaempfl
  3. Handles the parsing and loading of the modules (ppufiles)
  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 pmodules;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses fmodule;
  21. function proc_unit(curr: tmodule):boolean;
  22. function parse_unit_interface_declarations(curr : tmodule) : boolean;
  23. function proc_unit_implementation(curr: tmodule):boolean;
  24. function proc_package(curr: tmodule) : boolean;
  25. function proc_program(curr: tmodule; islibrary : boolean) : boolean;
  26. function proc_program_declarations(curr : tmodule; islibrary : boolean) : boolean;
  27. function finish_compile_unit(module:tmodule): boolean;
  28. function finish_unit(module:tmodule): boolean;
  29. implementation
  30. uses
  31. SysUtils,
  32. globtype,systems,tokens,
  33. cutils,cfileutl,cclasses,comphook,
  34. globals,verbose,finput,fppu,globstat,fpcp,fpkg,
  35. symconst,symbase,symtype,symdef,symsym,symtable,defutil,symcreat,
  36. wpoinfo,
  37. aasmtai,aasmdata,aasmbase,aasmcpu,
  38. cgbase,ngenutil,
  39. nbas,nutils,ncgutil,
  40. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  41. cresstr,procinfo,
  42. objcgutl,
  43. pkgutil,
  44. wpobase,
  45. scanner,pbase,pexpr,psystem,psub,pgenutil,pparautl,ncgvmt,ncgrtti,
  46. ctask,
  47. cpuinfo;
  48. procedure create_objectfile(curr : tmodule);
  49. var
  50. DLLScanner : TDLLScanner;
  51. s : string;
  52. KeepShared : TCmdStrList;
  53. begin
  54. { try to create import entries from system dlls }
  55. if (tf_has_dllscanner in target_info.flags) and
  56. (not curr.linkOtherSharedLibs.Empty) then
  57. begin
  58. { Init DLLScanner }
  59. if assigned(CDLLScanner[target_info.system]) then
  60. DLLScanner:=CDLLScanner[target_info.system].Create
  61. else
  62. internalerror(200104121);
  63. KeepShared:=TCmdStrList.Create;
  64. { Walk all shared libs }
  65. While not curr.linkOtherSharedLibs.Empty do
  66. begin
  67. S:=curr.linkOtherSharedLibs.Getusemask(link_always);
  68. if not DLLScanner.scan(s) then
  69. KeepShared.Concat(s);
  70. end;
  71. DLLscanner.Free;
  72. { Recreate import section }
  73. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  74. begin
  75. if assigned(current_asmdata.asmlists[al_imports]) then
  76. current_asmdata.asmlists[al_imports].clear
  77. else
  78. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  79. importlib.generatelib;
  80. end;
  81. { Readd the not processed files }
  82. while not KeepShared.Empty do
  83. begin
  84. s:=KeepShared.GetFirst;
  85. curr.linkOtherSharedLibs.add(s,link_always);
  86. end;
  87. KeepShared.Free;
  88. end;
  89. { allow a target-specific pass over all assembler code (used by LLVM
  90. to insert type definitions }
  91. cnodeutils.InsertObjectInfo;
  92. { Start and end module debuginfo, at least required for stabs
  93. to insert n_sourcefile lines }
  94. if (cs_debuginfo in current_settings.moduleswitches) or
  95. (cs_use_lineinfo in current_settings.globalswitches) then
  96. current_debuginfo.insertmoduleinfo;
  97. { create the .s file and assemble it }
  98. if not(create_smartlink_library) or not(tf_no_objectfiles_when_smartlinking in target_info.flags) then
  99. GenerateAsm(false);
  100. { Also create a smartlinked version ? }
  101. if create_smartlink_library then
  102. begin
  103. GenerateAsm(true);
  104. if (af_needar in target_asm.flags) then
  105. Linker.MakeStaticLibrary;
  106. end;
  107. { resource files }
  108. CompileResourceFiles;
  109. end;
  110. procedure insertobjectfile(curr : tmodule);
  111. { Insert the used object file for this unit in the used list for this unit }
  112. begin
  113. curr.linkunitofiles.add(curr.objfilename,link_static);
  114. curr.headerflags:=curr.headerflags or uf_static_linked;
  115. if create_smartlink_library then
  116. begin
  117. curr.linkunitstaticlibs.add(curr.staticlibfilename ,link_smart);
  118. curr.headerflags:=curr.headerflags or uf_smart_linked;
  119. end;
  120. if cs_lto in current_settings.moduleswitches then
  121. begin
  122. curr.linkunitofiles.add(ChangeFileExt(curr.objfilename,LTOExt),link_lto);
  123. curr.headerflags:=curr.headerflags or uf_lto_linked;
  124. end;
  125. end;
  126. procedure create_dwarf_frame;
  127. begin
  128. { Dwarf conflicts with smartlinking in separate .a files }
  129. if create_smartlink_library then
  130. exit;
  131. { Call frame information }
  132. { MWE: we write our own info, so dwarf asm support is not really needed }
  133. { if (af_supports_dwarf in target_asm.flags) and }
  134. { CFI is currently broken for Darwin }
  135. if not(target_info.system in systems_darwin) and
  136. (
  137. (tf_needs_dwarf_cfi in target_info.flags) or
  138. (target_dbg.id in [dbg_dwarf2, dbg_dwarf3])
  139. ) then
  140. begin
  141. current_asmdata.asmlists[al_dwarf_frame].Free;
  142. current_asmdata.asmlists[al_dwarf_frame] := TAsmList.create;
  143. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf_frame]);
  144. end;
  145. end;
  146. Function CheckResourcesUsed(curr : tmodule) : boolean;
  147. var
  148. hp : tused_unit;
  149. found : Boolean;
  150. begin
  151. CheckResourcesUsed:=tf_has_winlike_resources in target_info.flags;
  152. if not CheckResourcesUsed then exit;
  153. hp:=tused_unit(usedunits.first);
  154. found:=mf_has_resourcefiles in curr.moduleflags;
  155. while Assigned(hp) and not found do
  156. begin
  157. found:=mf_has_resourcefiles in hp.u.moduleflags;
  158. hp:=tused_unit(hp.next);
  159. end;
  160. CheckResourcesUsed:=found;
  161. end;
  162. function AddUnit(curr : tmodule; const s:string;addasused:boolean): tppumodule;
  163. var
  164. hp : tppumodule;
  165. unitsym : tunitsym;
  166. isnew,load_ok : boolean;
  167. begin
  168. { load unit }
  169. hp:=registerunit(curr,s,'',isnew);
  170. if isnew then
  171. usedunits.concat(tused_unit.create(hp,true,addasused,nil));
  172. load_ok:=hp.loadppu(curr);
  173. hp.adddependency(curr,curr.in_interface);
  174. if not load_ok then
  175. { We must schedule a compile. }
  176. task_handler.addmodule(hp);
  177. { add to symtable stack }
  178. if assigned(hp.globalsymtable) then
  179. symtablestack.push(hp.globalsymtable);
  180. if (m_mac in current_settings.modeswitches) and
  181. assigned(hp.globalmacrosymtable) then
  182. macrosymtablestack.push(hp.globalmacrosymtable);
  183. { insert unitsym }
  184. unitsym:=cunitsym.create(hp.modulename^,hp);
  185. inc(unitsym.refs);
  186. tabstractunitsymtable(curr.localsymtable).insertunit(unitsym);
  187. if addasused then
  188. { add to used units }
  189. curr.addusedunit(hp,false,unitsym);
  190. result:=hp;
  191. end;
  192. function AddUnit(curr :tmodule; const s:string):tppumodule;
  193. begin
  194. result:=AddUnit(curr,s,true);
  195. end;
  196. function maybeloadvariantsunit(curr : tmodule) : boolean;
  197. var
  198. hp : tmodule;
  199. addsystemnamespace : Boolean;
  200. begin
  201. result:=true;
  202. { Do we need the variants unit? Skip this
  203. for VarUtils unit for bootstrapping }
  204. if not(mf_uses_variants in curr.moduleflags) or
  205. (curr.modulename^='VARUTILS') or
  206. (curr.modulename^='SYSTEM.VARUTILS') then
  207. exit;
  208. { Variants unit already loaded? }
  209. hp:=tmodule(loaded_units.first);
  210. while assigned(hp) do
  211. begin
  212. if (hp.modulename^='VARIANTS') or (hp.modulename^='SYSTEM.VARIANTS') then
  213. exit;
  214. hp:=tmodule(hp.next);
  215. end;
  216. { Variants unit is not loaded yet, load it now }
  217. Message(parser_w_implicit_uses_of_variants_unit);
  218. addsystemnamespace:=namespacelist.Find('System')=Nil;
  219. if addsystemnamespace then
  220. namespacelist.concat('System');
  221. result:=AddUnit(curr,'variants').state in [ms_compiled,ms_processed];
  222. if addsystemnamespace then
  223. namespacelist.Remove('System');
  224. end;
  225. function MaybeRemoveResUnit(curr : tmodule) : boolean;
  226. var
  227. resources_used : boolean;
  228. hp : tmodule;
  229. uu : tused_unit;
  230. unitname : shortstring;
  231. begin
  232. { We simply remove the unit from:
  233. - usedunit list, so that things like init/finalization table won't
  234. contain references to this unit
  235. - loaded_units list, so that the unit object file doesn't get linked
  236. with the executable. }
  237. { Note: on windows we always need resources! }
  238. resources_used:=(target_info.system in systems_all_windows)
  239. or CheckResourcesUsed(curr);
  240. if (not resources_used) and (tf_has_winlike_resources in target_info.flags) then
  241. begin
  242. { resources aren't used, so we don't need this unit }
  243. if target_res.id=res_ext then
  244. unitname:='FPEXTRES'
  245. else
  246. unitname:='FPINTRES';
  247. Message1(unit_u_unload_resunit,unitname);
  248. { find the module }
  249. hp:=tmodule(loaded_units.first);
  250. while assigned(hp) do
  251. begin
  252. if hp.is_unit and (hp.modulename^=unitname) then break;
  253. hp:=tmodule(hp.next);
  254. end;
  255. if not assigned(hp) then
  256. internalerror(200801071);
  257. { find its tused_unit in the global list }
  258. uu:=tused_unit(usedunits.first);
  259. while assigned(uu) do
  260. begin
  261. if uu.u=hp then break;
  262. uu:=tused_unit(uu.next);
  263. end;
  264. if not assigned(uu) then
  265. internalerror(200801072);
  266. { remove the tused_unit }
  267. usedunits.Remove(uu);
  268. uu.Free;
  269. // Remove from local list
  270. uu:=tused_unit(curr.used_units.first);
  271. while assigned(uu) do
  272. begin
  273. if uu.u=hp then break;
  274. uu:=tused_unit(uu.next);
  275. end;
  276. if not assigned(uu) then
  277. internalerror(2024020701);
  278. curr.used_units.Remove(uu);
  279. uu.Free;
  280. { remove the module }
  281. loaded_units.Remove(hp);
  282. unloaded_units.Concat(hp);
  283. end;
  284. MaybeRemoveResUnit:=resources_used;
  285. end;
  286. function loadsystemunit(curr : tmodule) : boolean;
  287. var
  288. state: tglobalstate;
  289. sys : tmodule;
  290. begin
  291. Result:=False;
  292. { we are going to rebuild the symtablestack, clear it first }
  293. symtablestack.clear;
  294. macrosymtablestack.clear;
  295. { macro symtable }
  296. macrosymtablestack.push(initialmacrosymtable);
  297. { are we compiling the system unit? }
  298. if (cs_compilesystem in current_settings.moduleswitches) then
  299. begin
  300. systemunit:=tglobalsymtable(curr.localsymtable);
  301. { create system defines }
  302. create_intern_types;
  303. create_intern_symbols;
  304. { Set the owner of errorsym and errortype to symtable to
  305. prevent crashes when accessing .owner }
  306. generrorsym.owner:=systemunit;
  307. generrordef.owner:=systemunit;
  308. exit;
  309. end;
  310. { insert the system unit, it is allways the first. Load also the
  311. internal types from the system unit }
  312. Sys:=AddUnit(curr,'system');
  313. Result:=Assigned(Sys) and (Sys.State in [ms_processed,ms_compiled]);
  314. systemunit:=tglobalsymtable(symtablestack.top);
  315. { load_intern_types resets the scanner... }
  316. current_scanner.tempcloseinputfile;
  317. state:=tglobalstate.create(true);
  318. load_intern_types;
  319. state.restore(true);
  320. FreeAndNil(state);
  321. current_scanner.tempopeninputfile;
  322. { Set the owner of errorsym and errortype to symtable to
  323. prevent crashes when accessing .owner }
  324. generrorsym.owner:=systemunit;
  325. generrordef.owner:=systemunit;
  326. // Implicitly enable unicode strings in unicode RTL in modes objfpc/delphi.
  327. { TODO: Check if we should also do this for mode macpas }
  328. if not (cs_compilesystem in current_settings.moduleswitches) then
  329. if ([m_objfpc,m_delphi] * current_settings.modeswitches)<>[] then
  330. if is_systemunit_unicode then
  331. Include(current_settings.modeswitches,m_default_unicodestring);
  332. { default the extended RTTI options to that of TObject }
  333. if assigned(class_tobject) then
  334. current_module.rtti_directive.options:=class_tobject.rtti.options;
  335. end;
  336. { Return true if all units were loaded, no recompilation needed. }
  337. function loaddefaultunits(curr :tmodule) : boolean;
  338. Procedure CheckAddUnit(s: string);
  339. var
  340. OK : boolean;
  341. m : TModule;
  342. begin
  343. m:=AddUnit(curr,s,true);
  344. OK:=assigned(m) and (m.state in [ms_processed,ms_compiled]);
  345. if not ok then
  346. Message2(unit_f_cant_find_ppu,s,curr.realmodulename^);
  347. Result:=ok and Result;
  348. end;
  349. begin
  350. Result:=True;
  351. { Units only required for main module }
  352. if not(curr.is_unit) then
  353. begin
  354. { Heaptrc unit, load heaptrace before any other units especially objpas }
  355. if (cs_use_heaptrc in current_settings.globalswitches) then
  356. CheckAddUnit('heaptrc');
  357. { Valgrind requires c memory manager }
  358. if (cs_gdb_valgrind in current_settings.globalswitches) or
  359. (([cs_sanitize_address]*current_settings.moduleswitches)<>[]) then
  360. CheckAddUnit('cmem');
  361. { Lineinfo unit }
  362. if (cs_use_lineinfo in current_settings.globalswitches) then begin
  363. case target_dbg.id of
  364. dbg_stabs:
  365. CheckAddUnit('lineinfo');
  366. dbg_stabx:
  367. CheckAddUnit('lnfogdb');
  368. else
  369. CheckAddUnit('lnfodwrf');
  370. end;
  371. end;
  372. {$ifdef cpufpemu}
  373. { Floating point emulation unit?
  374. softfpu must be in the system unit anyways (FK)
  375. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  376. CheckAddUnit('softfpu');
  377. }
  378. {$endif cpufpemu}
  379. { Which kind of resource support?
  380. Note: if resources aren't used this unit will be removed later,
  381. otherwise we need it here since it must be loaded quite early }
  382. if (tf_has_winlike_resources in target_info.flags) then
  383. if target_res.id=res_ext then
  384. CheckAddUnit('fpextres')
  385. else
  386. CheckAddUnit('fpintres');
  387. end
  388. else if (cs_checkpointer in current_settings.localswitches) then
  389. CheckAddUnit('heaptrc');
  390. { Objpas unit? }
  391. if m_objpas in current_settings.modeswitches then
  392. CheckAddUnit('objpas');
  393. { Macpas unit? }
  394. if m_mac in current_settings.modeswitches then
  395. CheckAddUnit('macpas');
  396. if m_iso in current_settings.modeswitches then
  397. CheckAddUnit('iso7185');
  398. if m_extpas in current_settings.modeswitches then
  399. begin
  400. { basic procedures for Extended Pascal are for now provided by the iso unit }
  401. CheckAddUnit('iso7185');
  402. CheckAddUnit('extpas');
  403. end;
  404. { blocks support? }
  405. if m_blocks in current_settings.modeswitches then
  406. CheckAddUnit('blockrtl');
  407. { Determine char size. }
  408. // Ansi RTL ?
  409. if not is_systemunit_unicode then
  410. begin
  411. if m_default_unicodestring in current_settings.modeswitches then
  412. CheckAddUnit('uuchar'); // redefines char as widechar
  413. end
  414. else
  415. begin
  416. // Unicode RTL
  417. if not (m_default_ansistring in current_settings.modeswitches) then
  418. if not (curr.modulename^<>'UACHAR') then
  419. CheckAddUnit('uachar'); // redefines char as ansichar
  420. end;
  421. { Objective-C support unit? }
  422. if (m_objectivec1 in current_settings.modeswitches) then
  423. begin
  424. { interface to Objective-C run time }
  425. CheckAddUnit('objc');
  426. loadobjctypes;
  427. { NSObject }
  428. if not(curr.is_unit) or
  429. (curr.modulename^<>'OBJCBASE') then
  430. CheckAddUnit('objcbase');
  431. end;
  432. { Profile unit? Needed for go32v2 only }
  433. if (cs_profile in current_settings.moduleswitches) and
  434. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  435. CheckAddUnit('profile');
  436. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  437. begin
  438. CheckAddUnit('fpcylix');
  439. CheckAddUnit('dynlibs');
  440. end;
  441. {$push}
  442. {$warn 6018 off} { Unreachable code due to compile time evaluation }
  443. { CPU targets with microcontroller support can add a controller specific unit }
  444. if ControllerSupport and (target_info.system in (systems_embedded+systems_freertos)) and
  445. (current_settings.controllertype<>ct_none) and
  446. (embedded_controllers[current_settings.controllertype].controllerunitstr<>'') and
  447. (embedded_controllers[current_settings.controllertype].controllerunitstr<>curr.modulename^) then
  448. CheckAddUnit(embedded_controllers[current_settings.controllertype].controllerunitstr);
  449. {$pop}
  450. {$ifdef XTENSA}
  451. if not(curr.is_unit) and (target_info.system=system_xtensa_freertos) then
  452. if (current_settings.controllertype=ct_esp32) then
  453. begin
  454. if (idf_version>=40100) and (idf_version<40200) then
  455. CheckAddUnit('espidf_40100')
  456. else if (idf_version>=40200) and (idf_version<40400) then
  457. CheckAddUnit('espidf_40200')
  458. else if (idf_version>=40400) and (idf_version<50000) then
  459. CheckAddUnit('espidf_40400')
  460. else if (idf_version>=50000) and (idf_version<50200) then
  461. CheckAddUnit('espidf_50000')
  462. else if idf_version>=50200 then
  463. CheckAddUnit('espidf_50200')
  464. else
  465. Comment(V_Warning, 'Unsupported esp-idf version');
  466. end
  467. else if (current_settings.controllertype=ct_esp32s2) or (current_settings.controllertype=ct_esp32s3) then
  468. begin
  469. if (idf_version>=40400) and (idf_version<50000) then
  470. CheckAddUnit('espidf_40400')
  471. else if (idf_version>=50000) and (idf_version<50200) then
  472. CheckAddUnit('espidf_50000')
  473. else if idf_version>=50200 then
  474. CheckAddUnit('espidf_50200')
  475. else
  476. Message(unit_w_unsupported_esp_idf_version);
  477. end
  478. else if (current_settings.controllertype=ct_esp8266) then
  479. begin
  480. if (idf_version>=30300) and (idf_version<30400) then
  481. CheckAddUnit('esp8266rtos_30300')
  482. else if idf_version>=30400 then
  483. CheckAddUnit('esp8266rtos_30400')
  484. else
  485. Message(unit_w_unsupported_esp_idf_version);
  486. end;
  487. {$endif XTENSA}
  488. {$ifdef RISCV32}
  489. if not(curr.is_unit) and (target_info.system=system_riscv32_freertos) then
  490. if (current_settings.controllertype=ct_esp32c2) then
  491. begin
  492. if idf_version>=50200 then
  493. CheckAddUnit('esp32c2idf_50200')
  494. else if idf_version>=50000 then
  495. CheckAddUnit('esp32c2idf_50000')
  496. else if idf_version>=40400 then
  497. CheckAddUnit('esp32c2idf_40400')
  498. else
  499. Comment(V_Warning, 'Unsupported esp-idf version');
  500. end;
  501. if (current_settings.controllertype=ct_esp32c3) then
  502. begin
  503. if idf_version>=50200 then
  504. CheckAddUnit('esp32c3idf_50200')
  505. else if idf_version>=50000 then
  506. CheckAddUnit('esp32c3idf_50000')
  507. else if idf_version>=40400 then
  508. CheckAddUnit('esp32c3idf_40400')
  509. else
  510. Message(unit_w_unsupported_esp_idf_version);
  511. end;
  512. if (current_settings.controllertype=ct_esp32c6) then
  513. begin
  514. if idf_version>=50200 then
  515. CheckAddUnit('esp32c6idf_50200')
  516. else
  517. Comment(V_Warning, 'Unsupported esp-idf version');
  518. end;
  519. {$endif RISCV32}
  520. end;
  521. { Return true if all units were loaded, no recompilation needed. }
  522. function loadautounits(curr: tmodule) : boolean;
  523. Procedure CheckAddUnit(s: string);
  524. var
  525. OK : boolean;
  526. m : TModule;
  527. begin
  528. m:=AddUnit(curr,s,true);
  529. OK:=assigned(m) and (m.state in [ms_compiled,ms_processed]);
  530. Result:=ok and Result;
  531. end;
  532. var
  533. hs,s : string;
  534. begin
  535. Result:=True;
  536. hs:=autoloadunits;
  537. repeat
  538. s:=GetToken(hs,',');
  539. if s='' then
  540. break;
  541. CheckAddUnit(s);
  542. until false;
  543. end;
  544. procedure parseusesclause(curr: tmodule);
  545. var
  546. s,sorg : ansistring;
  547. fn : string;
  548. pu : tused_unit;
  549. hp2 : tmodule;
  550. unitsym : tunitsym;
  551. filepos : tfileposinfo;
  552. isnew : boolean;
  553. begin
  554. consume(_USES);
  555. repeat
  556. s:=pattern;
  557. sorg:=orgpattern;
  558. filepos:=current_tokenpos;
  559. consume(_ID);
  560. while token=_POINT do
  561. begin
  562. consume(_POINT);
  563. s:=s+'.'+pattern;
  564. sorg:=sorg+'.'+orgpattern;
  565. consume(_ID);
  566. end;
  567. { support "<unit> in '<file>'" construct, but not for tp7 }
  568. fn:='';
  569. if not(m_tp7 in current_settings.modeswitches) and
  570. try_to_consume(_OP_IN) then
  571. fn:=FixFileName(get_stringconst);
  572. { Give a warning if lineinfo is loaded }
  573. if s='LINEINFO' then
  574. begin
  575. Message(parser_w_no_lineinfo_use_switch);
  576. if (target_dbg.id in [dbg_dwarf2, dbg_dwarf3]) then
  577. s := 'LNFODWRF';
  578. sorg := s;
  579. end;
  580. { Give a warning if objpas is loaded }
  581. if s='OBJPAS' then
  582. Message(parser_w_no_objpas_use_mode);
  583. { Using the unit itself is not possible }
  584. if (s<>curr.modulename^) then
  585. begin
  586. { check if the unit is already used }
  587. hp2:=nil;
  588. pu:=tused_unit(curr.used_units.first);
  589. while assigned(pu) do
  590. begin
  591. if (pu.u.modulename^=s) then
  592. begin
  593. hp2:=pu.u;
  594. break;
  595. end;
  596. pu:=tused_unit(pu.next);
  597. end;
  598. if not assigned(hp2) then
  599. begin
  600. hp2:=registerunit(curr,sorg,fn,isnew);
  601. if isnew then
  602. usedunits.concat(tused_unit.create(hp2,curr.in_interface,true,nil));
  603. end
  604. else
  605. Message1(sym_e_duplicate_id,s);
  606. { Create unitsym, we need to use the name as specified, we
  607. can not use the modulename because that can be different
  608. when -Un is used }
  609. current_tokenpos:=filepos;
  610. unitsym:=cunitsym.create(sorg,nil);
  611. { the current module uses the unit hp2 }
  612. curr.addusedunit(hp2,true,unitsym);
  613. end
  614. else
  615. Message1(sym_e_duplicate_id,s);
  616. if token=_COMMA then
  617. begin
  618. pattern:='';
  619. consume(_COMMA);
  620. end
  621. else
  622. break;
  623. until false;
  624. end;
  625. function loadunits(curr: tmodule; frominterface : boolean) : boolean;
  626. var
  627. s : ansistring;
  628. pu : tused_unit;
  629. state: tglobalstate;
  630. isLoaded : Boolean;
  631. mwait : tmodule;
  632. lu : tmodule;
  633. procedure restorestate;
  634. begin
  635. state.restore(true);
  636. if assigned(current_scanner) and (current_module.scanner=current_scanner) then
  637. begin
  638. if assigned(current_scanner.inputfile) then
  639. current_scanner.tempopeninputfile;
  640. end;
  641. state.free;
  642. end;
  643. begin
  644. Result:=true;
  645. mwait:=nil;
  646. current_scanner.tempcloseinputfile;
  647. state:=tglobalstate.create(true);
  648. { Load the units }
  649. pu:=tused_unit(curr.used_units.first);
  650. while assigned(pu) do
  651. begin
  652. lu:=pu.u;
  653. { Only load the units that are in the current
  654. (interface/implementation) uses clause }
  655. if pu.in_uses and
  656. (pu.in_interface=frominterface) then
  657. begin
  658. if (lu.state in [ms_processed, ms_compiled,ms_compiling_waitimpl]) then
  659. isLoaded:=true
  660. else if (lu.state=ms_registered) then
  661. // try to load
  662. isLoaded:=tppumodule(lu).loadppu(curr)
  663. else
  664. isLoaded:=False;
  665. isLoaded:=IsLoaded and not lu.is_reset ;
  666. if not IsLoaded then
  667. begin
  668. if mwait=nil then
  669. mwait:=lu;
  670. // In case of is_reset, the task handler will discard the state if the module was already there
  671. task_handler.addmodule(lu);
  672. end;
  673. IsLoaded:=Isloaded and not curr.is_reset;
  674. Result:=Result and IsLoaded;
  675. { If we were reset, then used_units is no longer correct, and we must exit at once. }
  676. if curr.is_reset then
  677. break;
  678. { is our module compiled? then we can stop }
  679. if curr.state in [ms_compiled,ms_processed] then
  680. break;
  681. { add this unit to the dependencies }
  682. lu.adddependency(curr,frominterface);
  683. { check hints }
  684. pu.check_hints;
  685. end;
  686. pu:=tused_unit(pu.next);
  687. end;
  688. Restorestate;
  689. end;
  690. {
  691. Connect loaded units: check crc and add to symbol tables.
  692. this can only be called after all units were actually loaded!
  693. }
  694. procedure connect_loaded_units(_module : tmodule; preservest:tsymtable);
  695. var
  696. pu : tused_unit;
  697. sorg : ansistring;
  698. unitsymtable: tabstractunitsymtable;
  699. begin
  700. // writeln(_module.get_modulename,': Connecting units');
  701. pu:=tused_unit(_module.used_units.first);
  702. while assigned(pu) do
  703. begin
  704. {
  705. Writeln('Connect : ',Assigned(_module.modulename), ' ', assigned(pu.u), ' ' ,assigned(pu.u.modulename));
  706. if assigned(pu.u) then
  707. begin
  708. if assigned(pu.u.modulename) then
  709. Writeln(_module.modulename^,': Examining connect of file ',pu._fn,' (',pu.u.modulename^,')')
  710. else
  711. Writeln(_module.modulename^,': Examining connect of file ',pu._fn);
  712. end
  713. else
  714. Writeln(_module.modulename^,': Examining unit without module... ');
  715. }
  716. if not (pu.in_uses and
  717. (pu.in_interface=_module.in_interface)) then
  718. begin
  719. // writeln('Must not connect ',pu.u.modulename^,' (pu.in_interface: ',pu.in_interface,' <> module.in_interface',_module.in_interface,')');
  720. end
  721. else
  722. begin
  723. // writeln('Must connect ',pu.u.modulename^,'(sym: ',pu.unitsym.realname,')');
  724. { save crc values }
  725. pu.checksum:=pu.u.crc;
  726. pu.interface_checksum:=pu.u.interface_crc;
  727. pu.indirect_checksum:=pu.u.indirect_crc;
  728. if tppumodule(pu.u).nsprefix<>'' then
  729. begin
  730. { use the name as declared in the uses section for -Un }
  731. sorg:=tppumodule(pu.u).nsprefix+'.'+pu.unitsym.realname;
  732. { update unitsym now that we have access to the full name }
  733. pu.unitsym.free;
  734. pu.unitsym:=cunitsym.create(sorg,pu.u);
  735. end
  736. else
  737. begin
  738. { connect unitsym to the module }
  739. pu.unitsym.module:=pu.u;
  740. pu.unitsym.register_sym;
  741. end;
  742. {
  743. Add the unit symbol in the current symtable.
  744. localsymtable will be nil after the interface uses clause is parsed and the local symtable
  745. is moved to the global.
  746. }
  747. if assigned(_module.localsymtable) then
  748. unitsymtable:=tabstractunitsymtable(_module.localsymtable)
  749. else
  750. unitsymtable:=tabstractunitsymtable(_module.globalsymtable);
  751. // Writeln('Adding used unit sym ',pu.unitsym.realName,' to table ',unitsymtable.get_name);
  752. unitsymtable.insertunit(pu.unitsym);
  753. { add to symtable stack }
  754. // Writeln('Adding used unit symtable ',pu.u.globalsymtable.name^,' (',pu.u.globalsymtable.DefList.Count, ' defs) to stack');
  755. if assigned(preservest) then
  756. symtablestack.pushafter(pu.u.globalsymtable,preservest)
  757. else
  758. symtablestack.push(pu.u.globalsymtable);
  759. if (m_mac in current_settings.modeswitches) and
  760. assigned(pu.u.globalmacrosymtable) then
  761. macrosymtablestack.push(pu.u.globalmacrosymtable);
  762. end;
  763. pu:=tused_unit(pu.next);
  764. end;
  765. // writeln(_module.get_modulename,': Done Connecting units');
  766. end;
  767. procedure reset_all_defs(curr: tmodule);
  768. begin
  769. if assigned(curr.wpoinfo) then
  770. curr.wpoinfo.resetdefs;
  771. end;
  772. procedure free_localsymtables(st:TSymtable);
  773. var
  774. i : longint;
  775. def : tstoreddef;
  776. pd : tprocdef;
  777. begin
  778. for i:=0 to st.DefList.Count-1 do
  779. begin
  780. def:=tstoreddef(st.DefList[i]);
  781. if def.typ=procdef then
  782. begin
  783. pd:=tprocdef(def);
  784. if assigned(pd.localst) and
  785. (pd.localst.symtabletype<>staticsymtable) and
  786. not(po_inline in pd.procoptions) then
  787. begin
  788. free_localsymtables(pd.localst);
  789. pd.localst.free;
  790. pd.localst:=nil;
  791. end;
  792. pd.freeimplprocdefinfo;
  793. pd.done_paraloc_info(calleeside);
  794. end;
  795. end;
  796. end;
  797. procedure free_unregistered_localsymtable_elements(curr : tmodule);
  798. procedure remove_from_procdeflist(adef: tdef);
  799. var
  800. i: Integer;
  801. childdef: tdef;
  802. begin
  803. if adef=nil then exit;
  804. if (adef.typ in [objectdef, recorddef]) and (adef is tabstractrecorddef) then
  805. begin
  806. if tabstractrecorddef(adef).symtable<>nil then
  807. for i:=0 to tabstractrecorddef(adef).symtable.DefList.Count-1 do
  808. begin
  809. childdef:=tdef(tabstractrecorddef(adef).symtable.DefList[i]);
  810. remove_from_procdeflist(childdef);
  811. end;
  812. end
  813. else
  814. if adef.typ=procdef then
  815. begin
  816. tprocsym(tprocdef(adef).procsym).ProcdefList.Remove(adef);
  817. if tprocdef(adef).localst<>nil then
  818. for i:=0 to tprocdef(adef).localst.DefList.Count-1 do
  819. begin
  820. childdef:=tdef(tprocdef(adef).localst.DefList[i]);
  821. remove_from_procdeflist(childdef);
  822. end;
  823. end;
  824. end;
  825. var
  826. i: longint;
  827. def: tdef;
  828. sym: tsym;
  829. begin
  830. for i:=curr.localsymtable.deflist.count-1 downto 0 do
  831. begin
  832. def:=tdef(curr.localsymtable.deflist[i]);
  833. { since commit 48986 deflist might have NIL entries }
  834. if not assigned(def) then
  835. continue;
  836. { this also frees def, as the defs are owned by the symtable }
  837. if not def.is_registered and
  838. not(df_not_registered_no_free in def.defoptions) then
  839. begin
  840. { if it's a procdef, unregister it from its procsym first,
  841. unless that sym hasn't been registered either (it's possible
  842. to have one overload in the interface and another in the
  843. implementation) }
  844. remove_from_procdeflist(def);
  845. curr.localsymtable.deletedef(def);
  846. end;
  847. end;
  848. { from high to low so we hopefully have moves of less data }
  849. for i:=curr.localsymtable.symlist.count-1 downto 0 do
  850. begin
  851. sym:=tsym(curr.localsymtable.symlist[i]);
  852. { this also frees sym, as the symbols are owned by the symtable }
  853. if not sym.is_registered then
  854. curr.localsymtable.DeleteSym(sym);
  855. end;
  856. end;
  857. procedure setupglobalswitches;
  858. begin
  859. if (cs_create_pic in current_settings.moduleswitches) then
  860. begin
  861. def_system_macro('FPC_PIC');
  862. def_system_macro('PIC');
  863. end;
  864. end;
  865. function create_main_proc(const name:TSymStr;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  866. var
  867. ps : tprocsym;
  868. pd : tprocdef;
  869. begin
  870. { there should be no current_procinfo available }
  871. if assigned(current_procinfo) then
  872. internalerror(200304275);
  873. {Generate a procsym for main}
  874. ps:=cprocsym.create('$'+name);
  875. { always register the symbol }
  876. ps.register_sym;
  877. { main are allways used }
  878. inc(ps.refs);
  879. st.insertsym(ps);
  880. pd:=tprocdef(cnodeutils.create_main_procdef(target_info.cprefix+name,potype,ps));
  881. { We don't need a local symtable, change it into the static symtable }
  882. if not (potype in [potype_mainstub,potype_pkgstub,potype_libmainstub]) then
  883. begin
  884. pd.localst.free;
  885. pd.localst:=st;
  886. end
  887. else if (potype=potype_pkgstub) and
  888. (target_info.system in systems_all_windows+systems_nativent) then
  889. pd.proccalloption:=pocall_stdcall
  890. else
  891. pd.proccalloption:=pocall_cdecl;
  892. handle_calling_convention(pd,hcc_default_actions_impl);
  893. { set procinfo and current_procinfo.procdef }
  894. result:=tcgprocinfo(cprocinfo.create(nil));
  895. result.procdef:=pd;
  896. { main proc does always a call e.g. to init system unit }
  897. if potype<>potype_pkgstub then
  898. include(result.flags,pi_do_call);
  899. end;
  900. procedure release_main_proc(curr: tmodule; pi:tcgprocinfo);
  901. begin
  902. { remove localst as it was replaced by staticsymtable }
  903. pi.procdef.localst:=nil;
  904. { remove procinfo }
  905. curr.procinfo:=nil;
  906. pi.free;
  907. pi:=nil;
  908. end;
  909. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  910. procedure maybe_load_got(curr: tmodule);
  911. {$if defined(i386) or defined (sparcgen)}
  912. var
  913. gotvarsym : tstaticvarsym;
  914. {$endif i386 or sparcgen}
  915. begin
  916. {$if defined(i386) or defined(sparcgen)}
  917. if (cs_create_pic in current_settings.moduleswitches) and
  918. (tf_pic_uses_got in target_info.flags) then
  919. begin
  920. { insert symbol for got access in assembler code}
  921. gotvarsym:=cstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',
  922. vs_value,voidpointertype,[vo_is_external]);
  923. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  924. curr.localsymtable.insertsym(gotvarsym);
  925. { avoid unnecessary warnings }
  926. gotvarsym.varstate:=vs_read;
  927. gotvarsym.refs:=1;
  928. end;
  929. {$endif i386 or sparcgen}
  930. end;
  931. function gen_implicit_initfinal(curr: tmodule; flag:tmoduleflag;st:TSymtable):tcgprocinfo;
  932. begin
  933. { create procdef }
  934. case flag of
  935. mf_init :
  936. begin
  937. result:=create_main_proc(make_mangledname('',curr.localsymtable,'init_implicit$'),potype_unitinit,st);
  938. result.procdef.aliasnames.concat(make_mangledname('INIT$',curr.localsymtable,''));
  939. end;
  940. mf_finalize :
  941. begin
  942. result:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize_implicit$'),potype_unitfinalize,st);
  943. result.procdef.aliasnames.concat(make_mangledname('FINALIZE$',curr.localsymtable,''));
  944. if (not curr.is_unit) then
  945. result.procdef.aliasnames.concat('PASCALFINALIZE');
  946. end;
  947. else
  948. internalerror(200304253);
  949. end;
  950. result.code:=cnothingnode.create;
  951. end;
  952. procedure copy_macro(p:TObject; arg:pointer);
  953. begin
  954. TModule(arg).globalmacrosymtable.insertsym(tmacro(p).getcopy);
  955. end;
  956. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  957. var
  958. deprecated_seen,
  959. last_is_deprecated:boolean;
  960. begin
  961. try_consume_hintdirective:=false;
  962. deprecated_seen:=false;
  963. repeat
  964. last_is_deprecated:=false;
  965. case idtoken of
  966. _LIBRARY :
  967. begin
  968. include(moduleopt,mo_hint_library);
  969. try_consume_hintdirective:=true;
  970. end;
  971. _DEPRECATED :
  972. begin
  973. { allow deprecated only once }
  974. if deprecated_seen then
  975. break;
  976. include(moduleopt,mo_hint_deprecated);
  977. try_consume_hintdirective:=true;
  978. last_is_deprecated:=true;
  979. deprecated_seen:=true;
  980. end;
  981. _EXPERIMENTAL :
  982. begin
  983. include(moduleopt,mo_hint_experimental);
  984. try_consume_hintdirective:=true;
  985. end;
  986. _PLATFORM :
  987. begin
  988. include(moduleopt,mo_hint_platform);
  989. try_consume_hintdirective:=true;
  990. end;
  991. _UNIMPLEMENTED :
  992. begin
  993. include(moduleopt,mo_hint_unimplemented);
  994. try_consume_hintdirective:=true;
  995. end;
  996. else
  997. break;
  998. end;
  999. consume(Token);
  1000. { handle deprecated message }
  1001. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  1002. begin
  1003. if deprecatedmsg<>nil then
  1004. internalerror(201001221);
  1005. if token=_CSTRING then
  1006. deprecatedmsg:=stringdup(cstringpattern)
  1007. else
  1008. deprecatedmsg:=stringdup(pattern);
  1009. consume(token);
  1010. include(moduleopt,mo_has_deprecated_msg);
  1011. end;
  1012. until false;
  1013. end;
  1014. {$ifdef jvm}
  1015. procedure addmoduleclass(curr : tmodule);
  1016. var
  1017. def: tobjectdef;
  1018. typesym: ttypesym;
  1019. begin
  1020. { java_jlobject may not have been parsed yet (system unit); in any
  1021. case, we only use this to refer to the class type, so inheritance
  1022. does not matter }
  1023. def:=cobjectdef.create(odt_javaclass,'__FPC_JVM_Module_Class_Alias$',nil,true);
  1024. include(def.objectoptions,oo_is_external);
  1025. include(def.objectoptions,oo_is_sealed);
  1026. def.objextname:=stringdup(curr.realmodulename^);
  1027. typesym:=ctypesym.create('__FPC_JVM_Module_Class_Alias$',def);
  1028. symtablestack.top.insertsym(typesym);
  1029. end;
  1030. {$endif jvm}
  1031. type
  1032. tfinishstate=record
  1033. init_procinfo:tcgprocinfo;
  1034. finalize_procinfo:tcgprocinfo;
  1035. end;
  1036. pfinishstate=^tfinishstate;
  1037. function proc_unit_implementation(curr: tmodule):boolean;
  1038. var
  1039. init_procinfo,
  1040. finalize_procinfo : tcgprocinfo;
  1041. i,j : integer;
  1042. finishstate:pfinishstate;
  1043. begin
  1044. result:=true;
  1045. init_procinfo:=nil;
  1046. finalize_procinfo:=nil;
  1047. finishstate:=nil;
  1048. set_current_module(curr);
  1049. { We get here only after used modules were loaded }
  1050. connect_loaded_units(curr,curr.globalsymtable);
  1051. { All units are read, now give them a number }
  1052. curr.updatemaps;
  1053. { Consume the semicolon if needed.
  1054. At this point the units in the uses clause have at least been parsed
  1055. and are connected, and conditional compilation expressions can
  1056. use the symbols from those units }
  1057. if curr.consume_semicolon_after_uses then
  1058. begin
  1059. consume(_SEMICOLON);
  1060. curr.consume_semicolon_after_uses:=false;
  1061. end;
  1062. { further, changing the globalsymtable is not allowed anymore }
  1063. curr.globalsymtable.sealed:=true;
  1064. symtablestack.push(curr.localsymtable);
  1065. if not curr.interface_only then
  1066. begin
  1067. Message1(parser_u_parsing_implementation,curr.modulename^);
  1068. if curr.in_interface then
  1069. internalerror(200212285);
  1070. { Compile the unit }
  1071. init_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'init$'),potype_unitinit,curr.localsymtable);
  1072. init_procinfo.procdef.aliasnames.concat(make_mangledname('INIT$',curr.localsymtable,''));
  1073. init_procinfo.parse_body;
  1074. { save file pos for debuginfo }
  1075. curr.mainfilepos:=init_procinfo.entrypos;
  1076. { parse finalization section }
  1077. if token=_FINALIZATION then
  1078. begin
  1079. { Compile the finalize }
  1080. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  1081. finalize_procinfo.procdef.aliasnames.concat(make_mangledname('FINALIZE$',curr.localsymtable,''));
  1082. finalize_procinfo.parse_body;
  1083. end
  1084. end;
  1085. { remove all units that we are waiting for that are already waiting for
  1086. us => breaking up circles }
  1087. for i:=0 to curr.waitingunits.count-1 do
  1088. for j:=curr.waitingforunit.count-1 downto 0 do
  1089. if curr.waitingunits[i]=curr.waitingforunit[j] then
  1090. curr.waitingforunit.Delete(j);
  1091. {$ifdef DEBUG_UNITWAITING}
  1092. Writeln('Unit ', curr.modulename^, ' is waiting for units: ');
  1093. for i:=curr.waitingforunit.count-1 downto 0 do
  1094. writeln(' ',i,'/',curr.waitingforunit.count,' ',tmodule(curr.waitingforunit[i]).realmodulename^);
  1095. {$endif}
  1096. result:=curr.waitingforunit.count=0;
  1097. { save all information that is needed for finishing the unit }
  1098. New(finishstate);
  1099. finishstate^.init_procinfo:=init_procinfo;
  1100. finishstate^.finalize_procinfo:=finalize_procinfo;
  1101. curr.finishstate:=finishstate;
  1102. if result then
  1103. result:=finish_compile_unit(curr)
  1104. else
  1105. curr.state:=ms_compiling_waitfinish;
  1106. end;
  1107. function parse_unit_interface_declarations(curr : tmodule) : boolean;
  1108. begin
  1109. result:=true;
  1110. set_current_module(curr);
  1111. { update the symtable }
  1112. connect_loaded_units(curr,nil);
  1113. { We must do this again, because units can have been added to the list while another task was being handled }
  1114. curr.updatemaps;
  1115. { consume the semicolon after maps have been updated else conditional compiling expressions
  1116. might cause internal errors, see tw8611 }
  1117. if curr.consume_semicolon_after_uses then
  1118. begin
  1119. consume(_SEMICOLON);
  1120. curr.consume_semicolon_after_uses:=false;
  1121. end;
  1122. { now push our own symtable }
  1123. symtablestack.push(curr.globalsymtable);
  1124. { Dump stack
  1125. Write(curr.modulename^);
  1126. symtablestack.dump;
  1127. }
  1128. { ... parse the declarations }
  1129. Message1(parser_u_parsing_interface,curr.realmodulename^);
  1130. {$ifdef jvm}
  1131. { fake classdef to represent the class corresponding to the unit }
  1132. addmoduleclass(curr);
  1133. {$endif}
  1134. read_interface_declarations;
  1135. { Export macros defined in the interface for macpas. The macros
  1136. are put in the globalmacrosymtable that will only be used by other
  1137. units. The current unit continues to use the localmacrosymtable }
  1138. if (m_mac in current_settings.modeswitches) then
  1139. begin
  1140. curr.globalmacrosymtable:=tmacrosymtable.create(true);
  1141. curr.localmacrosymtable.SymList.ForEachCall(@copy_macro,curr);
  1142. end;
  1143. { leave when we got an error }
  1144. if (Errorcount>0) and not status.skip_error then
  1145. begin
  1146. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1147. status.skip_error:=true;
  1148. symtablestack.pop(curr.globalsymtable);
  1149. {$ifdef DEBUG_NODE_XML}
  1150. XMLFinalizeNodeFile('unit');
  1151. {$endif DEBUG_NODE_XML}
  1152. exit;
  1153. end;
  1154. { we need to be able to reference these in descendants,
  1155. so they must be generated and included in the interface }
  1156. if (target_cpu=tsystemcpu.cpu_wasm32) then
  1157. add_synthetic_interface_classes_for_st(curr.globalsymtable,true,false);
  1158. { Our interface is compiled, generate interface CRC and switch to implementation }
  1159. {$IFDEF Debug_WaitCRC}
  1160. writeln('parse_unit_interface_declarations ',curr.realmodulename^);
  1161. {$ENDIF}
  1162. if not(cs_compilesystem in current_settings.moduleswitches) and
  1163. (Errorcount=0) then
  1164. tppumodule(curr).getppucrc;
  1165. curr.in_interface:=false;
  1166. curr.interface_compiled:=true;
  1167. { First reload all units depending on our interface, we need to do this
  1168. in the implementation part to prevent erroneous circular references }
  1169. tppumodule(curr).setdefgeneration;
  1170. tppumodule(curr).reload_flagged_units;
  1171. { Parse the implementation section }
  1172. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  1173. curr.interface_only:=true
  1174. else
  1175. curr.interface_only:=false;
  1176. parse_only:=false;
  1177. { create static symbol table }
  1178. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  1179. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1180. maybe_load_got(curr);
  1181. if not curr.interface_only then
  1182. begin
  1183. consume(_IMPLEMENTATION);
  1184. Message1(unit_u_loading_implementation_units,curr.modulename^);
  1185. { Read the implementation units }
  1186. if token=_USES then
  1187. begin
  1188. parseusesclause(curr);
  1189. if not loadunits(curr,false) then
  1190. curr.state:=ms_compiling_waitimpl;
  1191. { do not consume the semicolon yet, because the units in the uses clause
  1192. may not yet be loaded and conditional compilation expressions may
  1193. depend on symbols from those units }
  1194. curr.consume_semicolon_after_uses:=True;
  1195. end
  1196. else
  1197. curr.consume_semicolon_after_uses:=False;
  1198. end;
  1199. if curr.state in [ms_compiled,ms_processed] then
  1200. begin
  1201. // Writeln('Popping global symtable ?');
  1202. symtablestack.pop(curr.globalsymtable);
  1203. end;
  1204. { Can we continue compiling ? }
  1205. result:=curr.state<>ms_compiling_waitimpl;
  1206. if result then
  1207. result:=proc_unit_implementation(curr);
  1208. end;
  1209. function proc_unit(curr: tmodule):boolean;
  1210. var
  1211. main_file: tinputfile;
  1212. s1,s2 : ^string; {Saves stack space}
  1213. unitname : ansistring;
  1214. unitname8 : string[8];
  1215. feature : tfeature;
  1216. load_ok : boolean;
  1217. begin
  1218. result:=true;
  1219. if m_mac in current_settings.modeswitches then
  1220. curr.mode_switch_allowed:= false;
  1221. consume(_UNIT);
  1222. if curr.is_initial then
  1223. Status.IsExe:=false;
  1224. unitname:=orgpattern;
  1225. consume(_ID);
  1226. while token=_POINT do
  1227. begin
  1228. consume(_POINT);
  1229. unitname:=unitname+'.'+orgpattern;
  1230. consume(_ID);
  1231. end;
  1232. { create filenames and unit name }
  1233. main_file := current_scanner.inputfile;
  1234. while assigned(main_file.next) do
  1235. main_file := main_file.next;
  1236. new(s1);
  1237. s1^:=curr.modulename^;
  1238. curr.SetFileName(main_file.path+main_file.name,true);
  1239. curr.SetModuleName(unitname);
  1240. {$ifdef DEBUG_NODE_XML}
  1241. XMLInitializeNodeFile('unit', unitname);
  1242. {$endif DEBUG_NODE_XML}
  1243. { check for system unit }
  1244. new(s2);
  1245. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name),''));
  1246. unitname8:=copy(curr.modulename^,1,8);
  1247. if (cs_check_unit_name in current_settings.globalswitches) and
  1248. (
  1249. not(
  1250. (curr.modulename^=s2^) or
  1251. (
  1252. (length(curr.modulename^)>8) and
  1253. (unitname8=s2^)
  1254. )
  1255. )
  1256. or
  1257. (
  1258. (length(s1^)>8) and
  1259. (s1^<>curr.modulename^)
  1260. )
  1261. ) then
  1262. Message2(unit_e_illegal_unit_name,curr.realmodulename^,s1^);
  1263. if (curr.modulename^='SYSTEM') then
  1264. include(current_settings.moduleswitches,cs_compilesystem);
  1265. dispose(s2);
  1266. dispose(s1);
  1267. if (target_info.system in systems_unit_program_exports) then
  1268. exportlib.preparelib(curr.realmodulename^);
  1269. { parse hint directives }
  1270. try_consume_hintdirective(curr.moduleoptions, curr.deprecatedmsg);
  1271. consume(_SEMICOLON);
  1272. { handle the global switches, do this before interface, because after interface has been
  1273. read, all following directives are parsed as well }
  1274. setupglobalswitches;
  1275. { generate now the global symboltable,
  1276. define first as local to overcome dependency conflicts }
  1277. curr.localsymtable:=tglobalsymtable.create(curr.modulename^,curr.moduleid);
  1278. { insert unitsym of this unit to prevent other units having
  1279. the same name }
  1280. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1281. { load default system unit, it must be loaded before interface is parsed
  1282. else we cannot use e.g. feature switches before the next real token }
  1283. load_ok:=loadsystemunit(curr);
  1284. { system unit is loaded, now insert feature defines }
  1285. for feature:=low(tfeature) to high(tfeature) do
  1286. if feature in features then
  1287. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1288. consume(_INTERFACE);
  1289. { global switches are read, so further changes aren't allowed }
  1290. curr.in_global:=false;
  1291. message1(unit_u_loading_interface_units,curr.modulename^);
  1292. { update status }
  1293. status.currentmodule:=curr.realmodulename^;
  1294. { maybe turn off m_objpas if we are compiling objpas }
  1295. if (curr.modulename^='OBJPAS') then
  1296. exclude(current_settings.modeswitches,m_objpas);
  1297. { maybe turn off m_mac if we are compiling macpas }
  1298. if (curr.modulename^='MACPAS') then
  1299. exclude(current_settings.modeswitches,m_mac);
  1300. parse_only:=true;
  1301. { load default units, like language mode units }
  1302. if not(cs_compilesystem in current_settings.moduleswitches) then
  1303. load_ok:=loaddefaultunits(curr) and load_ok;
  1304. { insert qualifier for the system unit (allows system.writeln) }
  1305. if not(cs_compilesystem in current_settings.moduleswitches) and
  1306. (token=_USES) then
  1307. begin
  1308. // We do this as late as possible.
  1309. if Assigned(curr) then
  1310. curr.Loadlocalnamespacelist
  1311. else
  1312. current_namespacelist:=Nil;
  1313. parseusesclause(curr);
  1314. load_ok:=loadunits(curr,true) and load_ok;
  1315. { has it been compiled at a higher level ?}
  1316. if curr.state in [ms_compiled,ms_processed] then
  1317. begin
  1318. Message1(parser_u_already_compiled,curr.realmodulename^);
  1319. exit;
  1320. end;
  1321. curr.consume_semicolon_after_uses:=true;
  1322. end
  1323. else
  1324. curr.consume_semicolon_after_uses:=false;
  1325. { move the global symtable from the temporary local to global }
  1326. current_module.globalsymtable:=current_module.localsymtable;
  1327. current_module.localsymtable:=nil;
  1328. { Now we check if we can continue. }
  1329. if not load_ok then
  1330. curr.state:=ms_compiling_waitintf;
  1331. { create whole program optimisation information (may already be
  1332. updated in the interface, e.g., in case of classrefdef typed
  1333. constants }
  1334. curr.wpoinfo:=tunitwpoinfo.create;
  1335. { Can we continue compiling ? }
  1336. result:=curr.state<>ms_compiling_waitintf;
  1337. if result then
  1338. result:=parse_unit_interface_declarations(curr);
  1339. end;
  1340. procedure module_is_done(curr: tmodule);inline;
  1341. begin
  1342. dispose(pfinishstate(curr.finishstate));
  1343. curr.finishstate:=nil;
  1344. end;
  1345. function finish_compile_unit(module: tmodule): boolean;
  1346. function is_assembler_generated:boolean;
  1347. var
  1348. hal : tasmlisttype;
  1349. begin
  1350. result:=false;
  1351. if Errorcount=0 then
  1352. begin
  1353. for hal:=low(TasmlistType) to high(TasmlistType) do
  1354. if not current_asmdata.asmlists[hal].empty then
  1355. begin
  1356. result:=true;
  1357. exit;
  1358. end;
  1359. end;
  1360. end;
  1361. var
  1362. {$ifdef EXTDEBUG}
  1363. store_crc,
  1364. {$endif EXTDEBUG}
  1365. force_init_final : boolean;
  1366. init_procinfo,
  1367. finalize_procinfo : tcgprocinfo;
  1368. i : longint;
  1369. ag : boolean;
  1370. finishstate : tfinishstate;
  1371. waitingmodule : tmodule;
  1372. begin
  1373. result:=true;
  1374. { curr is now module }
  1375. if not assigned(module.finishstate) then
  1376. internalerror(2012091801);
  1377. finishstate:=pfinishstate(module.finishstate)^;
  1378. finalize_procinfo:=finishstate.finalize_procinfo;
  1379. init_procinfo:=finishstate.init_procinfo;
  1380. { Generate specializations of objectdefs methods }
  1381. generate_specialization_procs;
  1382. // This needs to be done before we generate the VMTs
  1383. if (target_cpu=tsystemcpu.cpu_wasm32) then
  1384. begin
  1385. add_synthetic_interface_classes_for_st(module.globalsymtable,false,true);
  1386. add_synthetic_interface_classes_for_st(module.localsymtable,true,true);
  1387. end;
  1388. { generate construction functions for all attributes in the unit:
  1389. this must be done before writing the VMTs because
  1390. during VMT writing the extended field info is written }
  1391. generate_attr_constrs(current_module.used_rtti_attrs);
  1392. { Generate VMTs }
  1393. if Errorcount=0 then
  1394. begin
  1395. write_vmts(module.globalsymtable,true);
  1396. write_vmts(module.localsymtable,false);
  1397. end;
  1398. { add implementations for synthetic method declarations added by
  1399. the compiler }
  1400. add_synthetic_method_implementations(module.globalsymtable);
  1401. add_synthetic_method_implementations(module.localsymtable);
  1402. { if the unit contains ansi/widestrings, initialization and
  1403. finalization code must be forced }
  1404. force_init_final:=tglobalsymtable(module.globalsymtable).needs_init_final or
  1405. tstaticsymtable(module.localsymtable).needs_init_final;
  1406. { should we force unit initialization? }
  1407. { this is a hack, but how can it be done better ? }
  1408. { Now the sole purpose of this is to change 'init' to 'init_implicit',
  1409. is it needed at all? (Sergei) }
  1410. { it's needed in case cnodeutils.force_init = true }
  1411. if (force_init_final or cnodeutils.force_init) and
  1412. (
  1413. not assigned(init_procinfo) or
  1414. has_no_code(init_procinfo.code)
  1415. ) then
  1416. begin
  1417. { first release the not used init procinfo }
  1418. if assigned(init_procinfo) then
  1419. begin
  1420. release_proc_symbol(init_procinfo.procdef);
  1421. release_main_proc(module,init_procinfo);
  1422. end;
  1423. init_procinfo:=gen_implicit_initfinal(module,mf_init,module.localsymtable);
  1424. end;
  1425. if (force_init_final or cnodeutils.force_final) and
  1426. (
  1427. not assigned(finalize_procinfo) or
  1428. has_no_code(finalize_procinfo.code)
  1429. ) then
  1430. begin
  1431. { first release the not used finalize procinfo }
  1432. if assigned(finalize_procinfo) then
  1433. begin
  1434. release_proc_symbol(finalize_procinfo.procdef);
  1435. release_main_proc(module,finalize_procinfo);
  1436. end;
  1437. finalize_procinfo:=gen_implicit_initfinal(module,mf_finalize,module.localsymtable);
  1438. end;
  1439. { Now both init and finalize bodies are read and it is known
  1440. which variables are used in both init and finalize we can now
  1441. generate the code. This is required to prevent putting a variable in
  1442. a register that is also used in the finalize body (PFV) }
  1443. if assigned(init_procinfo) then
  1444. begin
  1445. if (force_init_final or cnodeutils.force_init) or
  1446. not(has_no_code(init_procinfo.code)) then
  1447. begin
  1448. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  1449. init_procinfo.generate_code_tree;
  1450. include(module.moduleflags,mf_init);
  1451. end
  1452. else
  1453. release_proc_symbol(init_procinfo.procdef);
  1454. init_procinfo.resetprocdef;
  1455. release_main_proc(module,init_procinfo);
  1456. end;
  1457. if assigned(finalize_procinfo) then
  1458. begin
  1459. if force_init_final or
  1460. cnodeutils.force_init or
  1461. not(has_no_code(finalize_procinfo.code)) then
  1462. begin
  1463. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  1464. finalize_procinfo.generate_code_tree;
  1465. include(module.moduleflags,mf_finalize);
  1466. end
  1467. else
  1468. release_proc_symbol(finalize_procinfo.procdef);
  1469. finalize_procinfo.resetprocdef;
  1470. release_main_proc(module,finalize_procinfo);
  1471. end;
  1472. symtablestack.pop(module.localsymtable);
  1473. symtablestack.pop(module.globalsymtable);
  1474. { the last char should always be a point }
  1475. { Do not attempt to read next token after dot,
  1476. there may be a #0 when the unit was finished in a separate stage }
  1477. consume_last_dot;
  1478. { reset wpo flags for all defs }
  1479. reset_all_defs(module);
  1480. if (Errorcount=0) then
  1481. begin
  1482. { tests, if all (interface) forwards are resolved }
  1483. tstoredsymtable(module.globalsymtable).check_forwards;
  1484. { check if all private fields are used }
  1485. tstoredsymtable(module.globalsymtable).allprivatesused;
  1486. { test static symtable }
  1487. tstoredsymtable(module.localsymtable).allsymbolsused;
  1488. tstoredsymtable(module.localsymtable).allprivatesused;
  1489. tstoredsymtable(module.localsymtable).check_forwards;
  1490. tstoredsymtable(module.localsymtable).checklabels;
  1491. { used units }
  1492. module.allunitsused;
  1493. end;
  1494. { leave when we got an error }
  1495. if (Errorcount>0) and not status.skip_error then
  1496. begin
  1497. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1498. status.skip_error:=true;
  1499. module_is_done(module);
  1500. {$ifdef DEBUG_NODE_XML}
  1501. XMLFinalizeNodeFile('unit');
  1502. {$endif DEBUG_NODE_XML}
  1503. exit;
  1504. end;
  1505. { if an Objective-C module, generate rtti and module info }
  1506. MaybeGenerateObjectiveCImageInfo(module.globalsymtable,module.localsymtable);
  1507. { do we need to add the variants unit? }
  1508. maybeloadvariantsunit(module);
  1509. { generate rtti/init tables }
  1510. write_persistent_type_info(module.globalsymtable,true);
  1511. write_persistent_type_info(module.localsymtable,false);
  1512. { Tables }
  1513. cnodeutils.InsertThreadvars;
  1514. { Resource strings }
  1515. GenerateResourceStrings;
  1516. { Widestring typed constants }
  1517. cnodeutils.InsertWideInits;
  1518. { Resourcestring references }
  1519. cnodeutils.InsertResStrInits;
  1520. { generate debuginfo }
  1521. if (cs_debuginfo in current_settings.moduleswitches) then
  1522. current_debuginfo.inserttypeinfo;
  1523. { generate imports }
  1524. if module.ImportLibraryList.Count>0 then
  1525. importlib.generatelib;
  1526. { insert own objectfile, or say that it's in a library
  1527. (no check for an .o when loading) }
  1528. ag:=is_assembler_generated;
  1529. if ag then
  1530. insertobjectfile(module)
  1531. else
  1532. begin
  1533. module.headerflags:=module.headerflags or uf_no_link;
  1534. exclude(module.moduleflags,mf_has_stabs_debuginfo);
  1535. exclude(module.moduleflags,mf_has_dwarf_debuginfo);
  1536. end;
  1537. if ag then
  1538. begin
  1539. { create callframe info }
  1540. create_dwarf_frame;
  1541. { assemble }
  1542. create_objectfile(module);
  1543. end;
  1544. // remove all waits for this unit
  1545. for i:=0 to module.waitingunits.count-1 do
  1546. begin
  1547. waitingmodule:=tmodule(module.waitingunits[i]);
  1548. waitingmodule.remove_from_waitingforunits(module);
  1549. end;
  1550. // compute CRC
  1551. if ErrorCount=0 then
  1552. begin
  1553. if not module.usedunitsfinalcrc(waitingmodule) then
  1554. begin
  1555. { Some used units are still compiling, so their CRCs can change.
  1556. Compute the final CRC of this module, for the case of a
  1557. circular dependency, and wait.
  1558. }
  1559. {$IFDEF Debug_WaitCRC}
  1560. writeln('finish_compile_unit ',module.realmodulename^,' waiting for used unit CRCs...');
  1561. {$ENDIF}
  1562. tppumodule(module).getppucrc;
  1563. module.crc_final:=true;
  1564. module.state:=ms_compiled_waitcrc;
  1565. exit(false);
  1566. end;
  1567. end;
  1568. result:=finish_unit(module);
  1569. end;
  1570. function finish_unit(module: tmodule): boolean;
  1571. var
  1572. store_interface_crc,
  1573. store_indirect_crc : cardinal;
  1574. i : longint;
  1575. waitingmodule : tmodule;
  1576. begin
  1577. result:=true;
  1578. { Write out the ppufile after the object file has been created }
  1579. store_interface_crc:=module.interface_crc;
  1580. store_indirect_crc:=module.indirect_crc;
  1581. {$ifdef EXTDEBUG}
  1582. store_crc:=module.crc;
  1583. {$endif EXTDEBUG}
  1584. if ErrorCount=0 then
  1585. tppumodule(module).writeppu;
  1586. if not(cs_compilesystem in current_settings.moduleswitches) then
  1587. begin
  1588. if store_interface_crc<>module.interface_crc then
  1589. Message1(unit_u_interface_crc_changed,module.ppufilename);
  1590. if store_indirect_crc<>module.indirect_crc then
  1591. Message1(unit_u_indirect_crc_changed,module.ppufilename);
  1592. end;
  1593. {$ifdef EXTDEBUG}
  1594. if not(cs_compilesystem in current_settings.moduleswitches) then
  1595. if (store_crc<>module.crc) then
  1596. Message1(unit_u_implementation_crc_changed,module.ppufilename);
  1597. {$endif EXTDEBUG}
  1598. { release unregistered defs/syms from the localsymtable }
  1599. free_unregistered_localsymtable_elements(module);
  1600. { release local symtables that are not needed anymore }
  1601. free_localsymtables(module.globalsymtable);
  1602. free_localsymtables(module.localsymtable);
  1603. { leave when we got an error }
  1604. if (Errorcount>0) and not status.skip_error then
  1605. begin
  1606. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1607. status.skip_error:=true;
  1608. module_is_done(module);
  1609. {$ifdef DEBUG_NODE_XML}
  1610. XMLFinalizeNodeFile('unit');
  1611. {$endif DEBUG_NODE_XML}
  1612. exit;
  1613. end;
  1614. {$ifdef debug_devirt}
  1615. { print out all instantiated class/object types }
  1616. writeln('constructed object/class/classreftypes in ',module.realmodulename^);
  1617. for i := 0 to module.wpoinfo.createdobjtypes.count-1 do
  1618. begin
  1619. write(' ',tdef(module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1620. case tdef(module.wpoinfo.createdobjtypes[i]).typ of
  1621. objectdef:
  1622. case tobjectdef(module.wpoinfo.createdobjtypes[i]).objecttype of
  1623. odt_object:
  1624. writeln(' (object)');
  1625. odt_class:
  1626. writeln(' (class)');
  1627. else
  1628. internalerror(2008101103);
  1629. end;
  1630. else
  1631. internalerror(2008101104);
  1632. end;
  1633. end;
  1634. for i := 0 to module.wpoinfo.createdclassrefobjtypes.count-1 do
  1635. begin
  1636. write(' Class Of ',tdef(module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1637. case tdef(module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1638. objectdef:
  1639. case tobjectdef(module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1640. odt_class:
  1641. writeln(' (classrefdef)');
  1642. else
  1643. internalerror(2008101105);
  1644. end
  1645. else
  1646. internalerror(2008101102);
  1647. end;
  1648. end;
  1649. {$endif debug_devirt}
  1650. Message1(unit_u_finished_compiling,module.modulename^);
  1651. module_is_done(module);
  1652. module.end_of_parsing;
  1653. {$ifdef DEBUG_NODE_XML}
  1654. XMLFinalizeNodeFile('unit');
  1655. {$endif DEBUG_NODE_XML}
  1656. end;
  1657. function proc_package(curr: tmodule) : boolean;
  1658. var
  1659. main_file : tinputfile;
  1660. hp,hp2 : tmodule;
  1661. pkg : tpcppackage;
  1662. main_procinfo : tcgprocinfo;
  1663. force_init_final : boolean;
  1664. uu : tused_unit;
  1665. module_name: ansistring;
  1666. pentry: ppackageentry;
  1667. feature : tfeature;
  1668. begin
  1669. Result:=True;
  1670. Status.IsPackage:=true;
  1671. Status.IsExe:=true;
  1672. parse_only:=false;
  1673. main_procinfo:=nil;
  1674. {init_procinfo:=nil;
  1675. finalize_procinfo:=nil;}
  1676. if not (tf_supports_packages in target_info.flags) then
  1677. message1(parser_e_packages_not_supported,target_info.name);
  1678. if not RelocSectionSetExplicitly then
  1679. RelocSection:=true;
  1680. { Relocation works only without stabs under Windows when }
  1681. { external linker (LD) is used. LD generates relocs for }
  1682. { stab sections which is not loaded in memory. It causes }
  1683. { AV error when DLL is loaded and relocation is needed. }
  1684. { Internal linker does not have this problem. }
  1685. if RelocSection and
  1686. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1687. (cs_link_extern in current_settings.globalswitches) then
  1688. begin
  1689. include(current_settings.globalswitches,cs_link_strip);
  1690. { Warning stabs info does not work with reloc section !! }
  1691. if (cs_debuginfo in current_settings.moduleswitches) and
  1692. (target_dbg.id=dbg_stabs) then
  1693. begin
  1694. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  1695. Message(parser_w_parser_win32_debug_needs_WN);
  1696. exclude(current_settings.moduleswitches,cs_debuginfo);
  1697. end;
  1698. end;
  1699. { get correct output names }
  1700. main_file := current_scanner.inputfile;
  1701. while assigned(main_file.next) do
  1702. main_file := main_file.next;
  1703. curr.SetFileName(main_file.path+main_file.name,true);
  1704. { consume _PACKAGE word }
  1705. consume(_ID);
  1706. module_name:=orgpattern;
  1707. consume(_ID);
  1708. while token=_POINT do
  1709. begin
  1710. consume(_POINT);
  1711. module_name:=module_name+'.'+orgpattern;
  1712. consume(_ID);
  1713. end;
  1714. curr.setmodulename(module_name);
  1715. curr.ispackage:=true;
  1716. exportlib.preparelib(module_name);
  1717. pkg:=tpcppackage.create(module_name);
  1718. if tf_library_needs_pic in target_info.flags then
  1719. include(current_settings.moduleswitches,cs_create_pic);
  1720. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1721. read, all following directives are parsed as well }
  1722. setupglobalswitches;
  1723. {$ifdef DEBUG_NODE_XML}
  1724. XMLInitializeNodeFile('package', module_name);
  1725. {$endif DEBUG_NODE_XML}
  1726. consume(_SEMICOLON);
  1727. { global switches are read, so further changes aren't allowed }
  1728. curr.in_global:=false;
  1729. { set implementation flag }
  1730. curr.in_interface:=false;
  1731. curr.interface_compiled:=true;
  1732. { insert after the unit symbol tables the static symbol table }
  1733. { of the program }
  1734. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  1735. { ensure that no packages are picked up from the options }
  1736. packagelist.clear;
  1737. // There should always be a requires, except for the system package. So we load here
  1738. if Assigned(curr) then
  1739. curr.Loadlocalnamespacelist
  1740. else
  1741. current_namespacelist:=Nil;
  1742. {Read the packages used by the package we compile.}
  1743. if (token=_ID) and (idtoken=_REQUIRES) then
  1744. begin
  1745. { consume _REQUIRES word }
  1746. consume(_ID);
  1747. while true do
  1748. begin
  1749. if token=_ID then
  1750. begin
  1751. module_name:=orgpattern;
  1752. consume(_ID);
  1753. while token=_POINT do
  1754. begin
  1755. consume(_POINT);
  1756. module_name:=module_name+'.'+orgpattern;
  1757. consume(_ID);
  1758. end;
  1759. add_package(module_name,false,true);
  1760. end
  1761. else
  1762. consume(_ID);
  1763. if token=_COMMA then
  1764. consume(_COMMA)
  1765. else
  1766. break;
  1767. end;
  1768. consume(_SEMICOLON);
  1769. end;
  1770. { now load all packages, so that we can determine whether a unit is
  1771. already provided by one of the loaded packages }
  1772. load_packages;
  1773. if packagelist.Count>0 then
  1774. begin
  1775. { this means the SYSTEM unit *must* be part of one of the required
  1776. packages, so load it }
  1777. AddUnit(curr,'system',false);
  1778. systemunit:=tglobalsymtable(symtablestack.top);
  1779. load_intern_types;
  1780. { system unit is loaded, now insert feature defines }
  1781. for feature:=low(tfeature) to high(tfeature) do
  1782. if feature in features then
  1783. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1784. end;
  1785. {Load the units used by the program we compile.}
  1786. if (token=_ID) and (idtoken=_CONTAINS) then
  1787. begin
  1788. { consume _CONTAINS word }
  1789. consume(_ID);
  1790. while true do
  1791. begin
  1792. if token=_ID then
  1793. begin
  1794. module_name:=orgpattern;
  1795. consume(_ID);
  1796. while token=_POINT do
  1797. begin
  1798. consume(_POINT);
  1799. module_name:=module_name+'.'+orgpattern;
  1800. consume(_ID);
  1801. end;
  1802. hp:=AddUnit(curr,module_name);
  1803. if (hp.modulename^='SYSTEM') and not assigned(systemunit) then
  1804. begin
  1805. systemunit:=tglobalsymtable(hp.globalsymtable);
  1806. load_intern_types;
  1807. end;
  1808. end
  1809. else
  1810. consume(_ID);
  1811. if token=_COMMA then
  1812. consume(_COMMA)
  1813. else break;
  1814. end;
  1815. consume(_SEMICOLON);
  1816. end;
  1817. { All units are read, now give them a number }
  1818. curr.updatemaps;
  1819. hp:=tmodule(loaded_units.first);
  1820. while assigned(hp) do
  1821. begin
  1822. if (hp<>curr) and not assigned(hp.package) then
  1823. begin
  1824. if mf_package_deny in hp.moduleflags then
  1825. message1(package_e_unit_deny_package,hp.realmodulename^);
  1826. { part of the package's used, aka contained units? }
  1827. uu:=tused_unit(curr.used_units.first);
  1828. while assigned(uu) do
  1829. begin
  1830. if uu.u=hp then
  1831. break;
  1832. uu:=tused_unit(uu.next);
  1833. end;
  1834. if not assigned(uu) then
  1835. message2(package_n_implicit_unit_import,hp.realmodulename^,curr.realmodulename^);
  1836. end;
  1837. { was this unit listed as a contained unit? If so => error }
  1838. if (hp<>curr) and assigned(hp.package) then
  1839. begin
  1840. uu:=tused_unit(curr.used_units.first);
  1841. while assigned(uu) do
  1842. begin
  1843. if uu.u=hp then
  1844. break;
  1845. uu:=tused_unit(uu.next);
  1846. end;
  1847. if assigned(uu) then
  1848. message2(package_e_unit_already_contained_in_package,hp.realmodulename^,hp.package.realpackagename^);
  1849. end;
  1850. hp:=tmodule(hp.next);
  1851. end;
  1852. {Insert the name of the main program into the symbol table.}
  1853. if curr.realmodulename^<>'' then
  1854. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1855. Message1(parser_u_parsing_implementation,curr.mainsource);
  1856. symtablestack.push(curr.localsymtable);
  1857. { create whole program optimisation information }
  1858. curr.wpoinfo:=tunitwpoinfo.create;
  1859. { should we force unit initialization? }
  1860. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  1861. if force_init_final or cnodeutils.force_init then
  1862. {init_procinfo:=gen_implicit_initfinal(mf_init,curr.localsymtable)};
  1863. { Add symbol to the exports section for win32 so smartlinking a
  1864. DLL will include the edata section }
  1865. if assigned(exportlib) and
  1866. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1867. (mf_has_exports in curr.moduleflags) then
  1868. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  1869. { all labels must be defined before generating code }
  1870. if Errorcount=0 then
  1871. tstoredsymtable(curr.localsymtable).checklabels;
  1872. symtablestack.pop(curr.localsymtable);
  1873. { consume the last point }
  1874. consume(_END);
  1875. consume(_POINT);
  1876. if (Errorcount=0) then
  1877. begin
  1878. { test static symtable }
  1879. tstoredsymtable(curr.localsymtable).allsymbolsused;
  1880. tstoredsymtable(curr.localsymtable).allprivatesused;
  1881. tstoredsymtable(curr.localsymtable).check_forwards;
  1882. { Note: all contained units are considered as used }
  1883. end;
  1884. if target_info.system in systems_all_windows+systems_nativent then
  1885. begin
  1886. main_procinfo:=create_main_proc('_PkgEntryPoint',potype_pkgstub,curr.localsymtable);
  1887. main_procinfo.procdef.aliasnames.concat('_DLLMainCRTStartup');
  1888. main_procinfo.code:=generate_pkg_stub(main_procinfo.procdef);
  1889. main_procinfo.generate_code;
  1890. end;
  1891. {$ifdef DEBUG_NODE_XML}
  1892. XMLFinalizeNodeFile('package');
  1893. {$endif DEBUG_NODE_XML}
  1894. { leave when we got an error }
  1895. if (Errorcount>0) and not status.skip_error then
  1896. begin
  1897. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1898. status.skip_error:=true;
  1899. pkg.free;
  1900. exit;
  1901. end;
  1902. { remove all unused units, this happends when units are removed
  1903. from the uses clause in the source and the ppu was already being loaded }
  1904. hp:=tmodule(loaded_units.first);
  1905. while assigned(hp) do
  1906. begin
  1907. hp2:=hp;
  1908. hp:=tmodule(hp.next);
  1909. if assigned(hp2.package) then
  1910. add_package_unit_ref(hp2.package);
  1911. if hp2.is_unit and
  1912. not assigned(hp2.globalsymtable) then
  1913. loaded_units.remove(hp2);
  1914. end;
  1915. exportlib.ignoreduplicates:=true;
  1916. { force exports }
  1917. uu:=tused_unit(usedunits.first);
  1918. while assigned(uu) do
  1919. begin
  1920. if not assigned(systemunit) and (uu.u.modulename^='SYSTEM') then
  1921. begin
  1922. systemunit:=tglobalsymtable(uu.u.globalsymtable);
  1923. load_intern_types;
  1924. end;
  1925. if not assigned(uu.u.package) then
  1926. export_unit(uu.u);
  1927. uu:=tused_unit(uu.next);
  1928. end;
  1929. {$ifdef arm}
  1930. { Insert .pdata section for arm-wince.
  1931. It is needed for exception handling. }
  1932. if target_info.system in [system_arm_wince] then
  1933. InsertPData;
  1934. {$endif arm}
  1935. { generate debuginfo }
  1936. if (cs_debuginfo in current_settings.moduleswitches) then
  1937. current_debuginfo.inserttypeinfo;
  1938. exportlib.generatelib;
  1939. exportlib.ignoreduplicates:=false;
  1940. { create import libraries for all packages }
  1941. if packagelist.count>0 then
  1942. createimportlibfromexternals;
  1943. { generate imports }
  1944. if curr.ImportLibraryList.Count>0 then
  1945. importlib.generatelib;
  1946. { Reference all DEBUGINFO sections from the main .fpc section }
  1947. if (cs_debuginfo in current_settings.moduleswitches) then
  1948. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1949. { insert own objectfile }
  1950. insertobjectfile(curr);
  1951. { assemble and link }
  1952. create_objectfile(curr);
  1953. { We might need the symbols info if not using
  1954. the default do_extractsymbolinfo
  1955. which is a dummy function PM }
  1956. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1957. { release all local symtables that are not needed anymore }
  1958. if (not needsymbolinfo) then
  1959. free_localsymtables(curr.localsymtable);
  1960. { leave when we got an error }
  1961. if (Errorcount>0) and not status.skip_error then
  1962. begin
  1963. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1964. status.skip_error:=true;
  1965. pkg.free;
  1966. exit;
  1967. end;
  1968. if (not curr.is_unit) then
  1969. begin
  1970. { we add all loaded units that are not part of a package to the
  1971. package; this includes units in the "contains" section as well
  1972. as implicitely imported ones }
  1973. hp:=tmodule(loaded_units.first);
  1974. while assigned(hp) do
  1975. begin
  1976. if (hp<>curr) then
  1977. begin
  1978. if not assigned(hp.package) then
  1979. begin
  1980. pkg.addunit(hp);
  1981. check_for_indirect_package_usages(hp.used_units);
  1982. end
  1983. else
  1984. begin
  1985. pentry:=ppackageentry(packagelist.find(hp.package.packagename^));
  1986. if not assigned(pentry) then
  1987. internalerror(2015112301);
  1988. pkg.add_required_package(hp.package);
  1989. end;
  1990. end;
  1991. hp:=tmodule(hp.next);
  1992. end;
  1993. pkg.initmoduleinfo(curr);
  1994. { create the executable when we are at level 1 }
  1995. if (curr.is_initial) then
  1996. begin
  1997. { create global resource file by collecting all resource files }
  1998. CollectResourceFiles;
  1999. { write .def file }
  2000. if (cs_link_deffile in current_settings.globalswitches) then
  2001. deffile.writefile;
  2002. { generate the pcp file }
  2003. pkg.savepcp;
  2004. { insert all .o files from all loaded units and
  2005. unload the units, we don't need them anymore.
  2006. Keep the curr because that is still needed }
  2007. hp:=tmodule(loaded_units.first);
  2008. while assigned(hp) do
  2009. begin
  2010. { only link in those units which should become part of this
  2011. package }
  2012. if not assigned(hp.package) then
  2013. linker.AddModuleFiles(hp);
  2014. hp2:=tmodule(hp.next);
  2015. if (hp<>curr) and
  2016. (not needsymbolinfo) then
  2017. begin
  2018. loaded_units.remove(hp);
  2019. hp.free;
  2020. end;
  2021. hp:=hp2;
  2022. end;
  2023. { add the library of directly used packages }
  2024. add_package_libs(linker);
  2025. { and now link the package library }
  2026. linker.MakeSharedLibrary
  2027. end;
  2028. { Give Fatal with error count for linker errors }
  2029. if (Errorcount>0) and not status.skip_error then
  2030. begin
  2031. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2032. status.skip_error:=true;
  2033. end;
  2034. pkg.free;
  2035. end;
  2036. end;
  2037. procedure proc_create_executable(curr, sysinitmod: tmodule; islibrary : boolean);
  2038. var
  2039. program_uses_checkpointer : boolean;
  2040. hp,hp2 : tmodule;
  2041. begin
  2042. { create global resource file by collecting all resource files }
  2043. CollectResourceFiles;
  2044. { write .def file }
  2045. if (cs_link_deffile in current_settings.globalswitches) then
  2046. deffile.writefile;
  2047. { link SysInit (if any) first, to have behavior consistent with
  2048. assembler startup files }
  2049. if assigned(sysinitmod) then
  2050. linker.AddModuleFiles(sysinitmod);
  2051. { Does any unit use checkpointer function }
  2052. program_uses_checkpointer:=false;
  2053. { insert all .o files from all loaded units and
  2054. unload the units, we don't need them anymore.
  2055. Keep the curr because that is still needed }
  2056. hp:=tmodule(loaded_units.first);
  2057. while assigned(hp) do
  2058. begin
  2059. if (hp<>sysinitmod) and not assigned(hp.package) then
  2060. begin
  2061. linker.AddModuleFiles(hp);
  2062. if mf_checkpointer_called in hp.moduleflags then
  2063. program_uses_checkpointer:=true;
  2064. end;
  2065. hp2:=tmodule(hp.next);
  2066. if assigned(hp.package) then
  2067. add_package_unit_ref(hp.package);
  2068. if (hp<>curr) and
  2069. (not needsymbolinfo) then
  2070. begin
  2071. loaded_units.remove(hp);
  2072. hp.free;
  2073. end;
  2074. hp:=hp2;
  2075. end;
  2076. { free also unneeded units we didn't free before }
  2077. if not needsymbolinfo then
  2078. unloaded_units.Clear;
  2079. { Does any unit use checkpointer function }
  2080. if program_uses_checkpointer then
  2081. Message1(link_w_program_uses_checkpointer,curr.modulename^);
  2082. { add all directly used packages as libraries }
  2083. add_package_libs(linker);
  2084. { finally we can create an executable }
  2085. if curr.islibrary then
  2086. linker.MakeSharedLibrary
  2087. else
  2088. linker.MakeExecutable;
  2089. { collect all necessary information for whole-program optimization }
  2090. wpoinfomanager.extractwpoinfofromprogram;
  2091. end;
  2092. procedure proc_program_after_parsing(curr : tmodule; islibrary : boolean);
  2093. var
  2094. sysinitmod, hp,hp2 : tmodule;
  2095. resources_used : boolean;
  2096. begin
  2097. sysinitmod:=nil;
  2098. hp:=nil;
  2099. hp2:=nil;
  2100. resources_used:=false;
  2101. {$ifdef DEBUG_NODE_XML}
  2102. if IsLibrary then
  2103. XMLFinalizeNodeFile('library')
  2104. else
  2105. XMLFinalizeNodeFile('program');
  2106. {$endif DEBUG_NODE_XML}
  2107. { reset wpo flags for all defs }
  2108. reset_all_defs(curr);
  2109. if (Errorcount=0) then
  2110. begin
  2111. { test static symtable }
  2112. tstoredsymtable(curr.localsymtable).allsymbolsused;
  2113. tstoredsymtable(curr.localsymtable).allprivatesused;
  2114. tstoredsymtable(curr.localsymtable).check_forwards;
  2115. curr.allunitsused;
  2116. end;
  2117. { leave when we got an error }
  2118. if (Errorcount>0) and not status.skip_error then
  2119. begin
  2120. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2121. status.skip_error:=true;
  2122. exit;
  2123. end;
  2124. { remove all unused units, this happens when units are removed
  2125. from the uses clause in the source and the ppu was already being loaded }
  2126. hp:=tmodule(loaded_units.first);
  2127. while assigned(hp) do
  2128. begin
  2129. hp2:=hp;
  2130. hp:=tmodule(hp.next);
  2131. if hp2.is_unit and
  2132. not assigned(hp2.globalsymtable) then
  2133. begin
  2134. loaded_units.remove(hp2);
  2135. unloaded_units.concat(hp2);
  2136. end;
  2137. end;
  2138. { do we need to add the variants unit? }
  2139. maybeloadvariantsunit(curr);
  2140. { Now that everything has been compiled we know if we need resource
  2141. support. If not, remove the unit. }
  2142. resources_used:=MaybeRemoveResUnit(curr);
  2143. linker.initsysinitunitname;
  2144. if target_info.system in systems_internal_sysinit then
  2145. begin
  2146. { add start/halt unit }
  2147. sysinitmod:=AddUnit(curr,linker.sysinitunit);
  2148. end
  2149. else
  2150. sysinitmod:=nil;
  2151. {$ifdef arm}
  2152. { Insert .pdata section for arm-wince.
  2153. It is needed for exception handling. }
  2154. if target_info.system in [system_arm_wince] then
  2155. InsertPData;
  2156. {$endif arm}
  2157. cnodeutils.InsertThreadvars;
  2158. { generate rtti/init tables }
  2159. write_persistent_type_info(curr.localsymtable,false);
  2160. { if an Objective-C module, generate rtti and module info }
  2161. MaybeGenerateObjectiveCImageInfo(nil,curr.localsymtable);
  2162. { generate debuginfo }
  2163. if (cs_debuginfo in current_settings.moduleswitches) then
  2164. current_debuginfo.inserttypeinfo;
  2165. if islibrary or (target_info.system in systems_unit_program_exports) then
  2166. exportlib.generatelib;
  2167. { Reference all DEBUGINFO sections from the main .fpc section }
  2168. if (cs_debuginfo in current_settings.moduleswitches) then
  2169. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2170. { Resource strings }
  2171. GenerateResourceStrings;
  2172. { Windows widestring needing initialization }
  2173. cnodeutils.InsertWideInits;
  2174. { Resourcestring references (const foo:string=someresourcestring) }
  2175. cnodeutils.InsertResStrInits;
  2176. { insert Tables and StackLength }
  2177. cnodeutils.InsertInitFinalTable(curr);
  2178. cnodeutils.InsertThreadvarTablesTable;
  2179. cnodeutils.InsertResourceTablesTable;
  2180. cnodeutils.InsertWideInitsTablesTable;
  2181. cnodeutils.InsertResStrTablesTable;
  2182. cnodeutils.InsertMemorySizes;
  2183. { Insert symbol to resource info }
  2184. cnodeutils.InsertResourceInfo(resources_used);
  2185. { create callframe info }
  2186. create_dwarf_frame;
  2187. { create import library for all packages }
  2188. if packagelist.count>0 then
  2189. createimportlibfromexternals;
  2190. { generate imports }
  2191. if curr.ImportLibraryList.Count>0 then
  2192. importlib.generatelib;
  2193. { insert own objectfile }
  2194. insertobjectfile(curr);
  2195. { assemble and link }
  2196. create_objectfile(curr);
  2197. { We might need the symbols info if not using
  2198. the default do_extractsymbolinfo
  2199. which is a dummy function PM }
  2200. needsymbolinfo:=
  2201. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2202. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2203. { release all local symtables that are not needed anymore }
  2204. if (not needsymbolinfo) then
  2205. free_localsymtables(curr.localsymtable);
  2206. { leave when we got an error }
  2207. if (Errorcount>0) and not status.skip_error then
  2208. begin
  2209. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2210. status.skip_error:=true;
  2211. exit;
  2212. end;
  2213. { create the executable when we are at level 1 }
  2214. if (not curr.is_unit) and (curr.is_initial) then
  2215. proc_create_executable(curr,sysinitmod,islibrary);
  2216. { Give Fatal with error count for linker errors }
  2217. if (Errorcount>0) and not status.skip_error then
  2218. begin
  2219. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2220. status.skip_error:=true;
  2221. end;
  2222. curr.state:=ms_processed;
  2223. end;
  2224. function proc_program_declarations(curr : tmodule; islibrary : boolean) : boolean;
  2225. var
  2226. initpd : tprocdef;
  2227. finalize_procinfo,
  2228. init_procinfo,
  2229. main_procinfo : tcgprocinfo;
  2230. force_init_final : boolean;
  2231. begin
  2232. result:=true;
  2233. main_procinfo:=nil;
  2234. init_procinfo:=nil;
  2235. finalize_procinfo:=nil;
  2236. set_current_module(curr);
  2237. { All units are read, now give them a number }
  2238. curr.updatemaps;
  2239. connect_loaded_units(curr,nil);
  2240. { consume the semicolon after maps have been updated else conditional compiling expressions
  2241. might cause internal errors, see tw8611 }
  2242. if curr.consume_semicolon_after_uses then
  2243. begin
  2244. consume(_SEMICOLON);
  2245. curr.consume_semicolon_after_uses:=false;
  2246. end;
  2247. {Insert the name of the main program into the symbol table.}
  2248. if curr.realmodulename^<>'' then
  2249. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  2250. Message1(parser_u_parsing_implementation,curr.mainsource);
  2251. symtablestack.push(curr.localsymtable);
  2252. {$ifdef jvm}
  2253. { fake classdef to represent the class corresponding to the unit }
  2254. addmoduleclass(curr);
  2255. {$endif}
  2256. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  2257. maybe_load_got(curr);
  2258. { create whole program optimisation information }
  2259. curr.wpoinfo:=tunitwpoinfo.create;
  2260. { The program intialization needs an alias, so it can be called
  2261. from the bootstrap code.}
  2262. if islibrary then
  2263. begin
  2264. initpd:=nil;
  2265. { ToDo: other systems that use indirect entry info, but check back with Windows! }
  2266. { we need to call FPC_LIBMAIN in sysinit which in turn will call PascalMain -> create dummy stub }
  2267. if target_info.system in systems_darwin then
  2268. begin
  2269. main_procinfo:=create_main_proc(make_mangledname('sysinitcallthrough',curr.localsymtable,'stub'),potype_libmainstub,curr.localsymtable);
  2270. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_LIBMAIN');
  2271. initpd:=main_procinfo.procdef;
  2272. main_procinfo.free;
  2273. end;
  2274. main_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,mainaliasname),potype_proginit,curr.localsymtable);
  2275. { Win32 startup code needs a single name }
  2276. if not(target_info.system in (systems_darwin+systems_aix)) then
  2277. main_procinfo.procdef.aliasnames.concat('PASCALMAIN')
  2278. else
  2279. main_procinfo.procdef.aliasnames.concat(target_info.Cprefix+'PASCALMAIN');
  2280. if not(target_info.system in systems_darwin) then
  2281. initpd:=main_procinfo.procdef;
  2282. cnodeutils.RegisterModuleInitFunction(initpd);
  2283. end
  2284. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macosclassic]+systems_darwin+systems_aix)) then
  2285. begin
  2286. { create a stub with the name of the desired main routine, with
  2287. the same signature as the C "main" function, and call through to
  2288. FPC_SYSTEMMAIN, which will initialise everything based on its
  2289. parameters. This function cannot be in the system unit, because
  2290. its name can be configured on the command line (for use with e.g.
  2291. SDL, where the main function should be called SDL_main) }
  2292. main_procinfo:=create_main_proc(mainaliasname,potype_mainstub,curr.localsymtable);
  2293. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_SYSTEMMAIN');
  2294. main_procinfo.free;
  2295. { now create the PASCALMAIN routine (which will be called from
  2296. FPC_SYSTEMMAIN) }
  2297. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,curr.localsymtable);
  2298. end
  2299. else
  2300. begin
  2301. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,curr.localsymtable);
  2302. main_procinfo.procdef.aliasnames.concat('PASCALMAIN');
  2303. end;
  2304. main_procinfo.parse_body;
  2305. { save file pos for debuginfo }
  2306. curr.mainfilepos:=main_procinfo.entrypos;
  2307. { finalize? }
  2308. if token=_FINALIZATION then
  2309. begin
  2310. { Parse the finalize }
  2311. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  2312. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',curr.localsymtable,''));
  2313. finalize_procinfo.procdef.aliasnames.concat('PASCALFINALIZE');
  2314. finalize_procinfo.parse_body;
  2315. end;
  2316. { Generate specializations of objectdefs methods }
  2317. if Errorcount=0 then
  2318. generate_specialization_procs;
  2319. { This needs to be done before we generate the VMTs }
  2320. if (target_cpu=tsystemcpu.cpu_wasm32) then
  2321. add_synthetic_interface_classes_for_st(curr.localsymtable,true,true);
  2322. { generate construction functions for all attributes in the program }
  2323. { before write_vmts that asume attributes for methods is ready }
  2324. generate_attr_constrs(curr.used_rtti_attrs);
  2325. { Generate VMTs }
  2326. if Errorcount=0 then
  2327. write_vmts(curr.localsymtable,false);
  2328. { add implementations for synthetic method declarations added by
  2329. the compiler }
  2330. add_synthetic_method_implementations(curr.localsymtable);
  2331. { should we force unit initialization? }
  2332. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  2333. if force_init_final or cnodeutils.force_init then
  2334. init_procinfo:=gen_implicit_initfinal(curr,mf_init,curr.localsymtable);
  2335. { Add symbol to the exports section for win32 so smartlinking a
  2336. DLL will include the edata section }
  2337. if assigned(exportlib) and
  2338. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  2339. (mf_has_exports in curr.moduleflags) then
  2340. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  2341. if (force_init_final or cnodeutils.force_final) and
  2342. (
  2343. not assigned(finalize_procinfo)
  2344. or has_no_code(finalize_procinfo.code)
  2345. ) then
  2346. begin
  2347. { first release the not used finalize procinfo }
  2348. if assigned(finalize_procinfo) then
  2349. begin
  2350. release_proc_symbol(finalize_procinfo.procdef);
  2351. release_main_proc(curr,finalize_procinfo);
  2352. end;
  2353. finalize_procinfo:=gen_implicit_initfinal(curr,mf_finalize,curr.localsymtable);
  2354. end;
  2355. { the finalization routine of libraries is generic (and all libraries need to }
  2356. { be finalized, so they can finalize any units they use }
  2357. { Place in "pure assembler" list so that the llvm assembler writer
  2358. directly emits the generated directives }
  2359. if (islibrary) then
  2360. cnodeutils.RegisterModuleFiniFunction(search_system_proc('fpc_lib_exit'));
  2361. { all labels must be defined before generating code }
  2362. if Errorcount=0 then
  2363. tstoredsymtable(curr.localsymtable).checklabels;
  2364. { See remark in unit init/final }
  2365. main_procinfo.generate_code_tree;
  2366. main_procinfo.resetprocdef;
  2367. release_main_proc(curr,main_procinfo);
  2368. if assigned(init_procinfo) then
  2369. begin
  2370. { initialization can be implicit only }
  2371. include(curr.moduleflags,mf_init);
  2372. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  2373. init_procinfo.generate_code;
  2374. init_procinfo.resetprocdef;
  2375. release_main_proc(curr,init_procinfo);
  2376. end;
  2377. if assigned(finalize_procinfo) then
  2378. begin
  2379. if force_init_final or
  2380. cnodeutils.force_init or
  2381. not(has_no_code(finalize_procinfo.code)) then
  2382. begin
  2383. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  2384. finalize_procinfo.generate_code_tree;
  2385. include(curr.moduleflags,mf_finalize);
  2386. end;
  2387. finalize_procinfo.resetprocdef;
  2388. release_main_proc(curr,finalize_procinfo);
  2389. end;
  2390. symtablestack.pop(curr.localsymtable);
  2391. { consume the last point }
  2392. consume(_POINT);
  2393. proc_program_after_parsing(curr,islibrary);
  2394. end;
  2395. procedure proc_library_header(curr: tmodule);
  2396. var
  2397. program_name : ansistring;
  2398. begin
  2399. consume(_LIBRARY);
  2400. program_name:=orgpattern;
  2401. consume(_ID);
  2402. while token=_POINT do
  2403. begin
  2404. consume(_POINT);
  2405. program_name:=program_name+'.'+orgpattern;
  2406. consume(_ID);
  2407. end;
  2408. curr.setmodulename(program_name);
  2409. curr.islibrary:=true;
  2410. exportlib.preparelib(program_name);
  2411. if tf_library_needs_pic in target_info.flags then
  2412. begin
  2413. include(current_settings.moduleswitches,cs_create_pic);
  2414. { also set create_pic for all unit compilation }
  2415. include(init_settings.moduleswitches,cs_create_pic);
  2416. end;
  2417. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2418. read, all following directives are parsed as well }
  2419. setupglobalswitches;
  2420. {$ifdef DEBUG_NODE_XML}
  2421. XMLInitializeNodeFile('library', program_name);
  2422. {$endif DEBUG_NODE_XML}
  2423. end;
  2424. type
  2425. TProgramParam = record
  2426. name : ansistring;
  2427. nr : dword;
  2428. end;
  2429. TProgramParamArray = array of TProgramParam;
  2430. procedure proc_program_header(curr: tmodule; out sc : TProgramParamArray);
  2431. var
  2432. program_name : ansistring;
  2433. paramnum : integer;
  2434. begin
  2435. sc:=nil;
  2436. consume(_PROGRAM);
  2437. program_name:=orgpattern;
  2438. consume(_ID);
  2439. while token=_POINT do
  2440. begin
  2441. consume(_POINT);
  2442. program_name:=program_name+'.'+orgpattern;
  2443. consume(_ID);
  2444. end;
  2445. curr.setmodulename(program_name);
  2446. if (target_info.system in systems_unit_program_exports) then
  2447. exportlib.preparelib(program_name);
  2448. if token=_LKLAMMER then
  2449. begin
  2450. consume(_LKLAMMER);
  2451. paramnum:=1;
  2452. repeat
  2453. if m_isolike_program_para in current_settings.modeswitches then
  2454. begin
  2455. if (pattern<>'INPUT') and (pattern<>'OUTPUT') then
  2456. begin
  2457. { the symtablestack is not setup here, so text must be created later on }
  2458. Setlength(sc,length(sc)+1);
  2459. with sc[high(sc)] do
  2460. begin
  2461. name:=pattern;
  2462. nr:=paramnum;
  2463. end;
  2464. inc(paramnum);
  2465. end;
  2466. end;
  2467. consume(_ID);
  2468. until not try_to_consume(_COMMA);
  2469. consume(_RKLAMMER);
  2470. end;
  2471. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2472. read, all following directives are parsed as well }
  2473. setupglobalswitches;
  2474. {$ifdef DEBUG_NODE_XML}
  2475. XMLInitializeNodeFile('program', program_name);
  2476. {$endif DEBUG_NODE_XML}
  2477. end;
  2478. function proc_program(curr: tmodule; islibrary : boolean) : boolean;
  2479. var
  2480. main_file : tinputfile;
  2481. consume_semicolon_after_loaded : boolean;
  2482. ps : tprogramparasym;
  2483. textsym : ttypesym;
  2484. sc : TProgramParamArray;
  2485. i : Longint;
  2486. feature : tfeature;
  2487. load_ok : boolean;
  2488. begin
  2489. result:=true;
  2490. Status.IsLibrary:=IsLibrary;
  2491. Status.IsPackage:=false;
  2492. Status.IsExe:=true;
  2493. parse_only:=false;
  2494. consume_semicolon_after_loaded:=false;
  2495. { make the compiler happy and avoid an uninitialized variable warning on Setlength(sc,length(sc)+1); }
  2496. sc:=nil;
  2497. { DLL defaults to create reloc info }
  2498. if islibrary or (target_info.system in [system_aarch64_win64]) then
  2499. begin
  2500. if not RelocSectionSetExplicitly then
  2501. RelocSection:=true;
  2502. end;
  2503. { Relocation works only without stabs under Windows when }
  2504. { external linker (LD) is used. LD generates relocs for }
  2505. { stab sections which is not loaded in memory. It causes }
  2506. { AV error when DLL is loaded and relocation is needed. }
  2507. { Internal linker does not have this problem. }
  2508. if RelocSection and
  2509. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  2510. (cs_link_extern in current_settings.globalswitches) then
  2511. begin
  2512. include(current_settings.globalswitches,cs_link_strip);
  2513. { Warning stabs info does not work with reloc section !! }
  2514. if (cs_debuginfo in current_settings.moduleswitches) and
  2515. (target_dbg.id=dbg_stabs) then
  2516. begin
  2517. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  2518. Message(parser_w_parser_win32_debug_needs_WN);
  2519. exclude(current_settings.moduleswitches,cs_debuginfo);
  2520. end;
  2521. end;
  2522. { get correct output names }
  2523. main_file := current_scanner.inputfile;
  2524. while assigned(main_file.next) do
  2525. main_file := main_file.next;
  2526. curr.SetFileName(main_file.path+main_file.name,true);
  2527. if islibrary then
  2528. begin
  2529. proc_library_header(curr);
  2530. consume_semicolon_after_loaded:=true;
  2531. end
  2532. else if token=_PROGRAM then
  2533. { is there an program head ? }
  2534. begin
  2535. proc_program_header(curr,sc);
  2536. consume_semicolon_after_loaded:=true;
  2537. end
  2538. else
  2539. begin
  2540. if (target_info.system in systems_unit_program_exports) then
  2541. exportlib.preparelib(curr.realmodulename^);
  2542. { setup things using the switches }
  2543. setupglobalswitches;
  2544. {$ifdef DEBUG_NODE_XML}
  2545. XMLInitializeNodeFile('program', curr.realmodulename^);
  2546. {$endif DEBUG_NODE_XML}
  2547. end;
  2548. { load all packages, so we know whether a unit is contained inside a
  2549. package or not }
  2550. load_packages;
  2551. { set implementation flag }
  2552. curr.in_interface:=false;
  2553. curr.interface_compiled:=true;
  2554. { insert after the unit symbol tables the static symbol table
  2555. of the program }
  2556. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  2557. { load system unit }
  2558. load_ok:=loadsystemunit(curr);
  2559. { consume the semicolon now that the system unit is loaded }
  2560. if consume_semicolon_after_loaded then
  2561. consume(_SEMICOLON);
  2562. { global switches are read, so further changes aren't allowed }
  2563. curr.in_global:=false;
  2564. { system unit is loaded, now insert feature defines }
  2565. for feature:=low(tfeature) to high(tfeature) do
  2566. if feature in features then
  2567. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  2568. { load standard units, e.g objpas,profile unit }
  2569. load_ok:=loaddefaultunits(curr) and load_ok;
  2570. { Load units provided on the command line }
  2571. load_ok:=loadautounits(curr) and load_ok;
  2572. { insert iso program parameters }
  2573. if length(sc)>0 then
  2574. begin
  2575. textsym:=search_system_type('TEXT');
  2576. if not(assigned(textsym)) then
  2577. internalerror(2013011201);
  2578. for i:=0 to high(sc) do
  2579. begin
  2580. ps:=cprogramparasym.create(sc[i].name,sc[i].nr);
  2581. curr.localsymtable.insertsym(ps,true);
  2582. end;
  2583. end;
  2584. { Load the units used by the program we compile. }
  2585. if token=_USES then
  2586. begin
  2587. // We can do this here: if there is no uses then the namespace directive makes no sense.
  2588. if Assigned(curr) then
  2589. curr.Loadlocalnamespacelist
  2590. else
  2591. current_namespacelist:=Nil;
  2592. parseusesclause(curr);
  2593. load_ok:=loadunits(curr,false) and load_ok;
  2594. curr.consume_semicolon_after_uses:=true;
  2595. end
  2596. else
  2597. curr.consume_semicolon_after_uses:=false;
  2598. if not load_ok then
  2599. curr.state:=ms_compiling_wait;
  2600. { Can we continue compiling ? }
  2601. result:=curr.state<>ms_compiling_wait;
  2602. if result then
  2603. result:=proc_program_declarations(curr,islibrary)
  2604. end;
  2605. end.