pmodules.pas 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008
  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. {$ifdef EXTDEBUG}
  1573. store_crc,
  1574. {$endif EXTDEBUG}
  1575. store_interface_crc,
  1576. store_indirect_crc : cardinal;
  1577. i : longint;
  1578. waitingmodule : tmodule;
  1579. begin
  1580. result:=true;
  1581. { Write out the ppufile after the object file has been created }
  1582. store_interface_crc:=module.interface_crc;
  1583. store_indirect_crc:=module.indirect_crc;
  1584. {$ifdef EXTDEBUG}
  1585. store_crc:=module.crc;
  1586. {$endif EXTDEBUG}
  1587. if ErrorCount=0 then
  1588. tppumodule(module).writeppu;
  1589. if not(cs_compilesystem in current_settings.moduleswitches) then
  1590. begin
  1591. if store_interface_crc<>module.interface_crc then
  1592. Message1(unit_u_interface_crc_changed,module.ppufilename);
  1593. if store_indirect_crc<>module.indirect_crc then
  1594. Message1(unit_u_indirect_crc_changed,module.ppufilename);
  1595. end;
  1596. {$ifdef EXTDEBUG}
  1597. if not(cs_compilesystem in current_settings.moduleswitches) then
  1598. if (store_crc<>module.crc) then
  1599. Message1(unit_u_implementation_crc_changed,module.ppufilename);
  1600. {$endif EXTDEBUG}
  1601. { release unregistered defs/syms from the localsymtable }
  1602. free_unregistered_localsymtable_elements(module);
  1603. { release local symtables that are not needed anymore }
  1604. free_localsymtables(module.globalsymtable);
  1605. free_localsymtables(module.localsymtable);
  1606. { leave when we got an error }
  1607. if (Errorcount>0) and not status.skip_error then
  1608. begin
  1609. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1610. status.skip_error:=true;
  1611. module_is_done(module);
  1612. {$ifdef DEBUG_NODE_XML}
  1613. XMLFinalizeNodeFile('unit');
  1614. {$endif DEBUG_NODE_XML}
  1615. exit;
  1616. end;
  1617. {$ifdef debug_devirt}
  1618. { print out all instantiated class/object types }
  1619. writeln('constructed object/class/classreftypes in ',module.realmodulename^);
  1620. for i := 0 to module.wpoinfo.createdobjtypes.count-1 do
  1621. begin
  1622. write(' ',tdef(module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1623. case tdef(module.wpoinfo.createdobjtypes[i]).typ of
  1624. objectdef:
  1625. case tobjectdef(module.wpoinfo.createdobjtypes[i]).objecttype of
  1626. odt_object:
  1627. writeln(' (object)');
  1628. odt_class:
  1629. writeln(' (class)');
  1630. else
  1631. internalerror(2008101103);
  1632. end;
  1633. else
  1634. internalerror(2008101104);
  1635. end;
  1636. end;
  1637. for i := 0 to module.wpoinfo.createdclassrefobjtypes.count-1 do
  1638. begin
  1639. write(' Class Of ',tdef(module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1640. case tdef(module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1641. objectdef:
  1642. case tobjectdef(module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1643. odt_class:
  1644. writeln(' (classrefdef)');
  1645. else
  1646. internalerror(2008101105);
  1647. end
  1648. else
  1649. internalerror(2008101102);
  1650. end;
  1651. end;
  1652. {$endif debug_devirt}
  1653. Message1(unit_u_finished_compiling,module.modulename^);
  1654. module_is_done(module);
  1655. module.end_of_parsing;
  1656. {$ifdef DEBUG_NODE_XML}
  1657. XMLFinalizeNodeFile('unit');
  1658. {$endif DEBUG_NODE_XML}
  1659. end;
  1660. function proc_package(curr: tmodule) : boolean;
  1661. var
  1662. main_file : tinputfile;
  1663. hp,hp2 : tmodule;
  1664. pkg : tpcppackage;
  1665. main_procinfo : tcgprocinfo;
  1666. force_init_final : boolean;
  1667. uu : tused_unit;
  1668. module_name: ansistring;
  1669. pentry: ppackageentry;
  1670. feature : tfeature;
  1671. begin
  1672. Result:=True;
  1673. Status.IsPackage:=true;
  1674. Status.IsExe:=true;
  1675. parse_only:=false;
  1676. main_procinfo:=nil;
  1677. {init_procinfo:=nil;
  1678. finalize_procinfo:=nil;}
  1679. if not (tf_supports_packages in target_info.flags) then
  1680. message1(parser_e_packages_not_supported,target_info.name);
  1681. if not RelocSectionSetExplicitly then
  1682. RelocSection:=true;
  1683. { Relocation works only without stabs under Windows when }
  1684. { external linker (LD) is used. LD generates relocs for }
  1685. { stab sections which is not loaded in memory. It causes }
  1686. { AV error when DLL is loaded and relocation is needed. }
  1687. { Internal linker does not have this problem. }
  1688. if RelocSection and
  1689. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1690. (cs_link_extern in current_settings.globalswitches) then
  1691. begin
  1692. include(current_settings.globalswitches,cs_link_strip);
  1693. { Warning stabs info does not work with reloc section !! }
  1694. if (cs_debuginfo in current_settings.moduleswitches) and
  1695. (target_dbg.id=dbg_stabs) then
  1696. begin
  1697. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  1698. Message(parser_w_parser_win32_debug_needs_WN);
  1699. exclude(current_settings.moduleswitches,cs_debuginfo);
  1700. end;
  1701. end;
  1702. { get correct output names }
  1703. main_file := current_scanner.inputfile;
  1704. while assigned(main_file.next) do
  1705. main_file := main_file.next;
  1706. curr.SetFileName(main_file.path+main_file.name,true);
  1707. { consume _PACKAGE word }
  1708. consume(_ID);
  1709. module_name:=orgpattern;
  1710. consume(_ID);
  1711. while token=_POINT do
  1712. begin
  1713. consume(_POINT);
  1714. module_name:=module_name+'.'+orgpattern;
  1715. consume(_ID);
  1716. end;
  1717. curr.setmodulename(module_name);
  1718. curr.ispackage:=true;
  1719. exportlib.preparelib(module_name);
  1720. pkg:=tpcppackage.create(module_name);
  1721. if tf_library_needs_pic in target_info.flags then
  1722. include(current_settings.moduleswitches,cs_create_pic);
  1723. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1724. read, all following directives are parsed as well }
  1725. setupglobalswitches;
  1726. {$ifdef DEBUG_NODE_XML}
  1727. XMLInitializeNodeFile('package', module_name);
  1728. {$endif DEBUG_NODE_XML}
  1729. consume(_SEMICOLON);
  1730. { global switches are read, so further changes aren't allowed }
  1731. curr.in_global:=false;
  1732. { set implementation flag }
  1733. curr.in_interface:=false;
  1734. curr.interface_compiled:=true;
  1735. { insert after the unit symbol tables the static symbol table }
  1736. { of the program }
  1737. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  1738. { ensure that no packages are picked up from the options }
  1739. packagelist.clear;
  1740. // There should always be a requires, except for the system package. So we load here
  1741. if Assigned(curr) then
  1742. curr.Loadlocalnamespacelist
  1743. else
  1744. current_namespacelist:=Nil;
  1745. {Read the packages used by the package we compile.}
  1746. if (token=_ID) and (idtoken=_REQUIRES) then
  1747. begin
  1748. { consume _REQUIRES word }
  1749. consume(_ID);
  1750. while true do
  1751. begin
  1752. if token=_ID then
  1753. begin
  1754. module_name:=orgpattern;
  1755. consume(_ID);
  1756. while token=_POINT do
  1757. begin
  1758. consume(_POINT);
  1759. module_name:=module_name+'.'+orgpattern;
  1760. consume(_ID);
  1761. end;
  1762. add_package(module_name,false,true);
  1763. end
  1764. else
  1765. consume(_ID);
  1766. if token=_COMMA then
  1767. consume(_COMMA)
  1768. else
  1769. break;
  1770. end;
  1771. consume(_SEMICOLON);
  1772. end;
  1773. { now load all packages, so that we can determine whether a unit is
  1774. already provided by one of the loaded packages }
  1775. load_packages;
  1776. if packagelist.Count>0 then
  1777. begin
  1778. { this means the SYSTEM unit *must* be part of one of the required
  1779. packages, so load it }
  1780. AddUnit(curr,'system',false);
  1781. systemunit:=tglobalsymtable(symtablestack.top);
  1782. load_intern_types;
  1783. { system unit is loaded, now insert feature defines }
  1784. for feature:=low(tfeature) to high(tfeature) do
  1785. if feature in features then
  1786. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1787. end;
  1788. {Load the units used by the program we compile.}
  1789. if (token=_ID) and (idtoken=_CONTAINS) then
  1790. begin
  1791. { consume _CONTAINS word }
  1792. consume(_ID);
  1793. while true do
  1794. begin
  1795. if token=_ID then
  1796. begin
  1797. module_name:=orgpattern;
  1798. consume(_ID);
  1799. while token=_POINT do
  1800. begin
  1801. consume(_POINT);
  1802. module_name:=module_name+'.'+orgpattern;
  1803. consume(_ID);
  1804. end;
  1805. hp:=AddUnit(curr,module_name);
  1806. if (hp.modulename^='SYSTEM') and not assigned(systemunit) then
  1807. begin
  1808. systemunit:=tglobalsymtable(hp.globalsymtable);
  1809. load_intern_types;
  1810. end;
  1811. end
  1812. else
  1813. consume(_ID);
  1814. if token=_COMMA then
  1815. consume(_COMMA)
  1816. else break;
  1817. end;
  1818. consume(_SEMICOLON);
  1819. end;
  1820. { All units are read, now give them a number }
  1821. curr.updatemaps;
  1822. hp:=tmodule(loaded_units.first);
  1823. while assigned(hp) do
  1824. begin
  1825. if (hp<>curr) and not assigned(hp.package) then
  1826. begin
  1827. if mf_package_deny in hp.moduleflags then
  1828. message1(package_e_unit_deny_package,hp.realmodulename^);
  1829. { part of the package's used, aka contained units? }
  1830. uu:=tused_unit(curr.used_units.first);
  1831. while assigned(uu) do
  1832. begin
  1833. if uu.u=hp then
  1834. break;
  1835. uu:=tused_unit(uu.next);
  1836. end;
  1837. if not assigned(uu) then
  1838. message2(package_n_implicit_unit_import,hp.realmodulename^,curr.realmodulename^);
  1839. end;
  1840. { was this unit listed as a contained unit? If so => error }
  1841. if (hp<>curr) and assigned(hp.package) then
  1842. begin
  1843. uu:=tused_unit(curr.used_units.first);
  1844. while assigned(uu) do
  1845. begin
  1846. if uu.u=hp then
  1847. break;
  1848. uu:=tused_unit(uu.next);
  1849. end;
  1850. if assigned(uu) then
  1851. message2(package_e_unit_already_contained_in_package,hp.realmodulename^,hp.package.realpackagename^);
  1852. end;
  1853. hp:=tmodule(hp.next);
  1854. end;
  1855. {Insert the name of the main program into the symbol table.}
  1856. if curr.realmodulename^<>'' then
  1857. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1858. Message1(parser_u_parsing_implementation,curr.mainsource);
  1859. symtablestack.push(curr.localsymtable);
  1860. { create whole program optimisation information }
  1861. curr.wpoinfo:=tunitwpoinfo.create;
  1862. { should we force unit initialization? }
  1863. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  1864. if force_init_final or cnodeutils.force_init then
  1865. {init_procinfo:=gen_implicit_initfinal(mf_init,curr.localsymtable)};
  1866. { Add symbol to the exports section for win32 so smartlinking a
  1867. DLL will include the edata section }
  1868. if assigned(exportlib) and
  1869. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1870. (mf_has_exports in curr.moduleflags) then
  1871. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  1872. { all labels must be defined before generating code }
  1873. if Errorcount=0 then
  1874. tstoredsymtable(curr.localsymtable).checklabels;
  1875. symtablestack.pop(curr.localsymtable);
  1876. { consume the last point }
  1877. consume(_END);
  1878. consume(_POINT);
  1879. if (Errorcount=0) then
  1880. begin
  1881. { test static symtable }
  1882. tstoredsymtable(curr.localsymtable).allsymbolsused;
  1883. tstoredsymtable(curr.localsymtable).allprivatesused;
  1884. tstoredsymtable(curr.localsymtable).check_forwards;
  1885. { Note: all contained units are considered as used }
  1886. end;
  1887. if target_info.system in systems_all_windows+systems_nativent then
  1888. begin
  1889. main_procinfo:=create_main_proc('_PkgEntryPoint',potype_pkgstub,curr.localsymtable);
  1890. main_procinfo.procdef.aliasnames.concat('_DLLMainCRTStartup');
  1891. main_procinfo.code:=generate_pkg_stub(main_procinfo.procdef);
  1892. main_procinfo.generate_code;
  1893. end;
  1894. {$ifdef DEBUG_NODE_XML}
  1895. XMLFinalizeNodeFile('package');
  1896. {$endif DEBUG_NODE_XML}
  1897. { leave when we got an error }
  1898. if (Errorcount>0) and not status.skip_error then
  1899. begin
  1900. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1901. status.skip_error:=true;
  1902. pkg.free;
  1903. exit;
  1904. end;
  1905. { remove all unused units, this happends when units are removed
  1906. from the uses clause in the source and the ppu was already being loaded }
  1907. hp:=tmodule(loaded_units.first);
  1908. while assigned(hp) do
  1909. begin
  1910. hp2:=hp;
  1911. hp:=tmodule(hp.next);
  1912. if assigned(hp2.package) then
  1913. add_package_unit_ref(hp2.package);
  1914. if hp2.is_unit and
  1915. not assigned(hp2.globalsymtable) then
  1916. loaded_units.remove(hp2);
  1917. end;
  1918. exportlib.ignoreduplicates:=true;
  1919. { force exports }
  1920. uu:=tused_unit(usedunits.first);
  1921. while assigned(uu) do
  1922. begin
  1923. if not assigned(systemunit) and (uu.u.modulename^='SYSTEM') then
  1924. begin
  1925. systemunit:=tglobalsymtable(uu.u.globalsymtable);
  1926. load_intern_types;
  1927. end;
  1928. if not assigned(uu.u.package) then
  1929. export_unit(uu.u);
  1930. uu:=tused_unit(uu.next);
  1931. end;
  1932. {$ifdef arm}
  1933. { Insert .pdata section for arm-wince.
  1934. It is needed for exception handling. }
  1935. if target_info.system in [system_arm_wince] then
  1936. InsertPData;
  1937. {$endif arm}
  1938. { generate debuginfo }
  1939. if (cs_debuginfo in current_settings.moduleswitches) then
  1940. current_debuginfo.inserttypeinfo;
  1941. exportlib.generatelib;
  1942. exportlib.ignoreduplicates:=false;
  1943. { create import libraries for all packages }
  1944. if packagelist.count>0 then
  1945. createimportlibfromexternals;
  1946. { generate imports }
  1947. if curr.ImportLibraryList.Count>0 then
  1948. importlib.generatelib;
  1949. { Reference all DEBUGINFO sections from the main .fpc section }
  1950. if (cs_debuginfo in current_settings.moduleswitches) then
  1951. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1952. { insert own objectfile }
  1953. insertobjectfile(curr);
  1954. { assemble and link }
  1955. create_objectfile(curr);
  1956. { We might need the symbols info if not using
  1957. the default do_extractsymbolinfo
  1958. which is a dummy function PM }
  1959. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1960. { release all local symtables that are not needed anymore }
  1961. if (not needsymbolinfo) then
  1962. free_localsymtables(curr.localsymtable);
  1963. { leave when we got an error }
  1964. if (Errorcount>0) and not status.skip_error then
  1965. begin
  1966. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1967. status.skip_error:=true;
  1968. pkg.free;
  1969. exit;
  1970. end;
  1971. if (not curr.is_unit) then
  1972. begin
  1973. { we add all loaded units that are not part of a package to the
  1974. package; this includes units in the "contains" section as well
  1975. as implicitely imported ones }
  1976. hp:=tmodule(loaded_units.first);
  1977. while assigned(hp) do
  1978. begin
  1979. if (hp<>curr) then
  1980. begin
  1981. if not assigned(hp.package) then
  1982. begin
  1983. pkg.addunit(hp);
  1984. check_for_indirect_package_usages(hp.used_units);
  1985. end
  1986. else
  1987. begin
  1988. pentry:=ppackageentry(packagelist.find(hp.package.packagename^));
  1989. if not assigned(pentry) then
  1990. internalerror(2015112301);
  1991. pkg.add_required_package(hp.package);
  1992. end;
  1993. end;
  1994. hp:=tmodule(hp.next);
  1995. end;
  1996. pkg.initmoduleinfo(curr);
  1997. { create the executable when we are at level 1 }
  1998. if (curr.is_initial) then
  1999. begin
  2000. { create global resource file by collecting all resource files }
  2001. CollectResourceFiles;
  2002. { write .def file }
  2003. if (cs_link_deffile in current_settings.globalswitches) then
  2004. deffile.writefile;
  2005. { generate the pcp file }
  2006. pkg.savepcp;
  2007. { insert all .o files from all loaded units and
  2008. unload the units, we don't need them anymore.
  2009. Keep the curr because that is still needed }
  2010. hp:=tmodule(loaded_units.first);
  2011. while assigned(hp) do
  2012. begin
  2013. { only link in those units which should become part of this
  2014. package }
  2015. if not assigned(hp.package) then
  2016. linker.AddModuleFiles(hp);
  2017. hp2:=tmodule(hp.next);
  2018. if (hp<>curr) and
  2019. (not needsymbolinfo) then
  2020. begin
  2021. loaded_units.remove(hp);
  2022. hp.free;
  2023. end;
  2024. hp:=hp2;
  2025. end;
  2026. { add the library of directly used packages }
  2027. add_package_libs(linker);
  2028. { and now link the package library }
  2029. linker.MakeSharedLibrary
  2030. end;
  2031. { Give Fatal with error count for linker errors }
  2032. if (Errorcount>0) and not status.skip_error then
  2033. begin
  2034. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2035. status.skip_error:=true;
  2036. end;
  2037. pkg.free;
  2038. end;
  2039. end;
  2040. procedure proc_create_executable(curr, sysinitmod: tmodule; islibrary : boolean);
  2041. var
  2042. program_uses_checkpointer : boolean;
  2043. hp,hp2 : tmodule;
  2044. begin
  2045. { create global resource file by collecting all resource files }
  2046. CollectResourceFiles;
  2047. { write .def file }
  2048. if (cs_link_deffile in current_settings.globalswitches) then
  2049. deffile.writefile;
  2050. { link SysInit (if any) first, to have behavior consistent with
  2051. assembler startup files }
  2052. if assigned(sysinitmod) then
  2053. linker.AddModuleFiles(sysinitmod);
  2054. { Does any unit use checkpointer function }
  2055. program_uses_checkpointer:=false;
  2056. { insert all .o files from all loaded units and
  2057. unload the units, we don't need them anymore.
  2058. Keep the curr because that is still needed }
  2059. hp:=tmodule(loaded_units.first);
  2060. while assigned(hp) do
  2061. begin
  2062. if (hp<>sysinitmod) and not assigned(hp.package) then
  2063. begin
  2064. linker.AddModuleFiles(hp);
  2065. if mf_checkpointer_called in hp.moduleflags then
  2066. program_uses_checkpointer:=true;
  2067. end;
  2068. hp2:=tmodule(hp.next);
  2069. if assigned(hp.package) then
  2070. add_package_unit_ref(hp.package);
  2071. if (hp<>curr) and
  2072. (not needsymbolinfo) then
  2073. begin
  2074. loaded_units.remove(hp);
  2075. hp.free;
  2076. end;
  2077. hp:=hp2;
  2078. end;
  2079. { free also unneeded units we didn't free before }
  2080. if not needsymbolinfo then
  2081. unloaded_units.Clear;
  2082. { Does any unit use checkpointer function }
  2083. if program_uses_checkpointer then
  2084. Message1(link_w_program_uses_checkpointer,curr.modulename^);
  2085. { add all directly used packages as libraries }
  2086. add_package_libs(linker);
  2087. { finally we can create an executable }
  2088. if curr.islibrary then
  2089. linker.MakeSharedLibrary
  2090. else
  2091. linker.MakeExecutable;
  2092. { collect all necessary information for whole-program optimization }
  2093. wpoinfomanager.extractwpoinfofromprogram;
  2094. end;
  2095. procedure proc_program_after_parsing(curr : tmodule; islibrary : boolean);
  2096. var
  2097. sysinitmod, hp,hp2 : tmodule;
  2098. resources_used : boolean;
  2099. begin
  2100. sysinitmod:=nil;
  2101. hp:=nil;
  2102. hp2:=nil;
  2103. resources_used:=false;
  2104. {$ifdef DEBUG_NODE_XML}
  2105. if IsLibrary then
  2106. XMLFinalizeNodeFile('library')
  2107. else
  2108. XMLFinalizeNodeFile('program');
  2109. {$endif DEBUG_NODE_XML}
  2110. { reset wpo flags for all defs }
  2111. reset_all_defs(curr);
  2112. if (Errorcount=0) then
  2113. begin
  2114. { test static symtable }
  2115. tstoredsymtable(curr.localsymtable).allsymbolsused;
  2116. tstoredsymtable(curr.localsymtable).allprivatesused;
  2117. tstoredsymtable(curr.localsymtable).check_forwards;
  2118. curr.allunitsused;
  2119. end;
  2120. { leave when we got an error }
  2121. if (Errorcount>0) and not status.skip_error then
  2122. begin
  2123. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2124. status.skip_error:=true;
  2125. exit;
  2126. end;
  2127. { remove all unused units, this happens when units are removed
  2128. from the uses clause in the source and the ppu was already being loaded }
  2129. hp:=tmodule(loaded_units.first);
  2130. while assigned(hp) do
  2131. begin
  2132. hp2:=hp;
  2133. hp:=tmodule(hp.next);
  2134. if hp2.is_unit and
  2135. not assigned(hp2.globalsymtable) then
  2136. begin
  2137. loaded_units.remove(hp2);
  2138. unloaded_units.concat(hp2);
  2139. end;
  2140. end;
  2141. { do we need to add the variants unit? }
  2142. maybeloadvariantsunit(curr);
  2143. { Now that everything has been compiled we know if we need resource
  2144. support. If not, remove the unit. }
  2145. resources_used:=MaybeRemoveResUnit(curr);
  2146. linker.initsysinitunitname;
  2147. if target_info.system in systems_internal_sysinit then
  2148. begin
  2149. { add start/halt unit }
  2150. sysinitmod:=AddUnit(curr,linker.sysinitunit);
  2151. end
  2152. else
  2153. sysinitmod:=nil;
  2154. {$ifdef arm}
  2155. { Insert .pdata section for arm-wince.
  2156. It is needed for exception handling. }
  2157. if target_info.system in [system_arm_wince] then
  2158. InsertPData;
  2159. {$endif arm}
  2160. cnodeutils.InsertThreadvars;
  2161. { generate rtti/init tables }
  2162. write_persistent_type_info(curr.localsymtable,false);
  2163. { if an Objective-C module, generate rtti and module info }
  2164. MaybeGenerateObjectiveCImageInfo(nil,curr.localsymtable);
  2165. { generate debuginfo }
  2166. if (cs_debuginfo in current_settings.moduleswitches) then
  2167. current_debuginfo.inserttypeinfo;
  2168. if islibrary or (target_info.system in systems_unit_program_exports) then
  2169. exportlib.generatelib;
  2170. { Reference all DEBUGINFO sections from the main .fpc section }
  2171. if (cs_debuginfo in current_settings.moduleswitches) then
  2172. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2173. { Resource strings }
  2174. GenerateResourceStrings;
  2175. { Windows widestring needing initialization }
  2176. cnodeutils.InsertWideInits;
  2177. { Resourcestring references (const foo:string=someresourcestring) }
  2178. cnodeutils.InsertResStrInits;
  2179. { insert Tables and StackLength }
  2180. cnodeutils.InsertInitFinalTable(curr);
  2181. cnodeutils.InsertThreadvarTablesTable;
  2182. cnodeutils.InsertResourceTablesTable;
  2183. cnodeutils.InsertWideInitsTablesTable;
  2184. cnodeutils.InsertResStrTablesTable;
  2185. cnodeutils.InsertMemorySizes;
  2186. { Insert symbol to resource info }
  2187. cnodeutils.InsertResourceInfo(resources_used);
  2188. { create callframe info }
  2189. create_dwarf_frame;
  2190. { create import library for all packages }
  2191. if packagelist.count>0 then
  2192. createimportlibfromexternals;
  2193. { generate imports }
  2194. if curr.ImportLibraryList.Count>0 then
  2195. importlib.generatelib;
  2196. { insert own objectfile }
  2197. insertobjectfile(curr);
  2198. { assemble and link }
  2199. create_objectfile(curr);
  2200. { We might need the symbols info if not using
  2201. the default do_extractsymbolinfo
  2202. which is a dummy function PM }
  2203. needsymbolinfo:=
  2204. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2205. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2206. { release all local symtables that are not needed anymore }
  2207. if (not needsymbolinfo) then
  2208. free_localsymtables(curr.localsymtable);
  2209. { leave when we got an error }
  2210. if (Errorcount>0) and not status.skip_error then
  2211. begin
  2212. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2213. status.skip_error:=true;
  2214. exit;
  2215. end;
  2216. { create the executable when we are at level 1 }
  2217. if (not curr.is_unit) and (curr.is_initial) then
  2218. proc_create_executable(curr,sysinitmod,islibrary);
  2219. { Give Fatal with error count for linker errors }
  2220. if (Errorcount>0) and not status.skip_error then
  2221. begin
  2222. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2223. status.skip_error:=true;
  2224. end;
  2225. curr.state:=ms_processed;
  2226. end;
  2227. function proc_program_declarations(curr : tmodule; islibrary : boolean) : boolean;
  2228. var
  2229. initpd : tprocdef;
  2230. finalize_procinfo,
  2231. init_procinfo,
  2232. main_procinfo : tcgprocinfo;
  2233. force_init_final : boolean;
  2234. begin
  2235. result:=true;
  2236. main_procinfo:=nil;
  2237. init_procinfo:=nil;
  2238. finalize_procinfo:=nil;
  2239. set_current_module(curr);
  2240. { All units are read, now give them a number }
  2241. curr.updatemaps;
  2242. connect_loaded_units(curr,nil);
  2243. { consume the semicolon after maps have been updated else conditional compiling expressions
  2244. might cause internal errors, see tw8611 }
  2245. if curr.consume_semicolon_after_uses then
  2246. begin
  2247. consume(_SEMICOLON);
  2248. curr.consume_semicolon_after_uses:=false;
  2249. end;
  2250. {Insert the name of the main program into the symbol table.}
  2251. if curr.realmodulename^<>'' then
  2252. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  2253. Message1(parser_u_parsing_implementation,curr.mainsource);
  2254. symtablestack.push(curr.localsymtable);
  2255. {$ifdef jvm}
  2256. { fake classdef to represent the class corresponding to the unit }
  2257. addmoduleclass(curr);
  2258. {$endif}
  2259. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  2260. maybe_load_got(curr);
  2261. { create whole program optimisation information }
  2262. curr.wpoinfo:=tunitwpoinfo.create;
  2263. { The program intialization needs an alias, so it can be called
  2264. from the bootstrap code.}
  2265. if islibrary then
  2266. begin
  2267. initpd:=nil;
  2268. { ToDo: other systems that use indirect entry info, but check back with Windows! }
  2269. { we need to call FPC_LIBMAIN in sysinit which in turn will call PascalMain -> create dummy stub }
  2270. if target_info.system in systems_darwin then
  2271. begin
  2272. main_procinfo:=create_main_proc(make_mangledname('sysinitcallthrough',curr.localsymtable,'stub'),potype_libmainstub,curr.localsymtable);
  2273. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_LIBMAIN');
  2274. initpd:=main_procinfo.procdef;
  2275. main_procinfo.free;
  2276. end;
  2277. main_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,mainaliasname),potype_proginit,curr.localsymtable);
  2278. { Win32 startup code needs a single name }
  2279. if not(target_info.system in (systems_darwin+systems_aix)) then
  2280. main_procinfo.procdef.aliasnames.concat('PASCALMAIN')
  2281. else
  2282. main_procinfo.procdef.aliasnames.concat(target_info.Cprefix+'PASCALMAIN');
  2283. if not(target_info.system in systems_darwin) then
  2284. initpd:=main_procinfo.procdef;
  2285. cnodeutils.RegisterModuleInitFunction(initpd);
  2286. end
  2287. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macosclassic]+systems_darwin+systems_aix)) then
  2288. begin
  2289. { create a stub with the name of the desired main routine, with
  2290. the same signature as the C "main" function, and call through to
  2291. FPC_SYSTEMMAIN, which will initialise everything based on its
  2292. parameters. This function cannot be in the system unit, because
  2293. its name can be configured on the command line (for use with e.g.
  2294. SDL, where the main function should be called SDL_main) }
  2295. main_procinfo:=create_main_proc(mainaliasname,potype_mainstub,curr.localsymtable);
  2296. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_SYSTEMMAIN');
  2297. main_procinfo.free;
  2298. { now create the PASCALMAIN routine (which will be called from
  2299. FPC_SYSTEMMAIN) }
  2300. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,curr.localsymtable);
  2301. end
  2302. else
  2303. begin
  2304. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,curr.localsymtable);
  2305. main_procinfo.procdef.aliasnames.concat('PASCALMAIN');
  2306. end;
  2307. main_procinfo.parse_body;
  2308. { save file pos for debuginfo }
  2309. curr.mainfilepos:=main_procinfo.entrypos;
  2310. { finalize? }
  2311. if token=_FINALIZATION then
  2312. begin
  2313. { Parse the finalize }
  2314. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  2315. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',curr.localsymtable,''));
  2316. finalize_procinfo.procdef.aliasnames.concat('PASCALFINALIZE');
  2317. finalize_procinfo.parse_body;
  2318. end;
  2319. { Generate specializations of objectdefs methods }
  2320. if Errorcount=0 then
  2321. generate_specialization_procs;
  2322. { This needs to be done before we generate the VMTs }
  2323. if (target_cpu=tsystemcpu.cpu_wasm32) then
  2324. add_synthetic_interface_classes_for_st(curr.localsymtable,true,true);
  2325. { generate construction functions for all attributes in the program }
  2326. { before write_vmts that asume attributes for methods is ready }
  2327. generate_attr_constrs(curr.used_rtti_attrs);
  2328. { Generate VMTs }
  2329. if Errorcount=0 then
  2330. write_vmts(curr.localsymtable,false);
  2331. { add implementations for synthetic method declarations added by
  2332. the compiler }
  2333. add_synthetic_method_implementations(curr.localsymtable);
  2334. { should we force unit initialization? }
  2335. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  2336. if force_init_final or cnodeutils.force_init then
  2337. init_procinfo:=gen_implicit_initfinal(curr,mf_init,curr.localsymtable);
  2338. { Add symbol to the exports section for win32 so smartlinking a
  2339. DLL will include the edata section }
  2340. if assigned(exportlib) and
  2341. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  2342. (mf_has_exports in curr.moduleflags) then
  2343. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  2344. if (force_init_final or cnodeutils.force_final) and
  2345. (
  2346. not assigned(finalize_procinfo)
  2347. or has_no_code(finalize_procinfo.code)
  2348. ) then
  2349. begin
  2350. { first release the not used finalize procinfo }
  2351. if assigned(finalize_procinfo) then
  2352. begin
  2353. release_proc_symbol(finalize_procinfo.procdef);
  2354. release_main_proc(curr,finalize_procinfo);
  2355. end;
  2356. finalize_procinfo:=gen_implicit_initfinal(curr,mf_finalize,curr.localsymtable);
  2357. end;
  2358. { the finalization routine of libraries is generic (and all libraries need to }
  2359. { be finalized, so they can finalize any units they use }
  2360. { Place in "pure assembler" list so that the llvm assembler writer
  2361. directly emits the generated directives }
  2362. if (islibrary) then
  2363. cnodeutils.RegisterModuleFiniFunction(search_system_proc('fpc_lib_exit'));
  2364. { all labels must be defined before generating code }
  2365. if Errorcount=0 then
  2366. tstoredsymtable(curr.localsymtable).checklabels;
  2367. { See remark in unit init/final }
  2368. main_procinfo.generate_code_tree;
  2369. main_procinfo.resetprocdef;
  2370. release_main_proc(curr,main_procinfo);
  2371. if assigned(init_procinfo) then
  2372. begin
  2373. { initialization can be implicit only }
  2374. include(curr.moduleflags,mf_init);
  2375. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  2376. init_procinfo.generate_code;
  2377. init_procinfo.resetprocdef;
  2378. release_main_proc(curr,init_procinfo);
  2379. end;
  2380. if assigned(finalize_procinfo) then
  2381. begin
  2382. if force_init_final or
  2383. cnodeutils.force_init or
  2384. not(has_no_code(finalize_procinfo.code)) then
  2385. begin
  2386. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  2387. finalize_procinfo.generate_code_tree;
  2388. include(curr.moduleflags,mf_finalize);
  2389. end;
  2390. finalize_procinfo.resetprocdef;
  2391. release_main_proc(curr,finalize_procinfo);
  2392. end;
  2393. symtablestack.pop(curr.localsymtable);
  2394. { consume the last point }
  2395. consume(_POINT);
  2396. proc_program_after_parsing(curr,islibrary);
  2397. end;
  2398. procedure proc_library_header(curr: tmodule);
  2399. var
  2400. program_name : ansistring;
  2401. begin
  2402. consume(_LIBRARY);
  2403. program_name:=orgpattern;
  2404. consume(_ID);
  2405. while token=_POINT do
  2406. begin
  2407. consume(_POINT);
  2408. program_name:=program_name+'.'+orgpattern;
  2409. consume(_ID);
  2410. end;
  2411. curr.setmodulename(program_name);
  2412. curr.islibrary:=true;
  2413. exportlib.preparelib(program_name);
  2414. if tf_library_needs_pic in target_info.flags then
  2415. begin
  2416. include(current_settings.moduleswitches,cs_create_pic);
  2417. { also set create_pic for all unit compilation }
  2418. include(init_settings.moduleswitches,cs_create_pic);
  2419. end;
  2420. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2421. read, all following directives are parsed as well }
  2422. setupglobalswitches;
  2423. {$ifdef DEBUG_NODE_XML}
  2424. XMLInitializeNodeFile('library', program_name);
  2425. {$endif DEBUG_NODE_XML}
  2426. end;
  2427. type
  2428. TProgramParam = record
  2429. name : ansistring;
  2430. nr : dword;
  2431. end;
  2432. TProgramParamArray = array of TProgramParam;
  2433. procedure proc_program_header(curr: tmodule; out sc : TProgramParamArray);
  2434. var
  2435. program_name : ansistring;
  2436. paramnum : integer;
  2437. begin
  2438. sc:=nil;
  2439. consume(_PROGRAM);
  2440. program_name:=orgpattern;
  2441. consume(_ID);
  2442. while token=_POINT do
  2443. begin
  2444. consume(_POINT);
  2445. program_name:=program_name+'.'+orgpattern;
  2446. consume(_ID);
  2447. end;
  2448. curr.setmodulename(program_name);
  2449. if (target_info.system in systems_unit_program_exports) then
  2450. exportlib.preparelib(program_name);
  2451. if token=_LKLAMMER then
  2452. begin
  2453. consume(_LKLAMMER);
  2454. paramnum:=1;
  2455. repeat
  2456. if m_isolike_program_para in current_settings.modeswitches then
  2457. begin
  2458. if (pattern<>'INPUT') and (pattern<>'OUTPUT') then
  2459. begin
  2460. { the symtablestack is not setup here, so text must be created later on }
  2461. Setlength(sc,length(sc)+1);
  2462. with sc[high(sc)] do
  2463. begin
  2464. name:=pattern;
  2465. nr:=paramnum;
  2466. end;
  2467. inc(paramnum);
  2468. end;
  2469. end;
  2470. consume(_ID);
  2471. until not try_to_consume(_COMMA);
  2472. consume(_RKLAMMER);
  2473. end;
  2474. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2475. read, all following directives are parsed as well }
  2476. setupglobalswitches;
  2477. {$ifdef DEBUG_NODE_XML}
  2478. XMLInitializeNodeFile('program', program_name);
  2479. {$endif DEBUG_NODE_XML}
  2480. end;
  2481. function proc_program(curr: tmodule; islibrary : boolean) : boolean;
  2482. var
  2483. main_file : tinputfile;
  2484. consume_semicolon_after_loaded : boolean;
  2485. ps : tprogramparasym;
  2486. textsym : ttypesym;
  2487. sc : TProgramParamArray;
  2488. i : Longint;
  2489. feature : tfeature;
  2490. load_ok : boolean;
  2491. begin
  2492. result:=true;
  2493. Status.IsLibrary:=IsLibrary;
  2494. Status.IsPackage:=false;
  2495. Status.IsExe:=true;
  2496. parse_only:=false;
  2497. consume_semicolon_after_loaded:=false;
  2498. { make the compiler happy and avoid an uninitialized variable warning on Setlength(sc,length(sc)+1); }
  2499. sc:=nil;
  2500. { DLL defaults to create reloc info }
  2501. if islibrary or (target_info.system in [system_aarch64_win64]) then
  2502. begin
  2503. if not RelocSectionSetExplicitly then
  2504. RelocSection:=true;
  2505. end;
  2506. { Relocation works only without stabs under Windows when }
  2507. { external linker (LD) is used. LD generates relocs for }
  2508. { stab sections which is not loaded in memory. It causes }
  2509. { AV error when DLL is loaded and relocation is needed. }
  2510. { Internal linker does not have this problem. }
  2511. if RelocSection and
  2512. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  2513. (cs_link_extern in current_settings.globalswitches) then
  2514. begin
  2515. include(current_settings.globalswitches,cs_link_strip);
  2516. { Warning stabs info does not work with reloc section !! }
  2517. if (cs_debuginfo in current_settings.moduleswitches) and
  2518. (target_dbg.id=dbg_stabs) then
  2519. begin
  2520. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  2521. Message(parser_w_parser_win32_debug_needs_WN);
  2522. exclude(current_settings.moduleswitches,cs_debuginfo);
  2523. end;
  2524. end;
  2525. { get correct output names }
  2526. main_file := current_scanner.inputfile;
  2527. while assigned(main_file.next) do
  2528. main_file := main_file.next;
  2529. curr.SetFileName(main_file.path+main_file.name,true);
  2530. if islibrary then
  2531. begin
  2532. proc_library_header(curr);
  2533. consume_semicolon_after_loaded:=true;
  2534. end
  2535. else if token=_PROGRAM then
  2536. { is there an program head ? }
  2537. begin
  2538. proc_program_header(curr,sc);
  2539. consume_semicolon_after_loaded:=true;
  2540. end
  2541. else
  2542. begin
  2543. if (target_info.system in systems_unit_program_exports) then
  2544. exportlib.preparelib(curr.realmodulename^);
  2545. { setup things using the switches }
  2546. setupglobalswitches;
  2547. {$ifdef DEBUG_NODE_XML}
  2548. XMLInitializeNodeFile('program', curr.realmodulename^);
  2549. {$endif DEBUG_NODE_XML}
  2550. end;
  2551. { load all packages, so we know whether a unit is contained inside a
  2552. package or not }
  2553. load_packages;
  2554. { set implementation flag }
  2555. curr.in_interface:=false;
  2556. curr.interface_compiled:=true;
  2557. { insert after the unit symbol tables the static symbol table
  2558. of the program }
  2559. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  2560. { load system unit }
  2561. load_ok:=loadsystemunit(curr);
  2562. { consume the semicolon now that the system unit is loaded }
  2563. if consume_semicolon_after_loaded then
  2564. consume(_SEMICOLON);
  2565. { global switches are read, so further changes aren't allowed }
  2566. curr.in_global:=false;
  2567. { system unit is loaded, now insert feature defines }
  2568. for feature:=low(tfeature) to high(tfeature) do
  2569. if feature in features then
  2570. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  2571. { load standard units, e.g objpas,profile unit }
  2572. load_ok:=loaddefaultunits(curr) and load_ok;
  2573. { Load units provided on the command line }
  2574. load_ok:=loadautounits(curr) and load_ok;
  2575. { insert iso program parameters }
  2576. if length(sc)>0 then
  2577. begin
  2578. textsym:=search_system_type('TEXT');
  2579. if not(assigned(textsym)) then
  2580. internalerror(2013011201);
  2581. for i:=0 to high(sc) do
  2582. begin
  2583. ps:=cprogramparasym.create(sc[i].name,sc[i].nr);
  2584. curr.localsymtable.insertsym(ps,true);
  2585. end;
  2586. end;
  2587. { Load the units used by the program we compile. }
  2588. if token=_USES then
  2589. begin
  2590. // We can do this here: if there is no uses then the namespace directive makes no sense.
  2591. if Assigned(curr) then
  2592. curr.Loadlocalnamespacelist
  2593. else
  2594. current_namespacelist:=Nil;
  2595. parseusesclause(curr);
  2596. load_ok:=loadunits(curr,false) and load_ok;
  2597. curr.consume_semicolon_after_uses:=true;
  2598. end
  2599. else
  2600. curr.consume_semicolon_after_uses:=false;
  2601. if not load_ok then
  2602. curr.state:=ms_compiling_wait;
  2603. { Can we continue compiling ? }
  2604. result:=curr.state<>ms_compiling_wait;
  2605. if result then
  2606. result:=proc_program_declarations(curr,islibrary)
  2607. end;
  2608. end.