pdecsub.pas 83 KB

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