pdecsub.pas 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  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 pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,symconst,symtype,symdef,symsym;
  22. type
  23. tpdflag=(
  24. pd_body, { directive needs a body }
  25. pd_implemen, { directive can be used implementation section }
  26. pd_interface, { directive can be used interface section }
  27. pd_object, { directive can be used object declaration }
  28. pd_procvar, { directive can be used procvar declaration }
  29. pd_notobject, { directive can not be used object declaration }
  30. pd_notobjintf, { directive can not be used interface declaration }
  31. pd_notprocvar, { directive can not be used procvar declaration }
  32. pd_dispinterface,{ directive can be used with dispinterface methods }
  33. pd_cppobject { directive can be used with cppclass }
  34. );
  35. tpdflags=set of tpdflag;
  36. function check_proc_directive(isprocvar:boolean):boolean;
  37. procedure insert_funcret_local(pd:tprocdef);
  38. function proc_add_definition(var currpd:tprocdef):boolean;
  39. function proc_get_importname(pd:tprocdef):string;
  40. procedure proc_set_mangledname(pd:tprocdef);
  41. procedure handle_calling_convention(pd:tabstractprocdef);
  42. procedure parse_parameter_dec(pd:tabstractprocdef);
  43. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  44. procedure parse_var_proc_directives(sym:tsym);
  45. procedure parse_object_proc_directives(pd:tabstractprocdef);
  46. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  47. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  48. implementation
  49. uses
  50. SysUtils,
  51. { common }
  52. cutils,cclasses,
  53. { global }
  54. globtype,globals,verbose,constexp,
  55. systems,
  56. cpuinfo,
  57. { symtable }
  58. symbase,symtable,defutil,defcmp,paramgr,cpupara,
  59. { pass 1 }
  60. fmodule,node,htypechk,
  61. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  62. { parser }
  63. scanner,
  64. pbase,pexpr,ptype,pdecl
  65. ;
  66. const
  67. { Please leave this here, this module should NOT use
  68. these variables.
  69. Declaring it as string here results in an error when compiling (PFV) }
  70. current_procinfo = 'error';
  71. procedure insert_funcret_para(pd:tabstractprocdef);
  72. var
  73. storepos : tfileposinfo;
  74. vs : tparavarsym;
  75. paranr : word;
  76. begin
  77. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  78. not is_void(pd.returndef) and
  79. paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  80. begin
  81. storepos:=current_tokenpos;
  82. if pd.typ=procdef then
  83. current_tokenpos:=tprocdef(pd).fileinfo;
  84. {$if defined(i386)}
  85. { For left to right add it at the end to be delphi compatible }
  86. if pd.proccalloption in (pushleftright_pocalls+[pocall_safecall]) then
  87. paranr:=paranr_result_leftright
  88. else
  89. {$elseif defined(x86) or defined(arm)}
  90. { other platforms don't have a "safecall" convention,
  91. and never reverse the parameter pushing order
  92. }
  93. if (pd.proccalloption = pocall_safecall) then
  94. paranr:=paranr_result_leftright
  95. else
  96. {$endif}
  97. paranr:=paranr_result;
  98. { Generate result variable accessing function result }
  99. vs:=tparavarsym.create('$result',paranr,vs_var,pd.returndef,[vo_is_funcret,vo_is_hidden_para]);
  100. pd.parast.insert(vs);
  101. { Store the this symbol as funcretsym for procedures }
  102. if pd.typ=procdef then
  103. tprocdef(pd).funcretsym:=vs;
  104. current_tokenpos:=storepos;
  105. end;
  106. end;
  107. procedure insert_parentfp_para(pd:tabstractprocdef);
  108. var
  109. storepos : tfileposinfo;
  110. vs : tparavarsym;
  111. begin
  112. if pd.parast.symtablelevel>normal_function_level then
  113. begin
  114. storepos:=current_tokenpos;
  115. if pd.typ=procdef then
  116. current_tokenpos:=tprocdef(pd).fileinfo;
  117. { Generate result variable accessing function result, it
  118. can't be put in a register since it must be accessable
  119. from the framepointer }
  120. vs:=tparavarsym.create('$parentfp',paranr_parentfp,vs_value
  121. ,voidpointertype,[vo_is_parentfp,vo_is_hidden_para]);
  122. vs.varregable:=vr_none;
  123. pd.parast.insert(vs);
  124. current_tokenpos:=storepos;
  125. end;
  126. end;
  127. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  128. var
  129. storepos : tfileposinfo;
  130. vs : tparavarsym;
  131. hdef : tdef;
  132. vsp : tvarspez;
  133. begin
  134. if (pd.typ=procvardef) and
  135. pd.is_methodpointer then
  136. begin
  137. { Generate self variable }
  138. vs:=tparavarsym.create('$self',paranr_self,vs_value,voidpointertype,[vo_is_self,vo_is_hidden_para]);
  139. pd.parast.insert(vs);
  140. end
  141. else
  142. begin
  143. if (pd.typ=procdef) and
  144. assigned(tprocdef(pd)._class) and
  145. (pd.parast.symtablelevel=normal_function_level) then
  146. begin
  147. { static class methods have no hidden self/vmt pointer }
  148. if (po_staticmethod in pd.procoptions) and
  149. (po_classmethod in pd.procoptions) then
  150. exit;
  151. storepos:=current_tokenpos;
  152. current_tokenpos:=tprocdef(pd).fileinfo;
  153. { Generate VMT variable for constructor/destructor }
  154. if (pd.proctypeoption in [potype_constructor,potype_destructor]) and not(is_cppclass(tprocdef(pd)._class)) then
  155. begin
  156. { can't use classrefdef as type because inheriting
  157. will then always file because of a type mismatch }
  158. vs:=tparavarsym.create('$vmt',paranr_vmt,vs_value,voidpointertype,[vo_is_vmt,vo_is_hidden_para]);
  159. pd.parast.insert(vs);
  160. end;
  161. { Generate self variable, for classes we need
  162. to use the generic voidpointer to be compatible with
  163. methodpointers }
  164. vsp:=vs_value;
  165. if (po_staticmethod in pd.procoptions) or
  166. (po_classmethod in pd.procoptions) then
  167. hdef:=tclassrefdef.create(tprocdef(pd)._class)
  168. else
  169. begin
  170. if is_object(tprocdef(pd)._class) then
  171. vsp:=vs_var;
  172. hdef:=tprocdef(pd)._class;
  173. end;
  174. vs:=tparavarsym.create('$self',paranr_self,vsp,hdef,[vo_is_self,vo_is_hidden_para]);
  175. pd.parast.insert(vs);
  176. current_tokenpos:=storepos;
  177. end;
  178. end;
  179. end;
  180. procedure insert_funcret_local(pd:tprocdef);
  181. var
  182. storepos : tfileposinfo;
  183. vs : tlocalvarsym;
  184. aliasvs : tabsolutevarsym;
  185. sl : tpropaccesslist;
  186. hs : string;
  187. begin
  188. { The result from constructors and destructors can't be accessed directly }
  189. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  190. not is_void(pd.returndef) then
  191. begin
  192. storepos:=current_tokenpos;
  193. current_tokenpos:=pd.fileinfo;
  194. { We need to insert a varsym for the result in the localst
  195. when it is returning in a register }
  196. if not paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  197. begin
  198. vs:=tlocalvarsym.create('$result',vs_value,pd.returndef,[vo_is_funcret]);
  199. pd.localst.insert(vs);
  200. pd.funcretsym:=vs;
  201. end;
  202. { insert the name of the procedure as alias for the function result,
  203. we can't use realname because that will not work for compilerprocs
  204. as the name is lowercase and unreachable from the code }
  205. if assigned(pd.resultname) then
  206. hs:=pd.resultname^
  207. else
  208. hs:=pd.procsym.name;
  209. sl:=tpropaccesslist.create;
  210. sl.addsym(sl_load,pd.funcretsym);
  211. aliasvs:=tabsolutevarsym.create_ref(hs,pd.returndef,sl);
  212. include(aliasvs.varoptions,vo_is_funcret);
  213. tlocalsymtable(pd.localst).insert(aliasvs);
  214. { insert result also if support is on }
  215. if (m_result in current_settings.modeswitches) then
  216. begin
  217. sl:=tpropaccesslist.create;
  218. sl.addsym(sl_load,pd.funcretsym);
  219. aliasvs:=tabsolutevarsym.create_ref('RESULT',pd.returndef,sl);
  220. include(aliasvs.varoptions,vo_is_funcret);
  221. include(aliasvs.varoptions,vo_is_result);
  222. tlocalsymtable(pd.localst).insert(aliasvs);
  223. end;
  224. current_tokenpos:=storepos;
  225. end;
  226. end;
  227. procedure insert_hidden_para(p:TObject;arg:pointer);
  228. var
  229. hvs : tparavarsym;
  230. pd : tabstractprocdef absolute arg;
  231. begin
  232. if (tsym(p).typ<>paravarsym) then
  233. exit;
  234. with tparavarsym(p) do
  235. begin
  236. { We need a local copy for a value parameter when only the
  237. address is pushed. Open arrays and Array of Const are
  238. an exception because they are allocated at runtime and the
  239. address that is pushed is patched }
  240. if (varspez=vs_value) and
  241. paramanager.push_addr_param(varspez,vardef,pd.proccalloption) and
  242. not(is_open_array(vardef) or
  243. is_array_of_const(vardef)) then
  244. include(varoptions,vo_has_local_copy);
  245. { needs high parameter ? }
  246. if paramanager.push_high_param(varspez,vardef,pd.proccalloption) then
  247. begin
  248. hvs:=tparavarsym.create('$high'+name,paranr+1,vs_const,sinttype,[vo_is_high_para,vo_is_hidden_para]);
  249. hvs.symoptions:=[];
  250. owner.insert(hvs);
  251. end
  252. else
  253. begin
  254. { Give a warning that cdecl routines does not include high()
  255. support }
  256. if (pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  257. paramanager.push_high_param(varspez,vardef,pocall_default) then
  258. begin
  259. if is_open_string(vardef) then
  260. Message(parser_w_cdecl_no_openstring);
  261. if not (po_external in pd.procoptions) then
  262. Message(parser_w_cdecl_has_no_high);
  263. end;
  264. if (vardef.typ=formaldef) and (Tformaldef(vardef).typed) then
  265. begin
  266. hvs:=tparavarsym.create('$typinfo'+name,paranr+1,vs_const,voidpointertype,
  267. [vo_is_typinfo_para,vo_is_hidden_para]);
  268. owner.insert(hvs);
  269. end;
  270. end;
  271. end;
  272. end;
  273. procedure check_c_para(pd:Tabstractprocdef);
  274. var
  275. i,
  276. lastparaidx : longint;
  277. sym : TSym;
  278. begin
  279. lastparaidx:=pd.parast.SymList.Count-1;
  280. for i:=0 to pd.parast.SymList.Count-1 do
  281. begin
  282. sym:=tsym(pd.parast.SymList[i]);
  283. if (sym.typ=paravarsym) and
  284. (tparavarsym(sym).vardef.typ=arraydef) then
  285. begin
  286. if not is_variant_array(tparavarsym(sym).vardef) and
  287. not is_array_of_const(tparavarsym(sym).vardef) and
  288. (tparavarsym(sym).varspez<>vs_var) then
  289. Message(parser_h_c_arrays_are_references);
  290. if is_array_of_const(tparavarsym(sym).vardef) and
  291. (i<lastparaidx) and
  292. (tsym(pd.parast.SymList[i+1]).typ=paravarsym) and
  293. not(vo_is_high_para in tparavarsym(pd.parast.SymList[i+1]).varoptions) then
  294. Message(parser_e_C_array_of_const_must_be_last);
  295. end;
  296. end;
  297. end;
  298. procedure check_msg_para(p:TObject;arg:pointer);
  299. begin
  300. if (tsym(p).typ<>paravarsym) then
  301. exit;
  302. with tparavarsym(p) do
  303. begin
  304. { Count parameters }
  305. if (paranr>=10) then
  306. inc(plongint(arg)^);
  307. { First parameter must be var }
  308. if (paranr=10) and
  309. (varspez<>vs_var) then
  310. Message(parser_e_ill_msg_param);
  311. end;
  312. end;
  313. procedure set_addr_param_regable(p:TObject;arg:pointer);
  314. begin
  315. if (tsym(p).typ<>paravarsym) then
  316. exit;
  317. with tparavarsym(p) do
  318. begin
  319. if not vardef.needs_inittable and
  320. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  321. varregable:=vr_intreg;
  322. end;
  323. end;
  324. procedure parse_parameter_dec(pd:tabstractprocdef);
  325. {
  326. handle_procvar needs the same changes
  327. }
  328. type
  329. tppv = (pv_none,pv_proc,pv_func);
  330. var
  331. sc : TFPObjectList;
  332. hdef : tdef;
  333. arrayelementdef : tdef;
  334. vs : tparavarsym;
  335. i : longint;
  336. srsym : tsym;
  337. pv : tprocvardef;
  338. varspez : Tvarspez;
  339. defaultvalue : tconstsym;
  340. defaultrequired : boolean;
  341. old_block_type : tblock_type;
  342. currparast : tparasymtable;
  343. parseprocvar : tppv;
  344. explicit_paraloc : boolean;
  345. locationstr : string;
  346. paranr : integer;
  347. dummytype : ttypesym;
  348. begin
  349. old_block_type:=block_type;
  350. explicit_paraloc:=false;
  351. consume(_LKLAMMER);
  352. { Delphi/Kylix supports nonsense like }
  353. { procedure p(); }
  354. if try_to_consume(_RKLAMMER) and
  355. not(m_tp7 in current_settings.modeswitches) then
  356. exit;
  357. { parsing a proc or procvar ? }
  358. currparast:=tparasymtable(pd.parast);
  359. { reset }
  360. sc:=TFPObjectList.create(false);
  361. defaultrequired:=false;
  362. paranr:=0;
  363. inc(testcurobject);
  364. block_type:=bt_var;
  365. repeat
  366. parseprocvar:=pv_none;
  367. if try_to_consume(_VAR) then
  368. varspez:=vs_var
  369. else
  370. if try_to_consume(_CONST) then
  371. varspez:=vs_const
  372. else
  373. if (m_out in current_settings.modeswitches) and
  374. try_to_consume(_OUT) then
  375. varspez:=vs_out
  376. else
  377. if (m_mac in current_settings.modeswitches) and
  378. try_to_consume(_POINTPOINTPOINT) then
  379. begin
  380. include(pd.procoptions,po_varargs);
  381. break;
  382. end
  383. else
  384. if (m_mac in current_settings.modeswitches) and
  385. try_to_consume(_PROCEDURE) then
  386. begin
  387. parseprocvar:=pv_proc;
  388. varspez:=vs_const;
  389. end
  390. else
  391. if (m_mac in current_settings.modeswitches) and
  392. try_to_consume(_FUNCTION) then
  393. begin
  394. parseprocvar:=pv_func;
  395. varspez:=vs_const;
  396. end
  397. else
  398. varspez:=vs_value;
  399. defaultvalue:=nil;
  400. hdef:=nil;
  401. { read identifiers and insert with error type }
  402. sc.clear;
  403. repeat
  404. inc(paranr);
  405. vs:=tparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  406. currparast.insert(vs);
  407. if assigned(vs.owner) then
  408. sc.add(vs)
  409. else
  410. vs.free;
  411. consume(_ID);
  412. until not try_to_consume(_COMMA);
  413. locationstr:='';
  414. { macpas anonymous procvar }
  415. if parseprocvar<>pv_none then
  416. begin
  417. pv:=tprocvardef.create(normal_function_level);
  418. if token=_LKLAMMER then
  419. parse_parameter_dec(pv);
  420. if parseprocvar=pv_func then
  421. begin
  422. block_type:=bt_var_type;
  423. consume(_COLON);
  424. single_type(pv.returndef,false);
  425. block_type:=bt_var;
  426. end;
  427. hdef:=pv;
  428. { possible proc directives }
  429. if check_proc_directive(true) then
  430. begin
  431. dummytype:=ttypesym.create('unnamed',hdef);
  432. parse_var_proc_directives(tsym(dummytype));
  433. dummytype.typedef:=nil;
  434. hdef.typesym:=nil;
  435. dummytype.free;
  436. end;
  437. { Add implicit hidden parameters and function result }
  438. handle_calling_convention(pv);
  439. end
  440. else
  441. { read type declaration, force reading for value and const paras }
  442. if (token=_COLON) or (varspez=vs_value) then
  443. begin
  444. consume(_COLON);
  445. { check for an open array }
  446. if token=_ARRAY then
  447. begin
  448. consume(_ARRAY);
  449. consume(_OF);
  450. { define range and type of range }
  451. hdef:=tarraydef.create(0,-1,s32inttype);
  452. { array of const ? }
  453. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  454. begin
  455. consume(_CONST);
  456. srsym:=search_system_type('TVARREC');
  457. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  458. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  459. end
  460. else
  461. begin
  462. { define field type }
  463. single_type(arrayelementdef,false);
  464. tarraydef(hdef).elementdef:=arrayelementdef;
  465. end;
  466. end
  467. else
  468. begin
  469. if (m_mac in current_settings.modeswitches) then
  470. try_to_consume(_UNIV); {currently does nothing}
  471. if try_to_consume(_TYPE) then
  472. hdef:=ctypedformaltype
  473. else
  474. begin
  475. block_type:=bt_var_type;
  476. single_type(hdef,false);
  477. block_type:=bt_var;
  478. end;
  479. { open string ? }
  480. if (varspez in [vs_out,vs_var]) and
  481. (cs_openstring in current_settings.moduleswitches) and
  482. is_shortstring(hdef) then
  483. hdef:=openshortstringtype;
  484. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  485. begin
  486. if (idtoken=_LOCATION) then
  487. begin
  488. consume(_LOCATION);
  489. locationstr:=pattern;
  490. consume(_CSTRING);
  491. end
  492. else
  493. begin
  494. if explicit_paraloc then
  495. Message(parser_e_paraloc_all_paras);
  496. locationstr:='';
  497. end;
  498. end
  499. else
  500. locationstr:='';
  501. { default parameter }
  502. if (m_default_para in current_settings.modeswitches) then
  503. begin
  504. if try_to_consume(_EQUAL) then
  505. begin
  506. vs:=tparavarsym(sc[0]);
  507. if sc.count>1 then
  508. Message(parser_e_default_value_only_one_para);
  509. { prefix 'def' to the parameter name }
  510. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  511. if assigned(defaultvalue) then
  512. begin
  513. include(defaultvalue.symoptions,sp_internal);
  514. pd.parast.insert(defaultvalue);
  515. end;
  516. defaultrequired:=true;
  517. end
  518. else
  519. begin
  520. if defaultrequired then
  521. Message1(parser_e_default_value_expected_for_para,vs.name);
  522. end;
  523. end;
  524. end;
  525. end
  526. else
  527. hdef:=cformaltype;
  528. { File types are only allowed for var and out parameters }
  529. if (hdef.typ=filedef) and
  530. not(varspez in [vs_out,vs_var]) then
  531. CGMessage(cg_e_file_must_call_by_reference);
  532. for i:=0 to sc.count-1 do
  533. begin
  534. vs:=tparavarsym(sc[i]);
  535. { update varsym }
  536. vs.vardef:=hdef;
  537. vs.defaultconstsym:=defaultvalue;
  538. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  539. begin
  540. if locationstr<>'' then
  541. begin
  542. if sc.count>1 then
  543. Message(parser_e_paraloc_only_one_para);
  544. if (paranr>1) and not(explicit_paraloc) then
  545. Message(parser_e_paraloc_all_paras);
  546. explicit_paraloc:=true;
  547. include(vs.varoptions,vo_has_explicit_paraloc);
  548. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  549. message(parser_e_illegal_explicit_paraloc);
  550. end
  551. else
  552. if explicit_paraloc then
  553. Message(parser_e_paraloc_all_paras);
  554. end;
  555. end;
  556. until not try_to_consume(_SEMICOLON);
  557. if explicit_paraloc then
  558. begin
  559. pd.has_paraloc_info:=true;
  560. include(pd.procoptions,po_explicitparaloc);
  561. end;
  562. { remove parasymtable from stack }
  563. sc.free;
  564. { reset object options }
  565. dec(testcurobject);
  566. block_type:=old_block_type;
  567. consume(_RKLAMMER);
  568. end;
  569. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  570. var
  571. hs : string;
  572. orgsp,sp : TIDString;
  573. srsym : tsym;
  574. srsymtable : TSymtable;
  575. checkstack : psymtablestackitem;
  576. storepos,
  577. procstartfilepos : tfileposinfo;
  578. searchagain : boolean;
  579. st,
  580. genericst : TSymtable;
  581. aprocsym : tprocsym;
  582. popclass : boolean;
  583. ImplIntf : TImplementedInterface;
  584. old_parse_generic : boolean;
  585. begin
  586. { Save the position where this procedure really starts }
  587. procstartfilepos:=current_tokenpos;
  588. old_parse_generic:=parse_generic;
  589. result:=false;
  590. pd:=nil;
  591. aprocsym:=nil;
  592. if (potype=potype_operator) then
  593. begin
  594. sp:=overloaded_names[optoken];
  595. orgsp:=sp;
  596. end
  597. else
  598. begin
  599. sp:=pattern;
  600. orgsp:=orgpattern;
  601. consume(_ID);
  602. end;
  603. { examine interface map: function/procedure iname.functionname=locfuncname }
  604. if assigned(aclass) and
  605. assigned(aclass.ImplementedInterfaces) and
  606. (aclass.ImplementedInterfaces.count>0) and
  607. try_to_consume(_POINT) then
  608. begin
  609. storepos:=current_tokenpos;
  610. current_tokenpos:=procstartfilepos;
  611. { get interface syms}
  612. searchsym(sp,srsym,srsymtable);
  613. if not assigned(srsym) then
  614. begin
  615. identifier_not_found(orgsp);
  616. srsym:=generrorsym;
  617. end;
  618. current_tokenpos:=storepos;
  619. { qualifier is interface? }
  620. ImplIntf:=nil;
  621. if (srsym.typ=typesym) and
  622. (ttypesym(srsym).typedef.typ=objectdef) then
  623. ImplIntf:=aclass.find_implemented_interface(tobjectdef(ttypesym(srsym).typedef));
  624. if ImplIntf=nil then
  625. Message(parser_e_interface_id_expected);
  626. consume(_ID);
  627. { Create unique name <interface>.<method> }
  628. hs:=sp+'.'+pattern;
  629. consume(_EQUAL);
  630. if assigned(ImplIntf) and
  631. (token=_ID) then
  632. ImplIntf.AddMapping(hs,pattern);
  633. consume(_ID);
  634. result:=true;
  635. exit;
  636. end;
  637. { method ? }
  638. if not assigned(aclass) and
  639. (potype<>potype_operator) and
  640. (symtablestack.top.symtablelevel=main_program_level) and
  641. try_to_consume(_POINT) then
  642. begin
  643. { search for object name }
  644. storepos:=current_tokenpos;
  645. current_tokenpos:=procstartfilepos;
  646. searchsym(sp,srsym,srsymtable);
  647. if not assigned(srsym) then
  648. begin
  649. identifier_not_found(orgsp);
  650. srsym:=generrorsym;
  651. end;
  652. current_tokenpos:=storepos;
  653. { consume proc name }
  654. sp:=pattern;
  655. orgsp:=orgpattern;
  656. procstartfilepos:=current_tokenpos;
  657. consume(_ID);
  658. { qualifier is class name ? }
  659. if (srsym.typ=typesym) and
  660. (ttypesym(srsym).typedef.typ=objectdef) then
  661. begin
  662. aclass:=tobjectdef(ttypesym(srsym).typedef);
  663. srsym:=tsym(aclass.symtable.Find(sp));
  664. if assigned(srsym) then
  665. begin
  666. if srsym.typ=procsym then
  667. aprocsym:=tprocsym(srsym)
  668. else
  669. begin
  670. { we use a different error message for tp7 so it looks more compatible }
  671. if (m_fpc in current_settings.modeswitches) then
  672. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  673. else
  674. Message(parser_e_methode_id_expected);
  675. { rename the name to an unique name to avoid an
  676. error when inserting the symbol in the symtable }
  677. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  678. end;
  679. end
  680. else
  681. begin
  682. Message(parser_e_methode_id_expected);
  683. { recover by making it a normal procedure instead of method }
  684. aclass:=nil;
  685. end;
  686. end
  687. else
  688. Message(parser_e_class_id_expected);
  689. end
  690. else
  691. begin
  692. { check for constructor/destructor which is not allowed here }
  693. if (not parse_only) and
  694. (potype in [potype_constructor,potype_destructor]) then
  695. Message(parser_e_constructors_always_objects);
  696. repeat
  697. searchagain:=false;
  698. current_tokenpos:=procstartfilepos;
  699. srsymtable:=symtablestack.top;
  700. srsym:=tsym(srsymtable.Find(sp));
  701. { Also look in the globalsymtable if we didn't found
  702. the symbol in the localsymtable }
  703. if not assigned(srsym) and
  704. not(parse_only) and
  705. (srsymtable=current_module.localsymtable) and
  706. assigned(current_module.globalsymtable) then
  707. srsym:=tsym(current_module.globalsymtable.Find(sp));
  708. { Check if overloaded is a procsym }
  709. if assigned(srsym) then
  710. begin
  711. if srsym.typ=procsym then
  712. aprocsym:=tprocsym(srsym)
  713. else
  714. begin
  715. { when the other symbol is a unit symbol then hide the unit
  716. symbol, this is not supported in tp7 }
  717. if not(m_tp7 in current_settings.modeswitches) and
  718. (srsym.typ=unitsym) then
  719. begin
  720. HideSym(srsym);
  721. searchagain:=true;
  722. end
  723. else
  724. begin
  725. { we use a different error message for tp7 so it looks more compatible }
  726. if (m_fpc in current_settings.modeswitches) then
  727. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  728. else
  729. Message1(sym_e_duplicate_id,srsym.realname);
  730. { rename the name to an unique name to avoid an
  731. error when inserting the symbol in the symtable }
  732. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  733. end;
  734. end;
  735. end;
  736. until not searchagain;
  737. end;
  738. { test again if assigned, it can be reset to recover }
  739. if not assigned(aprocsym) then
  740. begin
  741. { create a new procsym and set the real filepos }
  742. current_tokenpos:=procstartfilepos;
  743. { for operator we have only one procsym for each overloaded
  744. operation }
  745. if (potype=potype_operator) then
  746. begin
  747. Aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  748. if Aprocsym=nil then
  749. Aprocsym:=tprocsym.create('$'+sp);
  750. end
  751. else
  752. aprocsym:=tprocsym.create(orgsp);
  753. symtablestack.top.insert(aprocsym);
  754. end;
  755. { to get the correct symtablelevel we must ignore ObjectSymtables }
  756. st:=nil;
  757. checkstack:=symtablestack.stack;
  758. while assigned(checkstack) do
  759. begin
  760. st:=checkstack^.symtable;
  761. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  762. break;
  763. checkstack:=checkstack^.next;
  764. end;
  765. pd:=tprocdef.create(st.symtablelevel+1);
  766. pd._class:=aclass;
  767. pd.procsym:=aprocsym;
  768. pd.proctypeoption:=potype;
  769. { methods inherit df_generic or df_specialization from the objectdef }
  770. if assigned(pd._class) and
  771. (pd.parast.symtablelevel=normal_function_level) then
  772. begin
  773. if (df_generic in pd._class.defoptions) then
  774. begin
  775. include(pd.defoptions,df_generic);
  776. parse_generic:=true;
  777. end;
  778. if (df_specialization in pd._class.defoptions) then
  779. begin
  780. include(pd.defoptions,df_specialization);
  781. { Find corresponding genericdef, we need it later to
  782. replay the tokens to generate the body }
  783. if not assigned(pd._class.genericdef) then
  784. internalerror(200512113);
  785. genericst:=pd._class.genericdef.GetSymtable(gs_record);
  786. if not assigned(genericst) then
  787. internalerror(200512114);
  788. { We are parsing the same objectdef, the def index numbers
  789. are the same }
  790. pd.genericdef:=tstoreddef(genericst.DefList[pd.owner.DefList.IndexOf(pd)]);
  791. if not assigned(pd.genericdef) or
  792. (pd.genericdef.typ<>procdef) then
  793. internalerror(200512115);
  794. end;
  795. end;
  796. { methods need to be exported }
  797. if assigned(aclass) and
  798. (
  799. (symtablestack.top.symtabletype=ObjectSymtable) or
  800. (symtablestack.top.symtablelevel=main_program_level)
  801. ) then
  802. include(pd.procoptions,po_global);
  803. { symbol options that need to be kept per procdef }
  804. pd.fileinfo:=procstartfilepos;
  805. pd.visibility:=symtablestack.top.currentvisibility;
  806. { parse parameters }
  807. if token=_LKLAMMER then
  808. begin
  809. { Add ObjectSymtable to be able to find generic type definitions }
  810. popclass:=false;
  811. if assigned(pd._class) and
  812. (pd.parast.symtablelevel=normal_function_level) and
  813. (symtablestack.top.symtabletype<>ObjectSymtable) then
  814. begin
  815. symtablestack.push(pd._class.symtable);
  816. popclass:=true;
  817. end;
  818. { Add parameter symtable }
  819. if pd.parast.symtabletype<>staticsymtable then
  820. symtablestack.push(pd.parast);
  821. parse_parameter_dec(pd);
  822. if pd.parast.symtabletype<>staticsymtable then
  823. symtablestack.pop(pd.parast);
  824. if popclass then
  825. symtablestack.pop(pd._class.symtable);
  826. end;
  827. parse_generic:=old_parse_generic;
  828. result:=true;
  829. end;
  830. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  831. var
  832. pd : tprocdef;
  833. isclassmethod : boolean;
  834. locationstr: string;
  835. old_parse_generic,
  836. popclass : boolean;
  837. begin
  838. locationstr:='';
  839. pd:=nil;
  840. isclassmethod:=false;
  841. { read class method }
  842. if try_to_consume(_CLASS) then
  843. begin
  844. { class method only allowed for procedures and functions }
  845. if not(token in [_FUNCTION,_PROCEDURE]) then
  846. Message(parser_e_procedure_or_function_expected);
  847. if is_interface(aclass) then
  848. Message(parser_e_no_static_method_in_interfaces)
  849. else
  850. isclassmethod:=true;
  851. end;
  852. case token of
  853. _FUNCTION :
  854. begin
  855. consume(_FUNCTION);
  856. if parse_proc_head(aclass,potype_function,pd) then
  857. begin
  858. { pd=nil when it is a interface mapping }
  859. if assigned(pd) then
  860. begin
  861. if try_to_consume(_COLON) then
  862. begin
  863. old_parse_generic:=parse_generic;
  864. inc(testcurobject);
  865. { Add ObjectSymtable to be able to find generic type definitions }
  866. popclass:=false;
  867. if assigned(pd._class) and
  868. (pd.parast.symtablelevel=normal_function_level) and
  869. (symtablestack.top.symtabletype<>ObjectSymtable) then
  870. begin
  871. symtablestack.push(pd._class.symtable);
  872. popclass:=true;
  873. parse_generic:=(df_generic in pd._class.defoptions);
  874. end;
  875. single_type(pd.returndef,false);
  876. if popclass then
  877. symtablestack.pop(pd._class.symtable);
  878. dec(testcurobject);
  879. parse_generic:=old_parse_generic;
  880. if (target_info.system in [system_m68k_amiga]) then
  881. begin
  882. if (idtoken=_LOCATION) then
  883. begin
  884. if po_explicitparaloc in pd.procoptions then
  885. begin
  886. consume(_LOCATION);
  887. locationstr:=pattern;
  888. consume(_CSTRING);
  889. end
  890. else
  891. { I guess this needs a new message... (KB) }
  892. Message(parser_e_paraloc_all_paras);
  893. end
  894. else
  895. begin
  896. if po_explicitparaloc in pd.procoptions then
  897. { assign default locationstr, if none specified }
  898. { and we've arguments with explicit paraloc }
  899. locationstr:='D0';
  900. end;
  901. end;
  902. end
  903. else
  904. begin
  905. if (
  906. parse_only and
  907. not(is_interface(pd._class))
  908. ) or
  909. (m_repeat_forward in current_settings.modeswitches) then
  910. begin
  911. consume(_COLON);
  912. consume_all_until(_SEMICOLON);
  913. end;
  914. end;
  915. if isclassmethod then
  916. include(pd.procoptions,po_classmethod);
  917. end;
  918. end
  919. else
  920. begin
  921. { recover }
  922. consume(_COLON);
  923. consume_all_until(_SEMICOLON);
  924. end;
  925. end;
  926. _PROCEDURE :
  927. begin
  928. consume(_PROCEDURE);
  929. if parse_proc_head(aclass,potype_procedure,pd) then
  930. begin
  931. { pd=nil when it is a interface mapping }
  932. if assigned(pd) then
  933. begin
  934. pd.returndef:=voidtype;
  935. if isclassmethod then
  936. include(pd.procoptions,po_classmethod);
  937. end;
  938. end;
  939. end;
  940. _CONSTRUCTOR :
  941. begin
  942. consume(_CONSTRUCTOR);
  943. parse_proc_head(aclass,potype_constructor,pd);
  944. if assigned(pd) and
  945. assigned(pd._class) then
  946. begin
  947. { Set return type, class constructors return the
  948. created instance, object constructors return boolean }
  949. if is_class(pd._class) then
  950. pd.returndef:=pd._class
  951. else
  952. {$ifdef CPU64bitaddr}
  953. pd.returndef:=bool64type;
  954. {$else CPU64bitaddr}
  955. pd.returndef:=bool32type;
  956. {$endif CPU64bitaddr}
  957. end;
  958. end;
  959. _DESTRUCTOR :
  960. begin
  961. consume(_DESTRUCTOR);
  962. parse_proc_head(aclass,potype_destructor,pd);
  963. if assigned(pd) then
  964. pd.returndef:=voidtype;
  965. end;
  966. _OPERATOR :
  967. begin
  968. consume(_OPERATOR);
  969. if (token in [first_overloaded..last_overloaded]) then
  970. begin
  971. optoken:=token;
  972. end
  973. else
  974. begin
  975. case token of
  976. _CARET:
  977. Message1(parser_e_overload_operator_failed,'**');
  978. _UNEQUAL:
  979. Message1(parser_e_overload_operator_failed,'=');
  980. else
  981. Message1(parser_e_overload_operator_failed,'');
  982. end;
  983. { Use the dummy NOTOKEN that is also declared
  984. for the overloaded_operator[] }
  985. optoken:=NOTOKEN;
  986. end;
  987. consume(token);
  988. parse_proc_head(aclass,potype_operator,pd);
  989. if assigned(pd) then
  990. begin
  991. { operators always need to be searched in all units }
  992. include(pd.procoptions,po_overload);
  993. if pd.parast.symtablelevel>normal_function_level then
  994. Message(parser_e_no_local_operator);
  995. if token<>_ID then
  996. begin
  997. if not(m_result in current_settings.modeswitches) then
  998. consume(_ID);
  999. end
  1000. else
  1001. begin
  1002. pd.resultname:=stringdup(orgpattern);
  1003. consume(_ID);
  1004. end;
  1005. if not try_to_consume(_COLON) then
  1006. begin
  1007. consume(_COLON);
  1008. pd.returndef:=generrordef;
  1009. consume_all_until(_SEMICOLON);
  1010. end
  1011. else
  1012. begin
  1013. single_type(pd.returndef,false);
  1014. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  1015. ((pd.returndef.typ<>orddef) or
  1016. (torddef(pd.returndef).ordtype<>pasbool)) then
  1017. Message(parser_e_comparative_operator_return_boolean);
  1018. if (optoken=_ASSIGNMENT) and
  1019. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) then
  1020. message(parser_e_no_such_assignment)
  1021. else if not isoperatoracceptable(pd,optoken) then
  1022. Message(parser_e_overload_impossible);
  1023. end;
  1024. end
  1025. else
  1026. begin
  1027. { recover }
  1028. try_to_consume(_ID);
  1029. consume(_COLON);
  1030. consume_all_until(_SEMICOLON);
  1031. end;
  1032. end;
  1033. end;
  1034. { file types can't be function results }
  1035. if assigned(pd) and
  1036. (pd.returndef.typ=filedef) then
  1037. message(parser_e_illegal_function_result);
  1038. { support procedure proc stdcall export; }
  1039. if not(check_proc_directive(false)) then
  1040. consume(_SEMICOLON);
  1041. result:=pd;
  1042. if locationstr<>'' then
  1043. begin
  1044. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1045. { I guess this needs a new message... (KB) }
  1046. message(parser_e_illegal_explicit_paraloc);
  1047. end;
  1048. end;
  1049. {****************************************************************************
  1050. Procedure directive handlers
  1051. ****************************************************************************}
  1052. procedure pd_far(pd:tabstractprocdef);
  1053. begin
  1054. Message1(parser_w_proc_directive_ignored,'FAR');
  1055. end;
  1056. procedure pd_near(pd:tabstractprocdef);
  1057. begin
  1058. Message1(parser_w_proc_directive_ignored,'NEAR');
  1059. end;
  1060. procedure pd_export(pd:tabstractprocdef);
  1061. begin
  1062. if pd.typ<>procdef then
  1063. internalerror(200304264);
  1064. if assigned(tprocdef(pd)._class) then
  1065. Message(parser_e_methods_dont_be_export);
  1066. if pd.parast.symtablelevel>normal_function_level then
  1067. Message(parser_e_dont_nest_export);
  1068. end;
  1069. procedure pd_forward(pd:tabstractprocdef);
  1070. begin
  1071. if pd.typ<>procdef then
  1072. internalerror(200304265);
  1073. tprocdef(pd).forwarddef:=true;
  1074. end;
  1075. procedure pd_alias(pd:tabstractprocdef);
  1076. begin
  1077. if pd.typ<>procdef then
  1078. internalerror(200304266);
  1079. consume(_COLON);
  1080. tprocdef(pd).aliasnames.insert(get_stringconst);
  1081. include(pd.procoptions,po_has_public_name);
  1082. end;
  1083. procedure pd_public(pd:tabstractprocdef);
  1084. begin
  1085. if pd.typ<>procdef then
  1086. internalerror(200304266);
  1087. if try_to_consume(_NAME) then
  1088. begin
  1089. tprocdef(pd).aliasnames.insert(get_stringconst);
  1090. include(pd.procoptions,po_has_public_name);
  1091. end;
  1092. end;
  1093. procedure pd_asmname(pd:tabstractprocdef);
  1094. begin
  1095. if pd.typ<>procdef then
  1096. internalerror(200304267);
  1097. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1098. if token=_CCHAR then
  1099. consume(_CCHAR)
  1100. else
  1101. consume(_CSTRING);
  1102. { we don't need anything else }
  1103. tprocdef(pd).forwarddef:=false;
  1104. end;
  1105. procedure pd_internconst(pd:tabstractprocdef);
  1106. var v:Tconstexprint;
  1107. begin
  1108. if pd.typ<>procdef then
  1109. internalerror(200304268);
  1110. consume(_COLON);
  1111. v:=get_intconst;
  1112. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1113. message(parser_e_range_check_error)
  1114. else
  1115. Tprocdef(pd).extnumber:=longint(v.svalue);
  1116. end;
  1117. procedure pd_internproc(pd:tabstractprocdef);
  1118. var v:Tconstexprint;
  1119. begin
  1120. if pd.typ<>procdef then
  1121. internalerror(200304268);
  1122. consume(_COLON);
  1123. v:=get_intconst;
  1124. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1125. message(parser_e_range_check_error)
  1126. else
  1127. Tprocdef(pd).extnumber:=longint(v.svalue);
  1128. { the proc is defined }
  1129. tprocdef(pd).forwarddef:=false;
  1130. end;
  1131. procedure pd_interrupt(pd:tabstractprocdef);
  1132. begin
  1133. if pd.parast.symtablelevel>normal_function_level then
  1134. Message(parser_e_dont_nest_interrupt);
  1135. end;
  1136. procedure pd_abstract(pd:tabstractprocdef);
  1137. begin
  1138. if pd.typ<>procdef then
  1139. internalerror(200304269);
  1140. if (po_virtualmethod in pd.procoptions) then
  1141. include(pd.procoptions,po_abstractmethod)
  1142. else
  1143. Message(parser_e_only_virtual_methods_abstract);
  1144. { the method is defined }
  1145. tprocdef(pd).forwarddef:=false;
  1146. end;
  1147. procedure pd_virtual(pd:tabstractprocdef);
  1148. {$ifdef WITHDMT}
  1149. var
  1150. pt : tnode;
  1151. {$endif WITHDMT}
  1152. begin
  1153. if pd.typ<>procdef then
  1154. internalerror(2003042610);
  1155. if (pd.proctypeoption=potype_constructor) and
  1156. is_object(tprocdef(pd)._class) then
  1157. Message(parser_e_constructor_cannot_be_not_virtual);
  1158. {$ifdef WITHDMT}
  1159. if is_object(tprocdef(pd)._class) and
  1160. (token<>_SEMICOLON) then
  1161. begin
  1162. { any type of parameter is allowed here! }
  1163. pt:=comp_expr(true);
  1164. if is_constintnode(pt) then
  1165. begin
  1166. include(pd.procoptions,po_msgint);
  1167. pd.messageinf.i:=pt.value;
  1168. end
  1169. else
  1170. Message(parser_e_ill_msg_expr);
  1171. disposetree(pt);
  1172. end;
  1173. {$endif WITHDMT}
  1174. end;
  1175. procedure pd_dispid(pd:tabstractprocdef);
  1176. var pt:Tnode;
  1177. begin
  1178. if pd.typ<>procdef then
  1179. internalerror(200604301);
  1180. pt:=comp_expr(true);
  1181. if is_constintnode(pt) then
  1182. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1183. message(parser_e_range_check_error)
  1184. else
  1185. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1186. else
  1187. message(parser_e_dispid_must_be_ord_const);
  1188. pt.free;
  1189. end;
  1190. procedure pd_static(pd:tabstractprocdef);
  1191. begin
  1192. if (cs_static_keyword in current_settings.moduleswitches) then
  1193. begin
  1194. if pd.typ=procdef then
  1195. include(tprocdef(pd).procsym.symoptions,sp_static);
  1196. include(pd.procoptions,po_staticmethod);
  1197. end;
  1198. end;
  1199. procedure pd_override(pd:tabstractprocdef);
  1200. begin
  1201. if pd.typ<>procdef then
  1202. internalerror(2003042611);
  1203. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1204. Message(parser_e_no_object_override);
  1205. end;
  1206. procedure pd_overload(pd:tabstractprocdef);
  1207. begin
  1208. if pd.typ<>procdef then
  1209. internalerror(2003042612);
  1210. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1211. end;
  1212. procedure pd_message(pd:tabstractprocdef);
  1213. var
  1214. pt : tnode;
  1215. paracnt : longint;
  1216. begin
  1217. if pd.typ<>procdef then
  1218. internalerror(2003042613);
  1219. if not is_class(tprocdef(pd)._class) then
  1220. Message(parser_e_msg_only_for_classes);
  1221. { check parameter type }
  1222. paracnt:=0;
  1223. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1224. if paracnt<>1 then
  1225. Message(parser_e_ill_msg_param);
  1226. pt:=comp_expr(true);
  1227. if pt.nodetype=stringconstn then
  1228. begin
  1229. include(pd.procoptions,po_msgstr);
  1230. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1231. end
  1232. else
  1233. if is_constintnode(pt) then
  1234. begin
  1235. include(pd.procoptions,po_msgint);
  1236. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1237. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1238. message(parser_e_range_check_error)
  1239. else
  1240. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1241. end
  1242. else
  1243. Message(parser_e_ill_msg_expr);
  1244. pt.free;
  1245. end;
  1246. procedure pd_reintroduce(pd:tabstractprocdef);
  1247. begin
  1248. if pd.typ<>procdef then
  1249. internalerror(200401211);
  1250. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1251. Message(parser_e_no_object_reintroduce);
  1252. end;
  1253. procedure pd_syscall(pd:tabstractprocdef);
  1254. {$if defined(powerpc) or defined(m68k)}
  1255. var
  1256. vs : tparavarsym;
  1257. sym : tsym;
  1258. symtable : TSymtable;
  1259. v: Tconstexprint;
  1260. {$endif defined(powerpc) or defined(m68k)}
  1261. begin
  1262. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1263. internalerror(2003042614);
  1264. tprocdef(pd).forwarddef:=false;
  1265. {$ifdef m68k}
  1266. if target_info.system in [system_m68k_amiga] then
  1267. begin
  1268. include(pd.procoptions,po_syscall_legacy);
  1269. if consume_sym(sym,symtable) then
  1270. begin
  1271. if (sym.typ=staticvarsym) and
  1272. (
  1273. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1274. is_32bitint(tabstractvarsym(sym).vardef)
  1275. ) then
  1276. begin
  1277. tprocdef(pd).libsym:=sym;
  1278. if po_syscall_legacy in tprocdef(pd).procoptions then
  1279. begin
  1280. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1281. paramanager.parseparaloc(vs,'A6');
  1282. pd.parast.insert(vs);
  1283. end
  1284. end
  1285. else
  1286. Message(parser_e_32bitint_or_pointer_variable_expected);
  1287. end;
  1288. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1289. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1290. v:=get_intconst;
  1291. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1292. message(parser_e_range_check_error)
  1293. else
  1294. Tprocdef(pd).extnumber:=v.uvalue;
  1295. end;
  1296. {$endif m68k}
  1297. {$ifdef powerpc}
  1298. if target_info.system = system_powerpc_amiga then
  1299. begin
  1300. include(pd.procoptions,po_syscall_basesysv);
  1301. if consume_sym(sym,symtable) then
  1302. begin
  1303. if (sym.typ=staticvarsym) and
  1304. (
  1305. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1306. is_32bitint(tabstractvarsym(sym).vardef)
  1307. ) then
  1308. begin
  1309. tprocdef(pd).libsym:=sym;
  1310. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1311. pd.parast.insert(vs);
  1312. end
  1313. else
  1314. Message(parser_e_32bitint_or_pointer_variable_expected);
  1315. end;
  1316. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1317. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1318. v:=get_intconst;
  1319. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1320. message(parser_e_range_check_error)
  1321. else
  1322. Tprocdef(pd).extnumber:=v.uvalue;
  1323. end else
  1324. if target_info.system = system_powerpc_morphos then
  1325. begin
  1326. if idtoken=_LEGACY then
  1327. begin
  1328. consume(_LEGACY);
  1329. include(pd.procoptions,po_syscall_legacy);
  1330. end
  1331. else if idtoken=_SYSV then
  1332. begin
  1333. consume(_SYSV);
  1334. include(pd.procoptions,po_syscall_sysv);
  1335. end
  1336. else if idtoken=_BASESYSV then
  1337. begin
  1338. consume(_BASESYSV);
  1339. include(pd.procoptions,po_syscall_basesysv);
  1340. end
  1341. else if idtoken=_SYSVBASE then
  1342. begin
  1343. consume(_SYSVBASE);
  1344. include(pd.procoptions,po_syscall_sysvbase);
  1345. end
  1346. else if idtoken=_R12BASE then
  1347. begin
  1348. consume(_R12BASE);
  1349. include(pd.procoptions,po_syscall_r12base);
  1350. end
  1351. else
  1352. if syscall_convention='LEGACY' then
  1353. include(pd.procoptions,po_syscall_legacy)
  1354. else if syscall_convention='SYSV' then
  1355. include(pd.procoptions,po_syscall_sysv)
  1356. else if syscall_convention='BASESYSV' then
  1357. include(pd.procoptions,po_syscall_basesysv)
  1358. else if syscall_convention='SYSVBASE' then
  1359. include(pd.procoptions,po_syscall_sysvbase)
  1360. else if syscall_convention='R12BASE' then
  1361. include(pd.procoptions,po_syscall_r12base)
  1362. else
  1363. internalerror(2005010404);
  1364. if consume_sym(sym,symtable) then
  1365. begin
  1366. if (sym.typ=staticvarsym) and
  1367. (
  1368. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1369. is_32bitint(tabstractvarsym(sym).vardef)
  1370. ) then
  1371. begin
  1372. tprocdef(pd).libsym:=sym;
  1373. if po_syscall_legacy in tprocdef(pd).procoptions then
  1374. begin
  1375. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1376. paramanager.parseparaloc(vs,'A6');
  1377. pd.parast.insert(vs);
  1378. end
  1379. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1380. begin
  1381. { Nothing to be done for sysv here for now, but this might change }
  1382. end
  1383. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1384. begin
  1385. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1386. pd.parast.insert(vs);
  1387. end
  1388. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1389. begin
  1390. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1391. pd.parast.insert(vs);
  1392. end
  1393. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1394. begin
  1395. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1396. paramanager.parseparaloc(vs,'R12');
  1397. pd.parast.insert(vs);
  1398. end
  1399. else
  1400. internalerror(2005010501);
  1401. end
  1402. else
  1403. Message(parser_e_32bitint_or_pointer_variable_expected);
  1404. end;
  1405. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1406. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1407. v:=get_intconst;
  1408. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1409. message(parser_e_range_check_error)
  1410. else
  1411. Tprocdef(pd).extnumber:=v.uvalue;
  1412. end;
  1413. {$endif powerpc}
  1414. end;
  1415. procedure pd_external(pd:tabstractprocdef);
  1416. {
  1417. If import_dll=nil the procedure is assumed to be in another
  1418. object file. In that object file it should have the name to
  1419. which import_name is pointing to. Otherwise, the procedure is
  1420. assumed to be in the DLL to which import_dll is pointing to. In
  1421. that case either import_nr<>0 or import_name<>nil is true, so
  1422. the procedure is either imported by number or by name. (DM)
  1423. }
  1424. var
  1425. hs : string;
  1426. v:Tconstexprint;
  1427. begin
  1428. if pd.typ<>procdef then
  1429. internalerror(2003042615);
  1430. with tprocdef(pd) do
  1431. begin
  1432. forwarddef:=false;
  1433. { forbid local external procedures }
  1434. if parast.symtablelevel>normal_function_level then
  1435. Message(parser_e_no_local_proc_external);
  1436. { If the procedure should be imported from a DLL, a constant string follows.
  1437. This isn't really correct, an contant string expression follows
  1438. so we check if an semicolon follows, else a string constant have to
  1439. follow (FK) }
  1440. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1441. begin
  1442. { Always add library prefix and suffix to create an uniform name }
  1443. hs:=get_stringconst;
  1444. if ExtractFileExt(hs)='' then
  1445. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1446. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1447. hs:=target_info.sharedlibprefix+hs;
  1448. import_dll:=stringdup(hs);
  1449. include(procoptions,po_has_importdll);
  1450. if (idtoken=_NAME) then
  1451. begin
  1452. consume(_NAME);
  1453. import_name:=stringdup(get_stringconst);
  1454. include(procoptions,po_has_importname);
  1455. if import_name^='' then
  1456. message(parser_e_empty_import_name);
  1457. end;
  1458. if (idtoken=_INDEX) then
  1459. begin
  1460. {After the word index follows the index number in the DLL.}
  1461. consume(_INDEX);
  1462. v:=get_intconst;
  1463. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1464. message(parser_e_range_check_error)
  1465. else
  1466. import_nr:=longint(v.svalue);
  1467. end;
  1468. { default is to used the realname of the procedure }
  1469. if (import_nr=0) and not assigned(import_name) then
  1470. begin
  1471. import_name:=stringdup(procsym.realname);
  1472. include(procoptions,po_has_importname);
  1473. end;
  1474. end
  1475. else
  1476. begin
  1477. if (idtoken=_NAME) then
  1478. begin
  1479. consume(_NAME);
  1480. import_name:=stringdup(get_stringconst);
  1481. include(procoptions,po_has_importname);
  1482. if import_name^='' then
  1483. message(parser_e_empty_import_name);
  1484. end;
  1485. end;
  1486. end;
  1487. end;
  1488. procedure pd_weakexternal(pd:tabstractprocdef);
  1489. begin
  1490. if not(target_info.system in system_weak_linking) then
  1491. message(parser_e_weak_external_not_supported)
  1492. else
  1493. pd_external(pd);
  1494. end;
  1495. type
  1496. pd_handler=procedure(pd:tabstractprocdef);
  1497. proc_dir_rec=record
  1498. idtok : ttoken;
  1499. pd_flags : tpdflags;
  1500. handler : pd_handler;
  1501. pocall : tproccalloption;
  1502. pooption : tprocoptions;
  1503. mutexclpocall : tproccalloptions;
  1504. mutexclpotype : tproctypeoptions;
  1505. mutexclpo : tprocoptions;
  1506. end;
  1507. const
  1508. {Should contain the number of procedure directives we support.}
  1509. num_proc_directives=40;
  1510. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1511. (
  1512. (
  1513. idtok:_ABSTRACT;
  1514. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1515. handler : @pd_abstract;
  1516. pocall : pocall_none;
  1517. pooption : [po_abstractmethod];
  1518. mutexclpocall : [pocall_internproc];
  1519. mutexclpotype : [];
  1520. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1521. ),(
  1522. idtok:_ALIAS;
  1523. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1524. handler : @pd_alias;
  1525. pocall : pocall_none;
  1526. pooption : [];
  1527. mutexclpocall : [];
  1528. mutexclpotype : [];
  1529. mutexclpo : [po_external,po_inline]
  1530. ),(
  1531. idtok:_ASMNAME;
  1532. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1533. handler : @pd_asmname;
  1534. pocall : pocall_cdecl;
  1535. pooption : [po_external];
  1536. mutexclpocall : [pocall_internproc];
  1537. mutexclpotype : [];
  1538. mutexclpo : [po_external,po_inline]
  1539. ),(
  1540. idtok:_ASSEMBLER;
  1541. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1542. handler : nil;
  1543. pocall : pocall_none;
  1544. pooption : [po_assembler];
  1545. mutexclpocall : [];
  1546. mutexclpotype : [];
  1547. mutexclpo : [po_external]
  1548. ),(
  1549. idtok:_C; {same as cdecl for mode mac}
  1550. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1551. handler : nil;
  1552. pocall : pocall_cdecl;
  1553. pooption : [];
  1554. mutexclpocall : [];
  1555. mutexclpotype : [potype_constructor,potype_destructor];
  1556. mutexclpo : [po_assembler,po_external]
  1557. ),(
  1558. idtok:_CDECL;
  1559. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1560. handler : nil;
  1561. pocall : pocall_cdecl;
  1562. pooption : [];
  1563. mutexclpocall : [];
  1564. mutexclpotype : [potype_constructor,potype_destructor];
  1565. mutexclpo : [po_assembler,po_external]
  1566. ),(
  1567. idtok:_DISPID;
  1568. pd_flags : [pd_dispinterface];
  1569. handler : @pd_dispid;
  1570. pocall : pocall_none;
  1571. pooption : [po_dispid];
  1572. mutexclpocall : [pocall_internproc];
  1573. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1574. mutexclpo : [po_interrupt,po_external,po_inline]
  1575. ),(
  1576. idtok:_DYNAMIC;
  1577. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1578. handler : @pd_virtual;
  1579. pocall : pocall_none;
  1580. pooption : [po_virtualmethod];
  1581. mutexclpocall : [pocall_internproc];
  1582. mutexclpotype : [];
  1583. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1584. ),(
  1585. idtok:_EXPORT;
  1586. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1587. handler : @pd_export;
  1588. pocall : pocall_none;
  1589. pooption : [po_exports,po_global];
  1590. mutexclpocall : [pocall_internproc];
  1591. mutexclpotype : [potype_constructor,potype_destructor];
  1592. mutexclpo : [po_external,po_interrupt,po_inline]
  1593. ),(
  1594. idtok:_EXTERNAL;
  1595. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  1596. handler : @pd_external;
  1597. pocall : pocall_none;
  1598. pooption : [po_external];
  1599. mutexclpocall : [pocall_internproc,pocall_syscall];
  1600. { allowed for external cpp classes }
  1601. mutexclpotype : [{potype_constructor,potype_destructor}];
  1602. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1603. ),(
  1604. idtok:_FAR;
  1605. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1606. handler : @pd_far;
  1607. pocall : pocall_none;
  1608. pooption : [];
  1609. mutexclpocall : [pocall_internproc];
  1610. mutexclpotype : [];
  1611. mutexclpo : [po_inline]
  1612. ),(
  1613. idtok:_FAR16;
  1614. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1615. handler : nil;
  1616. pocall : pocall_far16;
  1617. pooption : [];
  1618. mutexclpocall : [];
  1619. mutexclpotype : [];
  1620. mutexclpo : [po_external]
  1621. ),(
  1622. idtok:_FORWARD;
  1623. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1624. handler : @pd_forward;
  1625. pocall : pocall_none;
  1626. pooption : [];
  1627. mutexclpocall : [pocall_internproc];
  1628. mutexclpotype : [];
  1629. mutexclpo : [po_external,po_inline]
  1630. ),(
  1631. idtok:_OLDFPCCALL;
  1632. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1633. handler : nil;
  1634. pocall : pocall_oldfpccall;
  1635. pooption : [];
  1636. mutexclpocall : [];
  1637. mutexclpotype : [];
  1638. mutexclpo : []
  1639. ),(
  1640. idtok:_INLINE;
  1641. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1642. handler : nil;
  1643. pocall : pocall_none;
  1644. pooption : [po_inline];
  1645. mutexclpocall : [];
  1646. mutexclpotype : [potype_constructor,potype_destructor];
  1647. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1648. ),(
  1649. idtok:_INTERNCONST;
  1650. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf];
  1651. handler : @pd_internconst;
  1652. pocall : pocall_none;
  1653. pooption : [po_internconst];
  1654. mutexclpocall : [];
  1655. mutexclpotype : [potype_operator];
  1656. mutexclpo : []
  1657. ),(
  1658. idtok:_INTERNPROC;
  1659. pd_flags : [pd_interface,pd_notobject,pd_notobjintf];
  1660. handler : @pd_internproc;
  1661. pocall : pocall_internproc;
  1662. pooption : [];
  1663. mutexclpocall : [];
  1664. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1665. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1666. ),(
  1667. idtok:_INTERRUPT;
  1668. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1669. handler : @pd_interrupt;
  1670. pocall : pocall_oldfpccall;
  1671. pooption : [po_interrupt];
  1672. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1673. pocall_pascal,pocall_far16,pocall_oldfpccall];
  1674. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1675. mutexclpo : [po_external,po_inline]
  1676. ),(
  1677. idtok:_IOCHECK;
  1678. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1679. handler : nil;
  1680. pocall : pocall_none;
  1681. pooption : [po_iocheck];
  1682. mutexclpocall : [pocall_internproc];
  1683. mutexclpotype : [];
  1684. mutexclpo : [po_external]
  1685. ),(
  1686. idtok:_LOCAL;
  1687. pd_flags : [pd_implemen,pd_body];
  1688. handler : nil;
  1689. pocall : pocall_none;
  1690. pooption : [po_kylixlocal];
  1691. mutexclpocall : [pocall_internproc,pocall_far16];
  1692. mutexclpotype : [];
  1693. mutexclpo : [po_external,po_exports]
  1694. ),(
  1695. idtok:_MESSAGE;
  1696. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1697. handler : @pd_message;
  1698. pocall : pocall_none;
  1699. pooption : []; { can be po_msgstr or po_msgint }
  1700. mutexclpocall : [pocall_internproc];
  1701. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1702. mutexclpo : [po_interrupt,po_external,po_inline]
  1703. ),(
  1704. idtok:_MWPASCAL;
  1705. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1706. handler : nil;
  1707. pocall : pocall_mwpascal;
  1708. pooption : [];
  1709. mutexclpocall : [];
  1710. mutexclpotype : [];
  1711. mutexclpo : []
  1712. ),(
  1713. idtok:_NEAR;
  1714. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1715. handler : @pd_near;
  1716. pocall : pocall_none;
  1717. pooption : [];
  1718. mutexclpocall : [pocall_internproc];
  1719. mutexclpotype : [];
  1720. mutexclpo : []
  1721. ),(
  1722. idtok:_NOSTACKFRAME;
  1723. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1724. handler : nil;
  1725. pocall : pocall_none;
  1726. pooption : [po_nostackframe];
  1727. mutexclpocall : [pocall_internproc];
  1728. mutexclpotype : [];
  1729. mutexclpo : []
  1730. ),(
  1731. idtok:_OVERLOAD;
  1732. pd_flags : [pd_implemen,pd_interface,pd_body];
  1733. handler : @pd_overload;
  1734. pocall : pocall_none;
  1735. pooption : [po_overload];
  1736. mutexclpocall : [pocall_internproc];
  1737. mutexclpotype : [];
  1738. mutexclpo : []
  1739. ),(
  1740. idtok:_OVERRIDE;
  1741. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1742. handler : @pd_override;
  1743. pocall : pocall_none;
  1744. pooption : [po_overridingmethod,po_virtualmethod];
  1745. mutexclpocall : [pocall_internproc];
  1746. mutexclpotype : [];
  1747. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_inline]
  1748. ),(
  1749. idtok:_PASCAL;
  1750. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1751. handler : nil;
  1752. pocall : pocall_pascal;
  1753. pooption : [];
  1754. mutexclpocall : [];
  1755. mutexclpotype : [potype_constructor,potype_destructor];
  1756. mutexclpo : [po_external]
  1757. ),(
  1758. idtok:_PUBLIC;
  1759. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1760. handler : @pd_public;
  1761. pocall : pocall_none;
  1762. pooption : [po_public,po_global];
  1763. mutexclpocall : [pocall_internproc];
  1764. mutexclpotype : [];
  1765. mutexclpo : [po_external,po_inline]
  1766. ),(
  1767. idtok:_REGISTER;
  1768. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1769. handler : nil;
  1770. pocall : pocall_register;
  1771. pooption : [];
  1772. mutexclpocall : [];
  1773. mutexclpotype : [potype_constructor,potype_destructor];
  1774. mutexclpo : [po_external]
  1775. ),(
  1776. idtok:_REINTRODUCE;
  1777. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1778. handler : @pd_reintroduce;
  1779. pocall : pocall_none;
  1780. pooption : [po_reintroduce];
  1781. mutexclpocall : [pocall_internproc];
  1782. mutexclpotype : [];
  1783. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1784. ),(
  1785. idtok:_SAFECALL;
  1786. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1787. handler : nil;
  1788. pocall : pocall_safecall;
  1789. pooption : [];
  1790. mutexclpocall : [];
  1791. mutexclpotype : [potype_constructor,potype_destructor];
  1792. mutexclpo : [po_external]
  1793. ),(
  1794. idtok:_SOFTFLOAT;
  1795. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1796. handler : nil;
  1797. pocall : pocall_softfloat;
  1798. pooption : [];
  1799. mutexclpocall : [];
  1800. mutexclpotype : [potype_constructor,potype_destructor];
  1801. { it's available with po_external because the libgcc floating point routines on the arm
  1802. uses this calling convention }
  1803. mutexclpo : []
  1804. ),(
  1805. idtok:_STATIC;
  1806. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_notobjintf];
  1807. handler : @pd_static;
  1808. pocall : pocall_none;
  1809. pooption : [po_staticmethod];
  1810. mutexclpocall : [pocall_internproc];
  1811. mutexclpotype : [potype_constructor,potype_destructor];
  1812. mutexclpo : [po_external,po_interrupt,po_exports,po_inline]
  1813. ),(
  1814. idtok:_STDCALL;
  1815. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1816. handler : nil;
  1817. pocall : pocall_stdcall;
  1818. pooption : [];
  1819. mutexclpocall : [];
  1820. mutexclpotype : [potype_constructor,potype_destructor];
  1821. mutexclpo : [po_external]
  1822. ),(
  1823. idtok:_SYSCALL;
  1824. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  1825. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  1826. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  1827. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1828. handler : @pd_syscall;
  1829. pocall : pocall_syscall;
  1830. pooption : [];
  1831. mutexclpocall : [];
  1832. mutexclpotype : [potype_constructor,potype_destructor];
  1833. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1834. ),(
  1835. idtok:_VIRTUAL;
  1836. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1837. handler : @pd_virtual;
  1838. pocall : pocall_none;
  1839. pooption : [po_virtualmethod];
  1840. mutexclpocall : [pocall_internproc];
  1841. mutexclpotype : [];
  1842. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1843. ),(
  1844. idtok:_CPPDECL;
  1845. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1846. handler : nil;
  1847. pocall : pocall_cppdecl;
  1848. pooption : [];
  1849. mutexclpocall : [];
  1850. mutexclpotype : [potype_constructor,potype_destructor];
  1851. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1852. ),(
  1853. idtok:_VARARGS;
  1854. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1855. handler : nil;
  1856. pocall : pocall_none;
  1857. pooption : [po_varargs];
  1858. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1859. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  1860. mutexclpotype : [];
  1861. mutexclpo : [po_assembler,po_interrupt,po_inline]
  1862. ),(
  1863. idtok:_COMPILERPROC;
  1864. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1865. handler : nil;
  1866. pocall : pocall_none;
  1867. pooption : [po_compilerproc];
  1868. mutexclpocall : [];
  1869. mutexclpotype : [potype_constructor,potype_destructor];
  1870. mutexclpo : [po_interrupt]
  1871. ),(
  1872. idtok:_WEAKEXTERNAL;
  1873. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  1874. handler : @pd_weakexternal;
  1875. pocall : pocall_none;
  1876. { mark it both external and weak external, so we don't have to
  1877. adapt all code for external symbols to also check for weak external
  1878. }
  1879. pooption : [po_external,po_weakexternal];
  1880. mutexclpocall : [pocall_internproc,pocall_syscall];
  1881. { allowed for external cpp classes }
  1882. mutexclpotype : [{potype_constructor,potype_destructor}];
  1883. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1884. )
  1885. );
  1886. function check_proc_directive(isprocvar:boolean):boolean;
  1887. var
  1888. i : longint;
  1889. begin
  1890. result:=false;
  1891. for i:=1 to num_proc_directives do
  1892. if proc_direcdata[i].idtok=idtoken then
  1893. begin
  1894. if ((not isprocvar) or
  1895. (pd_procvar in proc_direcdata[i].pd_flags)) and
  1896. { don't eat a public directive in classes }
  1897. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  1898. result:=true;
  1899. exit;
  1900. end;
  1901. end;
  1902. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  1903. {
  1904. Parse the procedure directive, returns true if a correct directive is found
  1905. }
  1906. var
  1907. p : longint;
  1908. found : boolean;
  1909. name : TIDString;
  1910. begin
  1911. parse_proc_direc:=false;
  1912. name:=tokeninfo^[idtoken].str;
  1913. found:=false;
  1914. { Hint directive? Then exit immediatly }
  1915. if (m_hintdirective in current_settings.modeswitches) then
  1916. begin
  1917. case idtoken of
  1918. _LIBRARY,
  1919. _PLATFORM,
  1920. _UNIMPLEMENTED,
  1921. _EXPERIMENTAL,
  1922. _DEPRECATED :
  1923. exit;
  1924. end;
  1925. end;
  1926. { C directive is MAC only, because it breaks too much existing code
  1927. on other platforms (PFV) }
  1928. if (idtoken=_C) and
  1929. not(m_mac in current_settings.modeswitches) then
  1930. exit;
  1931. { retrieve data for directive if found }
  1932. for p:=1 to num_proc_directives do
  1933. if proc_direcdata[p].idtok=idtoken then
  1934. begin
  1935. found:=true;
  1936. break;
  1937. end;
  1938. { Check if the procedure directive is known }
  1939. if not found then
  1940. begin
  1941. { parsing a procvar type the name can be any
  1942. next variable !! }
  1943. if ((pdflags * [pd_procvar,pd_object])=[]) and
  1944. not(idtoken=_PROPERTY) then
  1945. Message1(parser_w_unknown_proc_directive_ignored,name);
  1946. exit;
  1947. end;
  1948. { static needs a special treatment }
  1949. if (idtoken=_STATIC) and not (cs_static_keyword in current_settings.moduleswitches) then
  1950. exit;
  1951. { check if method and directive not for object, like public.
  1952. This needs to be checked also for procvars }
  1953. if (pd_notobject in proc_direcdata[p].pd_flags) and
  1954. (symtablestack.top.symtabletype=ObjectSymtable) and
  1955. { directive allowed for cpp classes? }
  1956. not(is_cppclass(tdef(symtablestack.top.defowner)) and (pd_cppobject in proc_direcdata[p].pd_flags)) then
  1957. exit;
  1958. { Conflicts between directives ? }
  1959. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1960. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  1961. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1962. begin
  1963. Message1(parser_e_proc_dir_conflict,name);
  1964. exit;
  1965. end;
  1966. { set calling convention }
  1967. if proc_direcdata[p].pocall<>pocall_none then
  1968. begin
  1969. if (po_hascallingconvention in pd.procoptions) then
  1970. begin
  1971. Message2(parser_w_proc_overriding_calling,
  1972. proccalloptionStr[pd.proccalloption],
  1973. proccalloptionStr[proc_direcdata[p].pocall]);
  1974. end;
  1975. { check if the target processor supports this calling convention }
  1976. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  1977. begin
  1978. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  1979. { recover }
  1980. proc_direcdata[p].pocall:=pocall_stdcall;
  1981. end;
  1982. pd.proccalloption:=proc_direcdata[p].pocall;
  1983. include(pd.procoptions,po_hascallingconvention);
  1984. end;
  1985. if pd.typ=procdef then
  1986. begin
  1987. { Check if the directive is only for objects }
  1988. if (pd_object in proc_direcdata[p].pd_flags) and
  1989. not assigned(tprocdef(pd)._class) then
  1990. exit;
  1991. { check if method and directive not for interface }
  1992. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  1993. is_interface(tprocdef(pd)._class) then
  1994. exit;
  1995. { check if method and directive not for interface }
  1996. if is_dispinterface(tprocdef(pd)._class) and
  1997. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  1998. exit;
  1999. end;
  2000. { consume directive, and turn flag on }
  2001. consume(token);
  2002. parse_proc_direc:=true;
  2003. { Check the pd_flags if the directive should be allowed }
  2004. if (pd_interface in pdflags) and
  2005. not(pd_interface in proc_direcdata[p].pd_flags) then
  2006. begin
  2007. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2008. exit;
  2009. end;
  2010. if (pd_implemen in pdflags) and
  2011. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2012. begin
  2013. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2014. exit;
  2015. end;
  2016. if (pd_procvar in pdflags) and
  2017. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2018. begin
  2019. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2020. exit;
  2021. end;
  2022. { Return the new pd_flags }
  2023. if not(pd_body in proc_direcdata[p].pd_flags) then
  2024. exclude(pdflags,pd_body);
  2025. { Add the correct flag }
  2026. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2027. { Call the handler }
  2028. if pointer(proc_direcdata[p].handler)<>nil then
  2029. proc_direcdata[p].handler(pd);
  2030. end;
  2031. function proc_get_importname(pd:tprocdef):string;
  2032. function maybe_cprefix(const s:string):string;
  2033. begin
  2034. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  2035. result:=s
  2036. else
  2037. result:=target_info.Cprefix+s;
  2038. end;
  2039. begin
  2040. result:='';
  2041. if not(po_external in pd.procoptions) then
  2042. internalerror(200412151);
  2043. { external name or number is specified }
  2044. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2045. begin
  2046. if assigned(pd.import_dll) then
  2047. begin
  2048. { If we are not using direct dll linking under win32 then imports
  2049. need to use the normal name since two functions can refer to the
  2050. same DLL function. This is also needed for compatability
  2051. with Delphi and TP7 }
  2052. (*
  2053. case target_info.system of
  2054. system_i386_emx,
  2055. system_i386_os2 :
  2056. begin
  2057. { keep normal mangledname }
  2058. if not (Assigned (PD.Import_Name)) then
  2059. Result := PD.MangledName;
  2060. end;
  2061. else
  2062. *)
  2063. if assigned(pd.import_name) then
  2064. begin
  2065. if target_info.system in (system_all_windows +
  2066. [system_i386_emx, system_i386_os2]) then
  2067. { cprefix is not used in DLL imports under Windows or OS/2 }
  2068. result:=pd.import_name^
  2069. else
  2070. result:=maybe_cprefix(pd.import_name^);
  2071. end
  2072. else
  2073. result:=ExtractFileName(pd.import_dll^)+'_index_'+tostr(pd.import_nr);
  2074. end
  2075. else
  2076. result:=maybe_cprefix(pd.import_name^);
  2077. end
  2078. else
  2079. begin
  2080. { Default names when importing variables }
  2081. case pd.proccalloption of
  2082. pocall_cdecl :
  2083. begin
  2084. if assigned(pd._class) then
  2085. result:=target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname
  2086. else
  2087. result:=target_info.Cprefix+pd.procsym.realname;
  2088. end;
  2089. pocall_cppdecl :
  2090. begin
  2091. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2092. end;
  2093. else
  2094. begin
  2095. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2096. if (m_mac in current_settings.modeswitches) then
  2097. result:=tprocdef(pd).procsym.realname;
  2098. end;
  2099. end;
  2100. end;
  2101. end;
  2102. procedure proc_set_mangledname(pd:tprocdef);
  2103. var
  2104. s : string;
  2105. begin
  2106. { When the mangledname is already set we aren't allowed to change
  2107. it because it can already be used somewhere (PFV) }
  2108. if not(po_has_mangledname in pd.procoptions) then
  2109. begin
  2110. if (po_external in pd.procoptions) then
  2111. begin
  2112. { External Procedures are only allowed to change the mangledname
  2113. in their first declaration }
  2114. if (pd.forwarddef or (not pd.hasforward)) then
  2115. begin
  2116. s:=proc_get_importname(pd);
  2117. if s<>'' then
  2118. begin
  2119. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  2120. { This allows to import VC++ mangled names from DLLs. }
  2121. { Do not perform replacement, if external symbol is not imported from DLL. }
  2122. if (target_info.system in system_all_windows) and (pd.import_dll<>nil) then
  2123. begin
  2124. Replace(s,'?','__q$$');
  2125. {$ifdef arm}
  2126. { @ symbol is not allowed in ARM assembler only }
  2127. Replace(s,'@','__a$$');
  2128. {$endif arm}
  2129. end;
  2130. pd.setmangledname(s);
  2131. end;
  2132. end;
  2133. end
  2134. else
  2135. { Normal procedures }
  2136. begin
  2137. if (po_compilerproc in pd.procoptions) then
  2138. begin
  2139. pd.setmangledname(lower(pd.procsym.name));
  2140. end;
  2141. end;
  2142. end;
  2143. { Public/exported alias names }
  2144. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2145. not(po_has_public_name in pd.procoptions) then
  2146. begin
  2147. case pd.proccalloption of
  2148. pocall_cdecl :
  2149. begin
  2150. if assigned(pd._class) then
  2151. pd.aliasnames.insert(target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname)
  2152. else
  2153. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2154. end;
  2155. pocall_cppdecl :
  2156. begin
  2157. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2158. end;
  2159. end;
  2160. { prevent adding the alias a second time }
  2161. include(pd.procoptions,po_has_public_name);
  2162. end;
  2163. end;
  2164. procedure handle_calling_convention(pd:tabstractprocdef);
  2165. begin
  2166. { set the default calling convention if none provided }
  2167. if not(po_hascallingconvention in pd.procoptions) then
  2168. pd.proccalloption:=current_settings.defproccall
  2169. else
  2170. begin
  2171. if pd.proccalloption=pocall_none then
  2172. internalerror(200309081);
  2173. end;
  2174. { handle proccall specific settings }
  2175. case pd.proccalloption of
  2176. pocall_cdecl,
  2177. pocall_cppdecl :
  2178. begin
  2179. { check C cdecl para types }
  2180. check_c_para(pd);
  2181. end;
  2182. pocall_far16 :
  2183. begin
  2184. { Temporary stub, must be rewritten to support OS/2 far16 }
  2185. Message1(parser_w_proc_directive_ignored,'FAR16');
  2186. end;
  2187. end;
  2188. { Inlining is enabled and supported? }
  2189. if (po_inline in pd.procoptions) and
  2190. not(cs_do_inline in current_settings.localswitches) then
  2191. begin
  2192. { Give an error if inline is not supported by the compiler mode,
  2193. otherwise only give a warning that this procedure will not be inlined }
  2194. if not(m_default_inline in current_settings.modeswitches) then
  2195. Message(parser_e_proc_inline_not_supported)
  2196. else
  2197. Message(parser_w_inlining_disabled);
  2198. exclude(pd.procoptions,po_inline);
  2199. end;
  2200. { For varargs directive also cdecl and external must be defined }
  2201. if (po_varargs in pd.procoptions) then
  2202. begin
  2203. { check first for external in the interface, if available there
  2204. then the cdecl must also be there since there is no implementation
  2205. available to contain it }
  2206. if parse_only then
  2207. begin
  2208. { if external is available, then cdecl must also be available,
  2209. procvars don't need external }
  2210. if not((po_external in pd.procoptions) or
  2211. (pd.typ=procvardef)) and
  2212. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_mwpascal]) then
  2213. Message(parser_e_varargs_need_cdecl_and_external);
  2214. end
  2215. else
  2216. begin
  2217. { both must be defined now }
  2218. if not((po_external in pd.procoptions) or
  2219. (pd.typ=procvardef)) or
  2220. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_mwpascal]) then
  2221. Message(parser_e_varargs_need_cdecl_and_external);
  2222. end;
  2223. end;
  2224. { insert hidden high parameters }
  2225. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2226. { insert hidden self parameter }
  2227. insert_self_and_vmt_para(pd);
  2228. { insert funcret parameter if required }
  2229. insert_funcret_para(pd);
  2230. { Make var parameters regable, this must be done after the calling
  2231. convention is set. }
  2232. { this must be done before parentfp is insert, because getting all cases
  2233. where parentfp must be in a memory location isn't catched properly so
  2234. we put parentfp never in a register }
  2235. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2236. { insert parentfp parameter if required }
  2237. insert_parentfp_para(pd);
  2238. { Calculate parameter tlist }
  2239. pd.calcparas;
  2240. end;
  2241. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2242. {
  2243. Parse the procedure directives. It does not matter if procedure directives
  2244. are written using ;procdir; or ['procdir'] syntax.
  2245. }
  2246. var
  2247. res : boolean;
  2248. begin
  2249. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2250. begin
  2251. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  2252. include(pd.procoptions,po_public);
  2253. include(pd.procoptions,po_has_public_name);
  2254. include(pd.procoptions,po_global);
  2255. end;
  2256. while token in [_ID,_LECKKLAMMER] do
  2257. begin
  2258. if try_to_consume(_LECKKLAMMER) then
  2259. begin
  2260. repeat
  2261. parse_proc_direc(pd,pdflags);
  2262. until not try_to_consume(_COMMA);
  2263. consume(_RECKKLAMMER);
  2264. { we always expect at least '[];' }
  2265. res:=true;
  2266. end
  2267. else
  2268. begin
  2269. res:=parse_proc_direc(pd,pdflags);
  2270. end;
  2271. { A procedure directive normally followed by a semicolon, but in
  2272. a const section or reading a type we should stop when _EQUAL is found,
  2273. because a constant/default value follows }
  2274. if res then
  2275. begin
  2276. if (block_type=bt_const_type) and
  2277. (token=_EQUAL) then
  2278. break;
  2279. { support procedure proc;stdcall export; }
  2280. if not(check_proc_directive((pd.typ=procvardef))) then
  2281. begin
  2282. { support "record p : procedure stdcall end;" and
  2283. "var p : procedure stdcall = nil;" }
  2284. if (pd_procvar in pdflags) and
  2285. (token in [_END,_RKLAMMER,_EQUAL]) then
  2286. break
  2287. else
  2288. consume(_SEMICOLON);
  2289. end;
  2290. end
  2291. else
  2292. break;
  2293. end;
  2294. end;
  2295. procedure parse_var_proc_directives(sym:tsym);
  2296. var
  2297. pdflags : tpdflags;
  2298. pd : tabstractprocdef;
  2299. begin
  2300. pdflags:=[pd_procvar];
  2301. pd:=nil;
  2302. case sym.typ of
  2303. fieldvarsym,
  2304. staticvarsym,
  2305. localvarsym,
  2306. paravarsym :
  2307. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2308. typesym :
  2309. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2310. else
  2311. internalerror(2003042617);
  2312. end;
  2313. if pd.typ<>procvardef then
  2314. internalerror(2003042618);
  2315. { names should never be used anyway }
  2316. parse_proc_directives(pd,pdflags);
  2317. end;
  2318. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2319. var
  2320. pdflags : tpdflags;
  2321. begin
  2322. pdflags:=[pd_object];
  2323. parse_proc_directives(pd,pdflags);
  2324. end;
  2325. function proc_add_definition(var currpd:tprocdef):boolean;
  2326. {
  2327. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2328. forwarddef is found and reused it returns true
  2329. }
  2330. var
  2331. fwpd : tprocdef;
  2332. currparasym,
  2333. fwparasym : tsym;
  2334. currparacnt,
  2335. fwparacnt,
  2336. curridx,
  2337. fwidx,
  2338. i : longint;
  2339. po_comp : tprocoptions;
  2340. paracompopt: tcompare_paras_options;
  2341. forwardfound : boolean;
  2342. begin
  2343. forwardfound:=false;
  2344. { check overloaded functions if the same function already exists }
  2345. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2346. begin
  2347. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2348. { Skip overloaded definitions that are declared in other units }
  2349. if fwpd.procsym<>currpd.procsym then
  2350. continue;
  2351. { check the parameters, for delphi/tp it is possible to
  2352. leave the parameters away in the implementation (forwarddef=false).
  2353. But for an overload declared function this is not allowed }
  2354. if { check if empty implementation arguments match is allowed }
  2355. (
  2356. not(m_repeat_forward in current_settings.modeswitches) and
  2357. not(currpd.forwarddef) and
  2358. is_bareprocdef(currpd) and
  2359. not(po_overload in fwpd.procoptions)
  2360. ) or
  2361. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2362. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2363. (
  2364. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden,cpo_openequalisexact])=te_exact) and
  2365. (fwpd.returndef=currpd.returndef)
  2366. ) then
  2367. begin
  2368. { Check if we've found the forwarddef, if found then
  2369. we need to update the forward def with the current
  2370. implementation settings }
  2371. if fwpd.forwarddef then
  2372. begin
  2373. forwardfound:=true;
  2374. if not(m_repeat_forward in current_settings.modeswitches) and
  2375. (fwpd.proccalloption<>currpd.proccalloption) then
  2376. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact]
  2377. else
  2378. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact];
  2379. { Check calling convention }
  2380. if (fwpd.proccalloption<>currpd.proccalloption) then
  2381. begin
  2382. { In delphi it is possible to specify the calling
  2383. convention in the interface or implementation if
  2384. there was no convention specified in the other
  2385. part }
  2386. if (m_delphi in current_settings.modeswitches) then
  2387. begin
  2388. if not(po_hascallingconvention in currpd.procoptions) then
  2389. currpd.proccalloption:=fwpd.proccalloption
  2390. else
  2391. if not(po_hascallingconvention in fwpd.procoptions) then
  2392. fwpd.proccalloption:=currpd.proccalloption
  2393. else
  2394. begin
  2395. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2396. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2397. { restore interface settings }
  2398. currpd.proccalloption:=fwpd.proccalloption;
  2399. end;
  2400. end
  2401. else
  2402. begin
  2403. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2404. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2405. { restore interface settings }
  2406. currpd.proccalloption:=fwpd.proccalloption;
  2407. end;
  2408. end;
  2409. { Check if the procedure type and return type are correct,
  2410. also the parameters must match also with the type }
  2411. if ((m_repeat_forward in current_settings.modeswitches) or
  2412. not is_bareprocdef(currpd)) and
  2413. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<>te_exact) or
  2414. (fwpd.returndef<>currpd.returndef)) then
  2415. begin
  2416. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2417. fwpd.fullprocname(false));
  2418. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2419. break;
  2420. end;
  2421. { Check if both are declared forward }
  2422. if fwpd.forwarddef and currpd.forwarddef then
  2423. begin
  2424. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2425. currpd.fullprocname(false));
  2426. end;
  2427. { internconst or internproc only need to be defined once }
  2428. if (fwpd.proccalloption=pocall_internproc) then
  2429. currpd.proccalloption:=fwpd.proccalloption
  2430. else
  2431. if (currpd.proccalloption=pocall_internproc) then
  2432. fwpd.proccalloption:=currpd.proccalloption;
  2433. { Check procedure options, Delphi requires that class is
  2434. repeated in the implementation for class methods }
  2435. if (m_fpc in current_settings.modeswitches) then
  2436. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2437. else
  2438. po_comp:=[po_classmethod,po_methodpointer];
  2439. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2440. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2441. begin
  2442. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2443. fwpd.fullprocname(false));
  2444. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2445. { This error is non-fatal, we can recover }
  2446. end;
  2447. { Forward declaration is external? }
  2448. if (po_external in fwpd.procoptions) then
  2449. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2450. { Check parameters }
  2451. if (m_repeat_forward in current_settings.modeswitches) or
  2452. (currpd.minparacount>0) then
  2453. begin
  2454. { If mangled names are equal then they have the same amount of arguments }
  2455. { We can check the names of the arguments }
  2456. { both symtables are in the same order from left to right }
  2457. curridx:=0;
  2458. fwidx:=0;
  2459. currparacnt:=currpd.parast.SymList.Count;
  2460. fwparacnt:=fwpd.parast.SymList.Count;
  2461. repeat
  2462. { skip default parameter constsyms }
  2463. while (curridx<currparacnt) and
  2464. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2465. inc(curridx);
  2466. while (fwidx<fwparacnt) and
  2467. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2468. inc(fwidx);
  2469. { stop when one of the two lists is at the end }
  2470. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2471. break;
  2472. { compare names of parameters, ignore implictly
  2473. renamed parameters }
  2474. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2475. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2476. if not(sp_implicitrename in currparasym.symoptions) and
  2477. not(sp_implicitrename in fwparasym.symoptions) then
  2478. begin
  2479. if (currparasym.name<>fwparasym.name) then
  2480. begin
  2481. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2482. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2483. break;
  2484. end;
  2485. end;
  2486. { next parameter }
  2487. inc(curridx);
  2488. inc(fwidx);
  2489. until false;
  2490. end;
  2491. { Everything is checked, now we can update the forward declaration
  2492. with the new data from the implementation }
  2493. fwpd.forwarddef:=currpd.forwarddef;
  2494. fwpd.hasforward:=true;
  2495. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2496. { marked as local but exported from unit? }
  2497. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2498. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2499. if fwpd.extnumber=$ffff then
  2500. fwpd.extnumber:=currpd.extnumber;
  2501. while not currpd.aliasnames.empty do
  2502. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2503. { update fileinfo so position references the implementation,
  2504. also update funcretsym if it is already generated }
  2505. fwpd.fileinfo:=currpd.fileinfo;
  2506. if assigned(fwpd.funcretsym) then
  2507. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2508. { import names }
  2509. if assigned(currpd.import_dll) then
  2510. begin
  2511. stringdispose(fwpd.import_dll);
  2512. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2513. end;
  2514. if assigned(currpd.import_name) then
  2515. begin
  2516. stringdispose(fwpd.import_name);
  2517. fwpd.import_name:=stringdup(currpd.import_name^);
  2518. end;
  2519. fwpd.import_nr:=currpd.import_nr;
  2520. { for compilerproc defines we need to rename and update the
  2521. symbolname to lowercase }
  2522. if (po_compilerproc in fwpd.procoptions) then
  2523. begin
  2524. { rename to lowercase so users can't access it }
  2525. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  2526. { the mangeled name is already changed by the pd_compilerproc }
  2527. { handler. It must be done immediately because if we have a }
  2528. { call to a compilerproc before it's implementation is }
  2529. { encountered, it must already use the new mangled name (JM) }
  2530. end;
  2531. { Release current procdef }
  2532. currpd.owner.deletedef(currpd);
  2533. currpd:=fwpd;
  2534. end
  2535. else
  2536. begin
  2537. { abstract methods aren't forward defined, but this }
  2538. { needs another error message }
  2539. if (po_abstractmethod in fwpd.procoptions) then
  2540. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  2541. else
  2542. begin
  2543. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  2544. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2545. end;
  2546. end;
  2547. { we found one proc with the same arguments, there are no others
  2548. so we can stop }
  2549. break;
  2550. end;
  2551. { check for allowing overload directive }
  2552. if not(m_fpc in current_settings.modeswitches) then
  2553. begin
  2554. { overload directive turns on overloading }
  2555. if ((po_overload in currpd.procoptions) or
  2556. (po_overload in fwpd.procoptions)) then
  2557. begin
  2558. { check if all procs have overloading, but not if the proc is a method or
  2559. already declared forward, then the check is already done }
  2560. if not(fwpd.hasforward or
  2561. assigned(currpd._class) or
  2562. (currpd.forwarddef<>fwpd.forwarddef) or
  2563. ((po_overload in currpd.procoptions) and
  2564. (po_overload in fwpd.procoptions))) then
  2565. begin
  2566. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2567. break;
  2568. end;
  2569. end
  2570. else
  2571. begin
  2572. if not(fwpd.forwarddef) then
  2573. begin
  2574. if (m_tp7 in current_settings.modeswitches) then
  2575. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  2576. else
  2577. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2578. break;
  2579. end;
  2580. end;
  2581. end; { equal arguments }
  2582. end;
  2583. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2584. list }
  2585. if not forwardfound then
  2586. begin
  2587. { can happen in Delphi mode }
  2588. if (currpd.proctypeoption = potype_function) and
  2589. is_void(currpd.returndef) then
  2590. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  2591. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  2592. end;
  2593. proc_add_definition:=forwardfound;
  2594. end;
  2595. end.