pdecsub.pas 86 KB

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