pdecsub.pas 83 KB

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