pdecsub.pas 95 KB

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