pdecsub.pas 98 KB

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