pdecsub.pas 100 KB

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