pdecsub.pas 87 KB

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